EasyPlayer RTSP播放器源碼解析系列之效率優化方案

測試發現,通過EasyPlayer拉取網絡攝像機的流, 其音頻可能是G711,G726等,而寫MP4或者轉推RTMP等都不支持這些音頻格式,那麼我們就需要將其音頻轉碼成AAC,可以使用libEasyAACEncoder庫進行轉碼,然後寫MP4或者推送;然而,在實際應用中,我們發現轉碼過程其實還是比較耗時的,它甚至會導致解碼線程來不及從而使直播延時增大,所以,我們採用隊列緩存+線程的方式來優化錄像和抓圖。

實現如下:

  1. 錄像優化
    1> 開啓錄像
            if (pThread->manuRecording == 0x01 && NULL==pThread->m_pMP4Writer && frameinfo.type==EASY_SDK_VIDEO_FRAME_I)//開啓錄製
            {
                //EnterCriticalSection(&pThread->critRecQueue);             
                if (!pThread->m_pMP4Writer)
                {
                    pThread->m_pMP4Writer = new EasyMP4Writer();
                }
                unsigned int timestamp = (unsigned int)time(NULL);
                time_t tt = timestamp;
                struct tm *_time = localtime(&tt);
                char szTime[64] = {0,};
                strftime(szTime, 32, "%Y%m%d%H%M%S", _time);

                int nRecordPathLen = strlen(pThread->manuRecordingPath);
                if (nRecordPathLen==0 || (pThread->manuRecordingPath[nRecordPathLen-1] != '/' && pThread->manuRecordingPath[nRecordPathLen-1] != '\\') )
                {
                    pThread->manuRecordingPath[nRecordPathLen] = '/';
                }

                char sFileName[512] = {0,};
                sprintf(sFileName, "%sch%d_%s.mp4", pThread->manuRecordingPath, pThread->channelId, szTime);

                if (!pThread->m_pMP4Writer->CreateMP4File(sFileName, ZOUTFILE_FLAG_FULL))
                {
                    delete pThread->m_pMP4Writer;
                    pThread->m_pMP4Writer = NULL;
                    //return -1;
                }       
                else
                {

                }
                //LeaveCriticalSection(&pThread->critRecQueue);
            }
2> 錄像數據寫緩存
                    if (NULL != pThread->pRecAVQueue)
                    {
                        SSQ_AddData(pThread->pRecAVQueue, channelid, MEDIA_TYPE_VIDEO, (MEDIA_FRAME_INFO*)&frameinfo, pbuf);
                    }
