Statistics
| Branch: | Revision:

ffmpeg / libavformat / rtmppkt.c @ b1eb53ab

History | View | Annotate | Download (8.84 KB)

1
/*
2
 * RTMP input format
3
 * Copyright (c) 2009 Kostya Shishkov
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
#include "libavcodec/bytestream.h"
23
#include "libavutil/avstring.h"
24
#include "avformat.h"
25

    
26
#include "rtmppkt.h"
27
#include "flv.h"
28

    
29
void ff_amf_write_bool(uint8_t **dst, int val)
30
{
31
    bytestream_put_byte(dst, AMF_DATA_TYPE_BOOL);
32
    bytestream_put_byte(dst, val);
33
}
34

    
35
void ff_amf_write_number(uint8_t **dst, double val)
36
{
37
    bytestream_put_byte(dst, AMF_DATA_TYPE_NUMBER);
38
    bytestream_put_be64(dst, av_dbl2int(val));
39
}
40

    
41
void ff_amf_write_string(uint8_t **dst, const char *str)
42
{
43
    bytestream_put_byte(dst, AMF_DATA_TYPE_STRING);
44
    bytestream_put_be16(dst, strlen(str));
45
    bytestream_put_buffer(dst, str, strlen(str));
46
}
47

    
48
void ff_amf_write_null(uint8_t **dst)
49
{
50
    bytestream_put_byte(dst, AMF_DATA_TYPE_NULL);
51
}
52

    
53
void ff_amf_write_object_start(uint8_t **dst)
54
{
55
    bytestream_put_byte(dst, AMF_DATA_TYPE_OBJECT);
56
}
57

    
58
void ff_amf_write_field_name(uint8_t **dst, const char *str)
59
{
60
    bytestream_put_be16(dst, strlen(str));
61
    bytestream_put_buffer(dst, str, strlen(str));
62
}
63

    
64
void ff_amf_write_object_end(uint8_t **dst)
65
{
66
    /* first two bytes are field name length = 0,
67
     * AMF object should end with it and end marker
68
     */
69
    bytestream_put_be24(dst, AMF_DATA_TYPE_OBJECT_END);
70
}
71

    
72
int ff_rtmp_packet_read(URLContext *h, RTMPPacket *p,
73
                        int chunk_size, RTMPPacket *prev_pkt)
74
{
75
    uint8_t hdr, t, buf[16];
76
    int channel_id, timestamp, data_size, offset = 0;
77
    uint32_t extra = 0;
78
    enum RTMPPacketType type;
79

    
80
    if (url_read(h, &hdr, 1) != 1)
81
        return AVERROR(EIO);
82
    channel_id = hdr & 0x3F;
83

    
84
    if (channel_id < 2) { //special case for channel number >= 64
85
        buf[1] = 0;
86
        if (url_read_complete(h, buf, channel_id + 1) != channel_id + 1)
87
            return AVERROR(EIO);
88
        channel_id = AV_RL16(buf) + 64;
89
    }
90
    data_size = prev_pkt[channel_id].data_size;
91
    type      = prev_pkt[channel_id].type;
92
    extra     = prev_pkt[channel_id].extra;
93

    
94
    hdr >>= 6;
95
    if (hdr == RTMP_PS_ONEBYTE) {
96
        timestamp = prev_pkt[channel_id].timestamp;
97
    } else {
98
        if (url_read_complete(h, buf, 3) != 3)
99
            return AVERROR(EIO);
100
        timestamp = AV_RB24(buf);
101
        if (hdr != RTMP_PS_FOURBYTES) {
102
            if (url_read_complete(h, buf, 3) != 3)
103
                return AVERROR(EIO);
104
            data_size = AV_RB24(buf);
105
            if (url_read_complete(h, buf, 1) != 1)
106
                return AVERROR(EIO);
107
            type = buf[0];
108
            if (hdr == RTMP_PS_TWELVEBYTES) {
109
                if (url_read_complete(h, buf, 4) != 4)
110
                    return AVERROR(EIO);
111
                extra = AV_RL32(buf);
112
            }
113
        }
114
        if (timestamp == 0xFFFFFF) {
115
            if (url_read_complete(h, buf, 4) != 4)
116
                return AVERROR(EIO);
117
            timestamp = AV_RB32(buf);
118
        }
119
    }
120
    if (ff_rtmp_packet_create(p, channel_id, type, timestamp, data_size))
121
        return -1;
122
    p->extra = extra;
123
    // save history
124
    prev_pkt[channel_id].channel_id = channel_id;
125
    prev_pkt[channel_id].type       = type;
126
    prev_pkt[channel_id].data_size  = data_size;
127
    prev_pkt[channel_id].timestamp  = timestamp;
128
    prev_pkt[channel_id].extra      = extra;
129
    while (data_size > 0) {
130
        int toread = FFMIN(data_size, chunk_size);
131
        if (url_read_complete(h, p->data + offset, toread) != toread) {
132
            ff_rtmp_packet_destroy(p);
133
            return AVERROR(EIO);
134
        }
135
        data_size -= chunk_size;
136
        offset    += chunk_size;
137
        if (data_size > 0) {
138
            url_read_complete(h, &t, 1); //marker
139
            if (t != (0xC0 + channel_id))
140
                return -1;
141
        }
142
    }
143
    return 0;
144
}
145

    
146
int ff_rtmp_packet_write(URLContext *h, RTMPPacket *pkt,
147
                         int chunk_size, RTMPPacket *prev_pkt)
