1
头图

音频和视频流最佳选择?SRT协议解析及报文识别

As we know, SRT is an open source video streaming protocol developed by Haivision and Wowza. Many people would think that it will be a substitute for RTMP in the near future. Because the RTMP protocol has lower security and relatively high latency, compared to the SRT protocol, it supports high quality, stability, sub-second latency, and powerful codec support. SRT is considered by many industry experts as a new protocol for video streaming. What exactly is SRT?

What is SRT?

Secure and Reliable Transmission (SRT) is an open source data transmission protocol. SRT uses the User Datagram Protocol (UDP), which is designed to send high-quality video over the public Internet, so this protocol is the best choice for audio and video streaming.

In many major open source technologies Wireshare and FFMpeg, the SRT safe and reliable transmission protocol is applied.

In which areas are SRT applied?

The main applications of the SRT protocol are in live broadcast, multi-streaming, video coding, gateways and other fields. In terms of technology, it provides reliable transmission similar to Transmission Control Protocol (TCP). However, the UDP protocol is used as the underlying transport layer.

img

SRT also supports low-latency (120 milliseconds by default) packet recovery and encryption using Advanced Encryption Standard (AES).

In short, through SRT, end-to-end stream security, video resilience, and real-time dynamic endpoint adjustment based on network conditions become possible.

high quality video transmission

SRT makes it easier to stream via Internet Protocol (IP) with low end-to-end latency. Up to now, there are few protocol preferences for low-latency streaming media.

This is because streaming via the public Internet may cause obstacles such as packet loss and jitter. SRT provides a solution to this problem.

In addition, the agreement also includes protection against packet loss, jitter, and bandwidth fluctuations. This means that if network conditions are unstable, your streaming may stop. But it can recover from this kind of packet loss almost immediately, and your viewers will hardly notice any problems while watching.

Other useful features for live streaming include:

1. Data packet transmission based on timestamp, through source time transmission to achieve better delay control
2. Control the speed of the sender
3. Prevent packet loss caused by not recovering in time
4. Periodic NAK report for packet retransmission

How SRT can better protect your video stream

If you use the SRT protocol to stream video, you will definitely benefit from its advantages. This protocol protects your video stream and ensures that all data is encrypted when sent. It also removes the burden of special Internet connections, because the protocol guarantees the quality of the video content you deliver.

SRT is known for providing encryption technology that ensures secure transmission of even the highest level of products. SRT can enable end-to-end AES 128/256 bit encryption algorithm, which is ideal for any content that needs to be protected. Even during periods of bandwidth fluctuations caused by unreliable WiFi or cellular connections, SRT can prevent video jitter and packet loss, protecting your video content from distribution.

SRT packet

Next, we need to further analyze the SRT protocol.

img

According to the square F framed in red in the figure above:

F=0 ;Data Packet

Data (content to transmit)

Filtering packet (FEC)

F=1;Control Packet

HANDSHAKE

KEEPALIVE

ACK

NAK (Loss Report)

SHUTDOWN

ACKACK

SRT streaming media transmission protocol handshake process

As the initiator of the connection, the caller knows the public IP address of the device corresponding to the Listener mode and the UDP port it monitors. While the Listener monitors the initiated SRT request, it needs to know which UDP port to use, and keep listening on this port.

Caller-Listener Handshake

img

The caller initiates the establishment of an SRT connection for point-to-point transmission, and the Listener monitors the request to initiate an SRT session.

Rendezvous Handshake

Both ends of Rendezvous negotiate to establish a connection, and basically do not use this type of connection.

img

SRT has obvious advantages in fast connection. The connection can be established after two successful handshake; simply understand the handshake process, and then the SRT protocol analysis will follow.

SRT protocol analysis and message identification

Below we analyze the SRT protocol.

