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