librtmp.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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/avstring.h"
  26. #include "libavutil/mathematics.h"
  27. #include "libavutil/opt.h"
  28. #include "avformat.h"
  29. #include "url.h"
  30. #include <librtmp/rtmp.h>
  31. #include <librtmp/log.h>
  32. typedef struct LibRTMPContext {
  33. const AVClass *class;
  34. RTMP rtmp;
  35. char *app;
  36. char *playpath;
  37. } LibRTMPContext;
  38. static void rtmp_log(int level, const char *fmt, va_list args)
  39. {
  40. switch (level) {
  41. default:
  42. case RTMP_LOGCRIT: level = AV_LOG_FATAL; break;
  43. case RTMP_LOGERROR: level = AV_LOG_ERROR; break;
  44. case RTMP_LOGWARNING: level = AV_LOG_WARNING; break;
  45. case RTMP_LOGINFO: level = AV_LOG_INFO; break;
  46. case RTMP_LOGDEBUG: level = AV_LOG_VERBOSE; break;
  47. case RTMP_LOGDEBUG2: level = AV_LOG_DEBUG; break;
  48. }
  49. av_vlog(NULL, level, fmt, args);
  50. av_log(NULL, level, "\n");
  51. }
  52. static int rtmp_close(URLContext *s)
  53. {
  54. LibRTMPContext *ctx = s->priv_data;
  55. RTMP *r = &ctx->rtmp;
  56. RTMP_Close(r);
  57. return 0;
  58. }
  59. /**
  60. * Open RTMP connection and verify that the stream can be played.
  61. *
  62. * URL syntax: rtmp://server[:port][/app][/playpath][ keyword=value]...
  63. * where 'app' is first one or two directories in the path
  64. * (e.g. /ondemand/, /flash/live/, etc.)
  65. * and 'playpath' is a file name (the rest of the path,
  66. * may be prefixed with "mp4:")
  67. *
  68. * Additional RTMP library options may be appended as
  69. * space-separated key-value pairs.
  70. */
  71. static int rtmp_open(URLContext *s, const char *uri, int flags)
  72. {
  73. LibRTMPContext *ctx = s->priv_data;
  74. RTMP *r = &ctx->rtmp;
  75. int rc = 0, level;
  76. char *filename = s->filename;
  77. switch (av_log_get_level()) {
  78. default:
  79. case AV_LOG_FATAL: level = RTMP_LOGCRIT; break;
  80. case AV_LOG_ERROR: level = RTMP_LOGERROR; break;
  81. case AV_LOG_WARNING: level = RTMP_LOGWARNING; break;
  82. case AV_LOG_INFO: level = RTMP_LOGINFO; break;
  83. case AV_LOG_VERBOSE: level = RTMP_LOGDEBUG; break;
  84. case AV_LOG_DEBUG: level = RTMP_LOGDEBUG2; break;
  85. }
  86. RTMP_LogSetLevel(level);
  87. RTMP_LogSetCallback(rtmp_log);
  88. if (ctx->app || ctx->playpath) {
  89. int len = strlen(s->filename) + 1;
  90. if (ctx->app) len += strlen(ctx->app) + sizeof(" app=");
  91. if (ctx->playpath) len += strlen(ctx->playpath) + sizeof(" playpath=");
  92. if (!(filename = av_malloc(len)))
  93. return AVERROR(ENOMEM);
  94. av_strlcpy(filename, s->filename, len);
  95. if (ctx->app) {
  96. av_strlcat(filename, " app=", len);
  97. av_strlcat(filename, ctx->app, len);
  98. }
  99. if (ctx->playpath) {
  100. av_strlcat(filename, " playpath=", len);
  101. av_strlcat(filename, ctx->playpath, len);
  102. }
  103. }
  104. RTMP_Init(r);
  105. if (!RTMP_SetupURL(r, filename)) {
  106. rc = AVERROR_UNKNOWN;
  107. goto fail;
  108. }
  109. if (flags & AVIO_FLAG_WRITE)
  110. RTMP_EnableWrite(r);
  111. if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) {
  112. rc = AVERROR_UNKNOWN;
  113. goto fail;
  114. }
  115. s->is_streamed = 1;
  116. rc = 0;
  117. fail:
  118. if (filename != s->filename)
  119. av_freep(&filename);
  120. return rc;
  121. }
  122. static int rtmp_write(URLContext *s, const uint8_t *buf, int size)
  123. {
  124. LibRTMPContext *ctx = s->priv_data;
  125. RTMP *r = &ctx->rtmp;
  126. return RTMP_Write(r, buf, size);
  127. }
  128. static int rtmp_read(URLContext *s, uint8_t *buf, int size)
  129. {
  130. LibRTMPContext *ctx = s->priv_data;
  131. RTMP *r = &ctx->rtmp;
  132. return RTMP_Read(r, buf, size);
  133. }
  134. static int rtmp_read_pause(URLContext *s, int pause)
  135. {
  136. LibRTMPContext *ctx = s->priv_data;
  137. RTMP *r = &ctx->rtmp;
  138. if (!RTMP_Pause(r, pause))
  139. return AVERROR_UNKNOWN;
  140. return 0;
  141. }
  142. static int64_t rtmp_read_seek(URLContext *s, int stream_index,
  143. int64_t timestamp, int flags)
  144. {
  145. LibRTMPContext *ctx = s->priv_data;
  146. RTMP *r = &ctx->rtmp;
  147. if (flags & AVSEEK_FLAG_BYTE)
  148. return AVERROR(ENOSYS);
  149. /* seeks are in milliseconds */
  150. if (stream_index < 0)
  151. timestamp = av_rescale_rnd(timestamp, 1000, AV_TIME_BASE,
  152. flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
  153. if (!RTMP_SendSeek(r, timestamp))
  154. return AVERROR_UNKNOWN;
  155. return timestamp;
  156. }
  157. static int rtmp_get_file_handle(URLContext *s)
  158. {
  159. LibRTMPContext *ctx = s->priv_data;
  160. RTMP *r = &ctx->rtmp;
  161. return RTMP_Socket(r);
  162. }
  163. #define OFFSET(x) offsetof(LibRTMPContext, x)
  164. #define DEC AV_OPT_FLAG_DECODING_PARAM
  165. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  166. static const AVOption options[] = {
  167. {"rtmp_app", "Name of application to connect to on the RTMP server", OFFSET(app), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
  168. {"rtmp_playpath", "Stream identifier to play or to publish", OFFSET(playpath), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
  169. { NULL },
  170. };
  171. #define RTMP_CLASS(flavor)\
  172. static const AVClass lib ## flavor ## _class = {\
  173. .class_name = "lib" #flavor " protocol",\
  174. .item_name = av_default_item_name,\
  175. .option = options,\
  176. .version = LIBAVUTIL_VERSION_INT,\
  177. };
  178. RTMP_CLASS(rtmp)
  179. URLProtocol ff_librtmp_protocol = {
  180. .name = "rtmp",
  181. .url_open = rtmp_open,
  182. .url_read = rtmp_read,
  183. .url_write = rtmp_write,
  184. .url_close = rtmp_close,
  185. .url_read_pause = rtmp_read_pause,
  186. .url_read_seek = rtmp_read_seek,
  187. .url_get_file_handle = rtmp_get_file_handle,
  188. .priv_data_size = sizeof(LibRTMPContext),
  189. .priv_data_class = &librtmp_class,
  190. .flags = URL_PROTOCOL_FLAG_NETWORK,
  191. };
  192. RTMP_CLASS(rtmpt)
  193. URLProtocol ff_librtmpt_protocol = {
  194. .name = "rtmpt",
  195. .url_open = rtmp_open,
  196. .url_read = rtmp_read,
  197. .url_write = rtmp_write,
  198. .url_close = rtmp_close,
  199. .url_read_pause = rtmp_read_pause,
  200. .url_read_seek = rtmp_read_seek,
  201. .url_get_file_handle = rtmp_get_file_handle,
  202. .priv_data_size = sizeof(LibRTMPContext),
  203. .priv_data_class = &librtmpt_class,
  204. .flags = URL_PROTOCOL_FLAG_NETWORK,
  205. };
  206. RTMP_CLASS(rtmpe)
  207. URLProtocol ff_librtmpe_protocol = {
  208. .name = "rtmpe",
  209. .url_open = rtmp_open,
  210. .url_read = rtmp_read,
  211. .url_write = rtmp_write,
  212. .url_close = rtmp_close,
  213. .url_read_pause = rtmp_read_pause,
  214. .url_read_seek = rtmp_read_seek,
  215. .url_get_file_handle = rtmp_get_file_handle,
  216. .priv_data_size = sizeof(LibRTMPContext),
  217. .priv_data_class = &librtmpe_class,
  218. .flags = URL_PROTOCOL_FLAG_NETWORK,
  219. };
  220. RTMP_CLASS(rtmpte)
  221. URLProtocol ff_librtmpte_protocol = {
  222. .name = "rtmpte",
  223. .url_open = rtmp_open,
  224. .url_read = rtmp_read,
  225. .url_write = rtmp_write,
  226. .url_close = rtmp_close,
  227. .url_read_pause = rtmp_read_pause,
  228. .url_read_seek = rtmp_read_seek,
  229. .url_get_file_handle = rtmp_get_file_handle,
  230. .priv_data_size = sizeof(LibRTMPContext),
  231. .priv_data_class = &librtmpte_class,
  232. .flags = URL_PROTOCOL_FLAG_NETWORK,
  233. };
  234. RTMP_CLASS(rtmps)
  235. URLProtocol ff_librtmps_protocol = {
  236. .name = "rtmps",
  237. .url_open = rtmp_open,
  238. .url_read = rtmp_read,
  239. .url_write = rtmp_write,
  240. .url_close = rtmp_close,
  241. .url_read_pause = rtmp_read_pause,
  242. .url_read_seek = rtmp_read_seek,
  243. .url_get_file_handle = rtmp_get_file_handle,
  244. .priv_data_size = sizeof(LibRTMPContext),
  245. .priv_data_class = &librtmps_class,
  246. .flags = URL_PROTOCOL_FLAG_NETWORK,
  247. };