Statistics
| Branch: | Revision:

ffmpeg / libavformat / rtmppkt.c @ bc371aca

History | View | Annotate | Download (14.3 KB)

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

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

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

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

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

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

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

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

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

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

    
82
    if (ffurl_read(h, &hdr, 1) != 1)
83
        return AVERROR(EIO);
84
    size++;
85
    channel_id = hdr & 0x3F;
86

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

    
98
    hdr >>= 6;
99
    if (hdr == RTMP_PS_ONEBYTE) {
100
        timestamp = prev_pkt[channel_id].ts_delta;
101
    } else {
102
        if (url_read_complete(h, buf, 3) != 3)
103
            return AVERROR(EIO);
104
        size += 3;
105
        timestamp = AV_RB24(buf);
106
        if (hdr != RTMP_PS_FOURBYTES) {
107
            if (url_read_complete(h, buf, 3) != 3)
108
                return AVERROR(EIO);
109
            size += 3;
110
            data_size = AV_RB24(buf);
111
            if (url_read_complete(h, buf, 1) != 1)
112
                return AVERROR(EIO);
113
            size++;
114
            type = buf[0];
115
            if (hdr == RTMP_PS_TWELVEBYTES) {
116
                if (url_read_complete(h, buf, 4) != 4)
117
                    return AVERROR(EIO);
118
                size += 4;
119
                extra = AV_RL32(buf);
120
            }
121
        }
122
        if (timestamp == 0xFFFFFF) {
123
            if (url_read_complete(h, buf, 4) != 4)
124
                return AVERROR(EIO);
125
            timestamp = AV_RB32(buf);
126
        }
127
    }
128
    if (hdr != RTMP_PS_TWELVEBYTES)
129
        timestamp += prev_pkt[channel_id].timestamp;
130

    
131
    if (ff_rtmp_packet_create(p, channel_id, type, timestamp, data_size))
132
        return -1;
133
    p->extra = extra;
134
    // save history
135
    prev_pkt[channel_id].channel_id = channel_id;
136
    prev_pkt[channel_id].type       = type;
137
    prev_pkt[channel_id].data_size  = data_size;
138
    prev_pkt[channel_id].ts_delta   = timestamp - prev_pkt[channel_id].timestamp;
139
    prev_pkt[channel_id].timestamp  = timestamp;
140
    prev_pkt[channel_id].extra      = extra;
141
    while (data_size > 0) {
142
        int toread = FFMIN(data_size, chunk_size);
143
        if (url_read_complete(h, p->data + offset, toread) != toread) {
144
            ff_rtmp_packet_destroy(p);
145
            return AVERROR(EIO);
146
        }
147
        data_size -= chunk_size;
148
        offset    += chunk_size;
149
        size      += chunk_size;
150
        if (data_size > 0) {
151
            url_read_complete(h, &t, 1); //marker
152
            size++;
153
            if (t != (0xC0 + channel_id))
154
                return -1;
155
        }
156
    }
157
    return size;
158
}
159

    
160
int ff_rtmp_packet_write(URLContext *h, RTMPPacket *pkt,
161
                         int chunk_size, RTMPPacket *prev_pkt)
