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"
33 #include <librtmp/rtmp.h>
34 #include <librtmp/log.h>
36 typedef struct LibRTMPContext {
43 static void rtmp_log(int level, const char *fmt, va_list args)
47 case RTMP_LOGCRIT: level = AV_LOG_FATAL; break;
48 case RTMP_LOGERROR: level = AV_LOG_ERROR; break;
49 case RTMP_LOGWARNING: level = AV_LOG_WARNING; break;
50 case RTMP_LOGINFO: level = AV_LOG_INFO; break;
51 case RTMP_LOGDEBUG: level = AV_LOG_VERBOSE; break;
52 case RTMP_LOGDEBUG2: level = AV_LOG_DEBUG; break;
55 av_vlog(NULL, level, fmt, args);
56 av_log(NULL, level, "\n");
59 static int rtmp_close(URLContext *s)
61 LibRTMPContext *ctx = s->priv_data;
69 * Open RTMP connection and verify that the stream can be played.
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:")
77 * Additional RTMP library options may be appended as
78 * space-separated key-value pairs.
80 static int rtmp_open(URLContext *s, const char *uri, int flags)
82 LibRTMPContext *ctx = s->priv_data;
85 char *filename = s->filename;
87 switch (av_log_get_level()) {
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;
96 RTMP_LogSetLevel(level);
97 RTMP_LogSetCallback(rtmp_log);
99 if (ctx->app || ctx->playpath) {
100 int len = strlen(s->filename) + 1;
101 if (ctx->app) len += strlen(ctx->app) + sizeof(" app=");
102 if (ctx->playpath) len += strlen(ctx->playpath) + sizeof(" playpath=");
104 if (!(filename = av_malloc(len)))
105 return AVERROR(ENOMEM);
107 av_strlcpy(filename, s->filename, len);
109 av_strlcat(filename, " app=", len);
110 av_strlcat(filename, ctx->app, len);
113 av_strlcat(filename, " playpath=", len);
114 av_strlcat(filename, ctx->playpath, len);
119 if (!RTMP_SetupURL(r, filename)) {
120 rc = AVERROR_UNKNOWN;
124 if (flags & AVIO_FLAG_WRITE)
127 if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) {
128 rc = AVERROR_UNKNOWN;
135 if (filename != s->filename)
143 static int rtmp_write(URLContext *s, const uint8_t *buf, int size)
145 LibRTMPContext *ctx = s->priv_data;
146 RTMP *r = &ctx->rtmp;
148 return RTMP_Write(r, buf, size);
151 static int rtmp_read(URLContext *s, uint8_t *buf, int size)
153 LibRTMPContext *ctx = s->priv_data;
154 RTMP *r = &ctx->rtmp;
156 return RTMP_Read(r, buf, size);
159 static int rtmp_read_pause(URLContext *s, int pause)
161 LibRTMPContext *ctx = s->priv_data;
162 RTMP *r = &ctx->rtmp;
164 if (!RTMP_Pause(r, pause))
165 return AVERROR_UNKNOWN;
169 static int64_t rtmp_read_seek(URLContext *s, int stream_index,
170 int64_t timestamp, int flags)
172 LibRTMPContext *ctx = s->priv_data;
173 RTMP *r = &ctx->rtmp;
175 if (flags & AVSEEK_FLAG_BYTE)
176 return AVERROR(ENOSYS);
178 /* seeks are in milliseconds */
179 if (stream_index < 0)
180 timestamp = av_rescale_rnd(timestamp, 1000, AV_TIME_BASE,
181 flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
183 if (!RTMP_SendSeek(r, timestamp))
184 return AVERROR_UNKNOWN;
188 static int rtmp_get_file_handle(URLContext *s)
190 LibRTMPContext *ctx = s->priv_data;
191 RTMP *r = &ctx->rtmp;
193 return RTMP_Socket(r);
196 #define OFFSET(x) offsetof(LibRTMPContext, x)
197 #define DEC AV_OPT_FLAG_DECODING_PARAM
198 #define ENC AV_OPT_FLAG_ENCODING_PARAM
199 static const AVOption options[] = {
200 {"rtmp_app", "Name of application to connect to on the RTMP server", OFFSET(app), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
201 {"rtmp_playpath", "Stream identifier to play or to publish", OFFSET(playpath), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
205 #define RTMP_CLASS(flavor)\
206 static const AVClass lib ## flavor ## _class = {\
207 .class_name = "lib" #flavor " protocol",\
208 .item_name = av_default_item_name,\
210 .version = LIBAVUTIL_VERSION_INT,\
214 URLProtocol ff_librtmp_protocol = {
216 .url_open = rtmp_open,
217 .url_read = rtmp_read,
218 .url_write = rtmp_write,
219 .url_close = rtmp_close,
220 .url_read_pause = rtmp_read_pause,
221 .url_read_seek = rtmp_read_seek,
222 .url_get_file_handle = rtmp_get_file_handle,
223 .priv_data_size = sizeof(LibRTMPContext),
224 .priv_data_class = &librtmp_class,
225 .flags = URL_PROTOCOL_FLAG_NETWORK,
229 URLProtocol ff_librtmpt_protocol = {
231 .url_open = rtmp_open,
232 .url_read = rtmp_read,
233 .url_write = rtmp_write,
234 .url_close = rtmp_close,
235 .url_read_pause = rtmp_read_pause,
236 .url_read_seek = rtmp_read_seek,
237 .url_get_file_handle = rtmp_get_file_handle,
238 .priv_data_size = sizeof(LibRTMPContext),
239 .priv_data_class = &librtmpt_class,
240 .flags = URL_PROTOCOL_FLAG_NETWORK,
244 URLProtocol ff_librtmpe_protocol = {
246 .url_open = rtmp_open,
247 .url_read = rtmp_read,
248 .url_write = rtmp_write,
249 .url_close = rtmp_close,
250 .url_read_pause = rtmp_read_pause,
251 .url_read_seek = rtmp_read_seek,
252 .url_get_file_handle = rtmp_get_file_handle,
253 .priv_data_size = sizeof(LibRTMPContext),
254 .priv_data_class = &librtmpe_class,
255 .flags = URL_PROTOCOL_FLAG_NETWORK,
259 URLProtocol ff_librtmpte_protocol = {
261 .url_open = rtmp_open,
262 .url_read = rtmp_read,
263 .url_write = rtmp_write,
264 .url_close = rtmp_close,
265 .url_read_pause = rtmp_read_pause,
266 .url_read_seek = rtmp_read_seek,
267 .url_get_file_handle = rtmp_get_file_handle,
268 .priv_data_size = sizeof(LibRTMPContext),
269 .priv_data_class = &librtmpte_class,
270 .flags = URL_PROTOCOL_FLAG_NETWORK,
274 URLProtocol ff_librtmps_protocol = {
276 .url_open = rtmp_open,
277 .url_read = rtmp_read,
278 .url_write = rtmp_write,
279 .url_close = rtmp_close,
280 .url_read_pause = rtmp_read_pause,
281 .url_read_seek = rtmp_read_seek,
282 .url_get_file_handle = rtmp_get_file_handle,
283 .priv_data_size = sizeof(LibRTMPContext),
284 .priv_data_class = &librtmps_class,
285 .flags = URL_PROTOCOL_FLAG_NETWORK,