Statistics
| Branch: | Revision:

ffmpeg / libavformat / rtmppkt.c @ f3cb1cd0

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 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
    int size = 0;
80

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

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

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

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

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

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

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

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

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

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

    
245
    return 0;
246
}
247

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

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

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

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

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

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

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

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

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