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