SRS 代码分析【RTMP Chunck数据发送】

1.RTMP 发送数据时会调用SrsProtocol::do_send_and_free_packet,该函数定义如下:

int SrsProtocol::do_send_and_free_packet(SrsPacket* packet, int stream_id)
{
    int ret = ERROR_SUCCESS;
    
    srs_assert(packet);
    SrsAutoFree(SrsPacket, packet);
    
    int size = 0;
    char* payload = NULL;
    if ((ret = packet->encode(size, payload)) != ERROR_SUCCESS) {
        srs_error("encode RTMP packet to bytes oriented RTMP message failed. ret=%d", ret);
        return ret;
    }
    
    // encode packet to payload and size.
    if (size <= 0 || payload == NULL) {
        srs_warn("packet is empty, ignore empty message.");
        return ret;
    }
    
    // to message
    SrsMessageHeader header;
    header.payload_length = size;
    header.message_type = packet->get_message_type();
    header.stream_id = stream_id;
    header.perfer_cid = packet->get_prefer_cid();
    
    ret = do_simple_send(&header, payload, size);
    srs_freepa(payload);
    if (ret == ERROR_SUCCESS) {
        ret = on_send_packet(&header, packet);
    }
    
    return ret;
}

1).调用SrsPacket::encode对发送的包进行编码并得到编码后的payload数据以及数据长度size。Srs中RTMP的消息和都有对应的类,例如SrsConnectAppPacket是建立连接的消息,SrsPacket是他们的基类,SrsPacket::encode就是将各个消息中的 AMF数据编码payload

2).生成消息头的信息SrsMessageHeader

3).调用SrsProtocol::do_simple_send发送消息头部和负载数据

2.SrsProtocol::do_simple_send将消息分成一个或多个chunk发送,该函数定义如下:

int SrsProtocol::do_simple_send(SrsMessageHeader* mh, char* payload, int size)
{
    int ret = ERROR_SUCCESS;
    
    // we directly send out the packet,
    // use very simple algorithm, not very fast,
    // but it's ok.
    char* p = payload;
    char* end = p + size;
    char c0c3[SRS_CONSTS_RTMP_MAX_FMT0_HEADER_SIZE];
    while (p < end) {
        int nbh = 0;
        if (p == payload) {
            nbh = srs_chunk_header_c0(
                                      mh->perfer_cid, (uint32_t)mh->timestamp, mh->payload_length,
                                      mh->message_type, mh->stream_id,
                                      c0c3, sizeof(c0c3));
        } else {
            nbh = srs_chunk_header_c3(
                                      mh->perfer_cid, (uint32_t)mh->timestamp,
                                      c0c3, sizeof(c0c3));
        }
        srs_assert(nbh > 0);;
        
        iovec iovs[2];
        iovs[0].iov_base = c0c3;
        iovs[0].iov_len = nbh;
        
        int payload_size = srs_min((int)(end - p), out_chunk_size);
        iovs[1].iov_base = p;
        iovs[1].iov_len = payload_size;
        p += payload_size;
        
        if ((ret = skt->writev(iovs, 2, NULL)) != ERROR_SUCCESS) {
            if (!srs_is_client_gracefully_close(ret)) {
                srs_error("send packet with writev failed. ret=%d", ret);
            }
            return ret;
        }
    }
    
    return ret;
}

1).if(p==payload)代表是发送第一个chunk stream包,因此message header头部包含timestamp(时间戳),message length(消息数据的长度), message type id(消息的类型id), msg stream id(消息的流id)

2).发送其他chunk stream块时,message header头部只会包含timestamp(时间戳),其他字段与第一个chunk包是相同的。