/* 实际解析数据包的代码 
 *
 */
 static void dissect_srt_control_packet(u_char *data_info,int PayloadLen)
 {
     int offset = 0;

     offset += 4;

     if (data_info[0] == 0x80 && data_info[1] == 0x02)/*UMSG_ACK*/
     {

        int ack_number = ntohl(*(uint32_t*)(data_info + offset));
        printf("ACK Number: %d\n",ack_number);

        offset += 4; /*Time Stamp*/

        int time_stamp = ntohl(*(uint32_t*)(data_info + offset));
        printf("Time Stamp: %d\n",time_stamp);

        offset += 4; /*Destination Socket ID*/

        int dst_sock_id = ntohl(*(uint32_t*)(data_info + offset));
        printf("Destination Socket ID: %d\n",dst_sock_id);

        offset += 4; /*ACKD_RCVLASTACK*/

        int ack_rcv = ntohl(*(uint32_t*)(data_info + offset));
        printf("ACKD_RCVLASTACK: %d \n",ack_rcv);

        offset += 4; /*ACKD_RTT*/

        int ack_rtt = ntohl(*(uint32_t*)(data_info + offset));
        printf("ACKD_RTT: %d us \n",ack_rtt);

        offset += 4; /*ACKD_RTTVAR*/

        int ack_rttvar = ntohl(*(uint32_t*)(data_info + offset));
        printf("ACKD_RTTVAR: %d us \n",ack_rttvar);

        offset += 4; /*ACKD_BUFFERLEFT*/

        int ack_buffer= ntohl(*(uint32_t*)(data_info + offset));
        printf("ACKD_BUFFERLEFT: %d pkts \n",ack_buffer);

        offset += 4; /*ACKD_RCVSPEED*/

        int ack_rcvspeed= ntohl(*(uint32_t*)(data_info + offset));
        printf("ACKD_RCVSPEED: %d pkts/s \n",ack_rcvspeed);


        offset += 4; /*ACKD_BANDWIDTH*/

        int ack_banwidth= ntohl(*(uint32_t*)(data_info + offset));
        printf("ACKD_BANDWIDTH: %d pkts/s \n",ack_banwidth);

        offset += 4; /*ACKD_RCVRATE*/

        int ack_rcvate= ntohl(*(uint32_t*)(data_info + offset));
        printf("ACKD_RCVRATE: %d pkts/s \n",ack_rcvate);

     }
     else if (data_info[0] == 0x80 && data_info[1] == 0x00)/*UMSG_HANDSHAKE*/
     {
        char ipbuf[IP_BUFFER_SIZE];
        const int final_length = PayloadLen;
        int baselen = 64;
        offset += 12;
        const int version = ntohl(*(uint32_t*)(data_info + offset));
        /*包含握手版本(当前为4或5) */
        printf("Handshake version:%d\n",version);

        offset += 2; /*Encryption Field*/

        offset += 2; /*Extended Field*/

        offset += 4; /*Initial Sequence Number*/

        int srt_handshake_isn= ntohl(*(uint32_t*)(data_info + offset));
        printf("Initial Sequence Number: %d\n",srt_handshake_isn);

        offset += 4; /*MTU*/

        int srt_handshake_mtu= ntohl(*(uint32_t*)(data_info + offset));
        printf("MTU: %d \n",srt_handshake_mtu);

        offset += 4; /*Flow Window*/

        int srt_handshake_flow_window= ntohl(*(uint32_t*)(data_info + offset));
        printf("Flow Window: %d\n",srt_handshake_flow_window);

        offset += 4; /*Hanshake Type*/

        int srt_handshake_reqtype= ntohl(*(uint32_t*)(data_info + offset));
        printf("Hanshake Type: %d\n",srt_handshake_reqtype);

        offset += 4; /*Socket ID*/

        int srt_handshake_id= ntohl(*(uint32_t*)(data_info + offset));
        printf("Socket ID: %d\n",srt_handshake_id);

        offset += 4; /*SYN Cookie*/

        int srt_handshake_cookie= ntohl(*(uint32_t*)(data_info + offset));
        printf("SYN Cookie: %d\n",srt_handshake_cookie);

        offset += 4; /*Peer IP Address*/

        srt_format_ip_address(ipbuf, sizeof ipbuf,strdup((const char*)(data_info+offset)));

        printf("Peer IP Address: %s\n",ipbuf);

        if (final_length > baselen)
        {

            /* 提取SRT握手扩展块
             并相应地增加baselen。 
            */
            int begin = baselen;

            for (;;)
            {
                const uint16_t blockid  = ntohs(*(uint16_t*)(data_info + begin));

                begin += 2;
                const uint16_t blocklen = ntohs(*(uint16_t*)(data_info + begin));


                // Shift to the payload
                begin += 2;

                switch (blockid)
                {
                    case SRT_CMD_HSREQ:
                    case SRT_CMD_HSRSP:
                        if (blocklen == 3)
                        {
                            //uint32_t version = 0;
                            const int vmajor = (data_info[begin+1]) & 0xff;
                            const int vminor = (data_info[begin+2]) & 0xff;
                            const int vpatch = data_info[begin+3] & 0xff;
                            printf("SRT HS Extension type:%d \n",blockid);
                            printf("SRT HS Extension size:%d \n",blocklen);
                            printf("SRT Version(%d.%d.%d)\n", vmajor, vminor, vpatch);

                        }
                        else
                        {

                        }
                        break;

                    case SRT_CMD_KMREQ:
                    case SRT_CMD_KMRSP:
                        // Rely on the extracted blocklen
                        //srt_format_kmx(tree, tvb, begin, blocklen*4);
                        break;

                    case SRT_CMD_SID:
                        break;

                    case SRT_CMD_CONJESTCTRL:
                        break;

                    default:
                        printf( "Ext Type value is %u\n",blockid);
                        break;
                }

                /* Move the index pointer past the block and repeat. */
                begin += blocklen * 4;

                /* OK, once one block is done, interrupt the loop. */
                if (begin >= final_length)
                    break;
            }
            baselen = begin;
        }


     }
     else
     {

     }


 }

