annotate rtmppkt.c @ 6047:4bedd06c54e2 libavformat

Enable AVFMT_GENERIC_INDEX for Ogg demuxer. This avoids the many seeks needed for binary search when seeking to a previously seen location.
author reimar
date Sun, 23 May 2010 23:33:38 +0000
parents 6d4ba584fcf2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
1 /*
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
2 * RTMP input format
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
3 * Copyright (c) 2009 Kostya Shishkov
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
4 *
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
5 * This file is part of FFmpeg.
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
6 *
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
11 *
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
15 * Lesser General Public License for more details.
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
16 *
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
20 */
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
21
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
22 #include "libavcodec/bytestream.h"
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
23 #include "libavutil/avstring.h"
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
24 #include "avformat.h"
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
25
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
26 #include "rtmppkt.h"
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
27 #include "flv.h"
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
28
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
29 void ff_amf_write_bool(uint8_t **dst, int val)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
30 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
31 bytestream_put_byte(dst, AMF_DATA_TYPE_BOOL);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
32 bytestream_put_byte(dst, val);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
33 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
34
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
35 void ff_amf_write_number(uint8_t **dst, double val)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
36 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
37 bytestream_put_byte(dst, AMF_DATA_TYPE_NUMBER);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
38 bytestream_put_be64(dst, av_dbl2int(val));
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
39 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
40
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
41 void ff_amf_write_string(uint8_t **dst, const char *str)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
42 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
43 bytestream_put_byte(dst, AMF_DATA_TYPE_STRING);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
44 bytestream_put_be16(dst, strlen(str));
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
45 bytestream_put_buffer(dst, str, strlen(str));
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
46 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
47
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
48 void ff_amf_write_null(uint8_t **dst)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
49 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
50 bytestream_put_byte(dst, AMF_DATA_TYPE_NULL);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
51 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
52
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
53 void ff_amf_write_object_start(uint8_t **dst)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
54 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
55 bytestream_put_byte(dst, AMF_DATA_TYPE_OBJECT);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
56 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
57
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
58 void ff_amf_write_field_name(uint8_t **dst, const char *str)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
59 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
60 bytestream_put_be16(dst, strlen(str));
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
61 bytestream_put_buffer(dst, str, strlen(str));
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
62 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
63
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
64 void ff_amf_write_object_end(uint8_t **dst)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
65 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
66 /* first two bytes are field name length = 0,
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
67 * AMF object should end with it and end marker
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
68 */
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
69 bytestream_put_be24(dst, AMF_DATA_TYPE_OBJECT_END);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
70 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
71
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
72 int ff_rtmp_packet_read(URLContext *h, RTMPPacket *p,
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
73 int chunk_size, RTMPPacket *prev_pkt)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
74 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
75 uint8_t hdr, t, buf[16];
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
76 int channel_id, timestamp, data_size, offset = 0;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
77 uint32_t extra = 0;
5360
a00cc1aac80d Use enum instead of integer types where appropriate.
cehoyos
parents: 5297
diff changeset
78 enum RTMPPacketType type;
5604
81b2509db2ad Make RTMP send/receive packet functions report number of bytes read or sent.
kostya
parents: 5603
diff changeset
79 int size = 0;
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
80
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
81 if (url_read(h, &hdr, 1) != 1)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
82 return AVERROR(EIO);
5604
81b2509db2ad Make RTMP send/receive packet functions report number of bytes read or sent.
kostya
parents: 5603
diff changeset
83 size++;
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
84 channel_id = hdr & 0x3F;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
85
5297
53688cb639b0 Support more than 64 channels in RTMP input
kostya
parents: 5296
diff changeset
86 if (channel_id < 2) { //special case for channel number >= 64
53688cb639b0 Support more than 64 channels in RTMP input
kostya
parents: 5296
diff changeset
87 buf[1] = 0;
53688cb639b0 Support more than 64 channels in RTMP input
kostya
parents: 5296
diff changeset
88 if (url_read_complete(h, buf, channel_id + 1) != channel_id + 1)
53688cb639b0 Support more than 64 channels in RTMP input
kostya
parents: 5296
diff changeset
89 return AVERROR(EIO);
5604
81b2509db2ad Make RTMP send/receive packet functions report number of bytes read or sent.
kostya
parents: 5603
diff changeset
90 size += channel_id + 1;
5297
53688cb639b0 Support more than 64 channels in RTMP input
kostya
parents: 5296
diff changeset
91 channel_id = AV_RL16(buf) + 64;
53688cb639b0 Support more than 64 channels in RTMP input
kostya
parents: 5296
diff changeset
92 }
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
93 data_size = prev_pkt[channel_id].data_size;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
94 type = prev_pkt[channel_id].type;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
95 extra = prev_pkt[channel_id].extra;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
96
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
97 hdr >>= 6;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
98 if (hdr == RTMP_PS_ONEBYTE) {
5410
f66e3c131106 RTMP packets with one-byte header use previous packet timestamp difference, so
kostya
parents: 5403
diff changeset
99 timestamp = prev_pkt[channel_id].ts_delta;
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
100 } else {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
101 if (url_read_complete(h, buf, 3) != 3)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
102 return AVERROR(EIO);
5604
81b2509db2ad Make RTMP send/receive packet functions report number of bytes read or sent.
kostya
parents: 5603
diff changeset
103 size += 3;
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
104 timestamp = AV_RB24(buf);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
105 if (hdr != RTMP_PS_FOURBYTES) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
106 if (url_read_complete(h, buf, 3) != 3)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
107 return AVERROR(EIO);
5604
81b2509db2ad Make RTMP send/receive packet functions report number of bytes read or sent.
kostya
parents: 5603
diff changeset
108 size += 3;
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
109 data_size = AV_RB24(buf);
5399
f042e114451f 7l trocadero: reading right into enum variable may cause unwanted effects, use
kostya
parents: 5378
diff changeset
110 if (url_read_complete(h, buf, 1) != 1)
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
111 return AVERROR(EIO);
5604
81b2509db2ad Make RTMP send/receive packet functions report number of bytes read or sent.
kostya
parents: 5603
diff changeset
112 size++;
5399
f042e114451f 7l trocadero: reading right into enum variable may cause unwanted effects, use
kostya
parents: 5378
diff changeset
113 type = buf[0];
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
114 if (hdr == RTMP_PS_TWELVEBYTES) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
115 if (url_read_complete(h, buf, 4) != 4)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
116 return AVERROR(EIO);
5604
81b2509db2ad Make RTMP send/receive packet functions report number of bytes read or sent.
kostya
parents: 5603
diff changeset
117 size += 4;
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
118 extra = AV_RL32(buf);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
119 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
120 }
5400
c7d1e90d4935 Read and write extended timestamps for RTMP packets.
kostya
parents: 5399
diff changeset
121 if (timestamp == 0xFFFFFF) {
c7d1e90d4935 Read and write extended timestamps for RTMP packets.
kostya
parents: 5399
diff changeset
122 if (url_read_complete(h, buf, 4) != 4)
c7d1e90d4935 Read and write extended timestamps for RTMP packets.
kostya
parents: 5399
diff changeset
123 return AVERROR(EIO);
c7d1e90d4935 Read and write extended timestamps for RTMP packets.
kostya
parents: 5399
diff changeset
124 timestamp = AV_RB32(buf);
c7d1e90d4935 Read and write extended timestamps for RTMP packets.
kostya
parents: 5399
diff changeset
125 }
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
126 }
5410
f66e3c131106 RTMP packets with one-byte header use previous packet timestamp difference, so
kostya
parents: 5403
diff changeset
127 if (hdr != RTMP_PS_TWELVEBYTES)
f66e3c131106 RTMP packets with one-byte header use previous packet timestamp difference, so
kostya
parents: 5403
diff changeset
128 timestamp += prev_pkt[channel_id].timestamp;
f66e3c131106 RTMP packets with one-byte header use previous packet timestamp difference, so
kostya
parents: 5403
diff changeset
129
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
130 if (ff_rtmp_packet_create(p, channel_id, type, timestamp, data_size))
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
131 return -1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
132 p->extra = extra;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
133 // save history
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
134 prev_pkt[channel_id].channel_id = channel_id;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
135 prev_pkt[channel_id].type = type;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
136 prev_pkt[channel_id].data_size = data_size;
5410
f66e3c131106 RTMP packets with one-byte header use previous packet timestamp difference, so
kostya
parents: 5403
diff changeset
137 prev_pkt[channel_id].ts_delta = timestamp - prev_pkt[channel_id].timestamp;
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
138 prev_pkt[channel_id].timestamp = timestamp;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
139 prev_pkt[channel_id].extra = extra;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
140 while (data_size > 0) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
141 int toread = FFMIN(data_size, chunk_size);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
142 if (url_read_complete(h, p->data + offset, toread) != toread) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
143 ff_rtmp_packet_destroy(p);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
144 return AVERROR(EIO);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
145 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
146 data_size -= chunk_size;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
147 offset += chunk_size;
5604
81b2509db2ad Make RTMP send/receive packet functions report number of bytes read or sent.
kostya
parents: 5603
diff changeset
148 size += chunk_size;
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
149 if (data_size > 0) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
150 url_read_complete(h, &t, 1); //marker
5604
81b2509db2ad Make RTMP send/receive packet functions report number of bytes read or sent.
kostya
parents: 5603
diff changeset
151 size++;
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
152 if (t != (0xC0 + channel_id))
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
153 return -1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
154 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
155 }
5604
81b2509db2ad Make RTMP send/receive packet functions report number of bytes read or sent.
kostya
parents: 5603
diff changeset
156 return size;
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
157 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
158
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
159 int ff_rtmp_packet_write(URLContext *h, RTMPPacket *pkt,
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
160 int chunk_size, RTMPPacket *prev_pkt)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
161 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
162 uint8_t pkt_hdr[16], *p = pkt_hdr;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
163 int mode = RTMP_PS_TWELVEBYTES;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
164 int off = 0;
5604
81b2509db2ad Make RTMP send/receive packet functions report number of bytes read or sent.
kostya
parents: 5603
diff changeset
165 int size = 0;
5603
8efa6bc0752e cosmetics: add an empty line between variable declarations and code
kostya
parents: 5473
diff changeset
166
5410
f66e3c131106 RTMP packets with one-byte header use previous packet timestamp difference, so
kostya
parents: 5403
diff changeset
167 pkt->ts_delta = pkt->timestamp - prev_pkt[pkt->channel_id].timestamp;
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
168
5415
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
169 //if channel_id = 0, this is first presentation of prev_pkt, send full hdr.
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
170 if (prev_pkt[pkt->channel_id].channel_id &&
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
171 pkt->extra == prev_pkt[pkt->channel_id].extra) {
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
172 if (pkt->type == prev_pkt[pkt->channel_id].type &&
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
173 pkt->data_size == prev_pkt[pkt->channel_id].data_size) {
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
174 mode = RTMP_PS_FOURBYTES;
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
175 if (pkt->ts_delta == prev_pkt[pkt->channel_id].ts_delta)
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
176 mode = RTMP_PS_ONEBYTE;
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
177 } else {
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
178 mode = RTMP_PS_EIGHTBYTES;
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
179 }
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
180 }
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
181
5401
432e1b6e1568 Write header for RTMP packets with channel_id >= 64 correctly
kostya
parents: 5400
diff changeset
182 if (pkt->channel_id < 64) {
432e1b6e1568 Write header for RTMP packets with channel_id >= 64 correctly
kostya
parents: 5400
diff changeset
183 bytestream_put_byte(&p, pkt->channel_id | (mode << 6));
432e1b6e1568 Write header for RTMP packets with channel_id >= 64 correctly
kostya
parents: 5400
diff changeset
184 } else if (pkt->channel_id < 64 + 256) {
432e1b6e1568 Write header for RTMP packets with channel_id >= 64 correctly
kostya
parents: 5400
diff changeset
185 bytestream_put_byte(&p, 0 | (mode << 6));
432e1b6e1568 Write header for RTMP packets with channel_id >= 64 correctly
kostya
parents: 5400
diff changeset
186 bytestream_put_byte(&p, pkt->channel_id - 64);
432e1b6e1568 Write header for RTMP packets with channel_id >= 64 correctly
kostya
parents: 5400
diff changeset
187 } else {
432e1b6e1568 Write header for RTMP packets with channel_id >= 64 correctly
kostya
parents: 5400
diff changeset
188 bytestream_put_byte(&p, 1 | (mode << 6));
432e1b6e1568 Write header for RTMP packets with channel_id >= 64 correctly
kostya
parents: 5400
diff changeset
189 bytestream_put_le16(&p, pkt->channel_id - 64);
432e1b6e1568 Write header for RTMP packets with channel_id >= 64 correctly
kostya
parents: 5400
diff changeset
190 }
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
191 if (mode != RTMP_PS_ONEBYTE) {
5403
6f2d4070ab5b Write timestamp deltas, not timestamps, for RTMP packets with partial header
kostya
parents: 5402
diff changeset
192 uint32_t timestamp = pkt->timestamp;
6f2d4070ab5b Write timestamp deltas, not timestamps, for RTMP packets with partial header
kostya
parents: 5402
diff changeset
193 if (mode != RTMP_PS_TWELVEBYTES)
5410
f66e3c131106 RTMP packets with one-byte header use previous packet timestamp difference, so
kostya
parents: 5403
diff changeset
194 timestamp = pkt->ts_delta;
5403
6f2d4070ab5b Write timestamp deltas, not timestamps, for RTMP packets with partial header
kostya
parents: 5402
diff changeset
195 bytestream_put_be24(&p, timestamp >= 0xFFFFFF ? 0xFFFFFF : timestamp);
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
196 if (mode != RTMP_PS_FOURBYTES) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
197 bytestream_put_be24(&p, pkt->data_size);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
198 bytestream_put_byte(&p, pkt->type);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
199 if (mode == RTMP_PS_TWELVEBYTES)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
200 bytestream_put_le32(&p, pkt->extra);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
201 }
5403
6f2d4070ab5b Write timestamp deltas, not timestamps, for RTMP packets with partial header
kostya
parents: 5402
diff changeset
202 if (timestamp >= 0xFFFFFF)
6f2d4070ab5b Write timestamp deltas, not timestamps, for RTMP packets with partial header
kostya
parents: 5402
diff changeset
203 bytestream_put_be32(&p, timestamp);
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
204 }
5415
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
205 // save history
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
206 prev_pkt[pkt->channel_id].channel_id = pkt->channel_id;
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
207 prev_pkt[pkt->channel_id].type = pkt->type;
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
208 prev_pkt[pkt->channel_id].data_size = pkt->data_size;
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
209 prev_pkt[pkt->channel_id].timestamp = pkt->timestamp;
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
210 if (mode != RTMP_PS_TWELVEBYTES) {
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
211 prev_pkt[pkt->channel_id].ts_delta = pkt->ts_delta;
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
212 } else {
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
213 prev_pkt[pkt->channel_id].ts_delta = pkt->timestamp;
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
214 }
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
215 prev_pkt[pkt->channel_id].extra = pkt->extra;
584c1ba93552 Compress headers for output RTMP packets.
kostya
parents: 5410
diff changeset
216
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
217 url_write(h, pkt_hdr, p-pkt_hdr);
5604
81b2509db2ad Make RTMP send/receive packet functions report number of bytes read or sent.
kostya
parents: 5603
diff changeset
218 size = p - pkt_hdr + pkt->data_size;
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
219 while (off < pkt->data_size) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
220 int towrite = FFMIN(chunk_size, pkt->data_size - off);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
221 url_write(h, pkt->data + off, towrite);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
222 off += towrite;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
223 if (off < pkt->data_size) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
224 uint8_t marker = 0xC0 | pkt->channel_id;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
225 url_write(h, &marker, 1);
5604
81b2509db2ad Make RTMP send/receive packet functions report number of bytes read or sent.
kostya
parents: 5603
diff changeset
226 size++;
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
227 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
228 }
5604
81b2509db2ad Make RTMP send/receive packet functions report number of bytes read or sent.
kostya
parents: 5603
diff changeset
229 return size;
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
230 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
231
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
232 int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type,
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
233 int timestamp, int size)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
234 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
235 pkt->data = av_malloc(size);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
236 if (!pkt->data)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
237 return AVERROR(ENOMEM);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
238 pkt->data_size = size;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
239 pkt->channel_id = channel_id;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
240 pkt->type = type;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
241 pkt->timestamp = timestamp;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
242 pkt->extra = 0;
5410
f66e3c131106 RTMP packets with one-byte header use previous packet timestamp difference, so
kostya
parents: 5403
diff changeset
243 pkt->ts_delta = 0;
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
244
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
245 return 0;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
246 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
247
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
248 void ff_rtmp_packet_destroy(RTMPPacket *pkt)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
249 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
250 if (!pkt)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
251 return;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
252 av_freep(&pkt->data);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
253 pkt->data_size = 0;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
254 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
255
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
256 int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
257 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
258 const uint8_t *base = data;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
259
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
260 if (data >= data_end)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
261 return -1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
262 switch (*data++) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
263 case AMF_DATA_TYPE_NUMBER: return 9;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
264 case AMF_DATA_TYPE_BOOL: return 2;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
265 case AMF_DATA_TYPE_STRING: return 3 + AV_RB16(data);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
266 case AMF_DATA_TYPE_LONG_STRING: return 5 + AV_RB32(data);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
267 case AMF_DATA_TYPE_NULL: return 1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
268 case AMF_DATA_TYPE_ARRAY:
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
269 data += 4;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
270 case AMF_DATA_TYPE_OBJECT:
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
271 for (;;) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
272 int size = bytestream_get_be16(&data);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
273 int t;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
274 if (!size) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
275 data++;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
276 break;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
277 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
278 if (data + size >= data_end || data + size < data)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
279 return -1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
280 data += size;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
281 t = ff_amf_tag_size(data, data_end);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
282 if (t < 0 || data + t >= data_end)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
283 return -1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
284 data += t;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
285 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
286 return data - base;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
287 case AMF_DATA_TYPE_OBJECT_END: return 1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
288 default: return -1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
289 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
290 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
291
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
292 int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
293 const uint8_t *name, uint8_t *dst, int dst_size)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
294 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
295 int namelen = strlen(name);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
296 int len;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
297
5378
c22a1e94e80f When searching for AMF object field value, try to find that object first
kostya
parents: 5360
diff changeset
298 while (*data != AMF_DATA_TYPE_OBJECT && data < data_end) {
c22a1e94e80f When searching for AMF object field value, try to find that object first
kostya
parents: 5360
diff changeset
299 len = ff_amf_tag_size(data, data_end);
c22a1e94e80f When searching for AMF object field value, try to find that object first
kostya
parents: 5360
diff changeset
300 if (len < 0)
c22a1e94e80f When searching for AMF object field value, try to find that object first
kostya
parents: 5360
diff changeset
301 len = data_end - data;
c22a1e94e80f When searching for AMF object field value, try to find that object first
kostya
parents: 5360
diff changeset
302 data += len;
c22a1e94e80f When searching for AMF object field value, try to find that object first
kostya
parents: 5360
diff changeset
303 }
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
304 if (data_end - data < 3)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
305 return -1;
5378
c22a1e94e80f When searching for AMF object field value, try to find that object first
kostya
parents: 5360
diff changeset
306 data++;
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
307 for (;;) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
308 int size = bytestream_get_be16(&data);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
309 if (!size)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
310 break;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
311 if (data + size >= data_end || data + size < data)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
312 return -1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
313 data += size;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
314 if (size == namelen && !memcmp(data-size, name, namelen)) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
315 switch (*data++) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
316 case AMF_DATA_TYPE_NUMBER:
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
317 snprintf(dst, dst_size, "%g", av_int2dbl(AV_RB64(data)));
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
318 break;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
319 case AMF_DATA_TYPE_BOOL:
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
320 snprintf(dst, dst_size, "%s", *data ? "true" : "false");
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
321 break;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
322 case AMF_DATA_TYPE_STRING:
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
323 len = bytestream_get_be16(&data);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
324 av_strlcpy(dst, data, FFMIN(len+1, dst_size));
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
325 break;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
326 default:
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
327 return -1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
328 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
329 return 0;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
330 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
331 len = ff_amf_tag_size(data, data_end);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
332 if (len < 0 || data + len >= data_end || data + len < data)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
333 return -1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
334 data += len;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
335 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
336 return -1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
337 }
5432
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
338
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
339 static const char* rtmp_packet_type(int type)
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
340 {
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
341 switch (type) {
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
342 case RTMP_PT_CHUNK_SIZE: return "chunk size";
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
343 case RTMP_PT_BYTES_READ: return "bytes read";
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
344 case RTMP_PT_PING: return "ping";
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
345 case RTMP_PT_SERVER_BW: return "server bandwidth";
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
346 case RTMP_PT_CLIENT_BW: return "client bandwidth";
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
347 case RTMP_PT_AUDIO: return "audio packet";
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
348 case RTMP_PT_VIDEO: return "video packet";
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
349 case RTMP_PT_FLEX_STREAM: return "Flex shared stream";
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
350 case RTMP_PT_FLEX_OBJECT: return "Flex shared object";
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
351 case RTMP_PT_FLEX_MESSAGE: return "Flex shared message";
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
352 case RTMP_PT_NOTIFY: return "notification";
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
353 case RTMP_PT_SHARED_OBJ: return "shared object";
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
354 case RTMP_PT_INVOKE: return "invoke";
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
355 case RTMP_PT_METADATA: return "metadata";
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
356 default: return "unknown";
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
357 }
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
358 }
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
359
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
360 static void ff_amf_tag_contents(void *ctx, const uint8_t *data, const uint8_t *data_end)
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
361 {
5473
c00ff770b4fc Remove some unused variables, fixes the warnings:
diego
parents: 5432
diff changeset
362 int size;
5432
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
363 char buf[1024];
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
364
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
365 if (data >= data_end)
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
366 return;
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
367 switch (*data++) {
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
368 case AMF_DATA_TYPE_NUMBER:
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
369 av_log(ctx, AV_LOG_DEBUG, " number %g\n", av_int2dbl(AV_RB64(data)));
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
370 return;
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
371 case AMF_DATA_TYPE_BOOL:
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
372 av_log(ctx, AV_LOG_DEBUG, " bool %d\n", *data);
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
373 return;
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
374 case AMF_DATA_TYPE_STRING:
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
375 case AMF_DATA_TYPE_LONG_STRING:
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
376 if (data[-1] == AMF_DATA_TYPE_STRING) {
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
377 size = bytestream_get_be16(&data);
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
378 } else {
5765
6d4ba584fcf2 1l trocadero: forgot reference operator on bytestream_get_be32() argument
kostya
parents: 5604
diff changeset
379 size = bytestream_get_be32(&data);
5432
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
380 }
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
381 size = FFMIN(size, 1023);
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
382 memcpy(buf, data, size);
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
383 buf[size] = 0;
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
384 av_log(ctx, AV_LOG_DEBUG, " string '%s'\n", buf);
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
385 return;
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
386 case AMF_DATA_TYPE_NULL:
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
387 av_log(ctx, AV_LOG_DEBUG, " NULL\n");
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
388 return;
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
389 case AMF_DATA_TYPE_ARRAY:
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
390 data += 4;
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
391 case AMF_DATA_TYPE_OBJECT:
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
392 av_log(ctx, AV_LOG_DEBUG, " {\n");
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
393 for (;;) {
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
394 int size = bytestream_get_be16(&data);
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
395 int t;
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
396 memcpy(buf, data, size);
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
397 buf[size] = 0;
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
398 if (!size) {
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
399 av_log(ctx, AV_LOG_DEBUG, " }\n");
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
400 data++;
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
401 break;
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
402 }
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
403 if (data + size >= data_end || data + size < data)
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
404 return;
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
405 data += size;
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
406 av_log(ctx, AV_LOG_DEBUG, " %s: ", buf);
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
407 ff_amf_tag_contents(ctx, data, data_end);
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
408 t = ff_amf_tag_size(data, data_end);
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
409 if (t < 0 || data + t >= data_end)
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
410 return;
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
411 data += t;
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
412 }
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
413 return;
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
414 case AMF_DATA_TYPE_OBJECT_END:
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
415 av_log(ctx, AV_LOG_DEBUG, " }\n");
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
416 return;
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
417 default:
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
418 return;
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
419 }
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
420 }
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
421
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
422 void ff_rtmp_packet_dump(void *ctx, RTMPPacket *p)
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
423 {
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
424 av_log(ctx, AV_LOG_DEBUG, "RTMP packet type '%s'(%d) for channel %d, timestamp %d, extra field %d size %d\n",
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
425 rtmp_packet_type(p->type), p->type, p->channel_id, p->timestamp, p->extra, p->data_size);
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
426 if (p->type == RTMP_PT_INVOKE || p->type == RTMP_PT_NOTIFY) {
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
427 uint8_t *src = p->data, *src_end = p->data + p->data_size;
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
428 while (src < src_end) {
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
429 int sz;
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
430 ff_amf_tag_contents(ctx, src, src_end);
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
431 sz = ff_amf_tag_size(src, src_end);
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
432 if (sz < 0)
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
433 break;
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
434 src += sz;
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
435 }
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
436 } else if (p->type == RTMP_PT_SERVER_BW){
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
437 av_log(ctx, AV_LOG_DEBUG, "Server BW = %d\n", AV_RB32(p->data));
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
438 } else if (p->type == RTMP_PT_CLIENT_BW){
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
439 av_log(ctx, AV_LOG_DEBUG, "Client BW = %d\n", AV_RB32(p->data));
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
440 } else if (p->type != RTMP_PT_AUDIO && p->type != RTMP_PT_VIDEO && p->type != RTMP_PT_METADATA) {
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
441 int i;
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
442 for (i = 0; i < p->data_size; i++)
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
443 av_log(ctx, AV_LOG_DEBUG, " %02X", p->data[i]);
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
444 av_log(ctx, AV_LOG_DEBUG, "\n");
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
445 }
f282f7758d6e Dump RTMP packet contents in debug mode
kostya
parents: 5415
diff changeset
446 }