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/mathematics.h"
31 #include <librtmp/rtmp.h>
32 #include <librtmp/log.h>
34 static void rtmp_log(int level, const char *fmt, va_list args)
38 case RTMP_LOGCRIT: level = AV_LOG_FATAL; break;
39 case RTMP_LOGERROR: level = AV_LOG_ERROR; break;
40 case RTMP_LOGWARNING: level = AV_LOG_WARNING; break;
41 case RTMP_LOGINFO: level = AV_LOG_INFO; break;
42 case RTMP_LOGDEBUG: level = AV_LOG_VERBOSE; break;
43 case RTMP_LOGDEBUG2: level = AV_LOG_DEBUG; break;
46 av_vlog(NULL, level, fmt, args);
47 av_log(NULL, level, "\n");
50 static int rtmp_close(URLContext *s)
52 RTMP *r = s->priv_data;
60 * Open RTMP connection and verify that the stream can be played.
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:")
68 * Additional RTMP library options may be appended as
69 * space-separated key-value pairs.
71 static int rtmp_open(URLContext *s, const char *uri, int flags)
76 r = av_mallocz(sizeof(RTMP));
78 return AVERROR(ENOMEM);
80 switch (av_log_get_level()) {
82 case AV_LOG_FATAL: rc = RTMP_LOGCRIT; break;
83 case AV_LOG_ERROR: rc = RTMP_LOGERROR; break;
84 case AV_LOG_WARNING: rc = RTMP_LOGWARNING; break;
85 case AV_LOG_INFO: rc = RTMP_LOGINFO; break;
86 case AV_LOG_VERBOSE: rc = RTMP_LOGDEBUG; break;
87 case AV_LOG_DEBUG: rc = RTMP_LOGDEBUG2; break;
90 RTMP_LogSetCallback(rtmp_log);
93 if (!RTMP_SetupURL(r, s->filename)) {
98 if (flags & AVIO_FLAG_WRITE)
101 if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) {
114 static int rtmp_write(URLContext *s, const uint8_t *buf, int size)
116 RTMP *r = s->priv_data;
118 return RTMP_Write(r, buf, size);
121 static int rtmp_read(URLContext *s, uint8_t *buf, int size)
123 RTMP *r = s->priv_data;
125 return RTMP_Read(r, buf, size);
128 static int rtmp_read_pause(URLContext *s, int pause)
130 RTMP *r = s->priv_data;
132 if (!RTMP_Pause(r, pause))
137 static int64_t rtmp_read_seek(URLContext *s, int stream_index,
138 int64_t timestamp, int flags)
140 RTMP *r = s->priv_data;
142 if (flags & AVSEEK_FLAG_BYTE)
143 return AVERROR(ENOSYS);
145 /* seeks are in milliseconds */
146 if (stream_index < 0)
147 timestamp = av_rescale_rnd(timestamp, 1000, AV_TIME_BASE,
148 flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
150 if (!RTMP_SendSeek(r, timestamp))
155 static int rtmp_get_file_handle(URLContext *s)
157 RTMP *r = s->priv_data;
159 return RTMP_Socket(r);
162 URLProtocol ff_rtmp_protocol = {
164 .url_open = rtmp_open,
165 .url_read = rtmp_read,
166 .url_write = rtmp_write,
167 .url_close = rtmp_close,
168 .url_read_pause = rtmp_read_pause,
169 .url_read_seek = rtmp_read_seek,
170 .url_get_file_handle = rtmp_get_file_handle
173 URLProtocol ff_rtmpt_protocol = {
175 .url_open = rtmp_open,
176 .url_read = rtmp_read,
177 .url_write = rtmp_write,
178 .url_close = rtmp_close,
179 .url_read_pause = rtmp_read_pause,
180 .url_read_seek = rtmp_read_seek,
181 .url_get_file_handle = rtmp_get_file_handle
184 URLProtocol ff_rtmpe_protocol = {
186 .url_open = rtmp_open,
187 .url_read = rtmp_read,
188 .url_write = rtmp_write,
189 .url_close = rtmp_close,
190 .url_read_pause = rtmp_read_pause,
191 .url_read_seek = rtmp_read_seek,
192 .url_get_file_handle = rtmp_get_file_handle
195 URLProtocol ff_rtmpte_protocol = {
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
206 URLProtocol ff_rtmps_protocol = {
208 .url_open = rtmp_open,
209 .url_read = rtmp_read,
210 .url_write = rtmp_write,
211 .url_close = rtmp_close,
212 .url_read_pause = rtmp_read_pause,
213 .url_read_seek = rtmp_read_seek,
214 .url_get_file_handle = rtmp_get_file_handle