static void dissect_srt(u_char *data_info,int PayloadLen)
{
    /* Other misc. local variables. */
    bool is_control = 0;

    /*必须至少有24个捕获的字节才能进行检查 */
    if (PayloadLen < 24)
        return ;      
    printf("SrtHdr 0x%.2X,0x%.2X,0x%.2X,0x%.2X\n",data_info[0],data_info[1],data_info[2],data_info[3]); 

    if ((data_info[0] == 0x80 && data_info[1] == 0x00 && data_info[2] == 0x00 && data_info[3] == 0x00)
    || (data_info[0] == 0x80 && data_info[1] == 0x02 && data_info[2] == 0x00 && data_info[3] == 0x00)/*UMSG_ACK*/
    || (data_info[0] == 0x80 && data_info[1] == 0x06 && data_info[2] == 0x00 && data_info[3] == 0x00)/*UMSG_ACKACK*/
    )
    {
        is_control = true;
    }

    if (is_control)
    {

        dissect_srt_control_packet(data_info,PayloadLen);
    }
    else
    {
        /*srt data type*/
    }
}

Compile and run:

img

The srt protocol is identified here, and each field is parsed.

Comparison of RTMP and SRT protocol features

RTMP is a real-time messaging protocol that maintains a durable, stable connection and allows low-latency communication. Another disadvantage of the RTMP protocol is that it may be interrupted due to low bandwidth until your stream may not start at all. Added to the list of disadvantages, due to the low security of the delivered video, some tight firewalls may not allow RTMP connections. Although, we have to say that this happens rarely.

The RTMP protocol currently uses the H.264 video codec and AAC audio codec, which are quite old and cannot provide the best quality.

Finally, summarize the advantages and disadvantages of RTMP:

Advantages: multicast support, low buffer, wide platform support.

Disadvantages: the old codec has lower security and relatively high latency.

SRT is a safe and reliable transmission protocol. SRT is an open source video streaming protocol developed by Haivision and Wowza. In the near future, it is widely regarded as a substitute for RTMP. Sharing the same advantages, SRT is taking the next step to make the dream of stable live broadcast with sub-second delay a reality. It allows you to broadcast your content via sub-optimal web. However, a big disadvantage is that the playback options are not available.

SRT can protect your real-time video from jitter, bandwidth fluctuations, and packet loss. In addition, in terms of sub-second delay, SRT is similar to FTL and WebRTC and can achieve near real-time communication.

In addition, it is stated that the protocol has nothing to do with codecs, which means that it supports any modern video and audio codecs.

Having said that, the advantages and disadvantages of SRT are:

Advantages: high quality, stability, sub-second delay, powerful codec support.

Disadvantages: The platform support is weak and cannot be played.

summary

If you use the SRT protocol to stream video, you will definitely benefit from its advantages. This protocol protects your video stream and ensures that all data is encrypted when sent. It also removes the burden of special Internet connections, because the protocol guarantees the quality of the video content you deliver.


RTE开发者社区
647 声望966 粉丝

RTE 开发者社区是聚焦实时互动领域的中立开发者社区。不止于纯粹的技术交流,我们相信开发者具备更加丰盈的个体价值。行业发展变革、开发者职涯发展、技术创业创新资源,我们将陪跑开发者,共享、共建、共成长。