⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtp.c

📁 ffmpeg源码分析
💻 C
📖 第 1 页 / 共 3 页
字号:
static void rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m){    RTPDemuxContext *s = s1->priv_data;#ifdef DEBUG    printf("rtp_send_data size=%d\n", len);#endif    /* build the RTP header */    put_byte(&s1->pb, (RTP_VERSION << 6));    put_byte(&s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));    put_be16(&s1->pb, s->seq);    put_be32(&s1->pb, s->timestamp);    put_be32(&s1->pb, s->ssrc);    put_buffer(&s1->pb, buf1, len);    put_flush_packet(&s1->pb);    s->seq++;    s->octet_count += len;    s->packet_count++;}/* send an integer number of samples and compute time stamp and fill   the rtp send buffer before sending. */static void rtp_send_samples(AVFormatContext *s1,                             const uint8_t *buf1, int size, int sample_size){    RTPDemuxContext *s = s1->priv_data;    int len, max_packet_size, n;    max_packet_size = (s->max_payload_size / sample_size) * sample_size;    /* not needed, but who nows */    if ((size % sample_size) != 0)        av_abort();    while (size > 0) {        len = (max_packet_size - (s->buf_ptr - s->buf));        if (len > size)            len = size;        /* copy data */        memcpy(s->buf_ptr, buf1, len);        s->buf_ptr += len;        buf1 += len;        size -= len;        n = (s->buf_ptr - s->buf);        /* if buffer full, then send it */        if (n >= max_packet_size) {            rtp_send_data(s1, s->buf, n, 0);            s->buf_ptr = s->buf;            /* update timestamp */            s->timestamp += n / sample_size;        }    }}/* NOTE: we suppose that exactly one frame is given as argument here *//* XXX: test it */static void rtp_send_mpegaudio(AVFormatContext *s1,                               const uint8_t *buf1, int size){    RTPDemuxContext *s = s1->priv_data;    AVStream *st = s1->streams[0];    int len, count, max_packet_size;    max_packet_size = s->max_payload_size;    /* test if we must flush because not enough space */    len = (s->buf_ptr - s->buf);    if ((len + size) > max_packet_size) {        if (len > 4) {            rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);            s->buf_ptr = s->buf + 4;            /* 90 KHz time stamp */            s->timestamp = s->base_timestamp +                (s->cur_timestamp * 90000LL) / st->codec->sample_rate;        }    }    /* add the packet */    if (size > max_packet_size) {        /* big packet: fragment */        count = 0;        while (size > 0) {            len = max_packet_size - 4;            if (len > size)                len = size;            /* build fragmented packet */            s->buf[0] = 0;            s->buf[1] = 0;            s->buf[2] = count >> 8;            s->buf[3] = count;            memcpy(s->buf + 4, buf1, len);            rtp_send_data(s1, s->buf, len + 4, 0);            size -= len;            buf1 += len;            count += len;        }    } else {        if (s->buf_ptr == s->buf + 4) {            /* no fragmentation possible */            s->buf[0] = 0;            s->buf[1] = 0;            s->buf[2] = 0;            s->buf[3] = 0;        }        memcpy(s->buf_ptr, buf1, size);        s->buf_ptr += size;    }    s->cur_timestamp += st->codec->frame_size;}/* NOTE: a single frame must be passed with sequence header if   needed. XXX: use slices. */static void rtp_send_mpegvideo(AVFormatContext *s1,                               const uint8_t *buf1, int size){    RTPDemuxContext *s = s1->priv_data;    AVStream *st = s1->streams[0];    int len, h, max_packet_size;    uint8_t *q;    max_packet_size = s->max_payload_size;    while (size > 0) {        /* XXX: more correct headers */        h = 0;        if (st->codec->sub_id == 2)            h |= 1 << 26; /* mpeg 2 indicator */        q = s->buf;        *q++ = h >> 24;        *q++ = h >> 16;        *q++ = h >> 8;        *q++ = h;        if (st->codec->sub_id == 2) {            h = 0;            *q++ = h >> 24;            *q++ = h >> 16;            *q++ = h >> 8;            *q++ = h;        }        len = max_packet_size - (q - s->buf);        if (len > size)            len = size;        memcpy(q, buf1, len);        q += len;        /* 90 KHz time stamp */        s->timestamp = s->base_timestamp +            av_rescale((int64_t)s->cur_timestamp * st->codec->time_base.num, 90000, st->codec->time_base.den); //FIXME pass timestamps        rtp_send_data(s1, s->buf, q - s->buf, (len == size));        buf1 += len;        size -= len;    }    s->cur_timestamp++;}static void rtp_send_raw(AVFormatContext *s1,                         const uint8_t *buf1, int size){    RTPDemuxContext *s = s1->priv_data;    AVStream *st = s1->streams[0];    int len, max_packet_size;    max_packet_size = s->max_payload_size;    while (size > 0) {        len = max_packet_size;        if (len > size)            len = size;        /* 90 KHz time stamp */        s->timestamp = s->base_timestamp +            av_rescale((int64_t)s->cur_timestamp * st->codec->time_base.num, 90000, st->codec->time_base.den); //FIXME pass timestamps        rtp_send_data(s1, buf1, len, (len == size));        buf1 += len;        size -= len;    }    s->cur_timestamp++;}/* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */static void rtp_send_mpegts_raw(AVFormatContext *s1,                                const uint8_t *buf1, int size){    RTPDemuxContext *s = s1->priv_data;    int len, out_len;    while (size >= TS_PACKET_SIZE) {        len = s->max_payload_size - (s->buf_ptr - s->buf);        if (len > size)            len = size;        memcpy(s->buf_ptr, buf1, len);        buf1 += len;        size -= len;        s->buf_ptr += len;        out_len = s->buf_ptr - s->buf;        if (out_len >= s->max_payload_size) {            rtp_send_data(s1, s->buf, out_len, 0);            s->buf_ptr = s->buf;        }    }}/* write an RTP packet. 'buf1' must contain a single specific frame. */static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt){    RTPDemuxContext *s = s1->priv_data;    AVStream *st = s1->streams[0];    int rtcp_bytes;    int64_t ntp_time;    int size= pkt->size;    uint8_t *buf1= pkt->data;#ifdef DEBUG    printf("%d: write len=%d\n", pkt->stream_index, size);#endif    /* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */    rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /        RTCP_TX_RATIO_DEN;    if (s->first_packet || rtcp_bytes >= 28) {        /* compute NTP time */        /* XXX: 90 kHz timestamp hardcoded */        ntp_time = (pkt->pts << 28) / 5625;        rtcp_send_sr(s1, ntp_time);        s->last_octet_count = s->octet_count;        s->first_packet = 0;    }    switch(st->codec->codec_id) {    case CODEC_ID_PCM_MULAW:    case CODEC_ID_PCM_ALAW:    case CODEC_ID_PCM_U8:    case CODEC_ID_PCM_S8:        rtp_send_samples(s1, buf1, size, 1 * st->codec->channels);        break;    case CODEC_ID_PCM_U16BE:    case CODEC_ID_PCM_U16LE:    case CODEC_ID_PCM_S16BE:    case CODEC_ID_PCM_S16LE:        rtp_send_samples(s1, buf1, size, 2 * st->codec->channels);        break;    case CODEC_ID_MP2:    case CODEC_ID_MP3:        rtp_send_mpegaudio(s1, buf1, size);        break;    case CODEC_ID_MPEG1VIDEO:        rtp_send_mpegvideo(s1, buf1, size);        break;    case CODEC_ID_MPEG2TS:        rtp_send_mpegts_raw(s1, buf1, size);        break;    default:        /* better than nothing : send the codec raw data */        rtp_send_raw(s1, buf1, size);        break;    }    return 0;}static int rtp_write_trailer(AVFormatContext *s1){    //    RTPDemuxContext *s = s1->priv_data;    return 0;}AVOutputFormat rtp_mux = {    "rtp",    "RTP output format",    NULL,    NULL,    sizeof(RTPDemuxContext),    CODEC_ID_PCM_MULAW,    CODEC_ID_NONE,    rtp_write_header,    rtp_write_packet,    rtp_write_trailer,};int rtp_init(void){    av_register_output_format(&rtp_mux);    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -