00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavcodec/bytestream.h"
00023 #include "libavutil/avstring.h"
00024 #include "avformat.h"
00025
00026 #include "rtmppkt.h"
00027 #include "flv.h"
00028
00029 void ff_amf_write_bool(uint8_t **dst, int val)
00030 {
00031 bytestream_put_byte(dst, AMF_DATA_TYPE_BOOL);
00032 bytestream_put_byte(dst, val);
00033 }
00034
00035 void ff_amf_write_number(uint8_t **dst, double val)
00036 {
00037 bytestream_put_byte(dst, AMF_DATA_TYPE_NUMBER);
00038 bytestream_put_be64(dst, av_dbl2int(val));
00039 }
00040
00041 void ff_amf_write_string(uint8_t **dst, const char *str)
00042 {
00043 bytestream_put_byte(dst, AMF_DATA_TYPE_STRING);
00044 bytestream_put_be16(dst, strlen(str));
00045 bytestream_put_buffer(dst, str, strlen(str));
00046 }
00047
00048 void ff_amf_write_null(uint8_t **dst)
00049 {
00050 bytestream_put_byte(dst, AMF_DATA_TYPE_NULL);
00051 }
00052
00053 void ff_amf_write_object_start(uint8_t **dst)
00054 {
00055 bytestream_put_byte(dst, AMF_DATA_TYPE_OBJECT);
00056 }
00057
00058 void ff_amf_write_field_name(uint8_t **dst, const char *str)
00059 {
00060 bytestream_put_be16(dst, strlen(str));
00061 bytestream_put_buffer(dst, str, strlen(str));
00062 }
00063
00064 void ff_amf_write_object_end(uint8_t **dst)
00065 {
00066
00067
00068
00069 bytestream_put_be24(dst, AMF_DATA_TYPE_OBJECT_END);
00070 }
00071
00072 int ff_rtmp_packet_read(URLContext *h, RTMPPacket *p,
00073 int chunk_size, RTMPPacket *prev_pkt)
00074 {
00075 uint8_t hdr, t, buf[16];
00076 int channel_id, timestamp, data_size, offset = 0;
00077 uint32_t extra = 0;
00078 enum RTMPPacketType type;
00079 int size = 0;
00080
00081 if (url_read(h, &hdr, 1) != 1)
00082 return AVERROR(EIO);
00083 size++;
00084 channel_id = hdr & 0x3F;
00085
00086 if (channel_id < 2) {
00087 buf[1] = 0;
00088 if (url_read_complete(h, buf, channel_id + 1) != channel_id + 1)
00089 return AVERROR(EIO);
00090 size += channel_id + 1;
00091 channel_id = AV_RL16(buf) + 64;
00092 }
00093 data_size = prev_pkt[channel_id].data_size;
00094 type = prev_pkt[channel_id].type;
00095 extra = prev_pkt[channel_id].extra;
00096
00097 hdr >>= 6;
00098 if (hdr == RTMP_PS_ONEBYTE) {
00099 timestamp = prev_pkt[channel_id].ts_delta;
00100 } else {
00101 if (url_read_complete(h, buf, 3) != 3)
00102 return AVERROR(EIO);
00103 size += 3;
00104 timestamp = AV_RB24(buf);
00105 if (hdr != RTMP_PS_FOURBYTES) {
00106 if (url_read_complete(h, buf, 3) != 3)
00107 return AVERROR(EIO);
00108 size += 3;
00109 data_size = AV_RB24(buf);
00110 if (url_read_complete(h, buf, 1) != 1)
00111 return AVERROR(EIO);
00112 size++;
00113 type = buf[0];
00114 if (hdr == RTMP_PS_TWELVEBYTES) {
00115 if (url_read_complete(h, buf, 4) != 4)
00116 return AVERROR(EIO);
00117 size += 4;
00118 extra = AV_RL32(buf);
00119 }
00120 }
00121 if (timestamp == 0xFFFFFF) {
00122 if (url_read_complete(h, buf, 4) != 4)
00123 return AVERROR(EIO);
00124 timestamp = AV_RB32(buf);
00125 }
00126 }
00127 if (hdr != RTMP_PS_TWELVEBYTES)
00128 timestamp += prev_pkt[channel_id].timestamp;
00129
00130 if (ff_rtmp_packet_create(p, channel_id, type, timestamp, data_size))
00131 return -1;
00132 p->extra = extra;
00133
00134 prev_pkt[channel_id].channel_id = channel_id;
00135 prev_pkt[channel_id].type = type;
00136 prev_pkt[channel_id].data_size = data_size;
00137 prev_pkt[channel_id].ts_delta = timestamp - prev_pkt[channel_id].timestamp;
00138 prev_pkt[channel_id].timestamp = timestamp;
00139 prev_pkt[channel_id].extra = extra;
00140 while (data_size > 0) {
00141 int toread = FFMIN(data_size, chunk_size);
00142 if (url_read_complete(h, p->data + offset, toread) != toread) {
00143 ff_rtmp_packet_destroy(p);
00144 return AVERROR(EIO);
00145 }
00146 data_size -= chunk_size;
00147 offset += chunk_size;
00148 size += chunk_size;
00149 if (data_size > 0) {
00150 url_read_complete(h, &t, 1);
00151 size++;
00152 if (t != (0xC0 + channel_id))
00153 return -1;
00154 }
00155 }
00156 return size;
00157 }
00158
00159 int ff_rtmp_packet_write(URLContext *h, RTMPPacket *pkt,
00160 int chunk_size, RTMPPacket *prev_pkt)
00161 {
00162 uint8_t pkt_hdr[16], *p = pkt_hdr;
00163 int mode = RTMP_PS_TWELVEBYTES;
00164 int off = 0;
00165 int size = 0;
00166
00167 pkt->ts_delta = pkt->timestamp - prev_pkt[pkt->channel_id].timestamp;
00168
00169
00170 if (prev_pkt[pkt->channel_id].channel_id &&
00171 pkt->extra == prev_pkt[pkt->channel_id].extra) {
00172 if (pkt->type == prev_pkt[pkt->channel_id].type &&
00173 pkt->data_size == prev_pkt[pkt->channel_id].data_size) {
00174 mode = RTMP_PS_FOURBYTES;
00175 if (pkt->ts_delta == prev_pkt[pkt->channel_id].ts_delta)
00176 mode = RTMP_PS_ONEBYTE;
00177 } else {
00178 mode = RTMP_PS_EIGHTBYTES;
00179 }
00180 }
00181
00182 if (pkt->channel_id < 64) {
00183 bytestream_put_byte(&p, pkt->channel_id | (mode << 6));
00184 } else if (pkt->channel_id < 64 + 256) {
00185 bytestream_put_byte(&p, 0 | (mode << 6));
00186 bytestream_put_byte(&p, pkt->channel_id - 64);
00187 } else {
00188 bytestream_put_byte(&p, 1 | (mode << 6));
00189 bytestream_put_le16(&p, pkt->channel_id - 64);
00190 }
00191 if (mode != RTMP_PS_ONEBYTE) {
00192 uint32_t timestamp = pkt->timestamp;
00193 if (mode != RTMP_PS_TWELVEBYTES)
00194 timestamp = pkt->ts_delta;
00195 bytestream_put_be24(&p, timestamp >= 0xFFFFFF ? 0xFFFFFF : timestamp);
00196 if (mode != RTMP_PS_FOURBYTES) {
00197 bytestream_put_be24(&p, pkt->data_size);
00198 bytestream_put_byte(&p, pkt->type);
00199 if (mode == RTMP_PS_TWELVEBYTES)
00200 bytestream_put_le32(&p, pkt->extra);
00201 }
00202 if (timestamp >= 0xFFFFFF)
00203 bytestream_put_be32(&p, timestamp);
00204 }
00205
00206 prev_pkt[pkt->channel_id].channel_id = pkt->channel_id;
00207 prev_pkt[pkt->channel_id].type = pkt->type;
00208 prev_pkt[pkt->channel_id].data_size = pkt->data_size;
00209 prev_pkt[pkt->channel_id].timestamp = pkt->timestamp;
00210 if (mode != RTMP_PS_TWELVEBYTES) {
00211 prev_pkt[pkt->channel_id].ts_delta = pkt->ts_delta;
00212 } else {
00213 prev_pkt[pkt->channel_id].ts_delta = pkt->timestamp;
00214 }
00215 prev_pkt[pkt->channel_id].extra = pkt->extra;
00216
00217 url_write(h, pkt_hdr, p-pkt_hdr);
00218 size = p - pkt_hdr + pkt->data_size;
00219 while (off < pkt->data_size) {
00220 int towrite = FFMIN(chunk_size, pkt->data_size - off);
00221 url_write(h, pkt->data + off, towrite);
00222 off += towrite;
00223 if (off < pkt->data_size) {
00224 uint8_t marker = 0xC0 | pkt->channel_id;
00225 url_write(h, &marker, 1);
00226 size++;
00227 }
00228 }
00229 return size;
00230 }
00231
00232 int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type,
00233 int timestamp, int size)
00234 {
00235 pkt->data = av_malloc(size);
00236 if (!pkt->data)
00237 return AVERROR(ENOMEM);
00238 pkt->data_size = size;
00239 pkt->channel_id = channel_id;
00240 pkt->type = type;
00241 pkt->timestamp = timestamp;
00242 pkt->extra = 0;
00243 pkt->ts_delta = 0;
00244
00245 return 0;
00246 }
00247
00248 void ff_rtmp_packet_destroy(RTMPPacket *pkt)
00249 {
00250 if (!pkt)
00251 return;
00252 av_freep(&pkt->data);
00253 pkt->data_size = 0;
00254 }
00255
00256 int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end)
00257 {
00258 const uint8_t *base = data;
00259
00260 if (data >= data_end)
00261 return -1;
00262 switch (*data++) {
00263 case AMF_DATA_TYPE_NUMBER: return 9;
00264 case AMF_DATA_TYPE_BOOL: return 2;
00265 case AMF_DATA_TYPE_STRING: return 3 + AV_RB16(data);
00266 case AMF_DATA_TYPE_LONG_STRING: return 5 + AV_RB32(data);
00267 case AMF_DATA_TYPE_NULL: return 1;
00268 case AMF_DATA_TYPE_ARRAY:
00269 data += 4;
00270 case AMF_DATA_TYPE_OBJECT:
00271 for (;;) {
00272 int size = bytestream_get_be16(&data);
00273 int t;
00274 if (!size) {
00275 data++;
00276 break;
00277 }
00278 if (data + size >= data_end || data + size < data)
00279 return -1;
00280 data += size;
00281 t = ff_amf_tag_size(data, data_end);
00282 if (t < 0 || data + t >= data_end)
00283 return -1;
00284 data += t;
00285 }
00286 return data - base;
00287 case AMF_DATA_TYPE_OBJECT_END: return 1;
00288 default: return -1;
00289 }
00290 }
00291
00292 int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
00293 const uint8_t *name, uint8_t *dst, int dst_size)
00294 {
00295 int namelen = strlen(name);
00296 int len;
00297
00298 while (*data != AMF_DATA_TYPE_OBJECT && data < data_end) {
00299 len = ff_amf_tag_size(data, data_end);
00300 if (len < 0)
00301 len = data_end - data;
00302 data += len;
00303 }
00304 if (data_end - data < 3)
00305 return -1;
00306 data++;
00307 for (;;) {
00308 int size = bytestream_get_be16(&data);
00309 if (!size)
00310 break;
00311 if (data + size >= data_end || data + size < data)
00312 return -1;
00313 data += size;
00314 if (size == namelen && !memcmp(data-size, name, namelen)) {
00315 switch (*data++) {
00316 case AMF_DATA_TYPE_NUMBER:
00317 snprintf(dst, dst_size, "%g", av_int2dbl(AV_RB64(data)));
00318 break;
00319 case AMF_DATA_TYPE_BOOL:
00320 snprintf(dst, dst_size, "%s", *data ? "true" : "false");
00321 break;
00322 case AMF_DATA_TYPE_STRING:
00323 len = bytestream_get_be16(&data);
00324 av_strlcpy(dst, data, FFMIN(len+1, dst_size));
00325 break;
00326 default:
00327 return -1;
00328 }
00329 return 0;
00330 }
00331 len = ff_amf_tag_size(data, data_end);
00332 if (len < 0 || data + len >= data_end || data + len < data)
00333 return -1;
00334 data += len;
00335 }
00336 return -1;
00337 }
00338
00339 static const char* rtmp_packet_type(int type)
00340 {
00341 switch (type) {
00342 case RTMP_PT_CHUNK_SIZE: return "chunk size";
00343 case RTMP_PT_BYTES_READ: return "bytes read";
00344 case RTMP_PT_PING: return "ping";
00345 case RTMP_PT_SERVER_BW: return "server bandwidth";
00346 case RTMP_PT_CLIENT_BW: return "client bandwidth";
00347 case RTMP_PT_AUDIO: return "audio packet";
00348 case RTMP_PT_VIDEO: return "video packet";
00349 case RTMP_PT_FLEX_STREAM: return "Flex shared stream";
00350 case RTMP_PT_FLEX_OBJECT: return "Flex shared object";
00351 case RTMP_PT_FLEX_MESSAGE: return "Flex shared message";
00352 case RTMP_PT_NOTIFY: return "notification";
00353 case RTMP_PT_SHARED_OBJ: return "shared object";
00354 case RTMP_PT_INVOKE: return "invoke";
00355 case RTMP_PT_METADATA: return "metadata";
00356 default: return "unknown";
00357 }
00358 }
00359
00360 static void ff_amf_tag_contents(void *ctx, const uint8_t *data, const uint8_t *data_end)
00361 {
00362 int size;
00363 char buf[1024];
00364
00365 if (data >= data_end)
00366 return;
00367 switch (*data++) {
00368 case AMF_DATA_TYPE_NUMBER:
00369 av_log(ctx, AV_LOG_DEBUG, " number %g\n", av_int2dbl(AV_RB64(data)));
00370 return;
00371 case AMF_DATA_TYPE_BOOL:
00372 av_log(ctx, AV_LOG_DEBUG, " bool %d\n", *data);
00373 return;
00374 case AMF_DATA_TYPE_STRING:
00375 case AMF_DATA_TYPE_LONG_STRING:
00376 if (data[-1] == AMF_DATA_TYPE_STRING) {
00377 size = bytestream_get_be16(&data);
00378 } else {
00379 size = bytestream_get_be32(&data);
00380 }
00381 size = FFMIN(size, 1023);
00382 memcpy(buf, data, size);
00383 buf[size] = 0;
00384 av_log(ctx, AV_LOG_DEBUG, " string '%s'\n", buf);
00385 return;
00386 case AMF_DATA_TYPE_NULL:
00387 av_log(ctx, AV_LOG_DEBUG, " NULL\n");
00388 return;
00389 case AMF_DATA_TYPE_ARRAY:
00390 data += 4;
00391 case AMF_DATA_TYPE_OBJECT:
00392 av_log(ctx, AV_LOG_DEBUG, " {\n");
00393 for (;;) {
00394 int size = bytestream_get_be16(&data);
00395 int t;
00396 memcpy(buf, data, size);
00397 buf[size] = 0;
00398 if (!size) {
00399 av_log(ctx, AV_LOG_DEBUG, " }\n");
00400 data++;
00401 break;
00402 }
00403 if (data + size >= data_end || data + size < data)
00404 return;
00405 data += size;
00406 av_log(ctx, AV_LOG_DEBUG, " %s: ", buf);
00407 ff_amf_tag_contents(ctx, data, data_end);
00408 t = ff_amf_tag_size(data, data_end);
00409 if (t < 0 || data + t >= data_end)
00410 return;
00411 data += t;
00412 }
00413 return;
00414 case AMF_DATA_TYPE_OBJECT_END:
00415 av_log(ctx, AV_LOG_DEBUG, " }\n");
00416 return;
00417 default:
00418 return;
00419 }
00420 }
00421
00422 void ff_rtmp_packet_dump(void *ctx, RTMPPacket *p)
00423 {
00424 av_log(ctx, AV_LOG_DEBUG, "RTMP packet type '%s'(%d) for channel %d, timestamp %d, extra field %d size %d\n",
00425 rtmp_packet_type(p->type), p->type, p->channel_id, p->timestamp, p->extra, p->data_size);
00426 if (p->type == RTMP_PT_INVOKE || p->type == RTMP_PT_NOTIFY) {
00427 uint8_t *src = p->data, *src_end = p->data + p->data_size;
00428 while (src < src_end) {
00429 int sz;
00430 ff_amf_tag_contents(ctx, src, src_end);
00431 sz = ff_amf_tag_size(src, src_end);
00432 if (sz < 0)
00433 break;
00434 src += sz;
00435 }
00436 } else if (p->type == RTMP_PT_SERVER_BW){
00437 av_log(ctx, AV_LOG_DEBUG, "Server BW = %d\n", AV_RB32(p->data));
00438 } else if (p->type == RTMP_PT_CLIENT_BW){
00439 av_log(ctx, AV_LOG_DEBUG, "Client BW = %d\n", AV_RB32(p->data));
00440 } else if (p->type != RTMP_PT_AUDIO && p->type != RTMP_PT_VIDEO && p->type != RTMP_PT_METADATA) {
00441 int i;
00442 for (i = 0; i < p->data_size; i++)
00443 av_log(ctx, AV_LOG_DEBUG, " %02X", p->data[i]);
00444 av_log(ctx, AV_LOG_DEBUG, "\n");
00445 }
00446 }