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