Implement support to RTMP, RTMPT, RTMPE, RTMPTE, RTMPS protocols via
[ffmpeg.git] / 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 libavformat/librtmp.c
24  * RTMP protocol based on http://rtmpdump.mplayerhq.hu librtmp
25  */
26
27 #include "avformat.h"
28
29 #include <librtmp/rtmp.h>
30 #include <librtmp/log.h>
31
32 static int rtmp_close(URLContext *s)
33 {
34     RTMP *r = s->priv_data;
35
36     RTMP_Close(r);
37     av_free(r);
38     return 0;
39 }
40
41 /**
42  * Opens RTMP connection and verifies that the stream can be played.
43  *
44  * URL syntax: rtmp://server[:port][/app][/playpath][ keyword=value]...
45  *             where 'app' is first one or two directories in the path
46  *             (e.g. /ondemand/, /flash/live/, etc.)
47  *             and 'playpath' is a file name (the rest of the path,
48  *             may be prefixed with "mp4:")
49  *
50  *             Additional RTMP library options may be appended as
51  *             space-separated key-value pairs.
52  */
53 static int rtmp_open(URLContext *s, const char *uri, int flags)
54 {
55     RTMP *r;
56     int rc;
57
58     r = av_mallocz(sizeof(RTMP));
59     if (!r)
60         return AVERROR(ENOMEM);
61
62     switch(av_log_get_level()) {
63     default:
64     case AV_LOG_FATAL:   rc = RTMP_LOGCRIT;    break;
65     case AV_LOG_ERROR:   rc = RTMP_LOGERROR;   break;
66     case AV_LOG_WARNING: rc = RTMP_LOGWARNING; break;
67     case AV_LOG_INFO:    rc = RTMP_LOGINFO;    break;
68     case AV_LOG_VERBOSE: rc = RTMP_LOGDEBUG;   break;
69     case AV_LOG_DEBUG:   rc = RTMP_LOGDEBUG2;  break;
70     }
71     RTMP_LogSetLevel(rc);
72
73     RTMP_Init(r);
74     if (!RTMP_SetupURL(r, s->filename)) {
75         rc = -1;
76         goto fail;
77     }
78
79     if (flags & URL_WRONLY)
80         r->Link.protocol |= RTMP_FEATURE_WRITE;
81
82     if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) {
83         rc = -1;
84         goto fail;
85     }
86
87     s->priv_data = r;
88     s->is_streamed = 1;
89     return 0;
90 fail:
91     av_free(r);
92     return rc;
93 }
94
95 static int rtmp_write(URLContext *s, uint8_t *buf, int size)
96 {
97     RTMP *r = s->priv_data;
98
99     return RTMP_Write(r, buf, size);
100 }
101
102 static int rtmp_read(URLContext *s, uint8_t *buf, int size)
103 {
104     RTMP *r = s->priv_data;
105
106     return RTMP_Read(r, buf, size);
107 }
108
109 static int rtmp_read_pause(URLContext *s, int pause)
110 {
111     RTMP *r = s->priv_data;
112
113     if (pause)
114         r->m_pauseStamp =
115             r->m_channelTimestamp[r->m_mediaChannel];
116     if (!RTMP_SendPause(r, pause, r->m_pauseStamp))
117         return -1;
118     return 0;
119 }
120
121 static int64_t rtmp_read_seek(URLContext *s, int stream_index,
122                               int64_t timestamp, int flags)
123 {
124     RTMP *r = s->priv_data;
125
126     if (flags & AVSEEK_FLAG_BYTE)
127         return AVERROR_NOTSUPP;
128
129     /* seeks are in milliseconds */
130     timestamp = av_rescale(timestamp, AV_TIME_BASE, 1000);
131     if (!RTMP_SendSeek(r, timestamp))
132         return -1;
133     return timestamp;
134 }
135
136 static int rtmp_get_file_handle(URLContext *s)
137 {
138     RTMP *r = s->priv_data;
139
140     return r->m_sb.sb_socket;
141 }
142
143 URLProtocol rtmp_protocol = {
144     "rtmp",
145     rtmp_open,
146     rtmp_read,
147     rtmp_write,
148     NULL,                   /* seek */
149     rtmp_close,
150     NULL,                   /* next */
151     rtmp_read_pause,
152     rtmp_read_seek,
153     rtmp_get_file_handle
154 };
155
156 URLProtocol rtmpt_protocol = {
157     "rtmpt",
158     rtmp_open,
159     rtmp_read,
160     rtmp_write,
161     NULL,                   /* seek */
162     rtmp_close,
163     NULL,                   /* next */
164     rtmp_read_pause,
165     rtmp_read_seek,
166     rtmp_get_file_handle
167 };
168
169 URLProtocol rtmpe_protocol = {
170     "rtmpe",
171     rtmp_open,
172     rtmp_read,
173     rtmp_write,
174     NULL,                   /* seek */
175     rtmp_close,
176     NULL,                   /* next */
177     rtmp_read_pause,
178     rtmp_read_seek,
179     rtmp_get_file_handle
180 };
181
182 URLProtocol rtmpte_protocol = {
183     "rtmpte",
184     rtmp_open,
185     rtmp_read,
186     rtmp_write,
187     NULL,                   /* seek */
188     rtmp_close,
189     NULL,                   /* next */
190     rtmp_read_pause,
191     rtmp_read_seek,
192     rtmp_get_file_handle
193 };
194
195 URLProtocol rtmps_protocol = {
196     "rtmps",
197     rtmp_open,
198     rtmp_read,
199     rtmp_write,
200     NULL,                   /* seek */
201     rtmp_close,
202     NULL,                   /* next */
203     rtmp_read_pause,
204     rtmp_read_seek,
205     rtmp_get_file_handle
206 };