123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166 |
- // libwmvrecord.cpp : 定义 DLL 应用程序的导出函数。
- //
- #include "libwmvrecord.h"
- #ifdef RVC_OS_WIN
- #include "io.h"
- #include <process.h>
- #include <windows.h> //包含头文件 windows.h
- #else
- #include <pthread.h>
- #endif
- #include <dirent.h>
- #include <stdint.h>
- #include <unistd.h>
- #include "libaudioqueue.h"
- #include "libvideoqueue.h"
- #include <stdio.h>
- #include <math.h>
- #include <exception>
- #include "FFmpegWriter.h"
- #include "videoutil.h"
- #include "cv.h"
- #include "highgui.h"
- #include "CvxText.h" // 用于中文字幕添加 add by ly
- #include <speex/speex_resampler.h>
- #include "iaudionsinterface.h"
- #include <semaphore.h>
- #define SLASH '/'
- #define BACK_SLASH '\\'
- #define MB_SLASH_STR "/"
- #define MB_BACK_SLASH_STR "\\"
- #define W_SLASH_STR L"/"
- #define W_BACK_SLASH_STR L"\\"
- #if defined(UNICODE) || defined(_UNICODE)
- #define SLASH_STR W_SLASH_STR
- #define BACK_SLASH_STR W_BACK_SLASH_STR
- #else
- #define SLASH_STR MB_SLASH_STR
- #define BACK_SLASH_STR MB_BACK_SLASH_STR
- #endif
- #ifdef _WIN32
- #define SPLIT_SLASH BACK_SLASH
- #define SPLIT_SLASH_STR BACK_SLASH_STR
- #define MB_SPLIT_SLASH_STR MB_BACK_SLASH_STR
- #define W_SPLIT_SLASH_STR W_BACK_SLASH_STR
- #define LINE_BREAK_STR "\r\n"
- #else
- #define SPLIT_SLASH SLASH
- #define SPLIT_SLASH_STR SLASH_STR
- #define MB_SPLIT_SLASH_STR MB_SLASH_STR
- #define W_SPLIT_SLASH_STR W_SLASH_STR
- #define LINE_BREAK_STR "\n"
- #endif
- #ifndef CAPTURE_CLOCK
- #define CAPTURE_CLOCK 8000
- #endif
- #ifndef CAPTURE_FRAME_TIME
- #define CAPTURE_FRAME_TIME 20 // 20ms per frame
- #endif
- #ifdef RVC_OS_WIN
- #include <DbgHelp.h>
- #pragma comment(lib, "dbghelp.lib")
- #endif
- #ifndef RVC_OS_WIN
- #include <time.h>
- unsigned int timeGetTime()
- {
- unsigned int uptime = 0;
- struct timespec on;
- if (clock_gettime(CLOCK_MONOTONIC, &on) == 0)
- uptime = on.tv_sec * 1000 + on.tv_nsec / 1000000;
- return uptime;
- }
- #endif
- void GetLocalTimeRVC(SYSTEMTIME& stTime)
- {
- #ifdef RVC_OS_WIN
- GetLocalTime(&stTime);
- #else
- struct tm* pst = NULL;
- time_t t = time(NULL);
- pst = localtime(&t);
- stTime.wYear = pst->tm_year + 1900;
- stTime.wMonth = pst->tm_mon + 1;//because the range of tm_mon is:0-11
- stTime.wDay = pst->tm_mday;
- stTime.wHour = pst->tm_hour;
- stTime.wMinute = pst->tm_min;
- stTime.wDayOfWeek = pst->tm_wday;
- stTime.wSecond = pst->tm_sec;
- stTime.wMilliseconds = 0;
- //Dbg("oiltest,time:%d,%d,%d,%d", stTime.wYear, stTime.wHour, stTime.wMinute, stTime.wDayOfWeek);
- #endif
- }
- class libwmvrecord_impl : public LogApi
- {
- public:
- libwmvrecord_impl(BOOL *pResult,CWmvHostApi *pHostAPI,LPCTSTR audioqueuename,LPCTSTR videoqueuename,LPCTSTR videoqueue2name,LPCTSTR salesaudioqueuename,LPCTSTR remotevideoqueuename,LPCTSTR remoteaudioqueuename)
- {
- m_bResult = pResult;
- m_pHostApi = pHostAPI;
- m_bStopRecord = FALSE;
- m_bCloseVideo = FALSE;
- m_eRecordType = eSingleSide;
- m_bIsAudioNsOn = FALSE;
- m_iNsPolicy = 2;
- m_bIsAudioTransOn = FALSE;
- m_bReNameVideo = FALSE;
- m_nFps = 0;
- m_bWholeSection = FALSE;
- m_bSessionManage = FALSE;
- videoframe = new videoq_frame;
- memset(videoframe,0,sizeof(videoq_frame));
- audioframe = new audio_frame;
- memset(audioframe,0,sizeof(audio_frame));
- m_nRecordthreadId = 0;
- sem_init(&m_semt, 0, 0);
- m_pAudioBufferTmp = NULL; //音频录制拼接BUFFER
- m_iAudioBufferSize = 0;
- memset(m_FileName,0,MAX_PATH);
- memset(m_WmvFileName,0,MAX_PATH);
- memset(m_PathName,0,MAX_PATH);
- memset(m_audioqueuename, 0, MAX_PATH);
- memset(m_salesaudioqueuename, 0, MAX_PATH);
- memset(m_videoqueuename, 0, MAX_PATH);
- memset(m_videoqueue2name, 0, MAX_PATH);
- memset(m_remotevideoqueuename, 0, MAX_PATH);
- memset(m_remoteaudioqueuename, 0, MAX_PATH);
- m_audioqueue = NULL;
- strncpy(m_audioqueuename, audioqueuename, (strlen(audioqueuename) > MAX_PATH) ? (MAX_PATH - 1): strlen(audioqueuename));
- m_salesaudioqueue = NULL;
- if (salesaudioqueuename){
- strncpy(m_salesaudioqueuename, salesaudioqueuename, (strlen(salesaudioqueuename) > MAX_PATH) ? (MAX_PATH - 1) : strlen(salesaudioqueuename));
- }
- m_videoqueue = NULL;
- if (videoqueuename){
- strncpy(m_videoqueuename, videoqueuename, (strlen(videoqueuename) > MAX_PATH) ? (MAX_PATH - 1) : strlen(videoqueuename));
- }
- m_videoqueue2 = NULL;
- if (videoqueue2name){
- strncpy(m_videoqueue2name, videoqueue2name, (strlen(videoqueue2name) > MAX_PATH) ? (MAX_PATH - 1) : strlen(videoqueue2name));
- }
- m_remotevideoqueue = NULL;
- if (remotevideoqueuename){
- strncpy(m_remotevideoqueuename, remotevideoqueuename, (strlen(remotevideoqueuename) > MAX_PATH) ? (MAX_PATH - 1) : strlen(remotevideoqueuename));
- }
- m_remoteaudioqueue = NULL;
- if (remoteaudioqueuename){
- strncpy(m_remoteaudioqueuename, remoteaudioqueuename, (strlen(remoteaudioqueuename) > MAX_PATH) ? (MAX_PATH - 1) : strlen(remoteaudioqueuename));
- }
-
- m_pWriter = NULL;
- m_pAudioNsObj = NULL;
- int err = 0;
- m_16kto8kst = speex_resampler_init_frac(1, 2*CAPTURE_CLOCK , CAPTURE_CLOCK, 2*CAPTURE_CLOCK, CAPTURE_CLOCK, 0, &err);
- *pResult = TRUE;
- }
- ~libwmvrecord_impl()
- {
- if (videoframe->data != NULL){
- delete videoframe->data;
- videoframe->data = NULL;
- }
- if (videoframe != NULL){
- delete videoframe;
- videoframe = NULL;
- }
- if (audioframe != NULL){
- delete audioframe;
- audioframe = NULL;
- }
- if (m_pWriter!= NULL){
- delete m_pWriter;
- m_pWriter = NULL;
- }
- if (m_localaudioqueue != NULL){
- delete m_localaudioqueue;
- m_localaudioqueue = NULL;
- }
- if (m_salesaudioqueue != NULL){
- delete m_salesaudioqueue;
- m_salesaudioqueue = NULL;
- }
- m_audioqueue = NULL;
- if (m_remoteaudioqueue != NULL){
- delete m_remoteaudioqueue;
- m_remoteaudioqueue = NULL;
- }
- if (m_videoqueue != NULL){
- delete m_videoqueue;
- m_videoqueue = NULL;
- }
- if (m_videoqueue2 != NULL){
- delete m_videoqueue2;
- m_videoqueue2 = NULL;
- }
- if (m_remotevideoqueue != NULL){
- delete m_remotevideoqueue;
- m_remotevideoqueue = NULL;
- }
- if (NULL != m_16kto8kst){
- speex_resampler_destroy(m_16kto8kst);
- m_16kto8kst = NULL;
- }
- if (NULL != m_pAudioNsObj){
- DestroyIAudioNsObj(m_pAudioNsObj);
- m_pAudioNsObj = NULL;
- }
- }
- CWmvHostApi* GetHostApi()
- {
- return m_pHostApi;
- }
- private:
- sem_t m_semt;
- BOOL m_bStopRecord;
- eRvcRecordType m_eRecordType; //录制双向的视频类型,0:单向录制,1:手机<->pad双录,2:坐席<->pad双录,3:坐席<->大机双录
- eAudioOutPutType m_eAudioType;
- BOOL m_bIsAudioNsOn; //是否开启音频降噪功能开关
- int m_iNsPolicy; //音频降噪等级
- BOOL m_bIsAudioTransOn; //是否开始音频传输到识别服务器开关
- int m_nFps;
- /************************************************************************/
- int m_videoquality;
- SubtitleParam m_SubtitleParam;
- BOOL m_bWholeSection;
- BOOL m_bSessionManage;
- /************************************************************************/
- videoq_frame* videoframe;
- audio_frame* audioframe;
- Clibaudioqueue*m_audioqueue;
- Clibvideoqueue*m_videoqueue;
- Clibvideoqueue*m_videoqueue2;
- Clibvideoqueue*m_remotevideoqueue; //远端视频,用于双向录像
- Clibaudioqueue*m_remoteaudioqueue; //远端音频,用于双向录像
- Clibaudioqueue*m_localaudioqueue; //本地音频
- Clibaudioqueue*m_salesaudioqueue; //现场销售音频
- char m_audioqueuename[MAX_PATH];
- char m_videoqueuename[MAX_PATH];
- char m_videoqueue2name[MAX_PATH];
- char m_salesaudioqueuename[MAX_PATH];
- char m_remotevideoqueuename[MAX_PATH];
- char m_remoteaudioqueuename[MAX_PATH];
- pthread_t m_nRecordthreadId;
- char m_FileName[MAX_PATH];
- char m_PathName[MAX_PATH];
- char m_WmvFileName[MAX_PATH];
- //编码器
- CWmvHostApi* m_pHostApi;
- BOOL*m_bResult;
- //音频录制拼接BUFFER
- char*m_pAudioBufferTmp;
- //音频拼接的缓冲区大小;
- int m_iAudioBufferSize;
- //wmv recorder
- FFmpegWmvWriter*m_pWriter;
- //audio noise suppression
- IAudioNs *m_pAudioNsObj;
- SpeexResamplerState *m_16kto8kst;
- BOOL EndRecord()
- {
- if (!m_pWriter->End()){
- return FALSE;
- }
- if (m_pWriter){
- delete m_pWriter;
- m_pWriter = NULL;
- }
- if (videoframe->data){
- delete videoframe->data;
- videoframe->data = NULL;
- }
- if (m_pAudioBufferTmp!=NULL){
- delete m_pAudioBufferTmp;
- m_pAudioBufferTmp = NULL;
- m_iAudioBufferSize = 0;
- }
- if (m_localaudioqueue != NULL){
- delete m_localaudioqueue;
- m_localaudioqueue = NULL;
- }
- if (m_salesaudioqueue != NULL){
- delete m_salesaudioqueue;
- m_salesaudioqueue = NULL;
- }
- m_audioqueue = NULL;
- if (m_remoteaudioqueue != NULL){
- delete m_remoteaudioqueue;
- m_remoteaudioqueue = NULL;
- }
- if (m_videoqueue != NULL){
- delete m_videoqueue;
- m_videoqueue = NULL;
- }
- if (m_videoqueue2 != NULL){
- delete m_videoqueue2;
- m_videoqueue2 = NULL;
- }
- if (m_remotevideoqueue != NULL){
- delete m_remotevideoqueue;
- m_remotevideoqueue = NULL;
- }
- return TRUE;
- }
- void FSleep(int ms)
- {
- struct timespec ts;
- clock_gettime(CLOCK_REALTIME, &ts);
- long unsec = ts.tv_nsec + (1000 * 1000 * ms);
- ts.tv_sec += (unsec / 1000000000);
- ts.tv_nsec = (unsec % 1000000000);
- if (-1 == sem_timedwait(&m_semt, &ts)) {
- if (ETIMEDOUT == errno) {
- }
- }
- else {
- throw std::exception();
- }
- }
- static void __recordlog(void* user_data, const char* fmt, va_list arg)
- {
- libwmvrecord_impl* pRecord = (libwmvrecord_impl*)user_data;
- if (NULL != pRecord){
- CWmvHostApi* pHost = pRecord->GetHostApi();
- if (NULL != pHost){
- pHost->Debug(fmt, arg);
- }
- }
- }
- //开始录制视频
- BOOL StartRecord()
- {
- BOOL bRet = FALSE;
- m_pWriter = new FFmpegWmvWriter(this);
- if (m_bIsAudioNsOn){
- audions_callback_t t_callback = { 0 };
- t_callback.debug = &__recordlog;
- t_callback.user_data = this;
- m_pAudioNsObj = CreateIAudioNsObj(&t_callback);
- }
-
- if (0 == pthread_create(&m_nRecordthreadId, NULL, VideoRecordThread, (void*)this)) {
- bRet = TRUE;
- m_pHostApi->Debug("create video record thread and thread id is %u.", m_nRecordthreadId);
- }
- else {
- m_pHostApi->Debug("create video record thread failed.");
- }
- return bRet;
- }
- //获取指定队列的SIZE
- int GetVideoFrameSize(int&nWidth,int&nHeight,Clibvideoqueue*queue) //add by chh 20170320
- {
- int size = queue->GetFrameSize(nWidth,nHeight);
- return size;
- }
- int GetVideoFrameSizeNew(int&nWidth,int&nHeight)
- {
- int size = 0;
- if (m_videoqueue == NULL)
- {
- m_pHostApi->Debug("本地视频队列未初始化,不存在!");
- size = 0;
- }
- if((m_videoqueue2)&&(eSingleSide == m_eRecordType)) //大机的正常计算
- {
- int width1 = 0;
- int height1 = 0;
- int width2 = 0;
- int height2 = 0;
- size = m_videoqueue->GetFrameSize(width1,height1);
- //m_pHostApi->Debug("eSingleSide Record, video queue size: %d, width1: %d, height1: %d.", size, width1, height1);
- if (size)
- {
- (width1>height1)?(nWidth=nHeight=width1):(nWidth=nHeight=height1);
- }
- else
- {
- size = m_videoqueue2->GetFrameSize(width2,height2);
- (width2>height2)?(nWidth=nHeight=width2):(nWidth=nHeight=height2);
- //m_pHostApi->Debug("eSingleSide Record, videoqueue2 size: %d, width2: %d, height2: %d.", size, width2, height2);
- }
- if (!m_SubtitleParam.bSubtitle || !m_SubtitleParam.bSubtitleSection)
- {
- size = nWidth*nHeight*3;
- }
- else
- {
- nHeight += 50;
- size = nWidth*nHeight*3;
- }
- //m_pHostApi->Debug("eSingleSide Record, bSubtitle: %d, size: %d, nWidth: %d, nHeight: %d.", m_SubtitleParam.bSubtitle, size, nWidth, nHeight);
- }
- else
- {
- if (m_remotevideoqueue && eSingleSide != m_eRecordType) //如果远端队列不为空,且录制双向视频
- {
- //如果PAD版下录制双向视频,视频大小由远端视频决定,如果远端为320*240,则视频大小为640*240
- if (ePhone2Pad == m_eRecordType || ePad2Agent == m_eRecordType) //手机到pad,坐席到pad的双向录像
- {
- int width1 = 0;
- int height1 = 0;
- size = m_remotevideoqueue->GetFrameSize(width1,height1);
- nWidth = width1*2;
- nHeight = height1;
- size = nWidth*nHeight*3;
- //m_pHostApi->Debug("ePad2Agent Record, remotevideoqueue size: %d, nWidth: %d, nHeight: %d.", size, nWidth, nHeight);
- }
- else if (eStand2Agent == m_eRecordType) //大机以终端的视频大小确定画布,终端为640*360,远端固定为320*240,则画布为640*640+320*240 = 960*640
- {
- //计算画布左边部分的分辨率,640*640
- int width1 = 0;
- int height1 = 0;
- int width2 = 0;
- int height2 = 0;
- size = m_videoqueue->GetFrameSize(width1,height1);
- //m_pHostApi->Debug("eStand2Agent Record, videoqueue size: %d, width1: %d, height1: %d.", size, width1, height1);
- if (size)
- {
- (width1>height1)?(nWidth=nHeight=width1):(nWidth=nHeight=height1);
- }
- else
- {
- size = m_videoqueue2->GetFrameSize(width2,height2);
- (width2>height2)?(nWidth=nHeight=width2):(nWidth=nHeight=height2);
- //m_pHostApi->Debug("eStand2Agent Record, videoqueue2 size: %d, width2: %d, height2: %d.", size, width2, height2);
- }
- //远端视频320*240
- nWidth =nWidth+ 480;
- if (!m_SubtitleParam.bSubtitle || !m_SubtitleParam.bSubtitleSection)
- {
- size = nWidth*nHeight*3;
- }
- else
- {
- nHeight += 50;
- size = nWidth*nHeight*3;
- }
- //m_pHostApi->Debug("eStand2Agent Record, bSubtitle: %d, size: %d, nWidth: %d, nHeight: %d.", m_SubtitleParam.bSubtitle, size, nWidth, nHeight);
- }
- }
- else //录制单向视频
- {
- if (!m_SubtitleParam.bSubtitle || !m_SubtitleParam.bSubtitleSection)
- {
- size = m_videoqueue->GetFrameSize(nWidth,nHeight);
- }
- else
- {
- int width1 = 0;
- int height1 = 0;
- size = m_videoqueue->GetFrameSize(width1,height1);
- nWidth=width1;
- nHeight=height1+50;
- size = nWidth*nHeight*3;
- }
- //m_pHostApi->Debug("eSingleSide Record No Local videoqueue2, bSubtitle: %d, size: %d, nWidth: %d, nHeight: %d.",
- // m_SubtitleParam.bSubtitle, size, nWidth, nHeight);
- }
- }
- return size;
- }
- //从指定队列读取video
- BOOL GetVideoFrame(video_frame* Video,int flags,Clibvideoqueue*queue) //add by chh 20170320
- {
- return queue->GetVideo2(Video,flags);
- }
- // chh@2019/10/16 获取拼接后的视频帧
- BOOL GetVideoFrameNew(videoq_frame* Video, int flags,const bool bSwitchCam, int iInitCam)
- {
- if (m_videoqueue2 == NULL) //如果是单摄像头pad
- {
- if (m_remotevideoqueue && eSingleSide != m_eRecordType) //如果远端队列不为空,且录制双向视频
- {
- if (ePhone2Pad == m_eRecordType || ePad2Agent == m_eRecordType)
- {
- //拼接两个方向视频的BUF
- if (m_videoqueue != NULL)
- {
- //合成一个图像
- int w = 0,h = 0;
- int nsize = GetVideoFrameSize(w,h,m_videoqueue); // 获取本地视频帧大小
- memset(Video->data,0,Video->framesize); // 初始化拼接后视频帧数据
- video_frame tmp_frm = {0};
- tmp_frm.data[0] = new unsigned char[nsize];
- tmp_frm.linesize[0] = w*3;
- tmp_frm.format = videoframe->format;
- tmp_frm.width = w;
- tmp_frm.height = h;
- BOOL bGetLocalvideo = FALSE;
- bGetLocalvideo = GetVideoFrame(&tmp_frm,1,m_videoqueue); //获取本地视频的数组形式数据
- //m_pHostApi->Debug("%s mode have remote video queue, local video queue format: %d, width: %d, height: %d.",
- // record_type_table[m_eRecordType], tmp_frm.format, tmp_frm.width, tmp_frm.height);
- if (bGetLocalvideo)
- {
- //本地录像需要偏移量
- int nOffset = 0;
- if (Video->height > h)
- {
- nOffset = (Video->height-h)/2*Video->width*3;
- }
- //m_pHostApi->Debug("%s mode local video queue offset: %d, Video width: %d, Video height: %d, width: %d, height: %d.",
- // record_type_table[m_eRecordType], nOffset, Video->width, Video->height, w, h);
- for (int i= 0;i<tmp_frm.height && i<Video->height;i++)
- {
- memcpy(Video->data+nOffset+i*Video->width*3,tmp_frm.data[0]+i*tmp_frm.linesize[0],tmp_frm.linesize[0]);
- }
- }
- delete tmp_frm.data[0];
- nsize = GetVideoFrameSize(w,h,m_remotevideoqueue);
- tmp_frm.data[0] = new unsigned char[nsize];
- tmp_frm.linesize[0] = w*3;
- tmp_frm.format = videoframe->format;
- tmp_frm.width = w;
- tmp_frm.height = h;
- BOOL bGetRemotevideo = FALSE;
- bGetRemotevideo = GetVideoFrame(&tmp_frm,1,m_remotevideoqueue);
- //m_pHostApi->Debug("%s mode have remote video queue, remote video queue format: %d, width: %d, height: %d.",
- // record_type_table[m_eRecordType], tmp_frm.format, tmp_frm.width, tmp_frm.height);
- if (bGetRemotevideo)
- {
- for (int i= 0;i<tmp_frm.height&&i<Video->height;i++)
- {
- memcpy(Video->data+i*Video->width*3+tmp_frm.linesize[0],tmp_frm.data[0]+i*tmp_frm.linesize[0],tmp_frm.linesize[0]);
- }
- }
- delete tmp_frm.data[0];
- if (!bGetLocalvideo&&!bGetRemotevideo)
- {
- return FALSE;
- }
- }
- else
- {
- return FALSE;
- }
- }
- else
- {
- m_pHostApi->Debug("the device type cannot record both way video");
- }
- }
- else //录制单向视频
- {
- if (!m_SubtitleParam.bSubtitle || !m_SubtitleParam.bSubtitleSection)
- {
- bool result = m_videoqueue->GetVideo(Video,flags);
- //m_pHostApi->Debug("%s mode No bSubtitle, local video queue format: %d, width: %d, height: %d.",
- // record_type_table[m_eRecordType], Video->format, Video->width, Video->height);
- return result;
- }
- else
- {
- BOOL bRslt =FALSE;
- int width = 0;
- int height = 0;
- m_videoqueue->GetFrameSize(width,height);
- memset(Video->data,0,Video->framesize);
- videoq_frame*tmp_frm = new videoq_frame;
- //下移30
- tmp_frm->data = Video->data+ Video->width*30*3;
- //m_pHostApi->Debug("%s mode local video queue offset: %d, Video width: %d, width: %d, height: %d.",
- // record_type_table[m_eRecordType], Video->width * 30 * 3, Video->width, width, height);
- bRslt = m_videoqueue->GetVideo(tmp_frm, flags);
- //m_pHostApi->Debug("%s mode local video queue format: %d, width: %d, height: %d.",
- // record_type_table[m_eRecordType], tmp_frm->format, tmp_frm->width, tmp_frm->height);
- if (!bRslt)
- {
- delete tmp_frm;
- m_pHostApi->Debug("get env video fail!");
- return FALSE;
- }
- delete tmp_frm;
- }
- }
- }
- else //如果是双摄像头大机
- {
- if (m_remotevideoqueue && eStand2Agent == m_eRecordType) //如果远端队列不为空,且录制双向视频
- {
- //将大机摄像头和远端视频拼接到一张画布中
- memset(Video->data,0,Video->framesize);
- BOOL bRslt = FALSE;
- int width = 0;
- int height = 0;
- m_videoqueue->GetFrameSize(width,height);
- video_frame localtmp_frm = {0};
- localtmp_frm.data[0] = new unsigned char[640*640*3];
- localtmp_frm.linesize[0] = 640*3;
- localtmp_frm.format = videoframe->format;
- localtmp_frm.width = 640;
- localtmp_frm.height = 640;
- memset(localtmp_frm.data[0],0,640*640*3);
-
- int nActiveCam = iInitCam;
- if (bSwitchCam)
- {
- nActiveCam = m_pHostApi->GetActiveCamera();
- }
- if (nActiveCam == 0) // get env snapshot
- {
- videoq_frame*tmp_frm = new videoq_frame;
- //if (!m_SubtitleParam.bSubtitle || !m_SubtitleParam.bSubtitleSection)
- tmp_frm->data = localtmp_frm.data[0]+ width*(width-height)/2*3; //在画布中加上偏移量
- //m_pHostApi->Debug("%s mode local video queue offset: %d, width: %d, height: %d.",
- // record_type_table[m_eRecordType], width* (width - height) / 2 * 3, width, height);
- //横向摄像头拼接
- bRslt = m_videoqueue->GetVideo(tmp_frm, flags);
- //m_pHostApi->Debug("%s mode local video queue format: %d, width: %d, height: %d.",
- // record_type_table[m_eRecordType], tmp_frm->format, tmp_frm->width, tmp_frm->height);
- if (!bRslt)
- {
- delete tmp_frm;
- m_pHostApi->Debug("stand device both way video,get env video fail!");
- return FALSE;
- }
- delete tmp_frm;
- }
- else if (nActiveCam == 1) // get opt snapshot
- {
- videoq_frame*tmp_frm = new videoq_frame;
- tmp_frm->data = localtmp_frm.data[0] + (width-height)/2*3; //在画布中加上偏移量
- //m_pHostApi->Debug("%s mode local videoqueue2 offset: %d, width: %d, height: %d.",
- // record_type_table[m_eRecordType], (width - height) / 2 * 3, width, height);
- //竖向摄像头图像,拼接
- bRslt = m_videoqueue2->GetVideo3(tmp_frm, flags);
- //m_pHostApi->Debug("%s mode local videoqueue2 format: %d, width: %d, height: %d.",
- // record_type_table[m_eRecordType], tmp_frm->format, tmp_frm->width, tmp_frm->height);
- if (!bRslt)
- {
- delete tmp_frm;
- m_pHostApi->Debug("stand device both way video,get opt video fail!");
- return FALSE;
- }
- delete tmp_frm;
- }
- else
- {
- return FALSE;
- }
-
- {
- //本地录像需要偏移量s
- int nOffset = 0;
- if (!m_SubtitleParam.bSubtitle || !m_SubtitleParam.bSubtitleSection)
- {
- nOffset = 0;
- }
- else
- {
- //下移25
- nOffset = 25*Video->width*3;
- }
- int imaxheight = localtmp_frm.height > Video->height ? Video->height : localtmp_frm.height;
- if (0 == nActiveCam) {
- for (int i = imaxheight - 1, j = 0; i >= 0 && j < imaxheight; i--, j++)
- {
- memcpy(Video->data + nOffset + j * Video->width * 3, localtmp_frm.data[0] + i * localtmp_frm.linesize[0], localtmp_frm.linesize[0]);
- }
- }
- else {
- for (int i = 0; i < imaxheight; i++)
- {
- memcpy(Video->data + nOffset + i * Video->width * 3, localtmp_frm.data[0] + i * localtmp_frm.linesize[0], localtmp_frm.linesize[0]);
- }
- }
- }
- delete localtmp_frm.data[0];
- //拼接远端视频
- //合成一个图像
- {
- int w = 0,h = 0;
- int nsize = GetVideoFrameSize(w,h,m_remotevideoqueue);
- video_frame tmp_frm = {0};
- tmp_frm.data[0] = new unsigned char[nsize];
- tmp_frm.linesize[0] = w*3;
- tmp_frm.format = videoframe->format;
- tmp_frm.width = w;
- tmp_frm.height = h;
- BOOL bGetRemotevideo = FALSE;
- bGetRemotevideo = GetVideoFrame(&tmp_frm,1,m_remotevideoqueue);
- //m_pHostApi->Debug("%s mode remote video queue format: %d, width: %d, height: %d.",
- // record_type_table[m_eRecordType], tmp_frm.format, tmp_frm.width, tmp_frm.height);
- if (bGetRemotevideo)
- {
- int imaxheight = tmp_frm.height > Video->height ? Video->height : tmp_frm.height;
- for (int i = imaxheight - 1, j = 0; i >= 0 && j < imaxheight; i--, j++)
- {
- memcpy(Video->data+((Video->height-tmp_frm.height)/2+j)*Video->width*3+640*3,tmp_frm.data[0]+i*tmp_frm.linesize[0],tmp_frm.linesize[0]);
- }
- }
- delete tmp_frm.data[0];
- if (!bGetRemotevideo)
- {
- return FALSE;
- }
- }
- }
- else
- {
- BOOL bRslt =FALSE;
- int width = 0;
- int height = 0;
- m_videoqueue->GetFrameSize(width,height);
- memset(Video->data,0,Video->framesize);
- int nActiveCam = iInitCam;
- if (bSwitchCam)
- {
- nActiveCam = m_pHostApi->GetActiveCamera();
- }
- if (nActiveCam == 0) // get env snapshot
- {
- videoq_frame*tmp_frm = new videoq_frame;
- if (!m_SubtitleParam.bSubtitle || !m_SubtitleParam.bSubtitleSection)
- {
- tmp_frm->data = Video->data+ Video->width*(width-height)/2*3;
- //m_pHostApi->Debug("%s mode no remote video queue, no bSubtitle offset: %d, width: %d, height: %d.",
- // record_type_table[m_eRecordType], Video->width* (width - height) / 2 * 3, width, height);
- }
- else
- {
- tmp_frm->data = Video->data+ Video->width*((width-height)/2+30)*3;
- //m_pHostApi->Debug("%s mode no remote video queue, have bSubtitle offset: %d, width: %d, height: %d.",
- // record_type_table[m_eRecordType], Video->width* ((width - height) / 2 + 30) * 3, width, height);
- }
- bRslt = m_videoqueue->GetVideo(tmp_frm, flags);
- //m_pHostApi->Debug("%s mode no remote video queue, local video queue format: %d, width: %d, height: %d.",
- // record_type_table[m_eRecordType], tmp_frm->format, tmp_frm->width, tmp_frm->height);
- if (!bRslt)
- {
- delete tmp_frm;
- m_pHostApi->Debug("get env video fail!");
- return FALSE;
- }
- delete tmp_frm;
- }
- else if (nActiveCam == 1) // get opt snapshot
- {
- videoq_frame*tmp_frm = new videoq_frame;
- if (!m_SubtitleParam.bSubtitle || !m_SubtitleParam.bSubtitleSection)
- {
- tmp_frm->data = Video->data + (width-height)/2*3;
- //m_pHostApi->Debug("%s mode no remote video queue, no bSubtitle offset: %d, width: %d, height: %d.",
- // record_type_table[m_eRecordType], (width - height) / 2 * 3, width, height);
- }
- else
- {
- tmp_frm->data = Video->data + (Video->width*30+(width-height)/2)*3;
- //m_pHostApi->Debug("%s mode no remote video queue, have bSubtitle offset: %d, width: %d, height: %d.",
- // record_type_table[m_eRecordType], (Video->width * 30 + (width - height) / 2) * 3, width, height);
- }
- //横向摄像头图像,拼接
- bRslt = m_videoqueue2->GetVideo3(tmp_frm, flags);
- //m_pHostApi->Debug("%s mode no remote video queue, local videoqueue2 format: %d, width: %d, height: %d.",
- // record_type_table[m_eRecordType], tmp_frm->format, tmp_frm->width, tmp_frm->height);
- if (!bRslt)
- {
- delete tmp_frm;
- m_pHostApi->Debug("get opt video fail!");
- return FALSE;
- }
- delete tmp_frm;
- }
- else
- {
- return FALSE;
- }
- }
- }
- return TRUE;
- }
- BOOL ReNameFile(const char*file,const char*newfilename)
- {
- #ifdef RVC_OS_WIN
- if (!_access(file, 0))
- #else
- if (!access(file, F_OK))
- #endif //RVC_OS_WIN
- {
- if (!rename(file,newfilename))
- {
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
- else
- {
- return FALSE;
- }
- }
- //混音
- void MixtureAudio(int buflens,char*localbuf,char*remotebuf)
- {
- for (int i =0;i<buflens-2;i+=2)
- {
- *(__int16*)(localbuf+i) = *(__int16*)(localbuf+i)
- +*(__int16*)(remotebuf+i)-((*(__int16*)(localbuf+i))*(*(__int16*)(remotebuf+i))>>0x10);
- if (*(__int16*)(localbuf+i) > 32767)
- {
- *(__int16*)(localbuf+i) = 32767;
- }
- else if (*(__int16*)(localbuf+i) < -32768)
- {
- *(__int16*)(localbuf+i) = -32768;
- }
- }
- return;
- }
-
-
- uint32_t BindPCMAudioData(uint32_t uBufferLen, char* pLocalAudios, uint32_t uLocalAudioLen, char* pRemoteAudios, uint32_t uRemoteAudioLen, uint32_t uBitDeepth, eStereoArrayType eType)
- {
- uint32_t uRet = 0;
-
- char* pBuffer = new char[uLocalAudioLen];
- memset(pBuffer, 0, uLocalAudioLen);
- if (NULL != pLocalAudios){
- memcpy(pBuffer, pLocalAudios, uLocalAudioLen);
- memset(pLocalAudios, 0, uBufferLen);
- }
- for (int i = 0; i < uLocalAudioLen/uBitDeepth; i++){
- if (eLocalLeft == eType){
- memcpy((uint32_t*)pLocalAudios + i, ((uint16_t*)(pBuffer))+i, uBitDeepth);
- uint16_t *pindex = (uint16_t*)((uint32_t*)pLocalAudios + i) + 1;
- memcpy(pindex, ((uint16_t*)(pRemoteAudios))+i, uBitDeepth);
- uRet += (2*uBitDeepth);
- }
- else{
- memcpy((uint32_t*)pLocalAudios + i, ((uint16_t*)(pRemoteAudios))+i, uBitDeepth);
- uint16_t *pindex = (uint16_t*)((uint32_t*)pLocalAudios + i) + 1;
- memcpy(pindex, ((uint16_t*)(pBuffer))+i, uBitDeepth);
- uRet += (2*uBitDeepth);
- }
- }
-
- if (NULL != pBuffer){
- delete []pBuffer;
- pBuffer = NULL;
- }
- return uRet;
- }
- size_t Get8KOutPutBitRate(int iChannels, eAudioOutPutType eType)
- {
- size_t uOutBitRate = 16*8;
- if (1 == iChannels){
- if (eUltraHD == eType){
- uOutBitRate = 1000*8;
- }
- else if (eHighDefinition == eType){
- uOutBitRate = 625*8;
- }
- else if (eStandardDefinition == eType){
- uOutBitRate = 16*8;
- }
- else{
- uOutBitRate = 128*1000;
- }
- }
- else{
- if (eStandardDefinition == eType){
- uOutBitRate = 16*8;
- }
- else if (eLowDefinition == eType){
- uOutBitRate = iChannels*128*1000;
- }
- else{
- uOutBitRate = 1500*8;
- }
- }
- return uOutBitRate;
- }
- size_t Get16KOutPutBitRate(int iChannels, eAudioOutPutType eType)
- {
- size_t uOutBitRate = 1000*8;
- if (1 == iChannels){
- if (eUltraHD == eType){
- uOutBitRate = 2000*8;
- }
- else if (eHighDefinition == eType){
- uOutBitRate = 1500*8;
- }
- else if(eStandardDefinition == eType){
- uOutBitRate = 1000*8;
- }
- else{
- uOutBitRate = 1000*256;
- }
- }
- else{
- if (eStandardDefinition == eType){
- uOutBitRate = 2000*8;
- }
- else{
- uOutBitRate = 2500*8;
- }
- }
- return uOutBitRate;
- }
- size_t Get32KOutPutBitRate(int iChannels, eAudioOutPutType eType)
- {
- size_t uOutBitRate = 1000*8;
- if (1 == iChannels){
- if (eUltraHD == eType){
- uOutBitRate = 2500*8;
- }
- else{
- uOutBitRate = 1000*8;
- }
- }
- else{
- if (eStandardDefinition == eType){
- uOutBitRate = 3000*8;
- }
- else{
- uOutBitRate = 6000*8;
- }
- }
- return uOutBitRate;
- }
- size_t Get44KOutPutBitRate(int iChannels, eAudioOutPutType eType)
- {
- size_t uOutBitRate = 2000*8;
- if (1 == iChannels){
- if (eUltraHD == eType){
- uOutBitRate = 6000*8;
- }
- else if(eHighDefinition == eType){
- uOutBitRate = 4000*8;
- }
- else{
- uOutBitRate = 2000*8;
- }
- }
- else{
- if (eStandardDefinition == eType){
- uOutBitRate = 10000*8;
- }
- else if (eHighDefinition == eType){
- uOutBitRate = 20000*8;
- }
- else{
- uOutBitRate = 40000*8;
- }
- }
- return uOutBitRate;
- }
- size_t GetAudioOutPutBitRate(int iInPutSamperate, int iChannels, eAudioOutPutType eType)
- {
- size_t uOutBitRate = 8000;
- switch (iInPutSamperate)
- {
- case 8000:
- uOutBitRate = Get8KOutPutBitRate(iChannels, eType);
- break;
- case 16000:
- uOutBitRate = Get16KOutPutBitRate(iChannels, eType);
- break;
- case 32000:
- uOutBitRate = Get32KOutPutBitRate(iChannels, eType);
- break;
- case 44100:
- uOutBitRate = Get44KOutPutBitRate(iChannels, eType);
- break;
- default:
- m_pHostApi->Debug("not support format, input sample rate is %d, channels number is %d.", iInPutSamperate, iChannels);
- break;
- }
- return uOutBitRate;
- }
- int VideoRecord()
- {
- BOOL bResult;
- BOOL bInitWmvParam = FALSE;
- m_bStopRecord = FALSE;
- DWORD nRecordTime = 0; //本段视频持续时间
- DWORD nTimeExpended = 0; //录制经过的时间
- DWORD nVideoNum = 0; //当前时间的视频数
- DWORD noldvideoNum = 0; //已经录制的视频数
- DWORD nAudioNum = 0; //当前时间的音频数
- DWORD noldAudioNum = 0; //已经录制的音频数
- DWORD nAudioFromQueNum = 0; //从音频队列中取出的音频数量
- DWORD nRecordStartTime = 0; //本段录像开始时间
- int WmvFileSerialNum = 0; //WMV文件的序列号
- int nAudioBufferLens = 0; //音频录制拼接BUFFER的当前长度
- int nRemoteAudioBufferLens = 0; //远端音频拼接BUFFER的当前长度
- int nAudioFrameSize = 0; //音频帧的大小
- int nVideoFrameSize = 0; //视频帧的大小
- int nWidth = 0;
- int nHeight = 0;
- int nPerSecBufferLens = 0; //每秒音频的长度
- int nAudioFps = 0; //音频频率
- int nRemoteAudioFps = 0; //远端音频频率
- int nRemoteAudioNum = 0; //远端音频数量
- char*pRemoteAudioBufTmp = NULL; //远端音频BUFFER
- // 添加字幕所需变量定义 add by ly
- //char subTitles[3][256];
- wchar_t timeSubtitle[MAX_PATH];
- const char *weekDict[] = {"日","一","二","三","四","五","六"};
- SYSTEMTIME st;
-
- DWORD nSubTitleTime = 0; //本段视频添加字幕耗时
- #ifdef RVC_OS_WIN
- string TtcDir = "C:\\Windows\\Fonts\\";
- #else
- //string TtcDir = "/usr/share/fonts/";
- string TtcDir = "./";
- #endif
- string fontfile = "";
- CvxText *pText = NULL;
- bool bSwitchCam = true;
- int nInitActiveCam = 0;
- m_pHostApi->Debug("FindMatchedFile");
- #ifdef RVC_OS_WIN
- if (FindMatchedFile(TtcDir.c_str(),"msyh.",fontfile))
- #else
- if (FindMatchedFile(TtcDir.c_str(), "wryh.", fontfile))
- #endif
- {
- m_pHostApi->Debug("Fontfile:%s", fontfile.c_str());
- TtcDir += fontfile;
- pText = new CvxText(m_pHostApi, TtcDir.c_str());
- int type = 0;
- CvScalar scalar = {16, 0.3, 0.2, 0}; //字体大小/空白比例/间隔比例/旋转角度
- bool underline = false;
- float diaphaneity = 1.0;
- pText->setFont(&type, &scalar, &underline, &diaphaneity); //字体类型/size/下滑线/透明度
- }
- m_pHostApi->Debug("setFont success");
- if (!pText)
- {
- if (m_SubtitleParam.bSubtitle)
- {
- m_pHostApi->Debug("Font is null, subtitles cannot be added!");
- m_bStopRecord = TRUE;
- m_pHostApi->OnRecordFailed();
- }
- }
- int nVideoSizeFailedTimes = 0, nAudioSizeFailedTimes = 0; // add by ly 20160713
- if (m_bWholeSection || !m_bSessionManage){
- sprintf_s(m_WmvFileName,MAX_PATH,"%s%s_%d.wmv",m_PathName,m_FileName,WmvFileSerialNum);
- }
- else{
- sprintf_s(m_WmvFileName,MAX_PATH,"%s%s_%d_end.wmv",m_PathName,m_FileName,WmvFileSerialNum);
- }
- m_pHostApi->Debug("m_WmvFileName = %s",m_WmvFileName);
- m_bCloseVideo = FALSE;
- m_bReNameVideo = FALSE;
- while (!m_bStopRecord)
- {
- try
- {
- if (!bInitWmvParam)
- {
- //获取视频帧大小
- if (nVideoFrameSize <= 0)
- {
- if (nVideoSizeFailedTimes >= 5000)
- {
- m_bStopRecord = TRUE;
- m_pHostApi->OnRecordFailed(true);
- continue;
- }
- m_pHostApi->Debug("Get Video FrameSize,record type is %s.",record_type_table[m_eRecordType]);
- nVideoFrameSize = GetVideoFrameSizeNew(nWidth,nHeight);
- if(nVideoFrameSize <= 0)
- {
- ++nVideoSizeFailedTimes;
- m_pHostApi->Debug("nVideoFrameSize = 0");
- FSleep(5);
- continue;
- }
- }
- //获取音频帧大小
- if (nAudioFrameSize <= 0)
- {
- if (nAudioSizeFailedTimes >= 5000)
- {
- m_bStopRecord = TRUE;
- m_pHostApi->OnRecordFailed(true);
- continue;
- }
- nAudioFrameSize = m_audioqueue->GetFrameSize();
- if (nAudioFrameSize <=0)
- {
- ++nAudioSizeFailedTimes;
- //m_pHostApi->Debug("nAudioFrameSize = 0");
- //nAudioFrameSize = m_remoteaudioqueue->GetFrameSize();
- //m_pHostApi->Debug("use m_remoteaudioqueue audio frame size init audio frame size.");
- FSleep(10);
- continue;
- }
- //m_pHostApi->Debug("Local Audio Frame Size = %d", nAudioFrameSize);
- }
- //初始化视频buffer
- if (videoframe->data == NULL)
- {
- videoframe->data = new unsigned char[nVideoFrameSize];
- videoframe->framesize = nVideoFrameSize;
- videoframe->height = nHeight;
- videoframe->width = nWidth;
- videoframe->format = VIDEO_FORMAT_RGB24;
- }
- //初始化WMV文件
- audio_frame*audio = new audio_frame;
- audio->data = new char[nAudioFrameSize];
- if (!m_audioqueue->GetAudio(audio))
- {
- delete audio->data;
- delete audio;
- FSleep(5);
- continue;
- }
- m_pHostApi->Debug("audio samplespersec=%d,framesize=%d,channels=%d,format=%d,bitspersample=%d,series number=%d.",audio->samplespersec,audio->framesize,audio->nchannels,audio->format, audio->bitspersample, audio->iseriesnumber);
- //计算每秒音频的长度
- nPerSecBufferLens = audio->samplespersec*audio->bitspersample/8;
- m_iAudioBufferSize = nPerSecBufferLens;
- m_pHostApi->Debug("nPerSecBufferLens=%d", nPerSecBufferLens);
- //计算音频帧的帧频,
- nAudioFps = nPerSecBufferLens/audio->framesize;
- m_pHostApi->Debug("音频帧的帧频 nAudioFps=%d", nAudioFps);
- if((m_audioqueue != NULL)&&((m_remoteaudioqueue != NULL)&& eSingleSide != m_eRecordType))
- {
- //计算远端音频包的封包频率,本地50个包,远端为手机端使用1秒100个音频包分包,远端为可视柜台坐席使用1秒50个包分包 ,新增渠道时需要检查此处
- if(ePhone2Pad == m_eRecordType) //手机渠道的双向录像
- {
- nRemoteAudioFps = 100;
- }
- else if(ePad2Agent == m_eRecordType || eStand2Agent == m_eRecordType) //可视柜台渠道的双向录像,本地端和坐席端声音采用左右声道方式录制
- {
- nRemoteAudioFps = 50;
- audio->nchannels = 2;
- if (CAPTURE_CLOCK*2 == audio->samplespersec){
- audio->samplespersec = CAPTURE_CLOCK;
- nPerSecBufferLens = CAPTURE_CLOCK*audio->bitspersample/8;
- }
- m_iAudioBufferSize = nPerSecBufferLens*2;
- }
- else
- {
- nRemoteAudioFps = 0;
- }
- m_pHostApi->Debug("calc remote audio fps = %d",nRemoteAudioFps);
- }
- m_pHostApi->Debug("audio fps = %d, audio buffer size = %d.",nAudioFps, m_iAudioBufferSize);
- //初始化音频buffer
- if (m_pAudioBufferTmp == NULL){
- m_pAudioBufferTmp = new char[m_iAudioBufferSize];
- memset(m_pAudioBufferTmp, 0, m_iAudioBufferSize);
- }
- if (pRemoteAudioBufTmp == NULL){
- pRemoteAudioBufTmp = new char[nPerSecBufferLens];
- memset(pRemoteAudioBufTmp,0,nPerSecBufferLens);
- }
- size_t uOutPutBitRate = GetAudioOutPutBitRate(audio->samplespersec,audio->nchannels,m_eAudioType);
- if (m_bIsAudioNsOn && NULL != m_pAudioNsObj){
- if (m_pAudioNsObj->SetNsParams(audio->samplespersec, CAPTURE_FRAME_TIME, m_iNsPolicy)){
- m_pHostApi->Debug("Ns audio set params failed!");
- }
- }
- m_pHostApi->Debug("InitWmvWriter audio param samplespersec=%d,audioOutChannels=%d,audioOutBitRate=%d bps.",audio->samplespersec,audio->nchannels,uOutPutBitRate);
- m_pHostApi->Debug("InitWmvWriter video param Width=%d,Height=%d,Fps=%d.", nWidth, nHeight, m_nFps);
- bResult = m_pWriter->InitWmvWriter(m_WmvFileName,nWidth,nHeight,24,m_nFps,audio->samplespersec,
- audio->nchannels,audio->bitspersample,6,m_videoquality,uOutPutBitRate, (int)m_eAudioType);
- delete audio->data;
- delete audio;
- if(!bResult)
- {
- m_pHostApi->Debug("WMV初始化失败!");
- *m_bResult = FALSE;
- // add by ly at 20160729
- m_bStopRecord = TRUE;
- m_pHostApi->OnRecordFailed();
- break;
- }
-
- //开始录像
- bResult = m_pWriter->StartWrite();
- if(!bResult)
- {
- m_pHostApi->Debug("开始录像失败!");
- *m_bResult = FALSE;
- // add by ly at 20160729
- m_bStopRecord = TRUE;
- m_pHostApi->OnRecordFailed();
- break;
- }
- bInitWmvParam = TRUE;
- //当第一次记录时删除当前音频只剩下1帧,使音视频数据能够同步
- m_audioqueue->ClearAudioQueue();
- nRecordStartTime = timeGetTime();//本段录像开始时间
- continue;
- }
- //控制音视频的同步,精确控制录制频率,确保暂停足够时间
- int sleeptime = 0;
- nRecordTime = timeGetTime() - nRecordStartTime - nSubTitleTime;
- //m_pHostApi->Debug("nRecordStartTime=%ld, nAudioFromQueNum=%ld, nRecordTime=%ld .", nRecordStartTime, nAudioFromQueNum, nRecordTime);
- if(m_nFps<nAudioFps)
- {
- if((m_audioqueue != NULL)&&((m_remoteaudioqueue != NULL)&& eSingleSide != m_eRecordType))//如果双向混音,则sleeptime以远端为准
- {
- if ((nRemoteAudioFps!=0)&&(nRemoteAudioFps>nAudioFps))
- {
- sleeptime = (int)((nRemoteAudioNum+1)*1000/nRemoteAudioFps-nRecordTime);
- }
- else
- {
- sleeptime = (int)((nAudioFromQueNum+1)*1000/nAudioFps-nRecordTime);
- }
- }
- else
- {
- sleeptime = (int)((nAudioFromQueNum+1)*1000/nAudioFps-nRecordTime);
- }
- }
- else
- {
- sleeptime = (int)((noldvideoNum+1)*1000/m_nFps-nRecordTime);
- }
- if(sleeptime>=0)
- {
- FSleep(sleeptime);
- }
- //计算经过的时间,计算已经经过几帧时间y
- nRecordTime = timeGetTime() - nRecordStartTime - nSubTitleTime;
- nVideoNum = (DWORD)(((double) nRecordTime /1000.0 ) * (double)m_nFps);
- //如果是第1帧,或者又经过了1帧的时间,则录制视频,对视频进行压缩
- if (((nVideoNum==0)&&(noldvideoNum == 0))||(nVideoNum>noldvideoNum))
- {
- BOOL bVideoLens = (m_videoqueue2 == NULL)?(m_videoqueue->GetVideoLens()>0):((m_videoqueue->GetVideoLens()>0)||(m_videoqueue2->GetVideoLens()>0));
- if (bVideoLens)
- {
- BOOL bGetvideo = FALSE;
- //bGetvideo = GetVideoFrame(videoframe,1,bSalesRecord);
- //获取拼接后的视频帧数据
- if (eStand2Agent == m_eRecordType && bSwitchCam){ //大机双向录像,只做一次摄像头选择 add by clp 20191025
- bSwitchCam = false;
- nInitActiveCam = m_pHostApi->GetActiveCamera();
- m_pHostApi->Debug("Stand2S Device Double Record local use %d CAMERA_TYPE_ENV[0] CAMERA_TYPE_OPT[1] Camera.", nInitActiveCam);
- }
- bGetvideo = GetVideoFrameNew(videoframe,1, bSwitchCam, nInitActiveCam);
- if (!bGetvideo)
- {
- FSleep(2);
- continue;
- }
- // 加入字幕
- if (m_SubtitleParam.bSubtitle)
- {
- DWORD nStartTime = timeGetTime();
- IplImage *pImg = cvCreateImage(cvSize(nWidth, nHeight), IPL_DEPTH_8U, 3);
- memcpy(pImg->imageData, videoframe->data, nVideoFrameSize);
- //cvFlip(pImg); // 先把图像翻转
- if (strlen(m_SubtitleParam.topSubtitleData) <= 0)
- {
- if (m_SubtitleParam.bSubtitleSection)
- {
- GetLocalTimeRVC(st);
- swprintf(timeSubtitle, MAX_PATH, L"%4d年%02d月%02d日 星期%s %02d:%02d:%02d",st.wYear,
- st.wMonth,st.wDay,weekDict[st.wDayOfWeek],st.wHour,st.wMinute, st.wSecond);
- }
- else
- {
- GetLocalTimeRVC(st);
- swprintf(timeSubtitle, MAX_PATH, L"%4d/%02d/%02d %02d:%02d:%02d",st.wYear,
- st.wMonth,st.wDay,st.wHour,st.wMinute, st.wSecond);
- }
- pText->putText(pImg, timeSubtitle, cvPoint(10, 42));
- }
- else
- {
- pText->putText(pImg, m_SubtitleParam.topSubtitleData, cvPoint(10, 42));
- }
-
- if (wcslen(m_SubtitleParam.bottomSubtitleData1) > 0)
- {
- pText->putText(pImg, m_SubtitleParam.bottomSubtitleData1, cvPoint(10, pImg->height - 47));
- }
- if (wcslen(m_SubtitleParam.bottomSubtitleData2) > 0)
- {
- pText->putText(pImg, m_SubtitleParam.bottomSubtitleData2, cvPoint(10, pImg->height - 23));
- }
- //拼接后的右侧视频添加坐席信息
- if (wcslen(m_SubtitleParam.strRightAgentInfo) > 0)
- {
- if (eStand2Agent == m_eRecordType){
- pText->putText(pImg, m_SubtitleParam.strRightAgentInfo, cvPoint(10+640, pImg->height - 47));
- }
- else{
- pText->putText(pImg, m_SubtitleParam.strRightAgentInfo, cvPoint(10+320, pImg->height - 47));
- }
- }
- //cvFlip(pImg); // 添完字幕再翻转
- memcpy(videoframe->data, pImg->imageData, nVideoFrameSize);
- cvReleaseImage(&pImg);
- nSubTitleTime += (timeGetTime()- nStartTime);
- }
- bResult = m_pWriter->ReceiveVideoData((BYTE*)videoframe->data,nVideoFrameSize);
- if (!bResult)
- {
- m_pHostApi->Debug("视频流写入失败!");
- *m_bResult = FALSE;
- // add by ly at 20160729
- m_bStopRecord = TRUE;
- m_pHostApi->OnRecordFailed();
- break;
- }
- noldvideoNum++;
- }
- }
- //本地音频不为空,远端为空或者远端不为空,但不是双向则只录制本地音频
- if((m_audioqueue != NULL)&&((m_remoteaudioqueue == NULL)||((m_remoteaudioqueue != NULL) && eSingleSide == m_eRecordType)))
- {
- //取音频数据,合并成1S的音频
- int nAudioLens = m_audioqueue->GetAudioLens();
- //m_pHostApi->Debug("AudioLens: %d!", nAudioLens);
- if (nAudioLens>0)
- {
- if (nAudioBufferLens < nPerSecBufferLens)
- {
- //取出音频
- audioframe->data = m_pAudioBufferTmp+nAudioBufferLens;
- BOOL bGetAudio = FALSE;
- if (nAudioLens < MAX_AUDIOQUEUE_LENS){
- bGetAudio = m_audioqueue->GetAudioAndDel(audioframe);
- }
- else{
- bGetAudio = m_audioqueue->GetAudio(audioframe);
- }
- //m_pHostApi->Debug("bGetAudio: %d!", bGetAudio);
- if (bGetAudio){
- if (m_bIsAudioNsOn && NULL != m_pAudioNsObj){
- char pOutAudio[MAX_PATH*3] = {0};
- int iRet = m_pAudioNsObj->NsProcess(pOutAudio,audioframe->framesize, audioframe->data, audioframe->framesize);
- if (0 == iRet){
- memset(m_pAudioBufferTmp+nAudioBufferLens, 0, audioframe->framesize);
- memcpy(m_pAudioBufferTmp+nAudioBufferLens, pOutAudio, audioframe->framesize);
- }
- }
- nAudioBufferLens+=audioframe->framesize;
- //m_pHostApi->Debug("audio get samplespersec=%d,framesize=%d,channels=%d,format=%d,bitspersample=%d,series number=%d.",
- // audioframe->samplespersec, audioframe->framesize, audioframe->nchannels, audioframe->format, audioframe->bitspersample, audioframe->iseriesnumber);
- nAudioFromQueNum++;
- if ((0 == nAudioFromQueNum%100) && 0 != audioframe->iseriesnumber){
- //m_pHostApi->Debug("single side record current audio queue len is %d and audio series number is: %d.",nAudioLens, audioframe->iseriesnumber);
- }
- }
- else{
- //m_pHostApi->Debug("get audio from m_audioqueue failed.");
- FSleep(2); //huchen add ,避免不停获取音频数据导致cpu占用高
- }
- }
- }
- else {
- //m_pHostApi->Debug("get audio length from m_audioqueue failed.");
- FSleep(2); //huchen add ,避免不停获取音频数据导致cpu占用高
- }
- }
- else if((m_audioqueue != NULL)&&((m_remoteaudioqueue != NULL)&& eSingleSide != m_eRecordType)) //客户端坐席端混音
- {
- //取音频数据,合并成1S的音频
- int nAudioLens = m_audioqueue->GetAudioLens();
- int nRemoteAudioLens = m_remoteaudioqueue->GetAudioLens();
- if((nAudioLens>0)&&(nRemoteAudioLens<=0)) //远端无数据不混音
- {
- if (nAudioBufferLens < nPerSecBufferLens)
- { //取出音频
- audioframe->data = m_pAudioBufferTmp+nAudioBufferLens;
- BOOL bGetAudio = FALSE;
- if (nAudioLens < MAX_AUDIOQUEUE_LENS){
- bGetAudio = m_audioqueue->GetAudioAndDel(audioframe);
- }
- else{
- bGetAudio = m_audioqueue->GetAudio(audioframe);
- }
- if (bGetAudio){
- //if (m_bIsAudioNsOn && NULL != m_pAudioNsObj){
- // char pOutAudio[MAX_PATH*3] = {0};
- // int iRet = m_pAudioNsObj->NsProcess(pOutAudio,audioframe->framesize, audioframe->data, audioframe->framesize);
- // if (0 == iRet){
- // memset(m_pAudioBufferTmp+nAudioBufferLens, 0, audioframe->framesize);
- // memcpy(m_pAudioBufferTmp+nAudioBufferLens, pOutAudio, audioframe->framesize);
- // }
- //}
- nAudioBufferLens += audioframe->framesize;
- nAudioFromQueNum++;
- if ((0 == nAudioFromQueNum%100) && (0 != audioframe->iseriesnumber)){
- //m_pHostApi->Debug("not single side record,but no remote audio data and current local audio queue len is %d, local audio series number is: %d.", nAudioLens, audioframe->iseriesnumber);
- }
- }
- else{
- //m_pHostApi->Debug("get audio from m_audioqueue failed.");
- FSleep(2);
- }
- }
- }
- else if ((nAudioLens>0)&&(nRemoteAudioLens>0))//混音
- {
- if(nRemoteAudioBufferLens < nPerSecBufferLens) //收到的远端音频不够1秒
- {
- //取远端音频
- BOOL bGetRemoteAudio = FALSE;
- audio_frame*RemoteAudio = new audio_frame;
- RemoteAudio->data = pRemoteAudioBufTmp+nRemoteAudioBufferLens;
- if (nRemoteAudioLens < MAX_AUDIOQUEUE_LENS){
- bGetRemoteAudio = m_remoteaudioqueue->GetAudioAndDel(RemoteAudio);
- }
- else{
- bGetRemoteAudio = m_remoteaudioqueue->GetAudio(RemoteAudio);
- }
- if (bGetRemoteAudio){
- //if (m_bIsAudioNsOn && NULL != m_pAudioNsObj){
- // char pOutAudio[MAX_PATH*3] = {0};
- // int iRet = m_pAudioNsObj->NsProcess(pOutAudio,RemoteAudio->framesize, RemoteAudio->data, RemoteAudio->framesize);
- // if (0 == iRet){
- // memset(pRemoteAudioBufTmp + nRemoteAudioBufferLens, 0, RemoteAudio->framesize);
- // memcpy(pRemoteAudioBufTmp + nRemoteAudioBufferLens, pOutAudio, RemoteAudio->framesize);
- // }
- //}
- nRemoteAudioBufferLens += RemoteAudio->framesize;
- nRemoteAudioNum++;
- }
- else {
- //m_pHostApi->Debug("get audio from m_remoteaudioqueue failed.");
- FSleep(2);
- }
- delete RemoteAudio;
- }
- int ncurtime = timeGetTime() - nRecordStartTime - nSubTitleTime;
- if(( nAudioBufferLens< nPerSecBufferLens)&&((ncurtime-nAudioFromQueNum*1000/nAudioFps)>=(1000/nAudioFps)))
- {
- //本地音频
- audioframe->data = m_pAudioBufferTmp+nAudioBufferLens;
- BOOL bGetAudio = FALSE;
- if (nAudioLens < MAX_AUDIOQUEUE_LENS){
- bGetAudio = m_audioqueue->GetAudioAndDel(audioframe);
- }
- else{
- bGetAudio = m_audioqueue->GetAudio(audioframe);
- }
- if (bGetAudio){
- if (CAPTURE_CLOCK == audioframe->samplespersec){
- //if (m_bIsAudioNsOn && NULL != m_pAudioNsObj){
- // char pOutAudio[MAX_PATH*3] = {0};
- // int iRet = m_pAudioNsObj->NsProcess(pOutAudio,audioframe->framesize, audioframe->data, audioframe->framesize);
- // if (0 == iRet){
- // memset(m_pAudioBufferTmp + nAudioBufferLens, 0, audioframe->framesize);
- // memcpy(m_pAudioBufferTmp + nAudioBufferLens, pOutAudio, audioframe->framesize);
- // }
- //}
- nAudioBufferLens += audioframe->framesize;
- }
- else if(CAPTURE_CLOCK*2 == audioframe->samplespersec){
- spx_uint32_t uInLen = audioframe->framesize;
- spx_uint32_t uOutLen = 512;
- spx_int16_t pOutAudio[512] = {0};
- int iRet = speex_resampler_process_int(m_16kto8kst, 0, (spx_int16_t*)audioframe->data, &uInLen, pOutAudio, &uOutLen);
- if (RESAMPLER_ERR_SUCCESS == iRet){
- memset(m_pAudioBufferTmp+nAudioBufferLens, 0, audioframe->framesize);
- memcpy(m_pAudioBufferTmp+nAudioBufferLens, pOutAudio, uOutLen);
- nAudioBufferLens += uOutLen;
- }
- //m_pHostApi->Debug("16kto8k inLen is %d, outLen is %d.",audioframe->framesize, uOutLen);
- }
- else{
- m_pHostApi->Debug("not support audio sample rate.");
- m_pHostApi->OnRecordFailed();
- }
- nAudioFromQueNum++;
- if ((0 == nAudioFromQueNum%100) && (0 != audioframe->iseriesnumber)){
- //m_pHostApi->Debug("not single side record,and current local audio queue len is %d audio series number is: %d.",nAudioLens, audioframe->iseriesnumber);
- }
- }
- else{
- //m_pHostApi->Debug("get audio from m_audioqueue failed.");
- FSleep(2);
- }
- }
- //混音
- if ((nRemoteAudioBufferLens==nPerSecBufferLens)&&(nAudioBufferLens == nPerSecBufferLens))
- {
- if(ePhone2Pad == m_eRecordType){
- MixtureAudio(nAudioBufferLens,m_pAudioBufferTmp,pRemoteAudioBufTmp);
- }
- else{//可视远程双录场景,本地和坐席端分别录制到左右声道
- uint32_t uOutlen = BindPCMAudioData(m_iAudioBufferSize, m_pAudioBufferTmp, nAudioBufferLens, pRemoteAudioBufTmp, nRemoteAudioBufferLens, audioframe->bitspersample/8, eLocalLeft);
- //m_pHostApi->Debug("double channel buffer length is %d, local buffer len is %d, remote buffer len is %d.",m_iAudioBufferSize, nAudioBufferLens, nRemoteAudioBufferLens);
- nAudioBufferLens = uOutlen;
- }
- }
- }
- else if((nAudioLens<=0)&&(nRemoteAudioLens>0)) //本地端无数据不混音,双录语音播报场景 add by clp 20191104
- {
- if (nAudioBufferLens < nPerSecBufferLens)
- { //取出音频
- audioframe->data = m_pAudioBufferTmp+nAudioBufferLens;
- BOOL bGetAudio = FALSE;
- if (nAudioLens < MAX_AUDIOQUEUE_LENS)
- {
- bGetAudio = m_remoteaudioqueue->GetAudioAndDel(audioframe);
- }
- else
- {
- bGetAudio = m_remoteaudioqueue->GetAudio(audioframe);
- }
- if (bGetAudio)
- {
- nAudioBufferLens+=audioframe->framesize;
- nAudioFromQueNum++;
- }
- else {
- //m_pHostApi->Debug("get audio from m_remoteaudioqueue failed.");
- FSleep(2);
- }
- }
- }
- else {
- //m_pHostApi->Debug("get audio length from m_remoteaudioqueue and m_audioqueue failed.");
- FSleep(2);
- }
- }
- //录制音频
- //计算经过的时间,计算已经经过几帧时间
- nRecordTime = timeGetTime() - nRecordStartTime - nSubTitleTime;
- nAudioNum = (DWORD)((double) nRecordTime /1000.0 );
- if ((nAudioNum > noldAudioNum)/*&&(noldvideoNum >= (m_nFps*(noldAudioNum+1)))*/&&(nAudioBufferLens >= nPerSecBufferLens))
- {
- if (m_pWriter->ReceiveAudioData((BYTE*)m_pAudioBufferTmp,nAudioBufferLens))
- {
- noldAudioNum++;
- nAudioBufferLens = 0;
- nRemoteAudioBufferLens = 0;
- memset(m_pAudioBufferTmp,0,m_iAudioBufferSize);
- memset(pRemoteAudioBufTmp,0,nPerSecBufferLens);
- }
- else
- {
- m_pHostApi->Debug("音频流写入失败!");
- *m_bResult = FALSE;
- // add by ly at 20160729
- m_bStopRecord = TRUE;
- m_pHostApi->OnRecordFailed();
- break;
- }
- }
- //计算文件大小,如果文件大小大于10分钟则建立新文件记录
- nRecordTime = timeGetTime() - nRecordStartTime - nSubTitleTime;
- if (!m_bWholeSection)
- {
- if((nRecordTime > WMVREC_MAX_FILE)||((m_bCloseVideo||m_bReNameVideo)&&m_bSessionManage))
- {
- m_pWriter->End();
- m_pHostApi->OnASectionFinished(m_WmvFileName); // add by ly at 2017/03/25
- m_bStopRecord = FALSE;
- bInitWmvParam = FALSE;
- nRecordTime = 0; //本段视频持续时间
- nRecordStartTime = timeGetTime(); //本段录像开始时间
- nTimeExpended = 0; //录制经过的时间
- nVideoNum = 0; //当前时间的视频数
- noldvideoNum = 0; //已经录制的视频数
- nAudioNum = 0; //当前时间的音频数
- noldAudioNum = 0; //已经录制的音频数
- nAudioFromQueNum = 0; //从音频队列中取出的音频数量
- nAudioBufferLens = 0; //音频录制拼接BUFFER的当前长度
- nRemoteAudioBufferLens = 0; //远端音频拼接BUFFER的当前长度
- nRemoteAudioNum=0; //远端音频数量
- nSubTitleTime = 0; //本段视频添加字幕耗时
- memset(videoframe->data,0,nVideoFrameSize);
- memset(m_pAudioBufferTmp,0,m_iAudioBufferSize);
- memset(pRemoteAudioBufTmp,0,nPerSecBufferLens);
- if (m_bSessionManage)
- {
- //sessionid切换,重命名录像文件
- if (m_bReNameVideo)
- {
- if (WmvFileSerialNum>0)//如果当前文件序号大于0,说明前一个文件与当前文件是同一个SESSION,需要修改前一个文件名+END,否则直接改当前文件的文件名
- {
- //sessionid切换,前一个文件需要加上END
- char m_NewFileName[MAX_PATH];
- char m_OldFileName[MAX_PATH];
- string FileName(m_WmvFileName);
- string name = FileName.substr(0,FileName.find_first_of('_')+1);
- //m_pHostApi->Debug("renamefile1 name = %s",name.c_str());
- //前一个文件名
- sprintf_s(m_OldFileName,MAX_PATH,"%s%d.wmv",name.c_str(),WmvFileSerialNum-1);
- sprintf_s(m_NewFileName,MAX_PATH,"%s%d_end.wmv",name.c_str(),WmvFileSerialNum-1);
- m_pHostApi->Debug("renamefile1 from %s to %s",m_OldFileName,m_NewFileName);
- ReNameFile(m_OldFileName,m_NewFileName);
- }
- //sessionid切换,当前文件需要重命名
- char m_NewFileName[MAX_PATH];
- WmvFileSerialNum = 0;
- sprintf_s(m_NewFileName,MAX_PATH,"%s%s_%d.wmv",m_PathName,m_FileName,WmvFileSerialNum);
- m_pHostApi->Debug("renamefile2 from %s to %s",m_WmvFileName,m_NewFileName);
- if (ReNameFile(m_WmvFileName,m_NewFileName))
- {
- WmvFileSerialNum++;
- }
- m_bReNameVideo = FALSE;
- }
- else
- {
- //继续录像,删除当前文件的"END"
- char m_NewFileName[MAX_PATH];
- sprintf_s(m_NewFileName,MAX_PATH,"%s%s_%d.wmv",m_PathName,m_FileName,WmvFileSerialNum);
- m_pHostApi->Debug("renamefile3 from %s to %s",m_WmvFileName,m_NewFileName);
- ReNameFile(m_WmvFileName,m_NewFileName);
- //WMV文件的序列号
- WmvFileSerialNum++;
- }
- //新文件增加END
- sprintf_s(m_WmvFileName,MAX_PATH,"%s%s_%d_end.wmv",m_PathName,m_FileName,WmvFileSerialNum);
- m_pHostApi->Debug("generate new file name %s",m_WmvFileName);
- m_bCloseVideo = FALSE;
- }
- else
- {
- //WMV文件的序列号
- WmvFileSerialNum++;
- sprintf_s(m_WmvFileName,MAX_PATH,"%s%s_%d.wmv",m_PathName,m_FileName,WmvFileSerialNum);
- }
- }
- }
- }
- catch (...)
- {
- m_pHostApi->Debug("IPP copy error or stop record.");
- Sleep(50);
- continue;
- }
- }
- if (pRemoteAudioBufTmp){
- delete pRemoteAudioBufTmp;
- pRemoteAudioBufTmp = NULL;
- }
- m_pHostApi->OnRecordFinished();
-
- EndRecord();
- if (pText)
- {
- delete pText;
- pText = NULL;
- }
- return 0;
- }
- #ifdef RVC_OS_WIN
- // 寻找某目录下与通配符匹配的文件 add by ly at 20170215
- BOOL FindMatchedFile(LPCSTR sFindPath, LPCSTR sFindFileName, std::string &sMatchedFile)
- {
- char sPath[MAX_PATH];
- memset(sPath, 0, MAX_PATH);
- char sFormatFileName[MAX_PATH+2] = "*";
- WIN32_FIND_DATA FindFileData;
- HANDLE hFind;
- strcpy(sFormatFileName, sFindPath);
- if(sFindPath[strlen(sFindPath)-1] != SPLIT_SLASH)
- {
- strcat(sFormatFileName, SPLIT_SLASH_STR);
- strcat(sFormatFileName, "*");
- }
- else
- {
- strcat(sFormatFileName, "*");
- }
- strcat(sFormatFileName, sFindFileName);
- strcat(sFormatFileName, "*");
- hFind = FindFirstFile(sFormatFileName, &FindFileData);
- if (hFind == INVALID_HANDLE_VALUE)
- {
- sMatchedFile = "";
- return FALSE;
- }
- else
- {
- sMatchedFile = FindFileData.cFileName;
- FindClose(hFind);
- }
- return TRUE;
- }
- #else
- BOOL FindMatchedFile(LPCSTR sFindPath, LPCSTR sFindFileName, std::string& sMatchedFile)
- {
- DIR* pDir = NULL;
- struct dirent* pFile = NULL;
- BOOL result = false;
- pDir = opendir(sFindPath);
- if (pDir == NULL) return result;
- while ((pFile = readdir(pDir)) != NULL) {
- if (pFile->d_type & DT_DIR) {
- //m_pHostApi->Debug("FindMatchedFile DIR name: %s.", pFile->d_name);
- if (strcmp(pFile->d_name, ".") == 0
- || strcmp(pFile->d_name, "..") == 0) continue;
- char Path[256] = {0};
- int len = strlen(sFindPath);
- strncpy(Path, sFindPath, len + 1);
- if (sFindPath[len - 1] != '/') strncat(Path, "/", 2);
- strncat(Path, pFile->d_name, strlen(pFile->d_name) + 1);
- //m_pHostApi->Debug("FindMatchedFile Path: %s.", Path);
- result = FindMatchedFile(Path, sFindFileName, sMatchedFile);
- if (result) {
- break;
- }
- }
- else {
- //m_pHostApi->Debug("FindMatchedFile FILE name: %s.", pFile->d_name);
- if (strstr(pFile->d_name, sFindFileName) != NULL) {
- char Path[256];
- int len = strlen(sFindPath);
- strncpy(Path, sFindPath, len + 1);
- if (sFindPath[len - 1] != '/') strncat(Path, "/", 2);
- strncat(Path, pFile->d_name, strlen(pFile->d_name) + 1);
- //m_pHostApi->Debug("FindMatchedFile Finded: %s.", Path);
- sMatchedFile = Path;
- result = true;
- break;
- }
- }
- }
- closedir(pDir);
- return result;
- }
- #endif
- #ifdef RVC_OS_WIN
- static void DeleteDumpFiles()
- {
- CSimpleStringA sourPath = ".\\";
- if (sourPath.GetLength() > 0 && sourPath[sourPath.GetLength()-1] != '\\') {
- sourPath += "\\";
- }
- char srcFilePath[MAX_PATH]={0};
- WIN32_FIND_DATA FindFileData;
- HANDLE hFind;
- BOOL fFinished = FALSE;
- sprintf_s(srcFilePath, MAX_PATH, "%s*.dmp", sourPath);
- hFind = FindFirstFile(srcFilePath, &FindFileData);
- if (INVALID_HANDLE_VALUE != hFind)
- {
- while (!fFinished)
- {
- if (FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes)
- {
- continue;
- }
- CSimpleStringA sourFileName = CSimpleStringA::Format("%s%s", (LPCSTR)sourPath, FindFileData.cFileName);
- if(!DeleteFile((LPCSTR)sourFileName)) {
- Dbg("Error Code %lu while delete %s ", GetLastError(), (LPCSTR)sourFileName);
- }
- else{
- Dbg("Delete file %s success.", (LPCSTR)sourFileName);
- }
-
- if (!FindNextFile(hFind, &FindFileData))
- {
- if (GetLastError() == ERROR_NO_MORE_FILES)
- {
- fFinished = TRUE;
- }
- else
- {
- break;
- }
- }
- }
- FindClose(hFind);
- }
-
- }
- static void wmvrecorddump_exception(PEXCEPTION_POINTERS ExceptionInfo, libwmvrecord_impl* pWmvRecord)
- {
- if (NULL != pWmvRecord){
- pWmvRecord->EndRecord();
- if (NULL != pWmvRecord->m_pHostApi){
- pWmvRecord->m_pHostApi->Debug("VideoRecord Thread Exception.");
- pWmvRecord->m_pHostApi->OnRecordEntityExcption();
- }
- }
- DeleteDumpFiles();
- char tmp[MAX_PATH] = {0};
- HANDLE hDumpFile;
- sprintf(tmp, ".\\wmvrecord_%d.dmp", GetCurrentProcessId());
- hDumpFile = CreateFileA( tmp, GENERIC_READ | GENERIC_WRITE,
- 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
- if( ( hDumpFile != NULL ) && ( hDumpFile != INVALID_HANDLE_VALUE ) )
- {
- MINIDUMP_EXCEPTION_INFORMATION mdei;
- MINIDUMP_TYPE mdt;
- mdei.ThreadId = GetCurrentThreadId();
- mdei.ExceptionPointers = ExceptionInfo;
- mdei.ClientPointers = FALSE;
- mdt = MiniDumpWithFullMemory;
- MiniDumpWriteDump( GetCurrentProcess(), GetCurrentProcessId(),
- hDumpFile, mdt, (ExceptionInfo != 0) ? &mdei : 0, 0, 0 );
- CloseHandle( hDumpFile );
- }
- }
- #endif
- //视频录制线程
- static void* VideoRecordThread(void* pParam)
- {
- libwmvrecord_impl* WmvRecord = (libwmvrecord_impl*)pParam;
- int iRet = -1;
- #ifdef _WIN32
- __try
- {
- iRet = WmvRecord->VideoRecord();
- }
- __except(wmvrecorddump_exception(GetExceptionInformation(),WmvRecord), EXCEPTION_EXECUTE_HANDLER) {
- }
- #else
- iRet = WmvRecord->VideoRecord();
- #endif
- return &iRet;
- }
- public:
- BOOL StartWmvRecord(int fps,int videoquality,const SubtitleParam *subtitleParam,BOOL bWholeSection,
- BOOL bSessionManage,const char *pathname,int pathlength,const char *wmvfilename,int filenamelength, Rvc_RecordAudioParam_t* pAudioParam)//开始记录
- {
- m_audioqueue = NULL;
- m_localaudioqueue = new Clibaudioqueue((char *)m_audioqueuename);
-
- m_salesaudioqueue = NULL;
- if (strlen(m_salesaudioqueuename) != 0){
- m_salesaudioqueue = new Clibaudioqueue((char*)m_salesaudioqueuename);
- }
- m_videoqueue = NULL;
- if (strlen(m_videoqueuename) != 0){
- m_videoqueue = new Clibvideoqueue((char*)m_videoqueuename);
- }
- m_videoqueue2 = NULL;
- if (strlen(m_videoqueue2name) != 0){
- m_videoqueue2 = new Clibvideoqueue((char*)m_videoqueue2name);
- }
- m_remotevideoqueue = NULL;
- if (strlen(m_remotevideoqueuename) != 0){
- m_remotevideoqueue = new Clibvideoqueue((char*)m_remotevideoqueuename);
- }
- m_remoteaudioqueue = NULL;
- if (strlen(m_remoteaudioqueuename) != 0){
- m_remoteaudioqueue = new Clibaudioqueue((char*)m_remoteaudioqueuename);
- }
- memset(m_FileName,0,MAX_PATH);
- memset(m_PathName,0,MAX_PATH);
- if (filenamelength == 0){
- SYSTEMTIME nowTime; // 系统时间结构体
- GetLocalTimeRVC(nowTime);
- sprintf_s(m_FileName,MAX_PATH,"%4d%02d%02d%02d%02d%02d",nowTime.wYear,nowTime.wMonth,nowTime.wDay,nowTime.wHour,nowTime.wMinute,nowTime.wSecond);
- }
- else{
- memcpy(m_FileName,wmvfilename,filenamelength);
- }
- if (pathlength!=0){
- memcpy(m_PathName,pathname,pathlength);
- }
- m_nFps = fps;
- if (videoquality > 0 || videoquality <= 100){
- m_videoquality = videoquality;
- }
- else{
- m_pHostApi->Debug("warn: videoquality is illegal, will use default instead!");
- m_videoquality = 75;
- }
- memset(&m_SubtitleParam, 0, sizeof(SubtitleParam));
- if(subtitleParam != NULL) // 接收字幕信息
- {
- memcpy(&m_SubtitleParam, subtitleParam, sizeof(SubtitleParam));
- }
- m_bWholeSection = bWholeSection;
- m_bSessionManage = bSessionManage;
- m_eRecordType = pAudioParam->eRecordType;
- m_eAudioType = pAudioParam->eOutPutType;
- m_bIsAudioNsOn = pAudioParam->bIsNsOn;
- m_iNsPolicy = pAudioParam->iNsPolicy;
- m_bIsAudioTransOn = pAudioParam->bIsTransOn;
- m_audioqueue = m_localaudioqueue;
- if (m_salesaudioqueue && (eSingleSide == m_eRecordType || eStand2Agent == m_eRecordType)) //当大机进行双录拼接时使用本地音频队列会出现抢占导致取音频失败,此处选择使用销售音频队列
- {
- m_audioqueue = m_salesaudioqueue;
- }
- m_pHostApi->Debug("record output audio quality type is = %s, noise suppression flag is %s, noise suppression policy is %d, audio transfer flag is %s.", audio_quality_type_table[m_eAudioType], m_bIsAudioNsOn ? "true":"false", m_iNsPolicy, m_bIsAudioTransOn ? "true":"false");
- if (0 != m_nRecordthreadId){
- StopWmvRecord();
- }
- return StartRecord();
- }
- BOOL StopWmvRecord() //退出
- {
- m_bStopRecord = TRUE;
- sem_post(&m_semt);
- if (0 == pthread_join(m_nRecordthreadId, NULL)) {
- m_pHostApi->Debug("thread join video record thread %u success!", m_nRecordthreadId);
- m_nRecordthreadId = 0;
- }
- else {
- m_pHostApi->Debug("thread join video record thread failed!");
- }
- return TRUE;
- }
- void ChangeFilename(const char*newfilename)
- {
- if (!m_bWholeSection)
- {
- memset(m_FileName,0,MAX_PATH);
- memcpy(m_FileName,newfilename,strlen(newfilename));
- m_bReNameVideo = TRUE;
- }
- }
- BOOL setRightWaterMark(wchar_t* strWaterMark, size_t ulen)
- {
- BOOL bRet = FALSE;
- memset(m_SubtitleParam.strRightAgentInfo,0,MAX_PATH);
- if (ulen < MAX_PATH){
- memcpy(m_SubtitleParam.strRightAgentInfo,strWaterMark,ulen);
- bRet = TRUE;
- }
- return bRet;
- }
- BOOL StopAudioStreamEnTransQueue() //停止音频包入传输队列
- {
- m_bIsAudioTransOn = false;
- return TRUE;
- }
- void Debug(const char* fmt, ...)
- {
- va_list arg;
- va_start(arg, fmt);
- char buf[512];
- int n = _vscprintf(fmt, arg);
- if (n+1 > 512)
- n = 511;
- vsnprintf(buf, n+1, fmt, arg);
- m_pHostApi->Debug(buf);
- va_end(arg);
- }
- BOOL m_bCloseVideo; //是否关闭当前录像
- BOOL m_bReNameVideo; //是否重命名录像文件
- };
- // 这是已导出类的构造函数。
- // 有关类定义的信息,请参阅 libwmvrecord.h
- Clibwmvrecord::Clibwmvrecord(BOOL *pResult, CWmvHostApi *pHostAPI,LPCTSTR audioqueuename,LPCTSTR videoqueuename,LPCTSTR videoqueue2name,LPCTSTR salesaudioqueuename,LPCTSTR remotevideoqueuename,LPCTSTR remoteaudioqueuename)
- {
- m_pImpl = new libwmvrecord_impl(pResult,pHostAPI,audioqueuename,videoqueuename,videoqueue2name,salesaudioqueuename,remotevideoqueuename,remoteaudioqueuename);
- return;
- }
- Clibwmvrecord::~Clibwmvrecord()
- {
- delete m_pImpl;
- return;
- }
- BOOL Clibwmvrecord::StartWmvRecord(
- int fps,
- int videoquality,
- Rvc_RecordAudioParam_t* pAudioParam,
- SubtitleParam *subtitleParam /* = NULL */,
- BOOL bWholeSection /* = FALSE */,
- BOOL bSessionManage /* = FALSE */,
- const char *pathname /* = NULL */,
- int pathlength /* = 0 */,
- const char *wmvfilename /* = NULL */,
- int filenamelength /* = 0 */
- )
- {
- return m_pImpl->StartWmvRecord(fps,videoquality,subtitleParam,bWholeSection,bSessionManage,pathname,pathlength,wmvfilename,filenamelength,pAudioParam);
- }
- BOOL Clibwmvrecord::StopWmvRecord() //退出
- {
- return m_pImpl->StopWmvRecord();
- }
- void Clibwmvrecord::CloseVideoFile() //结束当前录像
- {
- m_pImpl->m_bCloseVideo = TRUE;
- }
- BOOL Clibwmvrecord::ReNameVideoFile(const char*newfilename) //session变化,修改当前录像文件名
- {
- m_pImpl->ChangeFilename(newfilename);
- //m_pImpl->m_bReNameVideo = TRUE;
- return TRUE;
- }
- BOOL Clibwmvrecord::SetRightVideoWaterMark(wchar_t* strWaterMark)
- {
- BOOL bRet = FALSE;
- if (NULL == strWaterMark){
- return bRet;
- }
- size_t ulen = wcslen(strWaterMark);
- bRet = m_pImpl->setRightWaterMark(strWaterMark, ulen*sizeof(wchar_t));
- return bRet;
- }
- BOOL Clibwmvrecord::StopAudioEnTransQueue()
- {
- return m_pImpl->StopAudioStreamEnTransQueue();
- }
|