You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

413 lines
13KB

  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. #if CONFIG_NETWORK
  30. #include "network.h"
  31. #endif
  32. #include "url.h"
  33. #include <librtmp/rtmp.h>
  34. #include <librtmp/log.h>
  35. typedef struct LibRTMPContext {
  36. const AVClass *class;
  37. RTMP rtmp;
  38. char *app;
  39. char *conn;
  40. char *subscribe;
  41. char *playpath;
  42. char *tcurl;
  43. char *flashver;
  44. char *swfurl;
  45. char *swfverify;
  46. char *pageurl;
  47. char *client_buffer_time;
  48. int live;
  49. char *temp_filename;
  50. int buffer_size;
  51. } LibRTMPContext;
  52. static void rtmp_log(int level, const char *fmt, va_list args)
  53. {
  54. switch (level) {
  55. default:
  56. case RTMP_LOGCRIT: level = AV_LOG_FATAL; break;
  57. case RTMP_LOGERROR: level = AV_LOG_ERROR; break;
  58. case RTMP_LOGWARNING: level = AV_LOG_WARNING; break;
  59. case RTMP_LOGINFO: level = AV_LOG_INFO; break;
  60. case RTMP_LOGDEBUG: level = AV_LOG_VERBOSE; break;
  61. case RTMP_LOGDEBUG2: level = AV_LOG_DEBUG; break;
  62. }
  63. av_vlog(NULL, level, fmt, args);
  64. av_log(NULL, level, "\n");
  65. }
  66. static int rtmp_close(URLContext *s)
  67. {
  68. LibRTMPContext *ctx = s->priv_data;
  69. RTMP *r = &ctx->rtmp;
  70. RTMP_Close(r);
  71. av_freep(&ctx->temp_filename);
  72. return 0;
  73. }
  74. /**
  75. * Open RTMP connection and verify that the stream can be played.
  76. *
  77. * URL syntax: rtmp://server[:port][/app][/playpath][ keyword=value]...
  78. * where 'app' is first one or two directories in the path
  79. * (e.g. /ondemand/, /flash/live/, etc.)
  80. * and 'playpath' is a file name (the rest of the path,
  81. * may be prefixed with "mp4:")
  82. *
  83. * Additional RTMP library options may be appended as
  84. * space-separated key-value pairs.
  85. */
  86. static int rtmp_open(URLContext *s, const char *uri, int flags)
  87. {
  88. LibRTMPContext *ctx = s->priv_data;
  89. RTMP *r = &ctx->rtmp;
  90. int rc = 0, level;
  91. char *filename = s->filename;
  92. int len = strlen(s->filename) + 1;
  93. switch (av_log_get_level()) {
  94. default:
  95. case AV_LOG_FATAL: level = RTMP_LOGCRIT; break;
  96. case AV_LOG_ERROR: level = RTMP_LOGERROR; break;
  97. case AV_LOG_WARNING: level = RTMP_LOGWARNING; break;
  98. case AV_LOG_INFO: level = RTMP_LOGINFO; break;
  99. case AV_LOG_VERBOSE: level = RTMP_LOGDEBUG; break;
  100. case AV_LOG_DEBUG: level = RTMP_LOGDEBUG2; break;
  101. }
  102. RTMP_LogSetLevel(level);
  103. RTMP_LogSetCallback(rtmp_log);
  104. if (ctx->app) len += strlen(ctx->app) + sizeof(" app=");
  105. if (ctx->tcurl) len += strlen(ctx->tcurl) + sizeof(" tcUrl=");
  106. if (ctx->pageurl) len += strlen(ctx->pageurl) + sizeof(" pageUrl=");
  107. if (ctx->flashver) len += strlen(ctx->flashver) + sizeof(" flashver=");
  108. if (ctx->conn) {
  109. char *sep, *p = ctx->conn;
  110. int options = 0;
  111. while (p) {
  112. options++;
  113. p += strspn(p, " ");
  114. if (!*p)
  115. break;
  116. sep = strchr(p, ' ');
  117. if (sep)
  118. p = sep + 1;
  119. else
  120. break;
  121. }
  122. len += options * sizeof(" conn=");
  123. len += strlen(ctx->conn);
  124. }
  125. if (ctx->playpath)
  126. len += strlen(ctx->playpath) + sizeof(" playpath=");
  127. if (ctx->live)
  128. len += sizeof(" live=1");
  129. if (ctx->subscribe)
  130. len += strlen(ctx->subscribe) + sizeof(" subscribe=");
  131. if (ctx->client_buffer_time)
  132. len += strlen(ctx->client_buffer_time) + sizeof(" buffer=");
  133. if (ctx->swfurl || ctx->swfverify) {
  134. len += sizeof(" swfUrl=");
  135. if (ctx->swfverify)
  136. len += strlen(ctx->swfverify) + sizeof(" swfVfy=1");
  137. else
  138. len += strlen(ctx->swfurl);
  139. }
  140. if (!(ctx->temp_filename = filename = av_malloc(len)))
  141. return AVERROR(ENOMEM);
  142. av_strlcpy(filename, s->filename, len);
  143. if (ctx->app) {
  144. av_strlcat(filename, " app=", len);
  145. av_strlcat(filename, ctx->app, len);
  146. }
  147. if (ctx->tcurl) {
  148. av_strlcat(filename, " tcUrl=", len);
  149. av_strlcat(filename, ctx->tcurl, len);
  150. }
  151. if (ctx->pageurl) {
  152. av_strlcat(filename, " pageUrl=", len);
  153. av_strlcat(filename, ctx->pageurl, len);
  154. }
  155. if (ctx->swfurl) {
  156. av_strlcat(filename, " swfUrl=", len);
  157. av_strlcat(filename, ctx->swfurl, len);
  158. }
  159. if (ctx->flashver) {
  160. av_strlcat(filename, " flashVer=", len);
  161. av_strlcat(filename, ctx->flashver, len);
  162. }
  163. if (ctx->conn) {
  164. char *sep, *p = ctx->conn;
  165. while (p) {
  166. av_strlcat(filename, " conn=", len);
  167. p += strspn(p, " ");
  168. if (!*p)
  169. break;
  170. sep = strchr(p, ' ');
  171. if (sep)
  172. *sep = '\0';
  173. av_strlcat(filename, p, len);
  174. if (sep)
  175. p = sep + 1;
  176. }
  177. }
  178. if (ctx->playpath) {
  179. av_strlcat(filename, " playpath=", len);
  180. av_strlcat(filename, ctx->playpath, len);
  181. }
  182. if (ctx->live)
  183. av_strlcat(filename, " live=1", len);
  184. if (ctx->subscribe) {
  185. av_strlcat(filename, " subscribe=", len);
  186. av_strlcat(filename, ctx->subscribe, len);
  187. }
  188. if (ctx->client_buffer_time) {
  189. av_strlcat(filename, " buffer=", len);
  190. av_strlcat(filename, ctx->client_buffer_time, len);
  191. }
  192. if (ctx->swfurl || ctx->swfverify) {
  193. av_strlcat(filename, " swfUrl=", len);
  194. if (ctx->swfverify) {
  195. av_strlcat(filename, ctx->swfverify, len);
  196. av_strlcat(filename, " swfVfy=1", len);
  197. } else {
  198. av_strlcat(filename, ctx->swfurl, len);
  199. }
  200. }
  201. RTMP_Init(r);
  202. if (!RTMP_SetupURL(r, filename)) {
  203. rc = AVERROR_UNKNOWN;
  204. goto fail;
  205. }
  206. if (flags & AVIO_FLAG_WRITE)
  207. RTMP_EnableWrite(r);
  208. if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) {
  209. rc = AVERROR_UNKNOWN;
  210. goto fail;
  211. }
  212. #if CONFIG_NETWORK
  213. if (ctx->buffer_size >= 0 && (flags & AVIO_FLAG_WRITE)) {
  214. int tmp = ctx->buffer_size;
  215. setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp));
  216. }
  217. #endif
  218. s->is_streamed = 1;
  219. return 0;
  220. fail:
  221. av_freep(&ctx->temp_filename);
  222. if (rc)
  223. RTMP_Close(r);
  224. return rc;
  225. }
  226. static int rtmp_write(URLContext *s, const uint8_t *buf, int size)
  227. {
  228. LibRTMPContext *ctx = s->priv_data;
  229. RTMP *r = &ctx->rtmp;
  230. return RTMP_Write(r, buf, size);
  231. }
  232. static int rtmp_read(URLContext *s, uint8_t *buf, int size)
  233. {
  234. LibRTMPContext *ctx = s->priv_data;
  235. RTMP *r = &ctx->rtmp;
  236. return RTMP_Read(r, buf, size);
  237. }
  238. static int rtmp_read_pause(URLContext *s, int pause)
  239. {
  240. LibRTMPContext *ctx = s->priv_data;
  241. RTMP *r = &ctx->rtmp;
  242. if (!RTMP_Pause(r, pause))
  243. return AVERROR_UNKNOWN;
  244. return 0;
  245. }
  246. static int64_t rtmp_read_seek(URLContext *s, int stream_index,
  247. int64_t timestamp, int flags)
  248. {
  249. LibRTMPContext *ctx = s->priv_data;
  250. RTMP *r = &ctx->rtmp;
  251. if (flags & AVSEEK_FLAG_BYTE)
  252. return AVERROR(ENOSYS);
  253. /* seeks are in milliseconds */
  254. if (stream_index < 0)
  255. timestamp = av_rescale_rnd(timestamp, 1000, AV_TIME_BASE,
  256. flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
  257. if (!RTMP_SendSeek(r, timestamp))
  258. return AVERROR_UNKNOWN;
  259. return timestamp;
  260. }
  261. static int rtmp_get_file_handle(URLContext *s)
  262. {
  263. LibRTMPContext *ctx = s->priv_data;
  264. RTMP *r = &ctx->rtmp;
  265. return RTMP_Socket(r);
  266. }
  267. #define OFFSET(x) offsetof(LibRTMPContext, x)
  268. #define DEC AV_OPT_FLAG_DECODING_PARAM
  269. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  270. static const AVOption options[] = {
  271. {"rtmp_app", "Name of application to connect to on the RTMP server", OFFSET(app), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
  272. {"rtmp_buffer", "Set buffer time in milliseconds. The default is 3000.", OFFSET(client_buffer_time), AV_OPT_TYPE_STRING, {.str = "3000"}, 0, 0, DEC|ENC},
  273. {"rtmp_conn", "Append arbitrary AMF data to the Connect message", OFFSET(conn), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
  274. {"rtmp_flashver", "Version of the Flash plugin used to run the SWF player.", OFFSET(flashver), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
  275. {"rtmp_live", "Specify that the media is a live stream.", OFFSET(live), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, DEC, "rtmp_live"},
  276. {"any", "both", 0, AV_OPT_TYPE_CONST, {.i64 = -2}, 0, 0, DEC, "rtmp_live"},
  277. {"live", "live stream", 0, AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0, DEC, "rtmp_live"},
  278. {"recorded", "recorded stream", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 0, DEC, "rtmp_live"},
  279. {"rtmp_pageurl", "URL of the web page in which the media was embedded. By default no value will be sent.", OFFSET(pageurl), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC},
  280. {"rtmp_playpath", "Stream identifier to play or to publish", OFFSET(playpath), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
  281. {"rtmp_subscribe", "Name of live stream to subscribe to. Defaults to rtmp_playpath.", OFFSET(subscribe), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC},
  282. {"rtmp_swfurl", "URL of the SWF player. By default no value will be sent", OFFSET(swfurl), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
  283. {"rtmp_swfverify", "URL to player swf file, compute hash/size automatically. (unimplemented)", OFFSET(swfverify), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC},
  284. {"rtmp_tcurl", "URL of the target stream. Defaults to proto://host[:port]/app.", OFFSET(tcurl), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
  285. #if CONFIG_NETWORK
  286. {"rtmp_buffer_size", "set buffer size in bytes", OFFSET(buffer_size), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, DEC|ENC },
  287. #endif
  288. { NULL },
  289. };
  290. #define RTMP_CLASS(flavor)\
  291. static const AVClass lib ## flavor ## _class = {\
  292. .class_name = "lib" #flavor " protocol",\
  293. .item_name = av_default_item_name,\
  294. .option = options,\
  295. .version = LIBAVUTIL_VERSION_INT,\
  296. };
  297. RTMP_CLASS(rtmp)
  298. URLProtocol ff_librtmp_protocol = {
  299. .name = "rtmp",
  300. .url_open = rtmp_open,
  301. .url_read = rtmp_read,
  302. .url_write = rtmp_write,
  303. .url_close = rtmp_close,
  304. .url_read_pause = rtmp_read_pause,
  305. .url_read_seek = rtmp_read_seek,
  306. .url_get_file_handle = rtmp_get_file_handle,
  307. .priv_data_size = sizeof(LibRTMPContext),
  308. .priv_data_class = &librtmp_class,
  309. .flags = URL_PROTOCOL_FLAG_NETWORK,
  310. };
  311. RTMP_CLASS(rtmpt)
  312. URLProtocol ff_librtmpt_protocol = {
  313. .name = "rtmpt",
  314. .url_open = rtmp_open,
  315. .url_read = rtmp_read,
  316. .url_write = rtmp_write,
  317. .url_close = rtmp_close,
  318. .url_read_pause = rtmp_read_pause,
  319. .url_read_seek = rtmp_read_seek,
  320. .url_get_file_handle = rtmp_get_file_handle,
  321. .priv_data_size = sizeof(LibRTMPContext),
  322. .priv_data_class = &librtmpt_class,
  323. .flags = URL_PROTOCOL_FLAG_NETWORK,
  324. };
  325. RTMP_CLASS(rtmpe)
  326. URLProtocol ff_librtmpe_protocol = {
  327. .name = "rtmpe",
  328. .url_open = rtmp_open,
  329. .url_read = rtmp_read,
  330. .url_write = rtmp_write,
  331. .url_close = rtmp_close,
  332. .url_read_pause = rtmp_read_pause,
  333. .url_read_seek = rtmp_read_seek,
  334. .url_get_file_handle = rtmp_get_file_handle,
  335. .priv_data_size = sizeof(LibRTMPContext),
  336. .priv_data_class = &librtmpe_class,
  337. .flags = URL_PROTOCOL_FLAG_NETWORK,
  338. };
  339. RTMP_CLASS(rtmpte)
  340. URLProtocol ff_librtmpte_protocol = {
  341. .name = "rtmpte",
  342. .url_open = rtmp_open,
  343. .url_read = rtmp_read,
  344. .url_write = rtmp_write,
  345. .url_close = rtmp_close,
  346. .url_read_pause = rtmp_read_pause,
  347. .url_read_seek = rtmp_read_seek,
  348. .url_get_file_handle = rtmp_get_file_handle,
  349. .priv_data_size = sizeof(LibRTMPContext),
  350. .priv_data_class = &librtmpte_class,
  351. .flags = URL_PROTOCOL_FLAG_NETWORK,
  352. };
  353. RTMP_CLASS(rtmps)
  354. URLProtocol ff_librtmps_protocol = {
  355. .name = "rtmps",
  356. .url_open = rtmp_open,
  357. .url_read = rtmp_read,
  358. .url_write = rtmp_write,
  359. .url_close = rtmp_close,
  360. .url_read_pause = rtmp_read_pause,
  361. .url_read_seek = rtmp_read_seek,
  362. .url_get_file_handle = rtmp_get_file_handle,
  363. .priv_data_size = sizeof(LibRTMPContext),
  364. .priv_data_class = &librtmps_class,
  365. .flags = URL_PROTOCOL_FLAG_NETWORK,
  366. };