librtmp.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * RTMP network protocol
  3. * Copyright (c) 2010 Howard Chu
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * RTMP protocol based on http://rtmpdump.mplayerhq.hu/ librtmp
  24. */
  25. #include "libavutil/mathematics.h"
  26. #include "avformat.h"
  27. #include "url.h"
  28. #include <librtmp/rtmp.h>
  29. #include <librtmp/log.h>
  30. static void rtmp_log(int level, const char *fmt, va_list args)
  31. {
  32. switch (level) {
  33. default:
  34. case RTMP_LOGCRIT: level = AV_LOG_FATAL; break;
  35. case RTMP_LOGERROR: level = AV_LOG_ERROR; break;
  36. case RTMP_LOGWARNING: level = AV_LOG_WARNING; break;
  37. case RTMP_LOGINFO: level = AV_LOG_INFO; break;
  38. case RTMP_LOGDEBUG: level = AV_LOG_VERBOSE; break;
  39. case RTMP_LOGDEBUG2: level = AV_LOG_DEBUG; break;
  40. }
  41. av_vlog(NULL, level, fmt, args);
  42. av_log(NULL, level, "\n");
  43. }
  44. static int rtmp_close(URLContext *s)
  45. {
  46. RTMP *r = s->priv_data;
  47. RTMP_Close(r);
  48. av_free(r);
  49. return 0;
  50. }
  51. /**
  52. * Open RTMP connection and verify that the stream can be played.
  53. *
  54. * URL syntax: rtmp://server[:port][/app][/playpath][ keyword=value]...
  55. * where 'app' is first one or two directories in the path
  56. * (e.g. /ondemand/, /flash/live/, etc.)
  57. * and 'playpath' is a file name (the rest of the path,
  58. * may be prefixed with "mp4:")
  59. *
  60. * Additional RTMP library options may be appended as
  61. * space-separated key-value pairs.
  62. */
  63. static int rtmp_open(URLContext *s, const char *uri, int flags)
  64. {
  65. RTMP *r;
  66. int rc;
  67. r = av_mallocz(sizeof(RTMP));
  68. if (!r)
  69. return AVERROR(ENOMEM);
  70. switch (av_log_get_level()) {
  71. default:
  72. case AV_LOG_FATAL: rc = RTMP_LOGCRIT; break;
  73. case AV_LOG_ERROR: rc = RTMP_LOGERROR; break;
  74. case AV_LOG_WARNING: rc = RTMP_LOGWARNING; break;
  75. case AV_LOG_INFO: rc = RTMP_LOGINFO; break;
  76. case AV_LOG_VERBOSE: rc = RTMP_LOGDEBUG; break;
  77. case AV_LOG_DEBUG: rc = RTMP_LOGDEBUG2; break;
  78. }
  79. RTMP_LogSetLevel(rc);
  80. RTMP_LogSetCallback(rtmp_log);
  81. RTMP_Init(r);
  82. if (!RTMP_SetupURL(r, s->filename)) {
  83. rc = -1;
  84. goto fail;
  85. }
  86. if (flags & AVIO_WRONLY)
  87. RTMP_EnableWrite(r);
  88. if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) {
  89. rc = -1;
  90. goto fail;
  91. }
  92. s->priv_data = r;
  93. s->is_streamed = 1;
  94. return 0;
  95. fail:
  96. av_free(r);
  97. return rc;
  98. }
  99. static int rtmp_write(URLContext *s, const uint8_t *buf, int size)
  100. {
  101. RTMP *r = s->priv_data;
  102. return RTMP_Write(r, buf, size);
  103. }
  104. static int rtmp_read(URLContext *s, uint8_t *buf, int size)
  105. {
  106. RTMP *r = s->priv_data;
  107. return RTMP_Read(r, buf, size);
  108. }
  109. static int rtmp_read_pause(URLContext *s, int pause)
  110. {
  111. RTMP *r = s->priv_data;
  112. if (!RTMP_Pause(r, pause))
  113. return -1;
  114. return 0;
  115. }
  116. static int64_t rtmp_read_seek(URLContext *s, int stream_index,
  117. int64_t timestamp, int flags)
  118. {
  119. RTMP *r = s->priv_data;
  120. if (flags & AVSEEK_FLAG_BYTE)
  121. return AVERROR(ENOSYS);
  122. /* seeks are in milliseconds */
  123. if (stream_index < 0)
  124. timestamp = av_rescale_rnd(timestamp, 1000, AV_TIME_BASE,
  125. flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
  126. if (!RTMP_SendSeek(r, timestamp))
  127. return -1;
  128. return timestamp;
  129. }
  130. static int rtmp_get_file_handle(URLContext *s)
  131. {
  132. RTMP *r = s->priv_data;
  133. return RTMP_Socket(r);
  134. }
  135. URLProtocol ff_rtmp_protocol = {
  136. .name = "rtmp",
  137. .url_open = rtmp_open,
  138. .url_read = rtmp_read,
  139. .url_write = rtmp_write,
  140. .url_close = rtmp_close,
  141. .url_read_pause = rtmp_read_pause,
  142. .url_read_seek = rtmp_read_seek,
  143. .url_get_file_handle = rtmp_get_file_handle
  144. };
  145. URLProtocol ff_rtmpt_protocol = {
  146. .name = "rtmpt",
  147. .url_open = rtmp_open,
  148. .url_read = rtmp_read,
  149. .url_write = rtmp_write,
  150. .url_close = rtmp_close,
  151. .url_read_pause = rtmp_read_pause,
  152. .url_read_seek = rtmp_read_seek,
  153. .url_get_file_handle = rtmp_get_file_handle
  154. };
  155. URLProtocol ff_rtmpe_protocol = {
  156. .name = "rtmpe",
  157. .url_open = rtmp_open,
  158. .url_read = rtmp_read,
  159. .url_write = rtmp_write,
  160. .url_close = rtmp_close,
  161. .url_read_pause = rtmp_read_pause,
  162. .url_read_seek = rtmp_read_seek,
  163. .url_get_file_handle = rtmp_get_file_handle
  164. };
  165. URLProtocol ff_rtmpte_protocol = {
  166. .name = "rtmpte",
  167. .url_open = rtmp_open,
  168. .url_read = rtmp_read,
  169. .url_write = rtmp_write,
  170. .url_close = rtmp_close,
  171. .url_read_pause = rtmp_read_pause,
  172. .url_read_seek = rtmp_read_seek,
  173. .url_get_file_handle = rtmp_get_file_handle
  174. };
  175. URLProtocol ff_rtmps_protocol = {
  176. .name = "rtmps",
  177. .url_open = rtmp_open,
  178. .url_read = rtmp_read,
  179. .url_write = rtmp_write,
  180. .url_close = rtmp_close,
  181. .url_read_pause = rtmp_read_pause,
  182. .url_read_seek = rtmp_read_seek,
  183. .url_get_file_handle = rtmp_get_file_handle
  184. };