2 * RTMP network protocol
3 * Copyright (c) 2010 Howard Chu
5 * This file is part of FFmpeg.
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.
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.
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
24 * RTMP protocol based on http://rtmpdump.mplayerhq.hu/ librtmp
27 #include "libavutil/avstring.h"
28 #include "libavutil/mathematics.h"
29 #include "libavutil/opt.h"
36 #include <librtmp/rtmp.h>
37 #include <librtmp/log.h>
39 typedef struct LibRTMPContext {
51 char *client_buffer_time;
57 static void rtmp_log(int level, const char *fmt, va_list args)
61 case RTMP_LOGCRIT: level = AV_LOG_FATAL; break;
62 case RTMP_LOGERROR: level = AV_LOG_ERROR; break;
63 case RTMP_LOGWARNING: level = AV_LOG_WARNING; break;
64 case RTMP_LOGINFO: level = AV_LOG_INFO; break;
65 case RTMP_LOGDEBUG: level = AV_LOG_VERBOSE; break;
66 case RTMP_LOGDEBUG2: level = AV_LOG_DEBUG; break;
69 av_vlog(NULL, level, fmt, args);
70 av_log(NULL, level, "\n");
73 static int rtmp_close(URLContext *s)
75 LibRTMPContext *ctx = s->priv_data;
79 av_freep(&ctx->temp_filename);
84 * Open RTMP connection and verify that the stream can be played.
86 * URL syntax: rtmp://server[:port][/app][/playpath][ keyword=value]...
87 * where 'app' is first one or two directories in the path
88 * (e.g. /ondemand/, /flash/live/, etc.)
89 * and 'playpath' is a file name (the rest of the path,
90 * may be prefixed with "mp4:")
92 * Additional RTMP library options may be appended as
93 * space-separated key-value pairs.
95 static int rtmp_open(URLContext *s, const char *uri, int flags)
97 LibRTMPContext *ctx = s->priv_data;
100 char *filename = s->filename;
101 int len = strlen(s->filename) + 1;
103 switch (av_log_get_level()) {
105 case AV_LOG_FATAL: level = RTMP_LOGCRIT; break;
106 case AV_LOG_ERROR: level = RTMP_LOGERROR; break;
107 case AV_LOG_WARNING: level = RTMP_LOGWARNING; break;
108 case AV_LOG_INFO: level = RTMP_LOGINFO; break;
109 case AV_LOG_VERBOSE: level = RTMP_LOGDEBUG; break;
110 case AV_LOG_DEBUG: level = RTMP_LOGDEBUG2; break;
112 RTMP_LogSetLevel(level);
113 RTMP_LogSetCallback(rtmp_log);
115 if (ctx->app) len += strlen(ctx->app) + sizeof(" app=");
116 if (ctx->tcurl) len += strlen(ctx->tcurl) + sizeof(" tcUrl=");
117 if (ctx->pageurl) len += strlen(ctx->pageurl) + sizeof(" pageUrl=");
118 if (ctx->flashver) len += strlen(ctx->flashver) + sizeof(" flashver=");
121 char *sep, *p = ctx->conn;
129 sep = strchr(p, ' ');
135 len += options * sizeof(" conn=");
136 len += strlen(ctx->conn);
140 len += strlen(ctx->playpath) + sizeof(" playpath=");
142 len += sizeof(" live=1");
144 len += strlen(ctx->subscribe) + sizeof(" subscribe=");
146 if (ctx->client_buffer_time)
147 len += strlen(ctx->client_buffer_time) + sizeof(" buffer=");
149 if (ctx->swfurl || ctx->swfverify) {
150 len += sizeof(" swfUrl=");
153 len += strlen(ctx->swfverify) + sizeof(" swfVfy=1");
155 len += strlen(ctx->swfurl);
158 if (!(ctx->temp_filename = filename = av_malloc(len)))
159 return AVERROR(ENOMEM);
161 av_strlcpy(filename, s->filename, len);
163 av_strlcat(filename, " app=", len);
164 av_strlcat(filename, ctx->app, len);
167 av_strlcat(filename, " tcUrl=", len);
168 av_strlcat(filename, ctx->tcurl, len);
171 av_strlcat(filename, " pageUrl=", len);
172 av_strlcat(filename, ctx->pageurl, len);
175 av_strlcat(filename, " swfUrl=", len);
176 av_strlcat(filename, ctx->swfurl, len);
179 av_strlcat(filename, " flashVer=", len);
180 av_strlcat(filename, ctx->flashver, len);
183 char *sep, *p = ctx->conn;
185 av_strlcat(filename, " conn=", len);
189 sep = strchr(p, ' ');
192 av_strlcat(filename, p, len);
199 av_strlcat(filename, " playpath=", len);
200 av_strlcat(filename, ctx->playpath, len);
203 av_strlcat(filename, " live=1", len);
204 if (ctx->subscribe) {
205 av_strlcat(filename, " subscribe=", len);
206 av_strlcat(filename, ctx->subscribe, len);
208 if (ctx->client_buffer_time) {
209 av_strlcat(filename, " buffer=", len);
210 av_strlcat(filename, ctx->client_buffer_time, len);
212 if (ctx->swfurl || ctx->swfverify) {
213 av_strlcat(filename, " swfUrl=", len);
215 if (ctx->swfverify) {
216 av_strlcat(filename, ctx->swfverify, len);
217 av_strlcat(filename, " swfVfy=1", len);
219 av_strlcat(filename, ctx->swfurl, len);
224 if (!RTMP_SetupURL(r, filename)) {
225 rc = AVERROR_UNKNOWN;
229 if (flags & AVIO_FLAG_WRITE)
232 if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) {
233 rc = AVERROR_UNKNOWN;
238 if (ctx->buffer_size >= 0 && (flags & AVIO_FLAG_WRITE)) {
239 int tmp = ctx->buffer_size;
240 setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp));
247 av_freep(&ctx->temp_filename);
254 static int rtmp_write(URLContext *s, const uint8_t *buf, int size)
256 LibRTMPContext *ctx = s->priv_data;
257 RTMP *r = &ctx->rtmp;
259 return RTMP_Write(r, buf, size);
262 static int rtmp_read(URLContext *s, uint8_t *buf, int size)
264 LibRTMPContext *ctx = s->priv_data;
265 RTMP *r = &ctx->rtmp;
267 return RTMP_Read(r, buf, size);
270 static int rtmp_read_pause(URLContext *s, int pause)
272 LibRTMPContext *ctx = s->priv_data;
273 RTMP *r = &ctx->rtmp;
275 if (!RTMP_Pause(r, pause))
276 return AVERROR_UNKNOWN;
280 static int64_t rtmp_read_seek(URLContext *s, int stream_index,
281 int64_t timestamp, int flags)
283 LibRTMPContext *ctx = s->priv_data;
284 RTMP *r = &ctx->rtmp;
286 if (flags & AVSEEK_FLAG_BYTE)
287 return AVERROR(ENOSYS);
289 /* seeks are in milliseconds */
290 if (stream_index < 0)
291 timestamp = av_rescale_rnd(timestamp, 1000, AV_TIME_BASE,
292 flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
294 if (!RTMP_SendSeek(r, timestamp))
295 return AVERROR_UNKNOWN;
299 static int rtmp_get_file_handle(URLContext *s)
301 LibRTMPContext *ctx = s->priv_data;
302 RTMP *r = &ctx->rtmp;
304 return RTMP_Socket(r);
307 #define OFFSET(x) offsetof(LibRTMPContext, x)
308 #define DEC AV_OPT_FLAG_DECODING_PARAM
309 #define ENC AV_OPT_FLAG_ENCODING_PARAM
310 static const AVOption options[] = {
311 {"rtmp_app", "Name of application to connect to on the RTMP server", OFFSET(app), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
312 {"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},
313 {"rtmp_conn", "Append arbitrary AMF data to the Connect message", OFFSET(conn), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
314 {"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},
315 {"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"},
316 {"any", "both", 0, AV_OPT_TYPE_CONST, {.i64 = -2}, 0, 0, DEC, "rtmp_live"},
317 {"live", "live stream", 0, AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0, DEC, "rtmp_live"},
318 {"recorded", "recorded stream", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 0, DEC, "rtmp_live"},
319 {"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},
320 {"rtmp_playpath", "Stream identifier to play or to publish", OFFSET(playpath), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
321 {"rtmp_subscribe", "Name of live stream to subscribe to. Defaults to rtmp_playpath.", OFFSET(subscribe), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC},
322 {"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},
323 {"rtmp_swfverify", "URL to player swf file, compute hash/size automatically. (unimplemented)", OFFSET(swfverify), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC},
324 {"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},
326 {"rtmp_buffer_size", "set buffer size in bytes", OFFSET(buffer_size), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, DEC|ENC },
331 #define RTMP_CLASS(flavor)\
332 static const AVClass lib ## flavor ## _class = {\
333 .class_name = "lib" #flavor " protocol",\
334 .item_name = av_default_item_name,\
336 .version = LIBAVUTIL_VERSION_INT,\
340 URLProtocol ff_librtmp_protocol = {
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 = &librtmp_class,
351 .flags = URL_PROTOCOL_FLAG_NETWORK,
355 URLProtocol ff_librtmpt_protocol = {
357 .url_open = rtmp_open,
358 .url_read = rtmp_read,
359 .url_write = rtmp_write,
360 .url_close = rtmp_close,
361 .url_read_pause = rtmp_read_pause,
362 .url_read_seek = rtmp_read_seek,
363 .url_get_file_handle = rtmp_get_file_handle,
364 .priv_data_size = sizeof(LibRTMPContext),
365 .priv_data_class = &librtmpt_class,
366 .flags = URL_PROTOCOL_FLAG_NETWORK,
370 URLProtocol ff_librtmpe_protocol = {
372 .url_open = rtmp_open,
373 .url_read = rtmp_read,
374 .url_write = rtmp_write,
375 .url_close = rtmp_close,
376 .url_read_pause = rtmp_read_pause,
377 .url_read_seek = rtmp_read_seek,
378 .url_get_file_handle = rtmp_get_file_handle,
379 .priv_data_size = sizeof(LibRTMPContext),
380 .priv_data_class = &librtmpe_class,
381 .flags = URL_PROTOCOL_FLAG_NETWORK,
385 URLProtocol ff_librtmpte_protocol = {
387 .url_open = rtmp_open,
388 .url_read = rtmp_read,
389 .url_write = rtmp_write,
390 .url_close = rtmp_close,
391 .url_read_pause = rtmp_read_pause,
392 .url_read_seek = rtmp_read_seek,
393 .url_get_file_handle = rtmp_get_file_handle,
394 .priv_data_size = sizeof(LibRTMPContext),
395 .priv_data_class = &librtmpte_class,
396 .flags = URL_PROTOCOL_FLAG_NETWORK,
400 URLProtocol ff_librtmps_protocol = {
402 .url_open = rtmp_open,
403 .url_read = rtmp_read,
404 .url_write = rtmp_write,
405 .url_close = rtmp_close,
406 .url_read_pause = rtmp_read_pause,
407 .url_read_seek = rtmp_read_seek,
408 .url_get_file_handle = rtmp_get_file_handle,
409 .priv_data_size = sizeof(LibRTMPContext),
410 .priv_data_class = &librtmps_class,
411 .flags = URL_PROTOCOL_FLAG_NETWORK,