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;
59 * Open RTMP connection and verify that the stream can be played.
61 * URL syntax: rtmp://server[:port][/app][/playpath][ keyword=value]...
62 * where 'app' is first one or two directories in the path
63 * (e.g. /ondemand/, /flash/live/, etc.)
64 * and 'playpath' is a file name (the rest of the path,
65 * may be prefixed with "mp4:")
67 * Additional RTMP library options may be appended as
68 * space-separated key-value pairs.
70 static int rtmp_open(URLContext *s, const char *uri, int flags)
72 RTMP *r = s->priv_data;
75 switch (av_log_get_level()) {
77 case AV_LOG_FATAL: rc = RTMP_LOGCRIT; break;
78 case AV_LOG_ERROR: rc = RTMP_LOGERROR; break;
79 case AV_LOG_WARNING: rc = RTMP_LOGWARNING; break;
80 case AV_LOG_INFO: rc = RTMP_LOGINFO; break;
81 case AV_LOG_VERBOSE: rc = RTMP_LOGDEBUG; break;
82 case AV_LOG_DEBUG: rc = RTMP_LOGDEBUG2; break;
85 RTMP_LogSetCallback(rtmp_log);
88 if (!RTMP_SetupURL(r, s->filename)) {
93 if (flags & AVIO_FLAG_WRITE)
96 if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) {
107 static int rtmp_write(URLContext *s, const uint8_t *buf, int size)
109 RTMP *r = s->priv_data;
111 return RTMP_Write(r, buf, size);
114 static int rtmp_read(URLContext *s, uint8_t *buf, int size)
116 RTMP *r = s->priv_data;
118 return RTMP_Read(r, buf, size);
121 static int rtmp_read_pause(URLContext *s, int pause)
123 RTMP *r = s->priv_data;
125 if (!RTMP_Pause(r, pause))
130 static int64_t rtmp_read_seek(URLContext *s, int stream_index,
131 int64_t timestamp, int flags)
133 RTMP *r = s->priv_data;
135 if (flags & AVSEEK_FLAG_BYTE)
136 return AVERROR(ENOSYS);
138 /* seeks are in milliseconds */
139 if (stream_index < 0)
140 timestamp = av_rescale_rnd(timestamp, 1000, AV_TIME_BASE,
141 flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
143 if (!RTMP_SendSeek(r, timestamp))
148 static int rtmp_get_file_handle(URLContext *s)
150 RTMP *r = s->priv_data;
152 return RTMP_Socket(r);
155 URLProtocol ff_librtmp_protocol = {
157 .url_open = rtmp_open,
158 .url_read = rtmp_read,
159 .url_write = rtmp_write,
160 .url_close = rtmp_close,
161 .url_read_pause = rtmp_read_pause,
162 .url_read_seek = rtmp_read_seek,
163 .url_get_file_handle = rtmp_get_file_handle,
164 .priv_data_size = sizeof(RTMP),
165 .flags = URL_PROTOCOL_FLAG_NETWORK,
168 URLProtocol ff_librtmpt_protocol = {
170 .url_open = rtmp_open,
171 .url_read = rtmp_read,
172 .url_write = rtmp_write,
173 .url_close = rtmp_close,
174 .url_read_pause = rtmp_read_pause,
175 .url_read_seek = rtmp_read_seek,
176 .url_get_file_handle = rtmp_get_file_handle,
177 .priv_data_size = sizeof(RTMP),
178 .flags = URL_PROTOCOL_FLAG_NETWORK,
181 URLProtocol ff_librtmpe_protocol = {
183 .url_open = rtmp_open,
184 .url_read = rtmp_read,
185 .url_write = rtmp_write,
186 .url_close = rtmp_close,
187 .url_read_pause = rtmp_read_pause,
188 .url_read_seek = rtmp_read_seek,
189 .url_get_file_handle = rtmp_get_file_handle,
190 .priv_data_size = sizeof(RTMP),
191 .flags = URL_PROTOCOL_FLAG_NETWORK,
194 URLProtocol ff_librtmpte_protocol = {
196 .url_open = rtmp_open,
197 .url_read = rtmp_read,
198 .url_write = rtmp_write,
199 .url_close = rtmp_close,
200 .url_read_pause = rtmp_read_pause,
201 .url_read_seek = rtmp_read_seek,
202 .url_get_file_handle = rtmp_get_file_handle,
203 .priv_data_size = sizeof(RTMP),
204 .flags = URL_PROTOCOL_FLAG_NETWORK,
207 URLProtocol ff_librtmps_protocol = {
209 .url_open = rtmp_open,
210 .url_read = rtmp_read,
211 .url_write = rtmp_write,
212 .url_close = rtmp_close,
213 .url_read_pause = rtmp_read_pause,
214 .url_read_seek = rtmp_read_seek,
215 .url_get_file_handle = rtmp_get_file_handle,
216 .priv_data_size = sizeof(RTMP),
217 .flags = URL_PROTOCOL_FLAG_NETWORK,