~ubuntu-branches/ubuntu/oneiric/libav/oneiric

« back to all changes in this revision

Viewing changes to libavformat/librtmp.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2011-03-20 12:09:31 UTC
  • Revision ID: james.westby@ubuntu.com-20110320120931-nfhi9tiok27gxhw1
Tags: upstream-0.6.2
ImportĀ upstreamĀ versionĀ 0.6.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
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
 * Opens RTMP connection and verifies 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 & URL_WRONLY)
 
97
        r->Link.protocol |= RTMP_FEATURE_WRITE;
 
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, 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 (pause)
 
131
        r->m_pauseStamp =
 
132
            r->m_channelTimestamp[r->m_mediaChannel];
 
133
    if (!RTMP_SendPause(r, pause, r->m_pauseStamp))
 
134
        return -1;
 
135
    return 0;
 
136
}
 
137
 
 
138
static int64_t rtmp_read_seek(URLContext *s, int stream_index,
 
139
                              int64_t timestamp, int flags)
 
140
{
 
141
    RTMP *r = s->priv_data;
 
142
 
 
143
    if (flags & AVSEEK_FLAG_BYTE)
 
144
        return AVERROR(ENOSYS);
 
145
 
 
146
    /* seeks are in milliseconds */
 
147
    if (stream_index < 0)
 
148
        timestamp = av_rescale_rnd(timestamp, 1000, AV_TIME_BASE,
 
149
            flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
 
150
 
 
151
    if (!RTMP_SendSeek(r, timestamp))
 
152
        return -1;
 
153
    return timestamp;
 
154
}
 
155
 
 
156
static int rtmp_get_file_handle(URLContext *s)
 
157
{
 
158
    RTMP *r = s->priv_data;
 
159
 
 
160
    return r->m_sb.sb_socket;
 
161
}
 
162
 
 
163
URLProtocol rtmp_protocol = {
 
164
    "rtmp",
 
165
    rtmp_open,
 
166
    rtmp_read,
 
167
    rtmp_write,
 
168
    NULL,                   /* seek */
 
169
    rtmp_close,
 
170
    NULL,                   /* next */
 
171
    rtmp_read_pause,
 
172
    rtmp_read_seek,
 
173
    rtmp_get_file_handle
 
174
};
 
175
 
 
176
URLProtocol rtmpt_protocol = {
 
177
    "rtmpt",
 
178
    rtmp_open,
 
179
    rtmp_read,
 
180
    rtmp_write,
 
181
    NULL,                   /* seek */
 
182
    rtmp_close,
 
183
    NULL,                   /* next */
 
184
    rtmp_read_pause,
 
185
    rtmp_read_seek,
 
186
    rtmp_get_file_handle
 
187
};
 
188
 
 
189
URLProtocol rtmpe_protocol = {
 
190
    "rtmpe",
 
191
    rtmp_open,
 
192
    rtmp_read,
 
193
    rtmp_write,
 
194
    NULL,                   /* seek */
 
195
    rtmp_close,
 
196
    NULL,                   /* next */
 
197
    rtmp_read_pause,
 
198
    rtmp_read_seek,
 
199
    rtmp_get_file_handle
 
200
};
 
201
 
 
202
URLProtocol rtmpte_protocol = {
 
203
    "rtmpte",
 
204
    rtmp_open,
 
205
    rtmp_read,
 
206
    rtmp_write,
 
207
    NULL,                   /* seek */
 
208
    rtmp_close,
 
209
    NULL,                   /* next */
 
210
    rtmp_read_pause,
 
211
    rtmp_read_seek,
 
212
    rtmp_get_file_handle
 
213
};
 
214
 
 
215
URLProtocol rtmps_protocol = {
 
216
    "rtmps",
 
217
    rtmp_open,
 
218
    rtmp_read,
 
219
    rtmp_write,
 
220
    NULL,                   /* seek */
 
221
    rtmp_close,
 
222
    NULL,                   /* next */
 
223
    rtmp_read_pause,
 
224
    rtmp_read_seek,
 
225
    rtmp_get_file_handle
 
226
};