OSDN Git Service

libx264: fix setting the H.264 level
[coroid/libav_saccubus.git] / libavformat / librtmp.c
1 /*
2  * RTMP network protocol
3  * Copyright (c) 2010 Howard Chu
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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/mathematics.h"
28 #include "avformat.h"
29 #include "url.h"
30
31 #include <librtmp/rtmp.h>
32 #include <librtmp/log.h>
33
34 static void rtmp_log(int level, const char *fmt, va_list args)
35 {
36     switch (level) {
37     default:
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;
44     }
45
46     av_vlog(NULL, level, fmt, args);
47     av_log(NULL, level, "\n");
48 }
49
50 static int rtmp_close(URLContext *s)
51 {
52     RTMP *r = s->priv_data;
53
54     RTMP_Close(r);
55     av_free(r);
56     return 0;
57 }
58
59 /**
60  * Open RTMP connection and verify that the stream can be played.
61  *
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:")
67  *
68  *             Additional RTMP library options may be appended as
69  *             space-separated key-value pairs.
70  */
71 static int rtmp_open(URLContext *s, const char *uri, int flags)
72 {
73     RTMP *r;
74     int rc;
75
76     r = av_mallocz(sizeof(RTMP));
77     if (!r)
78         return AVERROR(ENOMEM);
79
80     switch (av_log_get_level()) {
81     default:
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;
88     }
89     RTMP_LogSetLevel(rc);
90     RTMP_LogSetCallback(rtmp_log);
91
92     RTMP_Init(r);
93     if (!RTMP_SetupURL(r, s->filename)) {
94         rc = -1;
95         goto fail;
96     }
97
98     if (flags & AVIO_FLAG_WRITE)
99         RTMP_EnableWrite(r);
100
101     if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) {
102         rc = -1;
103         goto fail;
104     }
105
106     s->priv_data   = r;
107     s->is_streamed = 1;
108     return 0;
109 fail:
110     av_free(r);
111     return rc;
112 }
113
114 static int rtmp_write(URLContext *s, const uint8_t *buf, int size)
115 {
116     RTMP *r = s->priv_data;
117
118     return RTMP_Write(r, buf, size);
119 }
120
121 static int rtmp_read(URLContext *s, uint8_t *buf, int size)
122 {
123     RTMP *r = s->priv_data;
124
125     return RTMP_Read(r, buf, size);
126 }
127
128 static int rtmp_read_pause(URLContext *s, int pause)
129 {
130     RTMP *r = s->priv_data;
131
132     if (!RTMP_Pause(r, pause))
133         return -1;
134     return 0;
135 }
136
137 static int64_t rtmp_read_seek(URLContext *s, int stream_index,
138                               int64_t timestamp, int flags)
139 {
140     RTMP *r = s->priv_data;
141
142     if (flags & AVSEEK_FLAG_BYTE)
143         return AVERROR(ENOSYS);
144
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);
149
150     if (!RTMP_SendSeek(r, timestamp))
151         return -1;
152     return timestamp;
153 }
154
155 static int rtmp_get_file_handle(URLContext *s)
156 {
157     RTMP *r = s->priv_data;
158
159     return RTMP_Socket(r);
160 }
161
162 URLProtocol ff_rtmp_protocol = {
163     .name                = "rtmp",
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
171 };
172
173 URLProtocol ff_rtmpt_protocol = {
174     .name                = "rtmpt",
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
182 };
183
184 URLProtocol ff_rtmpe_protocol = {
185     .name                = "rtmpe",
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
193 };
194
195 URLProtocol ff_rtmpte_protocol = {
196     .name                = "rtmpte",
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
204 };
205
206 URLProtocol ff_rtmps_protocol = {
207     .name                = "rtmps",
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
215 };