rtmp: Reindent
[libav.git] / libavformat / rtmppkt.c
CommitLineData
9fd6b843
KS
1/*
2 * RTMP input format
3 * Copyright (c) 2009 Kostya Shishkov
4 *
2912e87a 5 * This file is part of Libav.
9fd6b843 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
9fd6b843
KS
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
9fd6b843
KS
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
9fd6b843
KS
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"
bc371aca 28#include "url.h"
9fd6b843
KS
29
30void 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
36void 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
42void 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
49void ff_amf_write_null(uint8_t **dst)
50{
51 bytestream_put_byte(dst, AMF_DATA_TYPE_NULL);
52}
53
54void ff_amf_write_object_start(uint8_t **dst)
55{
56 bytestream_put_byte(dst, AMF_DATA_TYPE_OBJECT);
57}
58
59void 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
65void 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
73int 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;
0f87b771 79 enum RTMPPacketType type;
0b6b10d9 80 int size = 0;
9fd6b843 81
bc371aca 82 if (ffurl_read(h, &hdr, 1) != 1)
9fd6b843 83 return AVERROR(EIO);
0b6b10d9 84 size++;
9fd6b843
KS
85 channel_id = hdr & 0x3F;
86
5d660598
KS
87 if (channel_id < 2) { //special case for channel number >= 64
88 buf[1] = 0;
dce37564 89 if (ffurl_read_complete(h, buf, channel_id + 1) != channel_id + 1)
5d660598 90 return AVERROR(EIO);
0b6b10d9 91 size += channel_id + 1;
5d660598
KS
92 channel_id = AV_RL16(buf) + 64;
93 }
9fd6b843
KS
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) {
a352b605 100 timestamp = prev_pkt[channel_id].ts_delta;
9fd6b843 101 } else {
dce37564 102 if (ffurl_read_complete(h, buf, 3) != 3)
9fd6b843 103 return AVERROR(EIO);
0b6b10d9 104 size += 3;
9fd6b843
KS
105 timestamp = AV_RB24(buf);
106 if (hdr != RTMP_PS_FOURBYTES) {
dce37564 107 if (ffurl_read_complete(h, buf, 3) != 3)
9fd6b843 108 return AVERROR(EIO);
0b6b10d9 109 size += 3;
9fd6b843 110 data_size = AV_RB24(buf);
dce37564 111 if (ffurl_read_complete(h, buf, 1) != 1)
9fd6b843 112 return AVERROR(EIO);
0b6b10d9 113 size++;
1ab21f19 114 type = buf[0];
9fd6b843 115 if (hdr == RTMP_PS_TWELVEBYTES) {
dce37564 116 if (ffurl_read_complete(h, buf, 4) != 4)
9fd6b843 117 return AVERROR(EIO);
0b6b10d9 118 size += 4;
9fd6b843
KS
119 extra = AV_RL32(buf);
120 }
121 }
59b6482a 122 if (timestamp == 0xFFFFFF) {
dce37564 123 if (ffurl_read_complete(h, buf, 4) != 4)
59b6482a
S
124 return AVERROR(EIO);
125 timestamp = AV_RB32(buf);
126 }
9fd6b843 127 }
a352b605
S
128 if (hdr != RTMP_PS_TWELVEBYTES)
129 timestamp += prev_pkt[channel_id].timestamp;
130
9fd6b843
KS
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;
a352b605 138 prev_pkt[channel_id].ts_delta = timestamp - prev_pkt[channel_id].timestamp;
9fd6b843
KS
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);
dce37564 143 if (ffurl_read_complete(h, p->data + offset, toread) != toread) {
9fd6b843
KS
144 ff_rtmp_packet_destroy(p);
145 return AVERROR(EIO);
146 }
147 data_size -= chunk_size;
148 offset += chunk_size;
0b6b10d9 149 size += chunk_size;
9fd6b843 150 if (data_size > 0) {
dce37564 151 ffurl_read_complete(h, &t, 1); //marker
0b6b10d9 152 size++;
9fd6b843
KS
153 if (t != (0xC0 + channel_id))
154 return -1;
155 }
156 }
0b6b10d9 157 return size;
9fd6b843
KS
158}
159
160int 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;
0b6b10d9 166 int size = 0;
09b67cc4 167
a352b605 168 pkt->ts_delta = pkt->timestamp - prev_pkt[pkt->channel_id].timestamp;
9fd6b843 169
84a29dc1
S
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
b1eb53ab
KS
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 }
9fd6b843 192 if (mode != RTMP_PS_ONEBYTE) {
817d967d
KS
193 uint32_t timestamp = pkt->timestamp;
194 if (mode != RTMP_PS_TWELVEBYTES)
a352b605 195 timestamp = pkt->ts_delta;
817d967d 196 bytestream_put_be24(&p, timestamp >= 0xFFFFFF ? 0xFFFFFF : timestamp);
9fd6b843
KS
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 }
817d967d
KS
203 if (timestamp >= 0xFFFFFF)
204 bytestream_put_be32(&p, timestamp);
9fd6b843 205 }
84a29dc1
S
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
925e908b 218 ffurl_write(h, pkt_hdr, p-pkt_hdr);
0b6b10d9 219 size = p - pkt_hdr + pkt->data_size;
9fd6b843
KS
220 while (off < pkt->data_size) {
221 int towrite = FFMIN(chunk_size, pkt->data_size - off);
925e908b 222 ffurl_write(h, pkt->data + off, towrite);
9fd6b843
KS
223 off += towrite;
224 if (off < pkt->data_size) {
225 uint8_t marker = 0xC0 | pkt->channel_id;
925e908b 226 ffurl_write(h, &marker, 1);
0b6b10d9 227 size++;
9fd6b843
KS
228 }
229 }
0b6b10d9 230 return size;
9fd6b843
KS
231}
232
233int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type,
234 int timestamp, int size)
235{
271c869c 236 if (size) {
48b1fb13
MS
237 pkt->data = av_malloc(size);
238 if (!pkt->data)
239 return AVERROR(ENOMEM);
271c869c 240 }
9fd6b843
KS
241 pkt->data_size = size;
242 pkt->channel_id = channel_id;
243 pkt->type = type;
244 pkt->timestamp = timestamp;
245 pkt->extra = 0;
a352b605 246 pkt->ts_delta = 0;
9fd6b843
KS
247
248 return 0;
249}
250
251void ff_rtmp_packet_destroy(RTMPPacket *pkt)
252{
253 if (!pkt)
254 return;
255 av_freep(&pkt->data);
256 pkt->data_size = 0;
257}
258
259int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end)
260{
261 const uint8_t *base = data;
262
263 if (data >= data_end)
264 return -1;
265 switch (*data++) {
266 case AMF_DATA_TYPE_NUMBER: return 9;
267 case AMF_DATA_TYPE_BOOL: return 2;
268 case AMF_DATA_TYPE_STRING: return 3 + AV_RB16(data);
269 case AMF_DATA_TYPE_LONG_STRING: return 5 + AV_RB32(data);
270 case AMF_DATA_TYPE_NULL: return 1;
271 case AMF_DATA_TYPE_ARRAY:
272 data += 4;
273 case AMF_DATA_TYPE_OBJECT:
274 for (;;) {
275 int size = bytestream_get_be16(&data);
276 int t;
277 if (!size) {
278 data++;
279 break;
280 }
281 if (data + size >= data_end || data + size < data)
282 return -1;
283 data += size;
284 t = ff_amf_tag_size(data, data_end);
285 if (t < 0 || data + t >= data_end)
286 return -1;
287 data += t;
288 }
289 return data - base;
290 case AMF_DATA_TYPE_OBJECT_END: return 1;
291 default: return -1;
292 }
293}
294
295int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
296 const uint8_t *name, uint8_t *dst, int dst_size)
297{
298 int namelen = strlen(name);
299 int len;
300
87ca1b8f
KS
301 while (*data != AMF_DATA_TYPE_OBJECT && data < data_end) {
302 len = ff_amf_tag_size(data, data_end);
303 if (len < 0)
304 len = data_end - data;
305 data += len;
306 }
9fd6b843
KS
307 if (data_end - data < 3)
308 return -1;
87ca1b8f 309 data++;
9fd6b843
KS
310 for (;;) {
311 int size = bytestream_get_be16(&data);
312 if (!size)
313 break;
314 if (data + size >= data_end || data + size < data)
315 return -1;
316 data += size;
317 if (size == namelen && !memcmp(data-size, name, namelen)) {
318 switch (*data++) {
319 case AMF_DATA_TYPE_NUMBER:
320 snprintf(dst, dst_size, "%g", av_int2dbl(AV_RB64(data)));
321 break;
322 case AMF_DATA_TYPE_BOOL:
323 snprintf(dst, dst_size, "%s", *data ? "true" : "false");
324 break;
325 case AMF_DATA_TYPE_STRING:
326 len = bytestream_get_be16(&data);
327 av_strlcpy(dst, data, FFMIN(len+1, dst_size));
328 break;
329 default:
330 return -1;
331 }
332 return 0;
333 }
334 len = ff_amf_tag_size(data, data_end);
335 if (len < 0 || data + len >= data_end || data + len < data)
336 return -1;
337 data += len;
338 }
339 return -1;
340}
cfac91fe
KS
341
342static const char* rtmp_packet_type(int type)
343{
344 switch (type) {
345 case RTMP_PT_CHUNK_SIZE: return "chunk size";
346 case RTMP_PT_BYTES_READ: return "bytes read";
347 case RTMP_PT_PING: return "ping";
348 case RTMP_PT_SERVER_BW: return "server bandwidth";
349 case RTMP_PT_CLIENT_BW: return "client bandwidth";
350 case RTMP_PT_AUDIO: return "audio packet";
351 case RTMP_PT_VIDEO: return "video packet";
352 case RTMP_PT_FLEX_STREAM: return "Flex shared stream";
353 case RTMP_PT_FLEX_OBJECT: return "Flex shared object";
354 case RTMP_PT_FLEX_MESSAGE: return "Flex shared message";
355 case RTMP_PT_NOTIFY: return "notification";
356 case RTMP_PT_SHARED_OBJ: return "shared object";
357 case RTMP_PT_INVOKE: return "invoke";
358 case RTMP_PT_METADATA: return "metadata";
359 default: return "unknown";
360 }
361}
362
363static void ff_amf_tag_contents(void *ctx, const uint8_t *data, const uint8_t *data_end)
364{
25182289 365 int size;
cfac91fe
KS
366 char buf[1024];
367
368 if (data >= data_end)
369 return;
370 switch (*data++) {
371 case AMF_DATA_TYPE_NUMBER:
372 av_log(ctx, AV_LOG_DEBUG, " number %g\n", av_int2dbl(AV_RB64(data)));
373 return;
374 case AMF_DATA_TYPE_BOOL:
375 av_log(ctx, AV_LOG_DEBUG, " bool %d\n", *data);
376 return;
377 case AMF_DATA_TYPE_STRING:
378 case AMF_DATA_TYPE_LONG_STRING:
379 if (data[-1] == AMF_DATA_TYPE_STRING) {
380 size = bytestream_get_be16(&data);
381 } else {
f3cb1cd0 382 size = bytestream_get_be32(&data);
cfac91fe
KS
383 }
384 size = FFMIN(size, 1023);
385 memcpy(buf, data, size);
386 buf[size] = 0;
387 av_log(ctx, AV_LOG_DEBUG, " string '%s'\n", buf);
388 return;
389 case AMF_DATA_TYPE_NULL:
390 av_log(ctx, AV_LOG_DEBUG, " NULL\n");
391 return;
392 case AMF_DATA_TYPE_ARRAY:
393 data += 4;
394 case AMF_DATA_TYPE_OBJECT:
395 av_log(ctx, AV_LOG_DEBUG, " {\n");
396 for (;;) {
397 int size = bytestream_get_be16(&data);
398 int t;
399 memcpy(buf, data, size);
400 buf[size] = 0;
401 if (!size) {
402 av_log(ctx, AV_LOG_DEBUG, " }\n");
403 data++;
404 break;
405 }
406 if (data + size >= data_end || data + size < data)
407 return;
408 data += size;
409 av_log(ctx, AV_LOG_DEBUG, " %s: ", buf);
410 ff_amf_tag_contents(ctx, data, data_end);
411 t = ff_amf_tag_size(data, data_end);
412 if (t < 0 || data + t >= data_end)
413 return;
414 data += t;
415 }
416 return;
417 case AMF_DATA_TYPE_OBJECT_END:
418 av_log(ctx, AV_LOG_DEBUG, " }\n");
419 return;
420 default:
421 return;
422 }
423}
424
425void ff_rtmp_packet_dump(void *ctx, RTMPPacket *p)
426{
427 av_log(ctx, AV_LOG_DEBUG, "RTMP packet type '%s'(%d) for channel %d, timestamp %d, extra field %d size %d\n",
428 rtmp_packet_type(p->type), p->type, p->channel_id, p->timestamp, p->extra, p->data_size);
429 if (p->type == RTMP_PT_INVOKE || p->type == RTMP_PT_NOTIFY) {
430 uint8_t *src = p->data, *src_end = p->data + p->data_size;
431 while (src < src_end) {
432 int sz;
433 ff_amf_tag_contents(ctx, src, src_end);
434 sz = ff_amf_tag_size(src, src_end);
435 if (sz < 0)
436 break;
437 src += sz;
438 }
439 } else if (p->type == RTMP_PT_SERVER_BW){
440 av_log(ctx, AV_LOG_DEBUG, "Server BW = %d\n", AV_RB32(p->data));
441 } else if (p->type == RTMP_PT_CLIENT_BW){
442 av_log(ctx, AV_LOG_DEBUG, "Client BW = %d\n", AV_RB32(p->data));
443 } else if (p->type != RTMP_PT_AUDIO && p->type != RTMP_PT_VIDEO && p->type != RTMP_PT_METADATA) {
444 int i;
445 for (i = 0; i < p->data_size; i++)
446 av_log(ctx, AV_LOG_DEBUG, " %02X", p->data[i]);
447 av_log(ctx, AV_LOG_DEBUG, "\n");
448 }
449}