int srs_chunk_header_c0(int perfer_cid, uint32_t timestamp, int32_t payload_length, int8_t message_type, int32_t stream_id, char* cache, int nb_cache)
    {
        // to directly set the field.
        char* pp = NULL;
        
        // generate the header.
        char* p = cache;
        
        // no header.
        if (nb_cache < SRS_CONSTS_RTMP_MAX_FMT0_HEADER_SIZE) {
            return 0;
        }
        
        // write new chunk stream header, fmt is 0
        *p++ = 0x00 | (perfer_cid & 0x3F);
        
        // chunk message header, 11 bytes
        // timestamp, 3bytes, big-endian
        if (timestamp < RTMP_EXTENDED_TIMESTAMP) {
            pp = (char*)×tamp;
            *p++ = pp[2];
            *p++ = pp[1];
            *p++ = pp[0];
        } else {
            *p++ = 0xFF;
            *p++ = 0xFF;
            *p++ = 0xFF;
        }
        
        // message_length, 3bytes, big-endian
        pp = (char*)&payload_length;
        *p++ = pp[2];
        *p++ = pp[1];
        *p++ = pp[0];
        
        // message_type, 1bytes
        *p++ = message_type;
        
        // stream_id, 4bytes, little-endian
        pp = (char*)&stream_id;
        *p++ = pp[0];
        *p++ = pp[1];
        *p++ = pp[2];
        *p++ = pp[3];
        
        // for c0
        // chunk extended timestamp header, 0 or 4 bytes, big-endian
        //
        // for c3:
        // chunk extended timestamp header, 0 or 4 bytes, big-endian
        // 6.1.3. Extended Timestamp
        // This field is transmitted only when the normal time stamp in the
        // chunk message header is set to 0x00ffffff. If normal time stamp is
        // set to any value less than 0x00ffffff, this field MUST NOT be
        // present. This field MUST NOT be present if the timestamp field is not
        // present. Type 3 chunks MUST NOT have this field.
        // adobe changed for Type3 chunk:
        //        FMLE always sendout the extended-timestamp,
        //        must send the extended-timestamp to FMS,
        //        must send the extended-timestamp to flash-player.
        // @see: ngx_rtmp_prepare_message
        // @see: http://blog.csdn.net/win_lin/article/details/13363699
        // TODO: FIXME: extract to outer.
        if (timestamp >= RTMP_EXTENDED_TIMESTAMP) {
            pp = (char*)×tamp;
            *p++ = pp[3];
            *p++ = pp[2];
            *p++ = pp[1];
            *p++ = pp[0];
        }
        
        // always has header
        return (int)(p - cache);
    }
    
    int srs_chunk_header_c3(int perfer_cid, uint32_t timestamp, char* cache, int nb_cache)
    {
        // to directly set the field.
        char* pp = NULL;
        
        // generate the header.
        char* p = cache;
        
        // no header.
        if (nb_cache < SRS_CONSTS_RTMP_MAX_FMT3_HEADER_SIZE) {
            return 0;
        }
        
        // write no message header chunk stream, fmt is 3
        // @remark, if perfer_cid > 0x3F, that is, use 2B/3B chunk header,
        // SRS will rollback to 1B chunk header.
        *p++ = 0xC0 | (perfer_cid & 0x3F);
        
        // for c0
        // chunk extended timestamp header, 0 or 4 bytes, big-endian
        //
        // for c3:
        // chunk extended timestamp header, 0 or 4 bytes, big-endian
        // 6.1.3. Extended Timestamp
        // This field is transmitted only when the normal time stamp in the
        // chunk message header is set to 0x00ffffff. If normal time stamp is
        // set to any value less than 0x00ffffff, this field MUST NOT be
        // present. This field MUST NOT be present if the timestamp field is not
        // present. Type 3 chunks MUST NOT have this field.
        // adobe changed for Type3 chunk:
        //        FMLE always sendout the extended-timestamp,
        //        must send the extended-timestamp to FMS,
        //        must send the extended-timestamp to flash-player.
        // @see: ngx_rtmp_prepare_message
        // @see: http://blog.csdn.net/win_lin/article/details/13363699
        // TODO: FIXME: extract to outer.
        if (timestamp >= RTMP_EXTENDED_TIMESTAMP) {
            pp = (char*)×tamp;
            *p++ = pp[3];
            *p++ = pp[2];
            *p++ = pp[1];
            *p++ = pp[0];
        }
        
        // always has header
        return (int)(p - cache);
    }











  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值