162
{
163
    uint8_t pkt_hdr[16], *p = pkt_hdr;
164
    int mode = RTMP_PS_TWELVEBYTES;
165
    int off = 0;
166
    int size = 0;
167

    
168
    pkt->ts_delta = pkt->timestamp - prev_pkt[pkt->channel_id].timestamp;
169

    
170
    //if channel_id = 0, this is first presentation of prev_pkt, send full hdr.
171
    if (prev_pkt[pkt->channel_id].channel_id &&
172
        pkt->extra == prev_pkt[pkt->channel_id].extra) {
173
        if (pkt->type == prev_pkt[pkt->channel_id].type &&
174
            pkt->data_size == prev_pkt[pkt->channel_id].data_size) {
175
            mode = RTMP_PS_FOURBYTES;
176
            if (pkt->ts_delta == prev_pkt[pkt->channel_id].ts_delta)
177
                mode = RTMP_PS_ONEBYTE;
178
        } else {
179
            mode = RTMP_PS_EIGHTBYTES;
180
        }
181
    }
182

    
183
    if (pkt->channel_id < 64) {
184
        bytestream_put_byte(&p, pkt->channel_id | (mode << 6));
185
    } else if (pkt->channel_id < 64 + 256) {
186
        bytestream_put_byte(&p, 0               | (mode << 6));
187
        bytestream_put_byte(&p, pkt->channel_id - 64);
188
    } else {
189
        bytestream_put_byte(&p, 1               | (mode << 6));
190
        bytestream_put_le16(&p, pkt->channel_id - 64);
191
    }
192
    if (mode != RTMP_PS_ONEBYTE) {
193
        uint32_t timestamp = pkt->timestamp;
194
        if (mode != RTMP_PS_TWELVEBYTES)
195
            timestamp = pkt->ts_delta;
196
        bytestream_put_be24(&p, timestamp >= 0xFFFFFF ? 0xFFFFFF : timestamp);
197
        if (mode != RTMP_PS_FOURBYTES) {
198
            bytestream_put_be24(&p, pkt->data_size);
199
            bytestream_put_byte(&p, pkt->type);
200
            if (mode == RTMP_PS_TWELVEBYTES)
201
                bytestream_put_le32(&p, pkt->extra);
202
        }
203
        if (timestamp >= 0xFFFFFF)
204
            bytestream_put_be32(&p, timestamp);
205
    }
206
    // save history
207
    prev_pkt[pkt->channel_id].channel_id = pkt->channel_id;
208
    prev_pkt[pkt->channel_id].type       = pkt->type;
209
    prev_pkt[pkt->channel_id].data_size  = pkt->data_size;
210
    prev_pkt[pkt->channel_id].timestamp  = pkt->timestamp;
211
    if (mode != RTMP_PS_TWELVEBYTES) {
212
        prev_pkt[pkt->channel_id].ts_delta   = pkt->ts_delta;
213
    } else {
214
        prev_pkt[pkt->channel_id].ts_delta   = pkt->timestamp;
215
    }
216
    prev_pkt[pkt->channel_id].extra      = pkt->extra;
217

    
218
    url_write(h, pkt_hdr, p-pkt_hdr);
219
    size = p - pkt_hdr + pkt->data_size;
220
    while (off < pkt->data_size) {
221
        int towrite = FFMIN(chunk_size, pkt->data_size - off);
222
        url_write(h, pkt->data + off, towrite);
223
        off += towrite;
224
        if (off < pkt->data_size) {
225
            uint8_t marker = 0xC0 | pkt->channel_id;
226
            url_write(h, &marker, 1);
227
            size++;
228
        }
229
    }
230
    return size;
231
}
232

    
233
int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type,
234
                          int timestamp, int size)
