2 * RTMP network protocol
3 * Copyright (c) 2010 Howard Chu
5 * This file is part of Libav.
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.
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.
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
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"
33 #include <librtmp/rtmp.h>
34 #include <librtmp/log.h>
36 typedef struct LibRTMPContext {
48 char *client_buffer_time;
53 static void rtmp_log(int level, const char *fmt, va_list args)
57 case RTMP_LOGCRIT: level = AV_LOG_FATAL; break;
58 case RTMP_LOGERROR: level = AV_LOG_ERROR; break;
59 case RTMP_LOGWARNING: level = AV_LOG_WARNING; break;
60 case RTMP_LOGINFO: level = AV_LOG_INFO; break;
61 case RTMP_LOGDEBUG: level = AV_LOG_VERBOSE; break;
62 case RTMP_LOGDEBUG2: level = AV_LOG_DEBUG; break;
65 av_vlog(NULL, level, fmt, args);
66 av_log(NULL, level, "\n");
69 static int rtmp_close(URLContext *s)
71 LibRTMPContext *ctx = s->priv_data;
75 av_freep(&ctx->temp_filename);
80 * Open RTMP connection and verify that the stream can be played.
82 * URL syntax: rtmp://server[:port][/app][/playpath][ keyword=value]...
83 * where 'app' is first one or two directories in the path
84 * (e.g. /ondemand/, /flash/live/, etc.)
85 * and 'playpath' is a file name (the rest of the path,
86 * may be prefixed with "mp4:")
88 * Additional RTMP library options may be appended as
89 * space-separated key-value pairs.
91 static int rtmp_open(URLContext *s, const char *uri, int flags)
93 LibRTMPContext *ctx = s->priv_data;
96 char *filename = s->filename;
97 int len = strlen(s->filename) + 1;
99 switch (av_log_get_level()) {
101 case AV_LOG_FATAL: level = RTMP_LOGCRIT; break;
102 case AV_LOG_ERROR: level = RTMP_LOGERROR; break;
103 case AV_LOG_WARNING: level = RTMP_LOGWARNING; break;
104 case AV_LOG_INFO: level = RTMP_LOGINFO; break;
105 case AV_LOG_VERBOSE: level = RTMP_LOGDEBUG; break;
106 case AV_LOG_DEBUG: level = RTMP_LOGDEBUG2; break;
108 RTMP_LogSetLevel(level);
109 RTMP_LogSetCallback(rtmp_log);
111 if (ctx->app) len += strlen(ctx->app) + sizeof(" app=");
112 if (ctx->tcurl) len += strlen(ctx->tcurl) + sizeof(" tcUrl=");
113 if (ctx->pageurl) len += strlen(ctx->pageurl) + sizeof(" pageUrl=");
114 if (ctx->flashver) len += strlen(ctx->flashver) + sizeof(" flashver=");
117 char *sep, *p = ctx->conn;
125 sep = strchr(p, ' ');
131 len += options * sizeof(" conn=");
132 len += strlen(ctx->conn);
136 len += strlen(ctx->playpath) + sizeof(" playpath=");
138 len += sizeof(" live=1");
140 len += strlen(ctx->subscribe) + sizeof(" subscribe=");
142 if (ctx->client_buffer_time)
143 len += strlen(ctx->client_buffer_time) + sizeof(" buffer=");
145 if (ctx->swfurl || ctx->swfverify) {
146 len += sizeof(" swfUrl=");
149 len += strlen(ctx->swfverify) + sizeof(" swfVfy=1");
151 len += strlen(ctx->swfurl);
154 if (!(ctx->temp_filename = filename = av_malloc(len)))
155 return AVERROR(ENOMEM);
157 av_strlcpy(filename, s->filename, len);
159 av_strlcat(filename, " app=", len);
160 av_strlcat(filename, ctx->app, len);
163 av_strlcat(filename, " tcUrl=", len);
164 av_strlcat(filename, ctx->tcurl, len);
167 av_strlcat(filename, " pageUrl=", len);
168 av_strlcat(filename, ctx->pageurl, len);
171 av_strlcat(filename, " swfUrl=", len);
172 av_strlcat(filename, ctx->swfurl, len);
175 av_strlcat(filename, " flashVer=", len);
176 av_strlcat(filename, ctx->flashver, len);
179 char *sep, *p = ctx->conn;
181 av_strlcat(filename, " conn=", len);
185 sep = strchr(p, ' ');
188 av_strlcat(filename, p, len);
195 av_strlcat(filename, " playpath=", len);
196 av_strlcat(filename, ctx->playpath, len);
199 av_strlcat(filename, " live=1", len);
200 if (ctx->subscribe) {
201 av_strlcat(filename, " subscribe=", len);
202 av_strlcat(filename, ctx->subscribe, len);
204 if (ctx->client_buffer_time) {
205 av_strlcat(filename, " buffer=", len);
206 av_strlcat(filename, ctx->client_buffer_time, len);
208 if (ctx->swfurl || ctx->swfverify) {
209 av_strlcat(filename, " swfUrl=", len);
211 if (ctx->swfverify) {
212 av_strlcat(filename, ctx->swfverify, len);
213 av_strlcat(filename, " swfVfy=1", len);
215 av_strlcat(filename, ctx->swfurl, len);
220 if (!RTMP_SetupURL(r, filename)) {
221 rc = AVERROR_UNKNOWN;
225 if (flags & AVIO_FLAG_WRITE)
228 if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) {
229 rc = AVERROR_UNKNOWN;
236 av_freep(&ctx->temp_filename);
240 static int rtmp_write(URLContext *s, const uint8_t *buf, int size)
242 LibRTMPContext *ctx = s->priv_data;
243 RTMP *r = &ctx->rtmp;
245 return RTMP_Write(r, buf, size);
248 static int rtmp_read(URLContext *s, uint8_t *buf, int size)
250 LibRTMPContext *ctx = s->priv_data;
251 RTMP *r = &ctx->rtmp;
253 return RTMP_Read(r, buf, size);
256 static int rtmp_read_pause(URLContext *s, int pause)
258 LibRTMPContext *ctx = s->priv_data;
259 RTMP *r = &ctx->rtmp;
261 if (!RTMP_Pause(r, pause))
262 return AVERROR_UNKNOWN;
266 static int64_t rtmp_read_seek(URLContext *s, int stream_index,
267 int64_t timestamp, int flags)
269 LibRTMPContext *ctx = s->priv_data;
270 RTMP *r = &ctx->rtmp;
272 if (flags & AVSEEK_FLAG_BYTE)
273 return AVERROR(ENOSYS);
275 /* seeks are in milliseconds */
276 if (stream_index < 0)
277 timestamp = av_rescale_rnd(timestamp, 1000, AV_TIME_BASE,
278 flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
280 if (!RTMP_SendSeek(r, timestamp))
281 return AVERROR_UNKNOWN;
285 static int rtmp_get_file_handle(URLContext *s)
287 LibRTMPContext *ctx = s->priv_data;
288 RTMP *r = &ctx->rtmp;
290 return RTMP_Socket(r);
293 #define OFFSET(x) offsetof(LibRTMPContext, x)
294 #define DEC AV_OPT_FLAG_DECODING_PARAM
295 #define ENC AV_OPT_FLAG_ENCODING_PARAM
296 static const AVOption options[] = {
297 {"rtmp_app", "Name of application to connect to on the RTMP server", OFFSET(app), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
298 {"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},
299 {"rtmp_conn", "Append arbitrary AMF data to the Connect message", OFFSET(conn), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
300 {"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},
301 {"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"},
302 {"any", "both", 0, AV_OPT_TYPE_CONST, {.i64 = -2}, 0, 0, DEC, "rtmp_live"},
303 {"live", "live stream", 0, AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0, DEC, "rtmp_live"},
304 {"recorded", "recorded stream", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 0, DEC, "rtmp_live"},
305 {"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},
306 {"rtmp_playpath", "Stream identifier to play or to publish", OFFSET(playpath), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
307 {"rtmp_subscribe", "Name of live stream to subscribe to. Defaults to rtmp_playpath.", OFFSET(subscribe), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC},
308 {"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},
309 {"rtmp_swfverify", "URL to player swf file, compute hash/size automatically. (unimplemented)", OFFSET(swfverify), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC},
310 {"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},
314 #define RTMP_CLASS(flavor)\
315 static const AVClass lib ## flavor ## _class = {\
316 .class_name = "lib" #flavor " protocol",\
317 .item_name = av_default_item_name,\
319 .version = LIBAVUTIL_VERSION_INT,\
323 URLProtocol ff_librtmp_protocol = {
325 .url_open = rtmp_open,
326 .url_read = rtmp_read,
327 .url_write = rtmp_write,
328 .url_close = rtmp_close,
329 .url_read_pause = rtmp_read_pause,
330 .url_read_seek = rtmp_read_seek,
331 .url_get_file_handle = rtmp_get_file_handle,
332 .priv_data_size = sizeof(LibRTMPContext),
333 .priv_data_class = &librtmp_class,
334 .flags = URL_PROTOCOL_FLAG_NETWORK,
338 URLProtocol ff_librtmpt_protocol = {
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 = &librtmpt_class,
349 .flags = URL_PROTOCOL_FLAG_NETWORK,
353 URLProtocol ff_librtmpe_protocol = {
355 .url_open = rtmp_open,
356 .url_read = rtmp_read,
357 .url_write = rtmp_write,
358 .url_close = rtmp_close,
359 .url_read_pause = rtmp_read_pause,
360 .url_read_seek = rtmp_read_seek,
361 .url_get_file_handle = rtmp_get_file_handle,
362 .priv_data_size = sizeof(LibRTMPContext),
363 .priv_data_class = &librtmpe_class,
364 .flags = URL_PROTOCOL_FLAG_NETWORK,
368 URLProtocol ff_librtmpte_protocol = {
370 .url_open = rtmp_open,
371 .url_read = rtmp_read,
372 .url_write = rtmp_write,
373 .url_close = rtmp_close,
374 .url_read_pause = rtmp_read_pause,
375 .url_read_seek = rtmp_read_seek,
376 .url_get_file_handle = rtmp_get_file_handle,
377 .priv_data_size = sizeof(LibRTMPContext),
378 .priv_data_class = &librtmpte_class,
379 .flags = URL_PROTOCOL_FLAG_NETWORK,
383 URLProtocol ff_librtmps_protocol = {
385 .url_open = rtmp_open,
386 .url_read = rtmp_read,
387 .url_write = rtmp_write,
388 .url_close = rtmp_close,
389 .url_read_pause = rtmp_read_pause,
390 .url_read_seek = rtmp_read_seek,
391 .url_get_file_handle = rtmp_get_file_handle,
392 .priv_data_size = sizeof(LibRTMPContext),
393 .priv_data_class = &librtmps_class,
394 .flags = URL_PROTOCOL_FLAG_NETWORK,