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.

415 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. else
  177. break;
  178. }
  179. }
  180. if (ctx->playpath) {
  181. av_strlcat(filename, " playpath=", len);
  182. av_strlcat(filename, ctx->playpath, len);
  183. }
  184. if (ctx->live)
  185. av_strlcat(filename, " live=1", len);
  186. if (ctx->subscribe) {
  187. av_strlcat(filename, " subscribe=", len);
  188. av_strlcat(filename, ctx->subscribe, len);
  189. }
  190. if (ctx->client_buffer_time) {
  191. av_strlcat(filename, " buffer=", len);
  192. av_strlcat(filename, ctx->client_buffer_time, len);
  193. }
  194. if (ctx->swfurl || ctx->swfverify) {
  195. av_strlcat(filename, " swfUrl=", len);
  196. if (ctx->swfverify) {
  197. av_strlcat(filename, ctx->swfverify, len);
  198. av_strlcat(filename, " swfVfy=1", len);
  199. } else {
  200. av_strlcat(filename, ctx->swfurl, len);
  201. }
  202. }
  203. RTMP_Init(r);
  204. if (!RTMP_SetupURL(r, filename)) {
  205. rc = AVERROR_UNKNOWN;
  206. goto fail;
  207. }
  208. if (flags & AVIO_FLAG_WRITE)
  209. RTMP_EnableWrite(r);
  210. if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) {
  211. rc = AVERROR_UNKNOWN;
  212. goto fail;
  213. }
  214. #if CONFIG_NETWORK
  215. if (ctx->buffer_size >= 0 && (flags & AVIO_FLAG_WRITE)) {
  216. int tmp = ctx->buffer_size;
  217. setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp));
  218. }
  219. #endif
  220. s->is_streamed = 1;
  221. return 0;
  222. fail:
  223. av_freep(&ctx->temp_filename);
  224. if (rc)
  225. RTMP_Close(r);
  226. return rc;
  227. }
  228. static int rtmp_write(URLContext *s, const uint8_t *buf, int size)
  229. {
  230. LibRTMPContext *ctx = s->priv_data;
  231. RTMP *r = &ctx->rtmp;
  232. return RTMP_Write(r, buf, size);
  233. }
  234. static int rtmp_read(URLContext *s, uint8_t *buf, int size)
  235. {
  236. LibRTMPContext *ctx = s->priv_data;
  237. RTMP *r = &ctx->rtmp;
  238. return RTMP_Read(r, buf, size);
  239. }
  240. static int rtmp_read_pause(URLContext *s, int pause)
  241. {
  242. LibRTMPContext *ctx = s->priv_data;
  243. RTMP *r = &ctx->rtmp;
  244. if (!RTMP_Pause(r, pause))
  245. return AVERROR_UNKNOWN;
  246. return 0;
  247. }
  248. static int64_t rtmp_read_seek(URLContext *s, int stream_index,
  249. int64_t timestamp, int flags)
  250. {
  251. LibRTMPContext *ctx = s->priv_data;
  252. RTMP *r = &ctx->rtmp;
  253. if (flags & AVSEEK_FLAG_BYTE)
  254. return AVERROR(ENOSYS);
  255. /* seeks are in milliseconds */
  256. if (stream_index < 0)
  257. timestamp = av_rescale_rnd(timestamp, 1000, AV_TIME_BASE,
  258. flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
  259. if (!RTMP_SendSeek(r, timestamp))
  260. return AVERROR_UNKNOWN;
  261. return timestamp;
  262. }
  263. static int rtmp_get_file_handle(URLContext *s)
  264. {
  265. LibRTMPContext *ctx = s->priv_data;
  266. RTMP *r = &ctx->rtmp;
  267. return RTMP_Socket(r);
  268. }
  269. #define OFFSET(x) offsetof(LibRTMPContext, x)
  270. #define DEC AV_OPT_FLAG_DECODING_PARAM
  271. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  272. static const AVOption options[] = {
  273. {"rtmp_app", "Name of application to connect to on the RTMP server", OFFSET(app), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
  274. {"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},
  275. {"rtmp_conn", "Append arbitrary AMF data to the Connect message", OFFSET(conn), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
  276. {"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},
  277. {"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"},
  278. {"any", "both", 0, AV_OPT_TYPE_CONST, {.i64 = -2}, 0, 0, DEC, "rtmp_live"},
  279. {"live", "live stream", 0, AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0, DEC, "rtmp_live"},
  280. {"recorded", "recorded stream", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 0, DEC, "rtmp_live"},
  281. {"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},
  282. {"rtmp_playpath", "Stream identifier to play or to publish", OFFSET(playpath), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
  283. {"rtmp_subscribe", "Name of live stream to subscribe to. Defaults to rtmp_playpath.", OFFSET(subscribe), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC},
  284. {"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},
  285. {"rtmp_swfverify", "URL to player swf file, compute hash/size automatically. (unimplemented)", OFFSET(swfverify), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC},
  286. {"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},
  287. #if CONFIG_NETWORK
  288. {"rtmp_buffer_size", "set buffer size in bytes", OFFSET(buffer_size), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, DEC|ENC },
  289. #endif
  290. { NULL },
  291. };
  292. #define RTMP_CLASS(flavor)\
  293. static const AVClass lib ## flavor ## _class = {\
  294. .class_name = "lib" #flavor " protocol",\
  295. .item_name = av_default_item_name,\
  296. .option = options,\
  297. .version = LIBAVUTIL_VERSION_INT,\
  298. };
  299. RTMP_CLASS(rtmp)
  300. const URLProtocol ff_librtmp_protocol = {
  301. .name = "rtmp",
  302. .url_open = rtmp_open,
  303. .url_read = rtmp_read,
  304. .url_write = rtmp_write,
  305. .url_close = rtmp_close,
  306. .url_read_pause = rtmp_read_pause,
  307. .url_read_seek = rtmp_read_seek,
  308. .url_get_file_handle = rtmp_get_file_handle,
  309. .priv_data_size = sizeof(LibRTMPContext),
  310. .priv_data_class = &librtmp_class,
  311. .flags = URL_PROTOCOL_FLAG_NETWORK,
  312. };
  313. RTMP_CLASS(rtmpt)
  314. const URLProtocol ff_librtmpt_protocol = {
  315. .name = "rtmpt",
  316. .url_open = rtmp_open,
  317. .url_read = rtmp_read,
  318. .url_write = rtmp_write,
  319. .url_close = rtmp_close,
  320. .url_read_pause = rtmp_read_pause,
  321. .url_read_seek = rtmp_read_seek,
  322. .url_get_file_handle = rtmp_get_file_handle,
  323. .priv_data_size = sizeof(LibRTMPContext),
  324. .priv_data_class = &librtmpt_class,
  325. .flags = URL_PROTOCOL_FLAG_NETWORK,
  326. };
  327. RTMP_CLASS(rtmpe)
  328. const URLProtocol ff_librtmpe_protocol = {
  329. .name = "rtmpe",
  330. .url_open = rtmp_open,
  331. .url_read = rtmp_read,
  332. .url_write = rtmp_write,
  333. .url_close = rtmp_close,
  334. .url_read_pause = rtmp_read_pause,
  335. .url_read_seek = rtmp_read_seek,
  336. .url_get_file_handle = rtmp_get_file_handle,
  337. .priv_data_size = sizeof(LibRTMPContext),
  338. .priv_data_class = &librtmpe_class,
  339. .flags = URL_PROTOCOL_FLAG_NETWORK,
  340. };
  341. RTMP_CLASS(rtmpte)
  342. const URLProtocol ff_librtmpte_protocol = {
  343. .name = "rtmpte",
  344. .url_open = rtmp_open,
  345. .url_read = rtmp_read,
  346. .url_write = rtmp_write,
  347. .url_close = rtmp_close,
  348. .url_read_pause = rtmp_read_pause,
  349. .url_read_seek = rtmp_read_seek,
  350. .url_get_file_handle = rtmp_get_file_handle,
  351. .priv_data_size = sizeof(LibRTMPContext),
  352. .priv_data_class = &librtmpte_class,
  353. .flags = URL_PROTOCOL_FLAG_NETWORK,
  354. };
  355. RTMP_CLASS(rtmps)
  356. const URLProtocol ff_librtmps_protocol = {
  357. .name = "rtmps",
  358. .url_open = rtmp_open,
  359. .url_read = rtmp_read,
  360. .url_write = rtmp_write,
  361. .url_close = rtmp_close,
  362. .url_read_pause = rtmp_read_pause,
  363. .url_read_seek = rtmp_read_seek,
  364. .url_get_file_handle = rtmp_get_file_handle,
  365. .priv_data_size = sizeof(LibRTMPContext),
  366. .priv_data_class = &librtmps_class,
  367. .flags = URL_PROTOCOL_FLAG_NETWORK,
  368. };