Statistics
| Branch: | Revision:

ffmpeg / libavformat / librtmp.c @ f35ff97f

History | View | Annotate | Download (5.81 KB)

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 "avformat.h"
28

    
29
#include <librtmp/rtmp.h>
30
#include <librtmp/log.h>
31

    
32
static void rtmp_log(int level, const char *fmt, va_list args)
33
{
34
    switch (level) {
35
    default:
36
    case RTMP_LOGCRIT:    level = AV_LOG_FATAL;   break;
37
    case RTMP_LOGERROR:   level = AV_LOG_ERROR;   break;
38
    case RTMP_LOGWARNING: level = AV_LOG_WARNING; break;
39
    case RTMP_LOGINFO:    level = AV_LOG_INFO;    break;
40
    case RTMP_LOGDEBUG:   level = AV_LOG_VERBOSE; break;
41
    case RTMP_LOGDEBUG2:  level = AV_LOG_DEBUG;   break;
42
    }
43

    
44
    av_vlog(NULL, level, fmt, args);
45
    av_log(NULL, level, "\n");
46
}
47

    
48
static int rtmp_close(URLContext *s)
49
{
50
    RTMP *r = s->priv_data;
51

    
52
    RTMP_Close(r);
53
    av_free(r);
54
    return 0;
55
}
56

    
57
/**
58
 * Open RTMP connection and verify that the stream can be played.
59
 *
60
 * URL syntax: rtmp://server[:port][/app][/playpath][ keyword=value]...
61
 *             where 'app' is first one or two directories in the path
62
 *             (e.g. /ondemand/, /flash/live/, etc.)
63
 *             and 'playpath' is a file name (the rest of the path,
64
 *             may be prefixed with "mp4:")
65
 *
66
 *             Additional RTMP library options may be appended as
67
 *             space-separated key-value pairs.
68
 */
69
static int rtmp_open(URLContext *s, const char *uri, int flags)
70
{
71
    RTMP *r;
72
    int rc;
73

    
74
    r = av_mallocz(sizeof(RTMP));
75
    if (!r)
76
        return AVERROR(ENOMEM);
77

    
78
    switch (av_log_get_level()) {
79
    default:
80
    case AV_LOG_FATAL:   rc = RTMP_LOGCRIT;    break;
81
    case AV_LOG_ERROR:   rc = RTMP_LOGERROR;   break;
82
    case AV_LOG_WARNING: rc = RTMP_LOGWARNING; break;
83
    case AV_LOG_INFO:    rc = RTMP_LOGINFO;    break;
84
    case AV_LOG_VERBOSE: rc = RTMP_LOGDEBUG;   break;
85
    case AV_LOG_DEBUG:   rc = RTMP_LOGDEBUG2;  break;
86
    }
87
    RTMP_LogSetLevel(rc);
88
    RTMP_LogSetCallback(rtmp_log);
89

    
90
    RTMP_Init(r);
91
    if (!RTMP_SetupURL(r, s->filename)) {
92
        rc = -1;
93
        goto fail;
94
    }
95

    
96
    if (flags & AVIO_WRONLY)
97
        RTMP_EnableWrite(r);
98

    
99
    if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) {
100
        rc = -1;
101
        goto fail;
102
    }
103

    
104
    s->priv_data   = r;
105
    s->is_streamed = 1;
106
    return 0;
107
fail:
108
    av_free(r);
109
    return rc;
110
}
111

    
112
static int rtmp_write(URLContext *s, const uint8_t *buf, int size)
113
{
114
    RTMP *r = s->priv_data;
115

    
116
    return RTMP_Write(r, buf, size);
117
}
118

    
119
static int rtmp_read(URLContext *s, uint8_t *buf, int size)
120
{
121
    RTMP *r = s->priv_data;
122

    
123
    return RTMP_Read(r, buf, size);
124
}
125

    
126
static int rtmp_read_pause(URLContext *s, int pause)
127
{
128
    RTMP *r = s->priv_data;
129

    
130
    if (!RTMP_Pause(r, pause))
131
        return -1;
132
    return 0;
133
}
134

    
135
static int64_t rtmp_read_seek(URLContext *s, int stream_index,
136
                              int64_t timestamp, int flags)
137
{
138
    RTMP *r = s->priv_data;
139

    
140
    if (flags & AVSEEK_FLAG_BYTE)
141
        return AVERROR(ENOSYS);
142

    
143
    /* seeks are in milliseconds */
144
    if (stream_index < 0)
145
        timestamp = av_rescale_rnd(timestamp, 1000, AV_TIME_BASE,
146
            flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
147

    
148
    if (!RTMP_SendSeek(r, timestamp))
149
        return -1;
150
    return timestamp;
151
}
152

    
153
static int rtmp_get_file_handle(URLContext *s)
154
{
155
    RTMP *r = s->priv_data;
156

    
157
    return RTMP_Socket(r);
158
}
159

    
160
URLProtocol ff_rtmp_protocol = {
161
    .name                = "rtmp",
162
    .url_open            = rtmp_open,
163
    .url_read            = rtmp_read,
164
    .url_write           = rtmp_write,
165
    .url_close           = rtmp_close,
166
    .url_read_pause      = rtmp_read_pause,
167
    .url_read_seek       = rtmp_read_seek,
168
    .url_get_file_handle = rtmp_get_file_handle
169
};
170

    
171
URLProtocol ff_rtmpt_protocol = {
172
    .name                = "rtmpt",
173
    .url_open            = rtmp_open,
174
    .url_read            = rtmp_read,
175
    .url_write           = rtmp_write,
176
    .url_close           = rtmp_close,
177
    .url_read_pause      = rtmp_read_pause,
178
    .url_read_seek       = rtmp_read_seek,
179
    .url_get_file_handle = rtmp_get_file_handle
180
};
181

    
182
URLProtocol ff_rtmpe_protocol = {
183
    .name                = "rtmpe",
184
    .url_open            = rtmp_open,
185
    .url_read            = rtmp_read,
186
    .url_write           = rtmp_write,
187
    .url_close           = rtmp_close,
188
    .url_read_pause      = rtmp_read_pause,
189
    .url_read_seek       = rtmp_read_seek,
190
    .url_get_file_handle = rtmp_get_file_handle
191
};
192

    
193
URLProtocol ff_rtmpte_protocol = {
194
    .name                = "rtmpte",
195
    .url_open            = rtmp_open,
196
    .url_read            = rtmp_read,
197
    .url_write           = rtmp_write,
198
    .url_close           = rtmp_close,
199
    .url_read_pause      = rtmp_read_pause,
200
    .url_read_seek       = rtmp_read_seek,
201
    .url_get_file_handle = rtmp_get_file_handle
202
};
203

    
204
URLProtocol ff_rtmps_protocol = {
205
    .name                = "rtmps",
206
    .url_open            = rtmp_open,
207
    .url_read            = rtmp_read,
208
    .url_write           = rtmp_write,
209
    .url_close           = rtmp_close,
210
    .url_read_pause      = rtmp_read_pause,
211
    .url_read_seek       = rtmp_read_seek,
212
    .url_get_file_handle = rtmp_get_file_handle
213
};