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.

398 lines
13KB

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