EasyDarwin EasyClient开源流媒体客户端源码功能框架解析

EasyClient是EasyDarwin开源流媒体团队开发的一款功能丰富的开源PC客户端项目,目前支持Windows、Android版本,后续将支持ios版本,其中Windows版本的EasyClient支持多种源接入,包括Windows摄像头、麦克风、RTSP摄像机、屏幕桌面等,采集后经过x264编码、EasyPusher推送到EasyDarwin流媒体服务器进行转发,同时EasyClient还支持通过EasyRTSPClient拉取EasyDarwin直播流,进行显示、播放,非常稳定、易用,用户可以基于EasyClient项目,开发类似于课堂直播、视频对讲等项目!

EasyClient功能介绍:http://blog.csdn.net/xiejiashu/article/details/50088337#t1

功能讲解

一、程序框架
EasyClient主要包括三个模块:采集、推送和直播,主要功能封装管理类Class CSourceManager中实现,接口非常简单,各模块分别提供开始和结束接口函数,方便界面调用;
界面调用接口:

    //开始捕获(采集)
    int StartCapture(SOURCE_TYPE eSourceType, int nCamId, int nAudioId, HWND hCapWnd, char* szURL, int nVideoWidth, int nVideoHeight, int nFps=, int nBitRate);
    //停止采集
    void StopCapture();

    //开始推流
    int StartPush(char* ServerIp, int nPushPort, char* sPushName, int nPushBufSize = 1024);
    //停止推流
    void StopPush();

    //开始播放
    int StartPlay(char* szURL, HWND hShowWnd);
    //停止播放
    void StopPlay();

1、采集模块
采集分为本地音视频采集和RTSP流采集
本地音视频主要通过DShow进行采集,函数如下:

int CSourceManager::StartDSCapture(int nCamId, int nAudioId,HWND hShowWnd,int nVideoWidth, int nVideoHeight, int nFps, int nBitRate)

该函数主要实现本地音视频采集和音视频编码器的初始化(详见EasyClient源码),需要注意的是这里的参数设置:
(1) 本地采集的视频宽高和x264编码器的宽高需一致,数据格式建议设为YUY2(程序中默认为“YUY2”),因为在DShow的数据采集线程中需要进行编码前的格式转换(YUY2->I420),如果格式不统一,这里将要重写转换函数;
(2) 本地音频采样率默认为16000,这个设置在EasyPusher中表现最佳,其他采样率还有待测试;
(3) 其他设置请参照DEVICE_CONFIG_INFO结构和Encoder_Config_Info结构的详细说明;

细心的童鞋应该已经发现RTSP流采集和流播放采用的是同一个类Class EasyPlayerManager实现,如下:

    //接收网络RTSP流进行推流
    EasyPlayerManager m_netStreamCapture;
    //接收EasyDarwin推出的RTSP流进行播放
    EasyPlayerManager m_netStreamPlayer;

这个类封装了libEasyPlayer库提供的接口,方便调用;这个库集成在EasyClient源码中,也是EasyPlayer的核心;她主要实现了从网络接收RTSP流进行解析,获取H264编码数据和AAC编码数据分别进行解码并呈现和播放,当然,作为Capturer而言,我们只需要用她获取到编码数据即可。


2、推送模块
推送则显得异常简单,直接调用原生态的EasyPusher接口,即可实现:

//开始推流
int CSourceManager::StartPush(char* ServerIp, int nPushPort, char* sPushName, int nPushBufSize)
{
    //创建推送器指针
    m_sPushInfo.pusherHandle = EasyPusher_Create();
    strcpy(m_sPushInfo.pushServerAddr,  ServerIp);
    m_sPushInfo.pushServerPort = nPushPort;
    strcpy(m_sPushInfo.sdpName, sPushName);
    Easy_U32 nRet = 0;
    if (NULL != m_sPushInfo.pusherHandle )
    {
        //设置推送回调,可以获取推送器反馈的信息
        EasyPusher_SetEventCallback(m_sPushInfo.pusherHandle, __EasyPusher_Callback, 0, NULL);
        //开启流推送
        Easy_U32 nRet = EasyPusher_StartStream(m_sPushInfo.pusherHandle , 
            ServerIp, nPushPort, sPushName, "admin", "admin", (EASY_MEDIA_INFO_T*)&m_mediainfo, nPushBufSize, 0);//512-2048
        if(nRet>=0)
        {
            m_bPushing = TRUE;
        }
        else
        {
            StopPush();
        }
    }
    return nRet;
}
//停止推流
void CSourceManager::StopPush()
{
    //Close Pusher
    if (NULL != m_sPushInfo.pusherHandle)
    {
        EasyPusher_StopStream(m_sPushInfo.pusherHandle);
        EasyPusher_Release(m_sPushInfo.pusherHandle);
        m_sPushInfo.pusherHandle = NULL;
    }
    m_bPushing = FALSE;
    m_bAVSync = FALSE;
}

唯一需要注意的是推送标志m_bPushing,这个标志将在数据回调函数中起到真正的推送开关的作用(严格的说在StartPush中也应该调用m_bPushing判断是否推送已经进行)。


3、直播模块
直播在采集模块中采集网络流时提到过,也是直接调用类Class EasyPlayerManager接口实现:

//开始播放
int CSourceManager::StartPlay(char* szURL, HWND hShowWnd)
{
    m_sPlayInfo.rtspSourceId = m_netStreamPlayer.Start(szURL, hShowWnd, DISPLAY_FORMAT_RGB24_GDI, 0x01, "", "");
    m_netStreamPlayer.Config(3, TRUE, TRUE);
    return m_sPlayInfo.rtspSourceId ;
}
//停止播放
void CSourceManager::StopPlay()
{
    m_netStreamPlayer.Close();
}

源码下载:https://github.com/EasyDarwin/EasyClient

你可能感兴趣的:(EasyClient)