148
{
149
    uint8_t pkt_hdr[16], *p = pkt_hdr;
150
    int mode = RTMP_PS_TWELVEBYTES;
151
    int off = 0;
152

    
153
    //TODO: header compression
154
    if (pkt->channel_id < 64) {
155
        bytestream_put_byte(&p, pkt->channel_id | (mode << 6));
156
    } else if (pkt->channel_id < 64 + 256) {
157
        bytestream_put_byte(&p, 0               | (mode << 6));
158
        bytestream_put_byte(&p, pkt->channel_id - 64);
159
    } else {
160
        bytestream_put_byte(&p, 1               | (mode << 6));
161
        bytestream_put_le16(&p, pkt->channel_id - 64);
162
    }
163
    if (mode != RTMP_PS_ONEBYTE) {
164
        bytestream_put_be24(&p, pkt->timestamp >= 0xFFFFFF ? 0xFFFFFF : pkt->timestamp);
165
        if (mode != RTMP_PS_FOURBYTES) {
166
            bytestream_put_be24(&p, pkt->data_size);
167
            bytestream_put_byte(&p, pkt->type);
168
            if (mode == RTMP_PS_TWELVEBYTES)
169
                bytestream_put_le32(&p, pkt->extra);
170
        }
171
        if (pkt->timestamp >= 0xFFFFFF)
172
            bytestream_put_be32(&p, pkt->timestamp);
173
    }
174
    url_write(h, pkt_hdr, p-pkt_hdr);
175
    while (off < pkt->data_size) {
176
        int towrite = FFMIN(chunk_size, pkt->data_size - off);
177
        url_write(h, pkt->data + off, towrite);
178
        off += towrite;
179
        if (off < pkt->data_size) {
180
            uint8_t marker = 0xC0 | pkt->channel_id;
181
            url_write(h, &marker, 1);
182
        }
183
    }
184
    return 0;
185
}
186

    
187
int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type,
188
                          int timestamp, int size)
189
{
190
    pkt->data = av_malloc(size);
191
    if (!pkt->data)
192
        return AVERROR(ENOMEM);
193
    pkt->data_size  = size;
194
    pkt->channel_id = channel_id;
195
    pkt->type       = type;
196
    pkt->timestamp  = timestamp;
197
    pkt->extra      = 0;
198

    
199
    return 0;
200
}
201

    
202
void ff_rtmp_packet_destroy(RTMPPacket *pkt)
203
{
204
    if (!pkt)
205
        return;
206
    av_freep(&pkt->data);
207
    pkt->data_size = 0;
208
}
209

    
210
int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end)
211
{
212
    const uint8_t *base = data;
213

    
214
    if (data >= data_end)
215
        return -1;
216
    switch (*data++) {
217
    case AMF_DATA_TYPE_NUMBER:      return 9;
218
    case AMF_DATA_TYPE_BOOL:        return 2;
219
    case AMF_DATA_TYPE_STRING:      return 3 + AV_RB16(data);
220
    case AMF_DATA_TYPE_LONG_STRING: return 5 + AV_RB32(data);
221
    case AMF_DATA_TYPE_NULL:        return 1;
222
    case AMF_DATA_TYPE_ARRAY:
223
        data += 4;
224
    case AMF_DATA_TYPE_OBJECT:
225
        for (;;) {
226
            int size = bytestream_get_be16(&data);
227
            int t;
228
            if (!size) {
229
                data++;
230
                break;
231
            }
232
            if (data + size >= data_end || data + size < data)
233
                return -1;
234
            data += size;
235
            t = ff_amf_tag_size(data, data_end);
236
            if (t < 0 || data + t >= data_end)
237
                return -1;
238
            data += t;
239
        }
240
        return data - base;
241
    case AMF_DATA_TYPE_OBJECT_END:  return 1;
242
    default:                        return -1;
243
    }
244
}
245

    
246
int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
247
                           const uint8_t *name, uint8_t *dst, int dst_size)
248
{
249
    int namelen = strlen(name);
250
    int len;
251

    
252
    while (*data != AMF_DATA_TYPE_OBJECT && data < data_end) {
253
        len = ff_amf_tag_size(data, data_end);
254
        if (len < 0)
255
            len = data_end - data;
256
        data += len;
257
    }
258
    if (data_end - data < 3)
259
        return -1;
260
    data++;
261
    for (;;) {
262
        int size = bytestream_get_be16(&data);
263
        if (!size)
264
            break;
265
        if (data + size >= data_end || data + size < data)
266
            return -1;
267
        data += size;
268
        if (size == namelen && !memcmp(data-size, name, namelen)) {
269
            switch (*data++) {
270
            case AMF_DATA_TYPE_NUMBER:
271
                snprintf(dst, dst_size, "%g", av_int2dbl(AV_RB64(data)));
272
                break;
273
            case AMF_DATA_TYPE_BOOL:
274
                snprintf(dst, dst_size, "%s", *data ? "true" : "false");
275
                break;
276
            case AMF_DATA_TYPE_STRING:
277
                len = bytestream_get_be16(&data);
278
                av_strlcpy(dst, data, FFMIN(len+1, dst_size));
279
                break;
280
            default:
281
                return -1;
282
            }
283
            return 0;
284
        }
285
        len = ff_amf_tag_size(data, data_end);
286
        if (len < 0 || data + len >= data_end || data + len < data)
287
            return -1;
288
        data += len;
289
    }
290
    return -1;
291
}