3> 錄像線程處理
LPTHREAD_START_ROUTINE CChannelManager::_lpRecordThread( LPVOID _pParam )
{
    PLAY_THREAD_OBJ *pThread = (PLAY_THREAD_OBJ*)_pParam;
    if (NULL == pThread)            return 0;

    pThread->recordThread.flag  =   0x02;

#ifdef _DEBUG
    _TRACE("錄像線程[%d]已啓動. ThreadId:%d ...\n", pThread->channelId, GetCurrentThreadId());
#endif

    EasyAACEncoder_Handle m_pAACEncoderHandle = NULL;
    int buf_size = 1024*1024;

    char *pbuf = new char[buf_size];
    if (NULL == pbuf)
    {
        pThread->recordThread.flag  =   0x00;
        return 0;
    }

    char* m_pAACEncBufer = new char[buf_size];
    memset(m_pAACEncBufer, 0x00, buf_size);

    //#define AVCODEC_MAX_AUDIO_FRAME_SIZE  (192000)
#define AVCODEC_MAX_AUDIO_FRAME_SIZE    (64000)
    int audbuf_len = (AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2;
    unsigned char *audio_buf = new unsigned char[audbuf_len+1];
    memset(audio_buf, 0x00, audbuf_len);

    MEDIA_FRAME_INFO frameinfo;
    unsigned int channelid = 0;
    unsigned int mediatype = 0;

    while (1)
    {
        if (pThread->recordThread.flag == 0x03)     break;

        int ret = SSQ_GetData(pThread->pRecAVQueue, &channelid, &mediatype, &frameinfo, pbuf);
        if (ret < 0)
        {
            _VS_BEGIN_TIME_PERIOD(1);
            __VS_Delay(1);
            _VS_END_TIME_PERIOD(1);
            continue;
        }

        long long nTimeStamp = frameinfo.timestamp_sec*1000+frameinfo.timestamp_usec/1000;
        byte*pdata=NULL;
        int datasize=0;
        bool keyframe=false;
        try
        {   
            if (mediatype == MEDIA_TYPE_VIDEO)
            {
                pdata = (byte*)pbuf;//獲取到的編碼數據
                datasize = frameinfo.length;
                int nVideoWidth     = frameinfo.width;
                int nVideoHeight    = frameinfo.height;
                keyframe = frameinfo.type==EASY_SDK_VIDEO_FRAME_I?true:false;
                if (pThread->m_pMP4Writer)
                {
                    pThread->m_pMP4Writer->WriteMp4File((unsigned char*)pdata, datasize, keyframe,  nTimeStamp, nVideoWidth, nVideoHeight);
                }
            }
            else //音頻
            {
                pdata = (byte*)pbuf;//獲取到的編碼數據
                datasize = frameinfo.length;
                int bits_per_sample = frameinfo.bits_per_sample;
                int channels = frameinfo.channels;
                int sampleRate = frameinfo.sample_rate;

                if (EASY_SDK_AUDIO_CODEC_G711U == frameinfo.codec
                    || EASY_SDK_AUDIO_CODEC_G726 == frameinfo.codec 
                    || EASY_SDK_AUDIO_CODEC_G711A == frameinfo.codec ) 
                {
                    if (!m_pAACEncoderHandle)
                    {
                        InitParam initParam;
                        initParam.u32AudioSamplerate=frameinfo.sample_rate;
                        initParam.ucAudioChannel=frameinfo.channels;
                        initParam.u32PCMBitSize=frameinfo.bits_per_sample;
                        if (frameinfo.codec == EASY_SDK_AUDIO_CODEC_G711U)
                        {
                            initParam.ucAudioCodec = Law_ULaw;
                        } 
                        else if (frameinfo.codec == EASY_SDK_AUDIO_CODEC_G726)
                        {
                            initParam.ucAudioCodec = Law_G726;
                        }
                        else if (frameinfo.codec == EASY_SDK_AUDIO_CODEC_G711A)
                        {
                            initParam.ucAudioCodec = Law_ALaw;
                        }
                        m_pAACEncoderHandle = Easy_AACEncoder_Init( initParam);
                    }
                    unsigned int out_len = 0;
                    int nRet = Easy_AACEncoder_Encode(m_pAACEncoderHandle, 
                        (unsigned char*)pbuf, frameinfo.length, (unsigned char*)m_pAACEncBufer, &out_len) ;
                    if (nRet>0&&out_len>0)
                    {
                        pdata = (byte*)m_pAACEncBufer;
                        datasize = out_len;
                        frameinfo.codec = EASY_SDK_AUDIO_CODEC_AAC;
                    } 
                    else
                    {
                        continue;
                    }
                }

                if (pThread->m_pMP4Writer)
                {
                    if (pThread->m_pMP4Writer->CanWrite())
                    {
                        pThread->m_pMP4Writer->WriteAACToMp4File((unsigned char*)pdata, 
                            datasize, nTimeStamp, sampleRate, channels, bits_per_sample);
                    }
                }
            }
        }
        catch (...)
        {
            continue;
        }
    }

        pThread->recordThread.flag  =   0x00;

#ifdef _DEBUG
    _TRACE("錄像線程[%d]已退出 ThreadId:%d.\n", pThread->channelId, GetCurrentThreadId());
#endif

    return 0;
}
  1. 抓圖優化

抓圖原理同錄像,唯一區別是直接數據傳入線程,進行jpg編碼存文件,詳見代碼。

發佈了48 篇原創文章 · 獲贊 31 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章