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