235
{
236
    pkt->data = av_malloc(size);
237
    if (!pkt->data)
238
        return AVERROR(ENOMEM);
239
    pkt->data_size  = size;
240
    pkt->channel_id = channel_id;
241
    pkt->type       = type;
242
    pkt->timestamp  = timestamp;
243
    pkt->extra      = 0;
244
    pkt->ts_delta   = 0;
245

    
246
    return 0;
247
}
248

    
249
void ff_rtmp_packet_destroy(RTMPPacket *pkt)
250
{
251
    if (!pkt)
252
        return;
253
    av_freep(&pkt->data);
254
    pkt->data_size = 0;
255
}
256

    
257
int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end)
258
{
259
    const uint8_t *base = data;
260

    
261
    if (data >= data_end)
262
        return -1;
263
    switch (*data++) {
264
    case AMF_DATA_TYPE_NUMBER:      return 9;
265
    case AMF_DATA_TYPE_BOOL:        return 2;
266
    case AMF_DATA_TYPE_STRING:      return 3 + AV_RB16(data);
267
    case AMF_DATA_TYPE_LONG_STRING: return 5 + AV_RB32(data);
268
    case AMF_DATA_TYPE_NULL:        return 1;
269
    case AMF_DATA_TYPE_ARRAY:
270
        data += 4;
271
    case AMF_DATA_TYPE_OBJECT:
272
        for (;;) {
273
            int size = bytestream_get_be16(&data);
274
            int t;
275
            if (!size) {
276
                data++;
277
                break;
278
            }
279
            if (data + size >= data_end || data + size < data)
280
                return -1;
281
            data += size;
282
            t = ff_amf_tag_size(data, data_end);
283
            if (t < 0 || data + t >= data_end)
284
                return -1;
285
            data += t;
286
        }
287
        return data - base;
288
    case AMF_DATA_TYPE_OBJECT_END:  return 1;
289
    default:                        return -1;
290
    }
291
}
292

    
293
int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
294
                           const uint8_t *name, uint8_t *dst, int dst_size)
