流媒体iOS音视频开发视频直播

RTMP协议 01 入门

2017-05-26  本文已影响246人  FlyingPenguin

RTMP

Real-Time Messaging Protocol (RTMP) was an application-level protocol designed for multiplexing and packetizing multimedia transport streams (such as audio, video, and interactive content) over a suitable transport protocol (such as TCP).
RTMP was developed by Adobe for streaming audio, video and data over the Internet, between a Flash player and a server.
RTMP is a TCP-based protocol which maintains persistent connections and allows low-latency communication.

RTMP是一个应用层协议,主要用于在Flash player和服务器之间传输视频、音频、控制命令等内容。
该协议的突出优点是: 低延时

RTMP Based on TCP
RTMP基于TCP, 默认使用端口1935
基于TCP, 提高了RTMP的可靠性。但在直播过程中,如果网络条件差时,由于TCP存在重传的机制,所以导致RTMP存在累计延时

RTMP有个弱点就是累积误差,原因是RTMP基于TCP不会丢包。
所以当网络状态差时,服务器会将包缓存起来,导致累积的延迟;
待网络状况好了,就一起发给客户端。
这个的对策就是,当客户端的缓冲区很大,就断开重连
http://blog.csdn.net/yuan1125/article/details/51239300

累计延时也可以从流媒体服务器的角度进行优化,比如当服务器发现当前队列中未处理的的视频和音频帧数累计达到一定数目(如50帧),则清空该队列,直接处理最新的实时数据。

RTMP层次 (数据角度)

    /*
     librtmp接收rtmp流保存为flv文件.
    */

    RTMP *rtmp = RTMP_Alloc();
    ...

    FILE *fpFLVFile= NULL;
    fpFLVFile = fopen(szFLVFileName, "wb");
    ...

    int nBufSize   = 1024*1024*10;
    char *szBuffer = (char*)malloc(nBufSize);
    memset(szBuffer, 0, nBufSize);
    ...

    while (!g_bStop)
    {
            // RTMP_Read返回0时说明RTMP_READ_COMPLETE
            nRead = RTMP_Read(rtmp, szBuffer, nBufSize);

            if (0 == nRead)
            {
                g_strMsg.Format("RTMP流接收结束(也可能是流存在文件读取失败). RTMP_Read 0 Bytes");
                ::PostMessage(g_hAppWnd, WM_ADD_LOG_TO_LIST, NULL, (LPARAM)g_strMsg.GetBuffer());

                g_bStop = true;

                break;
            }

            if (fpFLVFile)
            {
                fwrite(szBuffer, 1, nRead, fpFLVFile);
                nCountBufSize += nRead;
            }
        ...
    }

    if(fpFLVFile) 
    {
        fclose(fpFLVFile);
        fpFLVFile = NULL;
    }
    ...

Message & Chunk

Message

RTMP中一个重要的概念就是消息。

RTMP Message Message Header RTMP Message分类

消息主要分为三类: 协议控制消息数据消息命令消息等。

Chunk

网络中实际发送的内容

Chunk Format
Message被切割成一个或多个Chunk,然后在网络上进行发送。
当发送时,一个chunk发送完毕后才可以发送下一个chunk。 Message被拆分成一个或多个Chunk,然后在网络上发送

拆分的时候,默认的Chunk Size是128字节,以Message大小为300字节举例,进行拆分。

300 = 128 + 128 + 44
Message 拆分成 Chunk举例

RTMP实质

RTMP的简单架构
http://www.ithao123.cn/content-6486208.html

RTMP层次 (协议角度)

RTMP层次 (协议角度)

References:

https://en.wikipedia.org/wiki/Real-Time_Messaging_Protocol
http://mingyangshang.github.io/2016/03/06/RTMP%E5%8D%8F%E8%AE%AE/
http://www.cnblogs.com/chgaowei/p/5445558.html
http://www.archnote.net/evernote/streaming/RTMP_Details.html
http://www.cnweblog.com/fly2700/archive/2008/04/09/281431.html
http://www.ithao123.cn/content-6486208.html
http://blog.csdn.net/leixiaohua1020/article/details/11694129
http://blog.csdn.net/yuan1125/article/details/51239300

上一篇 下一篇

猜你喜欢

热点阅读