Mercurial > libavformat.hg
annotate rtmppkt.c @ 5403:6f2d4070ab5b libavformat
Write timestamp deltas, not timestamps, for RTMP packets with partial header
| author | kostya |
|---|---|
| date | Tue, 01 Dec 2009 16:43:53 +0000 |
| parents | 99aeb2e385df |
| children | f66e3c131106 |
| rev | line source |
|---|---|
| 5123 | 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; | |
|
5360
a00cc1aac80d
Use enum instead of integer types where appropriate.
cehoyos
parents:
5297
diff
changeset
|
78 enum RTMPPacketType type; |
| 5123 | 79 |
| 80 if (url_read(h, &hdr, 1) != 1) | |
| 81 return AVERROR(EIO); | |
| 82 channel_id = hdr & 0x3F; | |
| 83 | |
| 5297 | 84 if (channel_id < 2) { //special case for channel number >= 64 |
| 85 buf[1] = 0; | |
| 86 if (url_read_complete(h, buf, channel_id + 1) != channel_id + 1) | |
| 87 return AVERROR(EIO); | |
| 88 channel_id = AV_RL16(buf) + 64; | |
| 89 } | |
| 5123 | 90 data_size = prev_pkt[channel_id].data_size; |
| 91 type = prev_pkt[channel_id].type; | |
| 92 extra = prev_pkt[channel_id].extra; | |
| 93 | |
| 94 hdr >>= 6; | |
| 95 if (hdr == RTMP_PS_ONEBYTE) { | |
| 5296 | 96 timestamp = prev_pkt[channel_id].timestamp; |
| 5123 | 97 } else { |
| 98 if (url_read_complete(h, buf, 3) != 3) | |
| 99 return AVERROR(EIO); | |
| 100 timestamp = AV_RB24(buf); | |
| 101 if (hdr != RTMP_PS_FOURBYTES) { | |
| 102 if (url_read_complete(h, buf, 3) != 3) | |
| 103 return AVERROR(EIO); | |
| 104 data_size = AV_RB24(buf); | |
|
5399
f042e114451f
7l trocadero: reading right into enum variable may cause unwanted effects, use
kostya
parents:
5378
diff
changeset
|
105 if (url_read_complete(h, buf, 1) != 1) |
| 5123 | 106 return AVERROR(EIO); |
|
5399
f042e114451f
7l trocadero: reading right into enum variable may cause unwanted effects, use
kostya
parents:
5378
diff
changeset
|
107 type = buf[0]; |
| 5123 | 108 if (hdr == RTMP_PS_TWELVEBYTES) { |
| 109 if (url_read_complete(h, buf, 4) != 4) | |
| 110 return AVERROR(EIO); | |
| 111 extra = AV_RL32(buf); | |
| 112 } | |
| 113 } | |
|
5400
c7d1e90d4935
Read and write extended timestamps for RTMP packets.
kostya
parents:
5399
diff
changeset
|
114 if (timestamp == 0xFFFFFF) { |
|
c7d1e90d4935
Read and write extended timestamps for RTMP packets.
kostya
parents:
5399
diff
changeset
|
115 if (url_read_complete(h, buf, 4) != 4) |
|
c7d1e90d4935
Read and write extended timestamps for RTMP packets.
kostya
parents:
5399
diff
changeset
|
116 return AVERROR(EIO); |
|
c7d1e90d4935
Read and write extended timestamps for RTMP packets.
kostya
parents:
5399
diff
changeset
|
117 timestamp = AV_RB32(buf); |
|
c7d1e90d4935
Read and write extended timestamps for RTMP packets.
kostya
parents:
5399
diff
changeset
|
118 } |
|
5402
99aeb2e385df
Full-header RTMP packets contain real timestamp, others contain timestamp
kostya
parents:
5401
diff
changeset
|
119 if (hdr != RTMP_PS_TWELVEBYTES) |
|
99aeb2e385df
Full-header RTMP packets contain real timestamp, others contain timestamp
kostya
parents:
5401
diff
changeset
|
120 timestamp += prev_pkt[channel_id].timestamp; |
| 5123 | 121 } |
| 122 if (ff_rtmp_packet_create(p, channel_id, type, timestamp, data_size)) | |
| 123 return -1; | |
| 124 p->extra = extra; | |
| 125 // save history | |
| 126 prev_pkt[channel_id].channel_id = channel_id; | |
| 127 prev_pkt[channel_id].type = type; | |
| 128 prev_pkt[channel_id].data_size = data_size; | |
| 129 prev_pkt[channel_id].timestamp = timestamp; | |
| 130 prev_pkt[channel_id].extra = extra; | |
| 131 while (data_size > 0) { | |
| 132 int toread = FFMIN(data_size, chunk_size); | |
| 133 if (url_read_complete(h, p->data + offset, toread) != toread) { | |
| 134 ff_rtmp_packet_destroy(p); | |
| 135 return AVERROR(EIO); | |
| 136 } | |
| 137 data_size -= chunk_size; | |
| 138 offset += chunk_size; | |
| 139 if (data_size > 0) { | |
| 140 url_read_complete(h, &t, 1); //marker | |
| 141 if (t != (0xC0 + channel_id)) | |
| 142 return -1; | |
| 143 } | |
| 144 } | |
| 145 return 0; | |
| 146 } | |
| 147 | |
| 148 int ff_rtmp_packet_write(URLContext *h, RTMPPacket *pkt, | |
| 149 int chunk_size, RTMPPacket *prev_pkt) | |
| 150 { | |
| 151 uint8_t pkt_hdr[16], *p = pkt_hdr; | |
| 152 int mode = RTMP_PS_TWELVEBYTES; | |
| 153 int off = 0; | |
| 154 | |
| 155 //TODO: header compression | |
|
5401
432e1b6e1568
Write header for RTMP packets with channel_id >= 64 correctly
kostya
parents:
5400
diff
changeset
|
156 if (pkt->channel_id < 64) { |
|
432e1b6e1568
Write header for RTMP packets with channel_id >= 64 correctly
kostya
parents:
5400
diff
changeset
|
157 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
|
158 } else if (pkt->channel_id < 64 + 256) { |
|
432e1b6e1568
Write header for RTMP packets with channel_id >= 64 correctly
kostya
parents:
5400
diff
changeset
|
159 bytestream_put_byte(&p, 0 | (mode << 6)); |
|
432e1b6e1568
Write header for RTMP packets with channel_id >= 64 correctly
kostya
parents:
5400
diff
changeset
|
160 bytestream_put_byte(&p, pkt->channel_id - 64); |
|
432e1b6e1568
Write header for RTMP packets with channel_id >= 64 correctly
kostya
parents:
5400
diff
changeset
|
161 } else { |
|
432e1b6e1568
Write header for RTMP packets with channel_id >= 64 correctly
kostya
parents:
5400
diff
changeset
|
162 bytestream_put_byte(&p, 1 | (mode << 6)); |
|
432e1b6e1568
Write header for RTMP packets with channel_id >= 64 correctly
kostya
parents:
5400
diff
changeset
|
163 bytestream_put_le16(&p, pkt->channel_id - 64); |
|
432e1b6e1568
Write header for RTMP packets with channel_id >= 64 correctly
kostya
parents:
5400
diff
changeset
|
164 } |
| 5123 | 165 if (mode != RTMP_PS_ONEBYTE) { |
|
5403
6f2d4070ab5b
Write timestamp deltas, not timestamps, for RTMP packets with partial header
kostya
parents:
5402
diff
changeset
|
166 uint32_t timestamp = pkt->timestamp; |
|
6f2d4070ab5b
Write timestamp deltas, not timestamps, for RTMP packets with partial header
kostya
parents:
5402
diff
changeset
|
167 if (mode != RTMP_PS_TWELVEBYTES) |
|
6f2d4070ab5b
Write timestamp deltas, not timestamps, for RTMP packets with partial header
kostya
parents:
5402
diff
changeset
|
168 timestamp -= prev_pkt[pkt->channel_id].timestamp; |
|
6f2d4070ab5b
Write timestamp deltas, not timestamps, for RTMP packets with partial header
kostya
parents:
5402
diff
changeset
|
169 bytestream_put_be24(&p, timestamp >= 0xFFFFFF ? 0xFFFFFF : timestamp); |
| 5123 | 170 if (mode != RTMP_PS_FOURBYTES) { |
| 171 bytestream_put_be24(&p, pkt->data_size); | |
| 172 bytestream_put_byte(&p, pkt->type); | |
| 173 if (mode == RTMP_PS_TWELVEBYTES) | |
| 174 bytestream_put_le32(&p, pkt->extra); | |
| 175 } | |
|
5403
6f2d4070ab5b
Write timestamp deltas, not timestamps, for RTMP packets with partial header
kostya
parents:
5402
diff
changeset
|
176 if (timestamp >= 0xFFFFFF) |
|
6f2d4070ab5b
Write timestamp deltas, not timestamps, for RTMP packets with partial header
kostya
parents:
5402
diff
changeset
|
177 bytestream_put_be32(&p, timestamp); |
| 5123 | 178 } |
| 179 url_write(h, pkt_hdr, p-pkt_hdr); | |
| 180 while (off < pkt->data_size) { | |
| 181 int towrite = FFMIN(chunk_size, pkt->data_size - off); | |
| 182 url_write(h, pkt->data + off, towrite); | |
| 183 off += towrite; | |
| 184 if (off < pkt->data_size) { | |
| 185 uint8_t marker = 0xC0 | pkt->channel_id; | |
| 186 url_write(h, &marker, 1); | |
| 187 } | |
| 188 } | |
| 189 return 0; | |
| 190 } | |
| 191 | |
| 192 int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type, | |
| 193 int timestamp, int size) | |
| 194 { | |
| 195 pkt->data = av_malloc(size); | |
| 196 if (!pkt->data) | |
| 197 return AVERROR(ENOMEM); | |
| 198 pkt->data_size = size; | |
| 199 pkt->channel_id = channel_id; | |
| 200 pkt->type = type; | |
| 201 pkt->timestamp = timestamp; | |
| 202 pkt->extra = 0; | |
| 203 | |
| 204 return 0; | |
| 205 } | |
| 206 | |
| 207 void ff_rtmp_packet_destroy(RTMPPacket *pkt) | |
| 208 { | |
| 209 if (!pkt) | |
| 210 return; | |
| 211 av_freep(&pkt->data); | |
| 212 pkt->data_size = 0; | |
| 213 } | |
| 214 | |
| 215 int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end) | |
| 216 { | |
| 217 const uint8_t *base = data; | |
| 218 | |
| 219 if (data >= data_end) | |
| 220 return -1; | |
| 221 switch (*data++) { | |
| 222 case AMF_DATA_TYPE_NUMBER: return 9; | |
| 223 case AMF_DATA_TYPE_BOOL: return 2; | |
| 224 case AMF_DATA_TYPE_STRING: return 3 + AV_RB16(data); | |
| 225 case AMF_DATA_TYPE_LONG_STRING: return 5 + AV_RB32(data); | |
| 226 case AMF_DATA_TYPE_NULL: return 1; | |
| 227 case AMF_DATA_TYPE_ARRAY: | |
| 228 data += 4; | |
| 229 case AMF_DATA_TYPE_OBJECT: | |
| 230 for (;;) { | |
| 231 int size = bytestream_get_be16(&data); | |
| 232 int t; | |
| 233 if (!size) { | |
| 234 data++; | |
| 235 break; | |
| 236 } | |
| 237 if (data + size >= data_end || data + size < data) | |
| 238 return -1; | |
| 239 data += size; | |
| 240 t = ff_amf_tag_size(data, data_end); | |
| 241 if (t < 0 || data + t >= data_end) | |
| 242 return -1; | |
| 243 data += t; | |
| 244 } | |
| 245 return data - base; | |
| 246 case AMF_DATA_TYPE_OBJECT_END: return 1; | |
| 247 default: return -1; | |
| 248 } | |
| 249 } | |
| 250 | |
| 251 int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end, | |
| 252 const uint8_t *name, uint8_t *dst, int dst_size) | |
| 253 { | |
| 254 int namelen = strlen(name); | |
| 255 int len; | |
| 256 | |
|
5378
c22a1e94e80f
When searching for AMF object field value, try to find that object first
kostya
parents:
5360
diff
changeset
|
257 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
|
258 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
|
259 if (len < 0) |
|
c22a1e94e80f
When searching for AMF object field value, try to find that object first
kostya
parents:
5360
diff
changeset
|
260 len = data_end - data; |
|
c22a1e94e80f
When searching for AMF object field value, try to find that object first
kostya
parents:
5360
diff
changeset
|
261 data += len; |
|
c22a1e94e80f
When searching for AMF object field value, try to find that object first
kostya
parents:
5360
diff
changeset
|
262 } |
| 5123 | 263 if (data_end - data < 3) |
| 264 return -1; | |
|
5378
c22a1e94e80f
When searching for AMF object field value, try to find that object first
kostya
parents:
5360
diff
changeset
|
265 data++; |
| 5123 | 266 for (;;) { |
| 267 int size = bytestream_get_be16(&data); | |
| 268 if (!size) | |
| 269 break; | |
| 270 if (data + size >= data_end || data + size < data) | |
| 271 return -1; | |
| 272 data += size; | |
| 273 if (size == namelen && !memcmp(data-size, name, namelen)) { | |
| 274 switch (*data++) { | |
| 275 case AMF_DATA_TYPE_NUMBER: | |
| 276 snprintf(dst, dst_size, "%g", av_int2dbl(AV_RB64(data))); | |
| 277 break; | |
| 278 case AMF_DATA_TYPE_BOOL: | |
| 279 snprintf(dst, dst_size, "%s", *data ? "true" : "false"); | |
| 280 break; | |
| 281 case AMF_DATA_TYPE_STRING: | |
| 282 len = bytestream_get_be16(&data); | |
| 283 av_strlcpy(dst, data, FFMIN(len+1, dst_size)); | |
| 284 break; | |
| 285 default: | |
| 286 return -1; | |
| 287 } | |
| 288 return 0; | |
| 289 } | |
| 290 len = ff_amf_tag_size(data, data_end); | |
| 291 if (len < 0 || data + len >= data_end || data + len < data) | |
| 292 return -1; | |
| 293 data += len; | |
| 294 } | |
| 295 return -1; | |
| 296 } |