295
{
296
    int namelen = strlen(name);
297
    int len;
298

    
299
    while (*data != AMF_DATA_TYPE_OBJECT && data < data_end) {
300
        len = ff_amf_tag_size(data, data_end);
301
        if (len < 0)
302
            len = data_end - data;
303
        data += len;
304
    }
305
    if (data_end - data < 3)
306
        return -1;
307
    data++;
308
    for (;;) {
309
        int size = bytestream_get_be16(&data);
310
        if (!size)
311
            break;
312
        if (data + size >= data_end || data + size < data)
313
            return -1;
314
        data += size;
315
        if (size == namelen && !memcmp(data-size, name, namelen)) {
316
            switch (*data++) {
317
            case AMF_DATA_TYPE_NUMBER:
318
                snprintf(dst, dst_size, "%g", av_int2dbl(AV_RB64(data)));
319
                break;
320
            case AMF_DATA_TYPE_BOOL:
321
                snprintf(dst, dst_size, "%s", *data ? "true" : "false");
322
                break;
323
            case AMF_DATA_TYPE_STRING:
324
                len = bytestream_get_be16(&data);
325
                av_strlcpy(dst, data, FFMIN(len+1, dst_size));
326
                break;
327
            default:
328
                return -1;
329
            }
330
            return 0;
331
        }
332
        len = ff_amf_tag_size(data, data_end);
333
        if (len < 0 || data + len >= data_end || data + len < data)
334
            return -1;
335
        data += len;
336
    }
337
    return -1;
338
}
339

    
340
static const char* rtmp_packet_type(int type)
341
{
342
    switch (type) {
343
    case RTMP_PT_CHUNK_SIZE:     return "chunk size";
344
    case RTMP_PT_BYTES_READ:     return "bytes read";
345
    case RTMP_PT_PING:           return "ping";
346
    case RTMP_PT_SERVER_BW:      return "server bandwidth";
347
    case RTMP_PT_CLIENT_BW:      return "client bandwidth";
348
    case RTMP_PT_AUDIO:          return "audio packet";
349
    case RTMP_PT_VIDEO:          return "video packet";
350
    case RTMP_PT_FLEX_STREAM:    return "Flex shared stream";
351
    case RTMP_PT_FLEX_OBJECT:    return "Flex shared object";
352
    case RTMP_PT_FLEX_MESSAGE:   return "Flex shared message";
353
    case RTMP_PT_NOTIFY:         return "notification";
354
    case RTMP_PT_SHARED_OBJ:     return "shared object";
355
    case RTMP_PT_INVOKE:         return "invoke";
356
    case RTMP_PT_METADATA:       return "metadata";
357
    default:                     return "unknown";
358
    }
359
}
360

    
361
static void ff_amf_tag_contents(void *ctx, const uint8_t *data, const uint8_t *data_end)
362
{
363
    int size;
364
    char buf[1024];
365

    
366
    if (data >= data_end)
367
        return;
368
    switch (*data++) {
369
    case AMF_DATA_TYPE_NUMBER:
370
        av_log(ctx, AV_LOG_DEBUG, " number %g\n", av_int2dbl(AV_RB64(data)));
371
        return;
372
    case AMF_DATA_TYPE_BOOL:
373
        av_log(ctx, AV_LOG_DEBUG, " bool %d\n", *data);
374
        return;
375
    case AMF_DATA_TYPE_STRING:
376
    case AMF_DATA_TYPE_LONG_STRING:
377
        if (data[-1] == AMF_DATA_TYPE_STRING) {
378
            size = bytestream_get_be16(&data);
379
        } else {
380
            size = bytestream_get_be32(&data);
381
        }
382
        size = FFMIN(size, 1023);
383
        memcpy(buf, data, size);
384
        buf[size] = 0;
385
        av_log(ctx, AV_LOG_DEBUG, " string '%s'\n", buf);
386
        return;
387
    case AMF_DATA_TYPE_NULL:
388
        av_log(ctx, AV_LOG_DEBUG, " NULL\n");
389
        return;
390
    case AMF_DATA_TYPE_ARRAY:
391
        data += 4;
392
    case AMF_DATA_TYPE_OBJECT:
393
        av_log(ctx, AV_LOG_DEBUG, " {\n");
394
        for (;;) {
395
            int size = bytestream_get_be16(&data);
396
            int t;
397
            memcpy(buf, data, size);
398
            buf[size] = 0;
399
            if (!size) {
400
                av_log(ctx, AV_LOG_DEBUG, " }\n");
401
                data++;
402
                break;
403
            }
404
            if (data + size >= data_end || data + size < data)
405
                return;
406
            data += size;
407
            av_log(ctx, AV_LOG_DEBUG, "  %s: ", buf);
408
            ff_amf_tag_contents(ctx, data, data_end);
409
            t = ff_amf_tag_size(data, data_end);
410
            if (t < 0 || data + t >= data_end)
411
                return;
412
            data += t;
413
        }
414
        return;
415
    case AMF_DATA_TYPE_OBJECT_END:
416
        av_log(ctx, AV_LOG_DEBUG, " }\n");
417
        return;
418
    default:
419
        return;
420
    }
421
}
422

    
423
void ff_rtmp_packet_dump(void *ctx, RTMPPacket *p)
424
{
425
    av_log(ctx, AV_LOG_DEBUG, "RTMP packet type '%s'(%d) for channel %d, timestamp %d, extra field %d size %d\n",
426
           rtmp_packet_type(p->type), p->type, p->channel_id, p->timestamp, p->extra, p->data_size);
427
    if (p->type == RTMP_PT_INVOKE || p->type == RTMP_PT_NOTIFY) {
428
        uint8_t *src = p->data, *src_end = p->data + p->data_size;
429
        while (src < src_end) {
430
            int sz;
431
            ff_amf_tag_contents(ctx, src, src_end);
432
            sz = ff_amf_tag_size(src, src_end);
433
            if (sz < 0)
434
                break;
435
            src += sz;
436
        }
437
    } else if (p->type == RTMP_PT_SERVER_BW){
438
        av_log(ctx, AV_LOG_DEBUG, "Server BW = %d\n", AV_RB32(p->data));
439
    } else if (p->type == RTMP_PT_CLIENT_BW){
440
        av_log(ctx, AV_LOG_DEBUG, "Client BW = %d\n", AV_RB32(p->data));
441
    } else if (p->type != RTMP_PT_AUDIO && p->type != RTMP_PT_VIDEO && p->type != RTMP_PT_METADATA) {
442
        int i;
443
        for (i = 0; i < p->data_size; i++)
444
            av_log(ctx, AV_LOG_DEBUG, " %02X", p->data[i]);
445
        av_log(ctx, AV_LOG_DEBUG, "\n");
446
    }
447
}