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.

287 lines
8.6KB

  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. if (rc)
  121. RTMP_Close(r);
  122. return rc;
  123. }
  124. static int rtmp_write(URLContext *s, const uint8_t *buf, int size)
  125. {
  126. LibRTMPContext *ctx = s->priv_data;
  127. RTMP *r = &ctx->rtmp;
  128. return RTMP_Write(r, buf, size);
  129. }
  130. static int rtmp_read(URLContext *s, uint8_t *buf, int size)
  131. {
  132. LibRTMPContext *ctx = s->priv_data;
  133. RTMP *r = &ctx->rtmp;
  134. return RTMP_Read(r, buf, size);
  135. }
  136. static int rtmp_read_pause(URLContext *s, int pause)
  137. {
  138. LibRTMPContext *ctx = s->priv_data;
  139. RTMP *r = &ctx->rtmp;
  140. if (!RTMP_Pause(r, pause))
  141. return AVERROR_UNKNOWN;
  142. return 0;
  143. }
  144. static int64_t rtmp_read_seek(URLContext *s, int stream_index,
  145. int64_t timestamp, int flags)
  146. {
  147. LibRTMPContext *ctx = s->priv_data;
  148. RTMP *r = &ctx->rtmp;
  149. if (flags & AVSEEK_FLAG_BYTE)
  150. return AVERROR(ENOSYS);
  151. /* seeks are in milliseconds */
  152. if (stream_index < 0)
  153. timestamp = av_rescale_rnd(timestamp, 1000, AV_TIME_BASE,
  154. flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
  155. if (!RTMP_SendSeek(r, timestamp))
  156. return AVERROR_UNKNOWN;
  157. return timestamp;
  158. }
  159. static int rtmp_get_file_handle(URLContext *s)
  160. {
  161. LibRTMPContext *ctx = s->priv_data;
  162. RTMP *r = &ctx->rtmp;
  163. return RTMP_Socket(r);
  164. }
  165. #define OFFSET(x) offsetof(LibRTMPContext, x)
  166. #define DEC AV_OPT_FLAG_DECODING_PARAM
  167. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  168. static const AVOption options[] = {
  169. {"rtmp_app", "Name of application to connect to on the RTMP server", OFFSET(app), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
  170. {"rtmp_playpath", "Stream identifier to play or to publish", OFFSET(playpath), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
  171. { NULL },
  172. };
  173. #define RTMP_CLASS(flavor)\
  174. static const AVClass lib ## flavor ## _class = {\
  175. .class_name = "lib" #flavor " protocol",\
  176. .item_name = av_default_item_name,\
  177. .option = options,\
  178. .version = LIBAVUTIL_VERSION_INT,\
  179. };
  180. RTMP_CLASS(rtmp)
  181. URLProtocol ff_librtmp_protocol = {
  182. .name = "rtmp",
  183. .url_open = rtmp_open,
  184. .url_read = rtmp_read,
  185. .url_write = rtmp_write,
  186. .url_close = rtmp_close,
  187. .url_read_pause = rtmp_read_pause,
  188. .url_read_seek = rtmp_read_seek,
  189. .url_get_file_handle = rtmp_get_file_handle,
  190. .priv_data_size = sizeof(LibRTMPContext),
  191. .priv_data_class = &librtmp_class,
  192. .flags = URL_PROTOCOL_FLAG_NETWORK,
  193. };
  194. RTMP_CLASS(rtmpt)
  195. URLProtocol ff_librtmpt_protocol = {
  196. .name = "rtmpt",
  197. .url_open = rtmp_open,
  198. .url_read = rtmp_read,
  199. .url_write = rtmp_write,
  200. .url_close = rtmp_close,
  201. .url_read_pause = rtmp_read_pause,
  202. .url_read_seek = rtmp_read_seek,
  203. .url_get_file_handle = rtmp_get_file_handle,
  204. .priv_data_size = sizeof(LibRTMPContext),
  205. .priv_data_class = &librtmpt_class,
  206. .flags = URL_PROTOCOL_FLAG_NETWORK,
  207. };
  208. RTMP_CLASS(rtmpe)
  209. URLProtocol ff_librtmpe_protocol = {
  210. .name = "rtmpe",
  211. .url_open = rtmp_open,
  212. .url_read = rtmp_read,
  213. .url_write = rtmp_write,
  214. .url_close = rtmp_close,
  215. .url_read_pause = rtmp_read_pause,
  216. .url_read_seek = rtmp_read_seek,
  217. .url_get_file_handle = rtmp_get_file_handle,
  218. .priv_data_size = sizeof(LibRTMPContext),
  219. .priv_data_class = &librtmpe_class,
  220. .flags = URL_PROTOCOL_FLAG_NETWORK,
  221. };
  222. RTMP_CLASS(rtmpte)
  223. URLProtocol ff_librtmpte_protocol = {
  224. .name = "rtmpte",
  225. .url_open = rtmp_open,
  226. .url_read = rtmp_read,
  227. .url_write = rtmp_write,
  228. .url_close = rtmp_close,
  229. .url_read_pause = rtmp_read_pause,
  230. .url_read_seek = rtmp_read_seek,
  231. .url_get_file_handle = rtmp_get_file_handle,
  232. .priv_data_size = sizeof(LibRTMPContext),
  233. .priv_data_class = &librtmpte_class,
  234. .flags = URL_PROTOCOL_FLAG_NETWORK,
  235. };
  236. RTMP_CLASS(rtmps)
  237. URLProtocol ff_librtmps_protocol = {
  238. .name = "rtmps",
  239. .url_open = rtmp_open,
  240. .url_read = rtmp_read,
  241. .url_write = rtmp_write,
  242. .url_close = rtmp_close,
  243. .url_read_pause = rtmp_read_pause,
  244. .url_read_seek = rtmp_read_seek,
  245. .url_get_file_handle = rtmp_get_file_handle,
  246. .priv_data_size = sizeof(LibRTMPContext),
  247. .priv_data_class = &librtmps_class,
  248. .flags = URL_PROTOCOL_FLAG_NETWORK,
  249. };