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