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.

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