libwmvrecord.cpp 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240
  1. // libwmvrecord.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include "libwmvrecord.h"
  4. #ifdef RVC_OS_WIN
  5. #include "io.h"
  6. #include <process.h>
  7. #include <windows.h> //包含头文件 windows.h
  8. #else
  9. #include <pthread.h>
  10. #endif
  11. #include <dirent.h>
  12. #include <stdint.h>
  13. #include <unistd.h>
  14. #include "libaudioqueue.h"
  15. #include "libvideoqueue.h"
  16. #include <stdio.h>
  17. #include <math.h>
  18. #include <exception>
  19. #include "FFmpegWriter.h"
  20. #include "videoutil.h"
  21. #include "cv.h"
  22. #include "highgui.h"
  23. #include "CvxText.h" // 用于中文字幕添加 add by ly
  24. #include <speex/speex_resampler.h>
  25. #include "iaudionsinterface.h"
  26. #include "libaudiotransqueue.h"
  27. #include <semaphore.h>
  28. #define SLASH '/'
  29. #define BACK_SLASH '\\'
  30. #define MB_SLASH_STR "/"
  31. #define MB_BACK_SLASH_STR "\\"
  32. #define W_SLASH_STR L"/"
  33. #define W_BACK_SLASH_STR L"\\"
  34. #if defined(UNICODE) || defined(_UNICODE)
  35. #define SLASH_STR W_SLASH_STR
  36. #define BACK_SLASH_STR W_BACK_SLASH_STR
  37. #else
  38. #define SLASH_STR MB_SLASH_STR
  39. #define BACK_SLASH_STR MB_BACK_SLASH_STR
  40. #endif
  41. #ifdef _WIN32
  42. #define SPLIT_SLASH BACK_SLASH
  43. #define SPLIT_SLASH_STR BACK_SLASH_STR
  44. #define MB_SPLIT_SLASH_STR MB_BACK_SLASH_STR
  45. #define W_SPLIT_SLASH_STR W_BACK_SLASH_STR
  46. #define LINE_BREAK_STR "\r\n"
  47. #else
  48. #define SPLIT_SLASH SLASH
  49. #define SPLIT_SLASH_STR SLASH_STR
  50. #define MB_SPLIT_SLASH_STR MB_SLASH_STR
  51. #define W_SPLIT_SLASH_STR W_SLASH_STR
  52. #define LINE_BREAK_STR "\n"
  53. #endif
  54. #ifndef CAPTURE_CLOCK
  55. #define CAPTURE_CLOCK 8000
  56. #endif
  57. #ifndef CAPTURE_FRAME_TIME
  58. #define CAPTURE_FRAME_TIME 20 // 20ms per frame
  59. #endif
  60. #ifdef RVC_OS_WIN
  61. #include <DbgHelp.h>
  62. #pragma comment(lib, "dbghelp.lib")
  63. #endif
  64. #ifndef RVC_OS_WIN
  65. #include <time.h>
  66. unsigned int timeGetTime()
  67. {
  68. unsigned int uptime = 0;
  69. struct timespec on;
  70. if (clock_gettime(CLOCK_MONOTONIC, &on) == 0)
  71. uptime = on.tv_sec * 1000 + on.tv_nsec / 1000000;
  72. return uptime;
  73. }
  74. #endif
  75. void GetLocalTimeRVC(SYSTEMTIME& stTime)
  76. {
  77. #ifdef RVC_OS_WIN
  78. GetLocalTime(&stTime);
  79. #else
  80. struct tm* pst = NULL;
  81. time_t t = time(NULL);
  82. pst = localtime(&t);
  83. stTime.wYear = pst->tm_year + 1900;
  84. stTime.wMonth = pst->tm_mon + 1;//because the range of tm_mon is:0-11
  85. stTime.wDay = pst->tm_mday;
  86. stTime.wHour = pst->tm_hour;
  87. stTime.wMinute = pst->tm_min;
  88. stTime.wDayOfWeek = pst->tm_wday;
  89. stTime.wSecond = pst->tm_sec;
  90. stTime.wMilliseconds = 0;
  91. //Dbg("oiltest,time:%d,%d,%d,%d", stTime.wYear, stTime.wHour, stTime.wMinute, stTime.wDayOfWeek);
  92. #endif
  93. }
  94. class libwmvrecord_impl : public LogApi
  95. {
  96. public:
  97. libwmvrecord_impl(BOOL *pResult,CWmvHostApi *pHostAPI,LPCTSTR audioqueuename,LPCTSTR videoqueuename,LPCTSTR videoqueue2name,LPCTSTR salesaudioqueuename,LPCTSTR remotevideoqueuename,LPCTSTR remoteaudioqueuename)
  98. {
  99. m_bResult = pResult;
  100. m_pHostApi = pHostAPI;
  101. m_bStopRecord = FALSE;
  102. m_bCloseVideo = FALSE;
  103. m_eRecordType = eSingleSide;
  104. m_bIsAudioNsOn = FALSE;
  105. m_iNsPolicy = 2;
  106. m_bIsAudioTransOn = FALSE;
  107. m_bReNameVideo = FALSE;
  108. m_nFps = 0;
  109. m_bWholeSection = FALSE;
  110. m_bSessionManage = FALSE;
  111. videoframe = new videoq_frame;
  112. memset(videoframe,0,sizeof(videoq_frame));
  113. audioframe = new audio_frame;
  114. memset(audioframe,0,sizeof(audio_frame));
  115. m_hRecordThread = NULL;
  116. m_nRecordthreadId = 0;
  117. sem_init(&m_semt, 0, 0);
  118. m_pAudioBufferTmp = NULL; //音频录制拼接BUFFER
  119. m_iAudioBufferSize = 0;
  120. memset(m_FileName,0,MAX_PATH);
  121. memset(m_WmvFileName,0,MAX_PATH);
  122. memset(m_PathName,0,MAX_PATH);
  123. memset(m_audioqueuename, 0, MAX_PATH);
  124. memset(m_salesaudioqueuename, 0, MAX_PATH);
  125. memset(m_videoqueuename, 0, MAX_PATH);
  126. memset(m_videoqueue2name, 0, MAX_PATH);
  127. memset(m_remotevideoqueuename, 0, MAX_PATH);
  128. memset(m_remoteaudioqueuename, 0, MAX_PATH);
  129. //音频传输队列,初始化为空
  130. m_salestransqueue = NULL;
  131. // 支持现场销售双录使用独立的音频队列 edit by ly@2018/06/13
  132. m_audioqueue = NULL;
  133. //m_localaudioqueue = new Clibaudioqueue((char *)audioqueuename);
  134. strncpy(m_audioqueuename, audioqueuename, (strlen(audioqueuename) > MAX_PATH) ? (MAX_PATH - 1): strlen(audioqueuename));
  135. m_salesaudioqueue = NULL;
  136. if (salesaudioqueuename)
  137. {
  138. strncpy(m_salesaudioqueuename, salesaudioqueuename, (strlen(salesaudioqueuename) > MAX_PATH) ? (MAX_PATH - 1) : strlen(salesaudioqueuename));
  139. }
  140. m_videoqueue = NULL;
  141. if (videoqueuename)
  142. {
  143. strncpy(m_videoqueuename, videoqueuename, (strlen(videoqueuename) > MAX_PATH) ? (MAX_PATH - 1) : strlen(videoqueuename));
  144. }
  145. m_videoqueue2 = NULL;
  146. if (videoqueue2name)
  147. {
  148. strncpy(m_videoqueue2name, videoqueue2name, (strlen(videoqueue2name) > MAX_PATH) ? (MAX_PATH - 1) : strlen(videoqueue2name));
  149. }
  150. m_remotevideoqueue = NULL;
  151. if (remotevideoqueuename)
  152. {
  153. strncpy(m_remotevideoqueuename, remotevideoqueuename, (strlen(remotevideoqueuename) > MAX_PATH) ? (MAX_PATH - 1) : strlen(remotevideoqueuename));
  154. }
  155. m_remoteaudioqueue = NULL;
  156. if (remoteaudioqueuename)
  157. {
  158. strncpy(m_remoteaudioqueuename, remoteaudioqueuename, (strlen(remoteaudioqueuename) > MAX_PATH) ? (MAX_PATH - 1) : strlen(remoteaudioqueuename));
  159. }
  160. m_pWriter = NULL;
  161. m_pAudioNsObj = NULL;
  162. int err = 0;
  163. m_16kto8kst = speex_resampler_init_frac(1, 2*CAPTURE_CLOCK , CAPTURE_CLOCK, 2*CAPTURE_CLOCK, CAPTURE_CLOCK, 0, &err);
  164. *pResult = TRUE;
  165. }
  166. ~libwmvrecord_impl()
  167. {
  168. if (videoframe->data != NULL)
  169. {
  170. delete videoframe->data;
  171. videoframe->data = NULL;
  172. }
  173. if (videoframe != NULL)
  174. {
  175. delete videoframe;
  176. videoframe = NULL;
  177. }
  178. if (audioframe != NULL)
  179. {
  180. delete audioframe;
  181. audioframe = NULL;
  182. }
  183. if (m_hRecordThread)
  184. {
  185. m_hRecordThread = NULL;
  186. }
  187. if (m_pWriter!= NULL)
  188. {
  189. delete m_pWriter;
  190. m_pWriter = NULL;
  191. }
  192. if (m_localaudioqueue != NULL)
  193. {
  194. delete m_localaudioqueue;
  195. m_localaudioqueue = NULL;
  196. }
  197. if (m_salesaudioqueue != NULL)
  198. {
  199. delete m_salesaudioqueue;
  200. m_salesaudioqueue = NULL;
  201. }
  202. m_audioqueue = NULL;
  203. if (m_remoteaudioqueue != NULL)
  204. {
  205. delete m_remoteaudioqueue;
  206. m_remoteaudioqueue = NULL;
  207. }
  208. if (m_videoqueue != NULL)
  209. {
  210. delete m_videoqueue;
  211. m_videoqueue = NULL;
  212. }
  213. if (m_videoqueue2 != NULL)
  214. {
  215. delete m_videoqueue2;
  216. m_videoqueue2 = NULL;
  217. }
  218. if (m_remotevideoqueue != NULL)
  219. {
  220. delete m_remotevideoqueue;
  221. m_remotevideoqueue = NULL;
  222. }
  223. if (NULL != m_16kto8kst){
  224. speex_resampler_destroy(m_16kto8kst);
  225. m_16kto8kst = NULL;
  226. }
  227. if (NULL != m_pAudioNsObj){
  228. DestroyIAudioNsObj(m_pAudioNsObj);
  229. m_pAudioNsObj = NULL;
  230. }
  231. if (NULL != m_salestransqueue){
  232. //Dbg("delete m_salestransqueue ing .......");
  233. delete m_salestransqueue;
  234. m_salestransqueue = NULL;
  235. }
  236. }
  237. CWmvHostApi* GetHostApi()
  238. {
  239. return m_pHostApi;
  240. }
  241. private:
  242. sem_t m_semt;
  243. BOOL m_bStopRecord;
  244. eRvcRecordType m_eRecordType; //录制双向的视频类型,0:单向录制,1:手机<->pad双录,2:坐席<->pad双录,3:坐席<->大机双录
  245. eAudioOutPutType m_eAudioType;
  246. BOOL m_bIsAudioNsOn; //是否开启音频降噪功能开关
  247. int m_iNsPolicy; //音频降噪等级
  248. BOOL m_bIsAudioTransOn; //是否开始音频传输到识别服务器开关
  249. int m_nFps;
  250. ////字幕信息 add by ly
  251. //struct RecordSubTitle m_SubTitle;
  252. /************************************************************************/
  253. int m_videoquality;
  254. //int m_audioOutBitRate;
  255. SubtitleParam m_SubtitleParam;
  256. BOOL m_bWholeSection;
  257. BOOL m_bSessionManage;
  258. /************************************************************************/
  259. videoq_frame* videoframe;
  260. audio_frame* audioframe;
  261. Clibaudioqueue*m_audioqueue;
  262. Clibvideoqueue*m_videoqueue;
  263. Clibvideoqueue*m_videoqueue2;
  264. Clibvideoqueue*m_remotevideoqueue; //远端视频,用于双向录像,add by chh 20170320
  265. Clibaudioqueue*m_remoteaudioqueue; //远端音频,用于双向录像,add by chh 20170320
  266. Clibaudioqueue*m_localaudioqueue; //本地音频,add by ly@2018/06/13
  267. Clibaudioqueue*m_salesaudioqueue; //现场销售音频,add by ly@2018/06/13
  268. char m_audioqueuename[MAX_PATH];
  269. char m_videoqueuename[MAX_PATH];
  270. char m_videoqueue2name[MAX_PATH];
  271. char m_salesaudioqueuename[MAX_PATH];
  272. char m_remotevideoqueuename[MAX_PATH];
  273. char m_remoteaudioqueuename[MAX_PATH];
  274. Clibaudiotransqueue *m_salestransqueue;
  275. pthread_t m_hRecordThread;
  276. pthread_t m_nRecordthreadId;
  277. char m_FileName[MAX_PATH];
  278. char m_PathName[MAX_PATH];
  279. char m_WmvFileName[MAX_PATH];
  280. //编码器
  281. CWmvHostApi* m_pHostApi;
  282. BOOL*m_bResult;
  283. //音频录制拼接BUFFER
  284. char*m_pAudioBufferTmp;
  285. //音频拼接的缓冲区大小;
  286. int m_iAudioBufferSize;
  287. //wmv recorder
  288. FFmpegWmvWriter*m_pWriter;
  289. //audio noise suppression
  290. IAudioNs *m_pAudioNsObj;
  291. SpeexResamplerState *m_16kto8kst;
  292. BOOL EndRecord()
  293. {
  294. if (!m_pWriter->End())
  295. {
  296. return FALSE;
  297. }
  298. if (m_pWriter)
  299. {
  300. delete m_pWriter;
  301. m_pWriter = NULL;
  302. }
  303. if (videoframe->data)
  304. {
  305. delete videoframe->data;
  306. videoframe->data = NULL;
  307. }
  308. if (m_pAudioBufferTmp!=NULL)
  309. {
  310. delete m_pAudioBufferTmp;
  311. m_pAudioBufferTmp = NULL;
  312. m_iAudioBufferSize = 0;
  313. }
  314. if (m_localaudioqueue != NULL)
  315. {
  316. delete m_localaudioqueue;
  317. m_localaudioqueue = NULL;
  318. }
  319. if (m_salesaudioqueue != NULL)
  320. {
  321. delete m_salesaudioqueue;
  322. m_salesaudioqueue = NULL;
  323. }
  324. m_audioqueue = NULL;
  325. if (m_remoteaudioqueue != NULL)
  326. {
  327. delete m_remoteaudioqueue;
  328. m_remoteaudioqueue = NULL;
  329. }
  330. if (m_videoqueue != NULL)
  331. {
  332. delete m_videoqueue;
  333. m_videoqueue = NULL;
  334. }
  335. if (m_videoqueue2 != NULL)
  336. {
  337. delete m_videoqueue2;
  338. m_videoqueue2 = NULL;
  339. }
  340. if (m_remotevideoqueue != NULL)
  341. {
  342. delete m_remotevideoqueue;
  343. m_remotevideoqueue = NULL;
  344. }
  345. return TRUE;
  346. }
  347. void FSleep(int ms)
  348. {
  349. struct timespec ts;
  350. clock_gettime(CLOCK_REALTIME, &ts);
  351. long unsec = ts.tv_nsec + (1000 * 1000 * ms);
  352. ts.tv_sec += (unsec / 1000000000);
  353. ts.tv_nsec = (unsec % 1000000000);
  354. if (-1 == sem_timedwait(&m_semt, &ts)) {
  355. if (ETIMEDOUT == errno) {
  356. }
  357. }
  358. else {
  359. throw std::exception();
  360. }
  361. }
  362. static void __recordlog(void* user_data, const char* fmt, va_list arg)
  363. {
  364. libwmvrecord_impl* pRecord = (libwmvrecord_impl*)user_data;
  365. if (NULL != pRecord){
  366. CWmvHostApi* pHost = pRecord->GetHostApi();
  367. if (NULL != pHost){
  368. pHost->Debug(fmt, arg);
  369. }
  370. }
  371. }
  372. //开始录制视频
  373. BOOL StartRecord()
  374. {
  375. m_pWriter = new FFmpegWmvWriter(this);
  376. if (m_bIsAudioNsOn){
  377. audions_callback_t t_callback = { 0 };
  378. t_callback.debug = &__recordlog;
  379. t_callback.user_data = this;
  380. m_pAudioNsObj = CreateIAudioNsObj(&t_callback);
  381. }
  382. if (0 == pthread_create(&m_nRecordthreadId, NULL, VideoRecordThread, (void*)this)) {
  383. m_pHostApi->Debug("create video record thread and thread id is %u.", m_nRecordthreadId);
  384. }
  385. else {
  386. m_pHostApi->Debug("create video record thread failed.");
  387. }
  388. return TRUE;
  389. }
  390. //获取指定队列的SIZE
  391. int GetVideoFrameSize(int&nWidth,int&nHeight,Clibvideoqueue*queue) //add by chh 20170320
  392. {
  393. int size = queue->GetFrameSize(nWidth,nHeight);
  394. return size;
  395. }
  396. int GetVideoFrameSizeNew(int&nWidth,int&nHeight)
  397. {
  398. int size = 0;
  399. if (m_videoqueue == NULL)
  400. {
  401. m_pHostApi->Debug("本地视频队列未初始化,不存在!");
  402. size = 0;
  403. }
  404. if((m_videoqueue2)&&(eSingleSide == m_eRecordType)) //大机的正常计算
  405. {
  406. int width1 = 0;
  407. int height1 = 0;
  408. int width2 = 0;
  409. int height2 = 0;
  410. size = m_videoqueue->GetFrameSize(width1,height1);
  411. //m_pHostApi->Debug("eSingleSide Record, video queue size: %d, width1: %d, height1: %d.", size, width1, height1);
  412. if (size)
  413. {
  414. (width1>height1)?(nWidth=nHeight=width1):(nWidth=nHeight=height1);
  415. }
  416. else
  417. {
  418. size = m_videoqueue2->GetFrameSize(width2,height2);
  419. (width2>height2)?(nWidth=nHeight=width2):(nWidth=nHeight=height2);
  420. //m_pHostApi->Debug("eSingleSide Record, videoqueue2 size: %d, width2: %d, height2: %d.", size, width2, height2);
  421. }
  422. if (!m_SubtitleParam.bSubtitle || !m_SubtitleParam.bSubtitleSection)
  423. {
  424. size = nWidth*nHeight*3;
  425. }
  426. else
  427. {
  428. nHeight += 50;
  429. size = nWidth*nHeight*3;
  430. }
  431. //m_pHostApi->Debug("eSingleSide Record, bSubtitle: %d, size: %d, nWidth: %d, nHeight: %d.", m_SubtitleParam.bSubtitle, size, nWidth, nHeight);
  432. }
  433. else
  434. {
  435. if (m_remotevideoqueue && eSingleSide != m_eRecordType) //如果远端队列不为空,且录制双向视频
  436. {
  437. //如果PAD版下录制双向视频,视频大小由远端视频决定,如果远端为320*240,则视频大小为640*240
  438. if (ePhone2Pad == m_eRecordType || ePad2Agent == m_eRecordType) //手机到pad,坐席到pad的双向录像
  439. {
  440. int width1 = 0;
  441. int height1 = 0;
  442. size = m_remotevideoqueue->GetFrameSize(width1,height1);
  443. nWidth = width1*2;
  444. nHeight = height1;
  445. size = nWidth*nHeight*3;
  446. //m_pHostApi->Debug("ePad2Agent Record, remotevideoqueue size: %d, nWidth: %d, nHeight: %d.", size, nWidth, nHeight);
  447. }
  448. else if (eStand2Agent == m_eRecordType) //大机以终端的视频大小确定画布,终端为640*360,远端固定为320*240,则画布为640*640+320*240 = 960*640
  449. {
  450. //计算画布左边部分的分辨率,640*640
  451. int width1 = 0;
  452. int height1 = 0;
  453. int width2 = 0;
  454. int height2 = 0;
  455. size = m_videoqueue->GetFrameSize(width1,height1);
  456. //m_pHostApi->Debug("eStand2Agent Record, videoqueue size: %d, width1: %d, height1: %d.", size, width1, height1);
  457. if (size)
  458. {
  459. (width1>height1)?(nWidth=nHeight=width1):(nWidth=nHeight=height1);
  460. }
  461. else
  462. {
  463. size = m_videoqueue2->GetFrameSize(width2,height2);
  464. (width2>height2)?(nWidth=nHeight=width2):(nWidth=nHeight=height2);
  465. //m_pHostApi->Debug("eStand2Agent Record, videoqueue2 size: %d, width2: %d, height2: %d.", size, width2, height2);
  466. }
  467. //远端视频320*240
  468. nWidth =nWidth+ 480;
  469. if (!m_SubtitleParam.bSubtitle || !m_SubtitleParam.bSubtitleSection)
  470. {
  471. size = nWidth*nHeight*3;
  472. }
  473. else
  474. {
  475. nHeight += 50;
  476. size = nWidth*nHeight*3;
  477. }
  478. //m_pHostApi->Debug("eStand2Agent Record, bSubtitle: %d, size: %d, nWidth: %d, nHeight: %d.", m_SubtitleParam.bSubtitle, size, nWidth, nHeight);
  479. }
  480. }
  481. else //录制单向视频
  482. {
  483. if (!m_SubtitleParam.bSubtitle || !m_SubtitleParam.bSubtitleSection)
  484. {
  485. size = m_videoqueue->GetFrameSize(nWidth,nHeight);
  486. }
  487. else
  488. {
  489. int width1 = 0;
  490. int height1 = 0;
  491. size = m_videoqueue->GetFrameSize(width1,height1);
  492. nWidth=width1;
  493. nHeight=height1+50;
  494. size = nWidth*nHeight*3;
  495. }
  496. //m_pHostApi->Debug("eSingleSide Record No Local videoqueue2, bSubtitle: %d, size: %d, nWidth: %d, nHeight: %d.",
  497. // m_SubtitleParam.bSubtitle, size, nWidth, nHeight);
  498. }
  499. }
  500. return size;
  501. }
  502. //从指定队列读取video
  503. BOOL GetVideoFrame(video_frame* Video,int flags,Clibvideoqueue*queue) //add by chh 20170320
  504. {
  505. return queue->GetVideo2(Video,flags);
  506. }
  507. // chh@2019/10/16 获取拼接后的视频帧
  508. BOOL GetVideoFrameNew(videoq_frame* Video, int flags,const bool bSwitchCam, int iInitCam)
  509. {
  510. if (m_videoqueue2 == NULL) //如果是单摄像头pad
  511. {
  512. if (m_remotevideoqueue && eSingleSide != m_eRecordType) //如果远端队列不为空,且录制双向视频
  513. {
  514. if (ePhone2Pad == m_eRecordType || ePad2Agent == m_eRecordType)
  515. {
  516. //拼接两个方向视频的BUF
  517. if (m_videoqueue != NULL)
  518. {
  519. //合成一个图像
  520. int w = 0,h = 0;
  521. int nsize = GetVideoFrameSize(w,h,m_videoqueue); // 获取本地视频帧大小
  522. memset(Video->data,0,Video->framesize); // 初始化拼接后视频帧数据
  523. video_frame tmp_frm = {0};
  524. tmp_frm.data[0] = new unsigned char[nsize];
  525. tmp_frm.linesize[0] = w*3;
  526. tmp_frm.format = videoframe->format;
  527. tmp_frm.width = w;
  528. tmp_frm.height = h;
  529. BOOL bGetLocalvideo = FALSE;
  530. bGetLocalvideo = GetVideoFrame(&tmp_frm,1,m_videoqueue); //获取本地视频的数组形式数据
  531. //m_pHostApi->Debug("%s mode have remote video queue, local video queue format: %d, width: %d, height: %d.",
  532. // record_type_table[m_eRecordType], tmp_frm.format, tmp_frm.width, tmp_frm.height);
  533. if (bGetLocalvideo)
  534. {
  535. //本地录像需要偏移量
  536. int nOffset = 0;
  537. if (Video->height > h)
  538. {
  539. nOffset = (Video->height-h)/2*Video->width*3;
  540. }
  541. //m_pHostApi->Debug("%s mode local video queue offset: %d, Video width: %d, Video height: %d, width: %d, height: %d.",
  542. // record_type_table[m_eRecordType], nOffset, Video->width, Video->height, w, h);
  543. for (int i= 0;i<tmp_frm.height && i<Video->height;i++)
  544. {
  545. memcpy(Video->data+nOffset+i*Video->width*3,tmp_frm.data[0]+i*tmp_frm.linesize[0],tmp_frm.linesize[0]);
  546. }
  547. }
  548. delete tmp_frm.data[0];
  549. nsize = GetVideoFrameSize(w,h,m_remotevideoqueue);
  550. tmp_frm.data[0] = new unsigned char[nsize];
  551. tmp_frm.linesize[0] = w*3;
  552. tmp_frm.format = videoframe->format;
  553. tmp_frm.width = w;
  554. tmp_frm.height = h;
  555. BOOL bGetRemotevideo = FALSE;
  556. bGetRemotevideo = GetVideoFrame(&tmp_frm,1,m_remotevideoqueue);
  557. //m_pHostApi->Debug("%s mode have remote video queue, remote video queue format: %d, width: %d, height: %d.",
  558. // record_type_table[m_eRecordType], tmp_frm.format, tmp_frm.width, tmp_frm.height);
  559. if (bGetRemotevideo)
  560. {
  561. for (int i= 0;i<tmp_frm.height&&i<Video->height;i++)
  562. {
  563. memcpy(Video->data+i*Video->width*3+tmp_frm.linesize[0],tmp_frm.data[0]+i*tmp_frm.linesize[0],tmp_frm.linesize[0]);
  564. }
  565. }
  566. delete tmp_frm.data[0];
  567. if (!bGetLocalvideo&&!bGetRemotevideo)
  568. {
  569. return FALSE;
  570. }
  571. }
  572. else
  573. {
  574. return FALSE;
  575. }
  576. }
  577. else
  578. {
  579. m_pHostApi->Debug("the device type cannot record both way video");
  580. }
  581. }
  582. else //录制单向视频
  583. {
  584. if (!m_SubtitleParam.bSubtitle || !m_SubtitleParam.bSubtitleSection)
  585. {
  586. bool result = m_videoqueue->GetVideo(Video,flags);
  587. //m_pHostApi->Debug("%s mode No bSubtitle, local video queue format: %d, width: %d, height: %d.",
  588. // record_type_table[m_eRecordType], Video->format, Video->width, Video->height);
  589. return result;
  590. }
  591. else
  592. {
  593. BOOL bRslt =FALSE;
  594. int width = 0;
  595. int height = 0;
  596. m_videoqueue->GetFrameSize(width,height);
  597. memset(Video->data,0,Video->framesize);
  598. videoq_frame*tmp_frm = new videoq_frame;
  599. //下移30
  600. tmp_frm->data = Video->data+ Video->width*30*3;
  601. //m_pHostApi->Debug("%s mode local video queue offset: %d, Video width: %d, width: %d, height: %d.",
  602. // record_type_table[m_eRecordType], Video->width * 30 * 3, Video->width, width, height);
  603. bRslt = m_videoqueue->GetVideo(tmp_frm, flags);
  604. //m_pHostApi->Debug("%s mode local video queue format: %d, width: %d, height: %d.",
  605. // record_type_table[m_eRecordType], tmp_frm->format, tmp_frm->width, tmp_frm->height);
  606. if (!bRslt)
  607. {
  608. delete tmp_frm;
  609. m_pHostApi->Debug("get env video fail!");
  610. return FALSE;
  611. }
  612. delete tmp_frm;
  613. }
  614. }
  615. }
  616. else //如果是双摄像头大机
  617. {
  618. if (m_remotevideoqueue && eStand2Agent == m_eRecordType) //如果远端队列不为空,且录制双向视频
  619. {
  620. //将大机摄像头和远端视频拼接到一张画布中
  621. memset(Video->data,0,Video->framesize);
  622. BOOL bRslt =FALSE;
  623. int width = 0;
  624. int height = 0;
  625. m_videoqueue->GetFrameSize(width,height);
  626. video_frame localtmp_frm = {0};
  627. localtmp_frm.data[0] = new unsigned char[640*640*3];
  628. localtmp_frm.linesize[0] = 640*3;
  629. localtmp_frm.format = videoframe->format;
  630. localtmp_frm.width = 640;
  631. localtmp_frm.height = 640;
  632. memset(localtmp_frm.data[0],0,640*640*3);
  633. int nActiveCam = iInitCam;
  634. if (bSwitchCam)
  635. {
  636. nActiveCam = m_pHostApi->GetActiveCamera();
  637. }
  638. if (nActiveCam == 0) // get env snapshot
  639. {
  640. videoq_frame*tmp_frm = new videoq_frame;
  641. //if (!m_SubtitleParam.bSubtitle || !m_SubtitleParam.bSubtitleSection)
  642. tmp_frm->data = localtmp_frm.data[0]+ width*(width-height)/2*3; //在画布中加上偏移量
  643. //m_pHostApi->Debug("%s mode local video queue offset: %d, width: %d, height: %d.",
  644. // record_type_table[m_eRecordType], width* (width - height) / 2 * 3, width, height);
  645. //横向摄像头拼接
  646. bRslt = m_videoqueue->GetVideo(tmp_frm, flags);
  647. //m_pHostApi->Debug("%s mode local video queue format: %d, width: %d, height: %d.",
  648. // record_type_table[m_eRecordType], tmp_frm->format, tmp_frm->width, tmp_frm->height);
  649. if (!bRslt)
  650. {
  651. delete tmp_frm;
  652. m_pHostApi->Debug("stand device both way video,get env video fail!");
  653. return FALSE;
  654. }
  655. delete tmp_frm;
  656. }
  657. else if (nActiveCam == 1) // get opt snapshot
  658. {
  659. videoq_frame*tmp_frm = new videoq_frame;
  660. tmp_frm->data = localtmp_frm.data[0] + (width-height)/2*3; //在画布中加上偏移量
  661. //m_pHostApi->Debug("%s mode local videoqueue2 offset: %d, width: %d, height: %d.",
  662. // record_type_table[m_eRecordType], (width - height) / 2 * 3, width, height);
  663. //竖向摄像头图像,拼接
  664. bRslt = m_videoqueue2->GetVideo3(tmp_frm, flags);
  665. //m_pHostApi->Debug("%s mode local videoqueue2 format: %d, width: %d, height: %d.",
  666. // record_type_table[m_eRecordType], tmp_frm->format, tmp_frm->width, tmp_frm->height);
  667. if (!bRslt)
  668. {
  669. delete tmp_frm;
  670. m_pHostApi->Debug("stand device both way video,get opt video fail!");
  671. return FALSE;
  672. }
  673. delete tmp_frm;
  674. }
  675. else
  676. {
  677. return FALSE;
  678. }
  679. {
  680. //本地录像需要偏移量s
  681. int nOffset = 0;
  682. if (!m_SubtitleParam.bSubtitle || !m_SubtitleParam.bSubtitleSection)
  683. {
  684. nOffset = 0;
  685. }
  686. else
  687. {
  688. //下移25
  689. nOffset = 25*Video->width*3;
  690. }
  691. int imaxheight = localtmp_frm.height > Video->height ? Video->height : localtmp_frm.height;
  692. if (0 == nActiveCam) {
  693. for (int i = imaxheight - 1, j = 0; i >= 0 && j < imaxheight; i--, j++)
  694. {
  695. memcpy(Video->data + nOffset + j * Video->width * 3, localtmp_frm.data[0] + i * localtmp_frm.linesize[0], localtmp_frm.linesize[0]);
  696. }
  697. }
  698. else {
  699. for (int i = 0; i < imaxheight; i++)
  700. {
  701. memcpy(Video->data + nOffset + i * Video->width * 3, localtmp_frm.data[0] + i * localtmp_frm.linesize[0], localtmp_frm.linesize[0]);
  702. }
  703. }
  704. }
  705. delete localtmp_frm.data[0];
  706. //拼接远端视频
  707. //合成一个图像
  708. {
  709. int w = 0,h = 0;
  710. int nsize = GetVideoFrameSize(w,h,m_remotevideoqueue);
  711. video_frame tmp_frm = {0};
  712. tmp_frm.data[0] = new unsigned char[nsize];
  713. tmp_frm.linesize[0] = w*3;
  714. tmp_frm.format = videoframe->format;
  715. tmp_frm.width = w;
  716. tmp_frm.height = h;
  717. BOOL bGetRemotevideo = FALSE;
  718. bGetRemotevideo = GetVideoFrame(&tmp_frm,1,m_remotevideoqueue);
  719. //m_pHostApi->Debug("%s mode remote video queue format: %d, width: %d, height: %d.",
  720. // record_type_table[m_eRecordType], tmp_frm.format, tmp_frm.width, tmp_frm.height);
  721. if (bGetRemotevideo)
  722. {
  723. int imaxheight = tmp_frm.height > Video->height ? Video->height : tmp_frm.height;
  724. for (int i = imaxheight - 1, j = 0; i >= 0 && j < imaxheight; i--, j++)
  725. {
  726. 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]);
  727. }
  728. }
  729. delete tmp_frm.data[0];
  730. if (!bGetRemotevideo)
  731. {
  732. return FALSE;
  733. }
  734. }
  735. }
  736. else
  737. {
  738. BOOL bRslt =FALSE;
  739. int width = 0;
  740. int height = 0;
  741. m_videoqueue->GetFrameSize(width,height);
  742. memset(Video->data,0,Video->framesize);
  743. int nActiveCam = iInitCam;
  744. if (bSwitchCam)
  745. {
  746. nActiveCam = m_pHostApi->GetActiveCamera();
  747. }
  748. if (nActiveCam == 0) // get env snapshot
  749. {
  750. videoq_frame*tmp_frm = new videoq_frame;
  751. if (!m_SubtitleParam.bSubtitle || !m_SubtitleParam.bSubtitleSection)
  752. {
  753. tmp_frm->data = Video->data+ Video->width*(width-height)/2*3;
  754. //m_pHostApi->Debug("%s mode no remote video queue, no bSubtitle offset: %d, width: %d, height: %d.",
  755. // record_type_table[m_eRecordType], Video->width* (width - height) / 2 * 3, width, height);
  756. }
  757. else
  758. {
  759. tmp_frm->data = Video->data+ Video->width*((width-height)/2+30)*3;
  760. //m_pHostApi->Debug("%s mode no remote video queue, have bSubtitle offset: %d, width: %d, height: %d.",
  761. // record_type_table[m_eRecordType], Video->width* ((width - height) / 2 + 30) * 3, width, height);
  762. }
  763. bRslt = m_videoqueue->GetVideo(tmp_frm, flags);
  764. //m_pHostApi->Debug("%s mode no remote video queue, local video queue format: %d, width: %d, height: %d.",
  765. // record_type_table[m_eRecordType], tmp_frm->format, tmp_frm->width, tmp_frm->height);
  766. if (!bRslt)
  767. {
  768. delete tmp_frm;
  769. m_pHostApi->Debug("get env video fail!");
  770. return FALSE;
  771. }
  772. delete tmp_frm;
  773. }
  774. else if (nActiveCam == 1) // get opt snapshot
  775. {
  776. videoq_frame*tmp_frm = new videoq_frame;
  777. if (!m_SubtitleParam.bSubtitle || !m_SubtitleParam.bSubtitleSection)
  778. {
  779. tmp_frm->data = Video->data + (width-height)/2*3;
  780. //m_pHostApi->Debug("%s mode no remote video queue, no bSubtitle offset: %d, width: %d, height: %d.",
  781. // record_type_table[m_eRecordType], (width - height) / 2 * 3, width, height);
  782. }
  783. else
  784. {
  785. tmp_frm->data = Video->data + (Video->width*30+(width-height)/2)*3;
  786. //m_pHostApi->Debug("%s mode no remote video queue, have bSubtitle offset: %d, width: %d, height: %d.",
  787. // record_type_table[m_eRecordType], (Video->width * 30 + (width - height) / 2) * 3, width, height);
  788. }
  789. //横向摄像头图像,拼接
  790. bRslt = m_videoqueue2->GetVideo3(tmp_frm, flags);
  791. //m_pHostApi->Debug("%s mode no remote video queue, local videoqueue2 format: %d, width: %d, height: %d.",
  792. // record_type_table[m_eRecordType], tmp_frm->format, tmp_frm->width, tmp_frm->height);
  793. if (!bRslt)
  794. {
  795. delete tmp_frm;
  796. m_pHostApi->Debug("get opt video fail!");
  797. return FALSE;
  798. }
  799. delete tmp_frm;
  800. }
  801. else
  802. {
  803. return FALSE;
  804. }
  805. }
  806. }
  807. return TRUE;
  808. }
  809. BOOL ReNameFile(const char*file,const char*newfilename)
  810. {
  811. #ifdef RVC_OS_WIN
  812. if (!_access(file, 0))
  813. #else
  814. if (!access(file, F_OK))
  815. #endif //RVC_OS_WIN
  816. {
  817. if (!rename(file,newfilename))
  818. {
  819. return TRUE;
  820. }
  821. else
  822. {
  823. return FALSE;
  824. }
  825. }
  826. else
  827. {
  828. return FALSE;
  829. }
  830. }
  831. //混音
  832. void MixtureAudio(int buflens,char*localbuf,char*remotebuf)
  833. {
  834. for (int i =0;i<buflens-2;i+=2)
  835. {
  836. *(__int16*)(localbuf+i) = *(__int16*)(localbuf+i)
  837. +*(__int16*)(remotebuf+i)-((*(__int16*)(localbuf+i))*(*(__int16*)(remotebuf+i))>>0x10);
  838. if (*(__int16*)(localbuf+i) > 32767)
  839. {
  840. *(__int16*)(localbuf+i) = 32767;
  841. }
  842. else if (*(__int16*)(localbuf+i) < -32768)
  843. {
  844. *(__int16*)(localbuf+i) = -32768;
  845. }
  846. }
  847. return;
  848. }
  849. uint32_t BindPCMAudioData(uint32_t uBufferLen, char* pLocalAudios, uint32_t uLocalAudioLen, char* pRemoteAudios, uint32_t uRemoteAudioLen, uint32_t uBitDeepth, eStereoArrayType eType)
  850. {
  851. uint32_t uRet = 0;
  852. char* pBuffer = new char[uLocalAudioLen];
  853. memset(pBuffer, 0, uLocalAudioLen);
  854. if (NULL != pLocalAudios){
  855. memcpy(pBuffer, pLocalAudios, uLocalAudioLen);
  856. memset(pLocalAudios, 0, uBufferLen);
  857. }
  858. for (int i = 0; i < uLocalAudioLen/uBitDeepth; i++){
  859. if (eLocalLeft == eType){
  860. memcpy((uint32_t*)pLocalAudios + i, ((uint16_t*)(pBuffer))+i, uBitDeepth);
  861. uint16_t *pindex = (uint16_t*)((uint32_t*)pLocalAudios + i) + 1;
  862. memcpy(pindex, ((uint16_t*)(pRemoteAudios))+i, uBitDeepth);
  863. uRet += (2*uBitDeepth);
  864. }
  865. else{
  866. memcpy((uint32_t*)pLocalAudios + i, ((uint16_t*)(pRemoteAudios))+i, uBitDeepth);
  867. uint16_t *pindex = (uint16_t*)((uint32_t*)pLocalAudios + i) + 1;
  868. memcpy(pindex, ((uint16_t*)(pBuffer))+i, uBitDeepth);
  869. uRet += (2*uBitDeepth);
  870. }
  871. }
  872. if (NULL != pBuffer){
  873. delete []pBuffer;
  874. pBuffer = NULL;
  875. }
  876. return uRet;
  877. }
  878. size_t Get8KOutPutBitRate(int iChannels, eAudioOutPutType eType)
  879. {
  880. size_t uOutBitRate = 16*8;
  881. if (1 == iChannels){
  882. if (eUltraHD == eType){
  883. uOutBitRate = 1000*8;
  884. }
  885. else if (eHighDefinition == eType){
  886. uOutBitRate = 625*8;
  887. }
  888. else if (eStandardDefinition == eType){
  889. uOutBitRate = 16*8;
  890. }
  891. else{
  892. uOutBitRate = 128*1000;
  893. }
  894. }
  895. else{
  896. if (eStandardDefinition == eType){
  897. uOutBitRate = 16*8;
  898. }
  899. else if (eLowDefinition == eType){
  900. uOutBitRate = iChannels*128*1000;
  901. }
  902. else{
  903. uOutBitRate = 1500*8;
  904. }
  905. }
  906. return uOutBitRate;
  907. }
  908. size_t Get16KOutPutBitRate(int iChannels, eAudioOutPutType eType)
  909. {
  910. size_t uOutBitRate = 1000*8;
  911. if (1 == iChannels){
  912. if (eUltraHD == eType){
  913. uOutBitRate = 2000*8;
  914. }
  915. else if (eHighDefinition == eType){
  916. uOutBitRate = 1500*8;
  917. }
  918. else if(eStandardDefinition == eType){
  919. uOutBitRate = 1000*8;
  920. }
  921. else{
  922. uOutBitRate = 1000*256;
  923. }
  924. }
  925. else{
  926. if (eStandardDefinition == eType){
  927. uOutBitRate = 2000*8;
  928. }
  929. else{
  930. uOutBitRate = 2500*8;
  931. }
  932. }
  933. return uOutBitRate;
  934. }
  935. size_t Get32KOutPutBitRate(int iChannels, eAudioOutPutType eType)
  936. {
  937. size_t uOutBitRate = 1000*8;
  938. if (1 == iChannels){
  939. if (eUltraHD == eType){
  940. uOutBitRate = 2500*8;
  941. }
  942. else{
  943. uOutBitRate = 1000*8;
  944. }
  945. }
  946. else{
  947. if (eStandardDefinition == eType){
  948. uOutBitRate = 3000*8;
  949. }
  950. else{
  951. uOutBitRate = 6000*8;
  952. }
  953. }
  954. return uOutBitRate;
  955. }
  956. size_t Get44KOutPutBitRate(int iChannels, eAudioOutPutType eType)
  957. {
  958. size_t uOutBitRate = 2000*8;
  959. if (1 == iChannels){
  960. if (eUltraHD == eType){
  961. uOutBitRate = 6000*8;
  962. }
  963. else if(eHighDefinition == eType){
  964. uOutBitRate = 4000*8;
  965. }
  966. else{
  967. uOutBitRate = 2000*8;
  968. }
  969. }
  970. else{
  971. if (eStandardDefinition == eType){
  972. uOutBitRate = 10000*8;
  973. }
  974. else if (eHighDefinition == eType){
  975. uOutBitRate = 20000*8;
  976. }
  977. else{
  978. uOutBitRate = 40000*8;
  979. }
  980. }
  981. return uOutBitRate;
  982. }
  983. size_t GetAudioOutPutBitRate(int iInPutSamperate, int iChannels, eAudioOutPutType eType)
  984. {
  985. size_t uOutBitRate = 8000;
  986. switch (iInPutSamperate)
  987. {
  988. case 8000:
  989. uOutBitRate = Get8KOutPutBitRate(iChannels, eType);
  990. break;
  991. case 16000:
  992. uOutBitRate = Get16KOutPutBitRate(iChannels, eType);
  993. break;
  994. case 32000:
  995. uOutBitRate = Get32KOutPutBitRate(iChannels, eType);
  996. break;
  997. case 44100:
  998. uOutBitRate = Get44KOutPutBitRate(iChannels, eType);
  999. break;
  1000. default:
  1001. m_pHostApi->Debug("not support format, input sample rate is %d, channels number is %d.", iInPutSamperate, iChannels);
  1002. break;
  1003. }
  1004. return uOutBitRate;
  1005. }
  1006. int VideoRecord()
  1007. {
  1008. BOOL bResult;
  1009. BOOL bInitWmvParam = FALSE;
  1010. m_bStopRecord = FALSE;
  1011. DWORD nRecordTime = 0; //本段视频持续时间
  1012. DWORD nTimeExpended = 0; //录制经过的时间
  1013. DWORD nVideoNum = 0; //当前时间的视频数
  1014. DWORD noldvideoNum = 0; //已经录制的视频数
  1015. DWORD nAudioNum = 0; //当前时间的音频数
  1016. DWORD noldAudioNum = 0; //已经录制的音频数
  1017. DWORD nAudioFromQueNum = 0; //从音频队列中取出的音频数量
  1018. DWORD nRecordStartTime = 0; //本段录像开始时间
  1019. int WmvFileSerialNum = 0; //WMV文件的序列号
  1020. int nAudioBufferLens = 0; //音频录制拼接BUFFER的当前长度
  1021. int nRemoteAudioBufferLens = 0; //远端音频拼接BUFFER的当前长度
  1022. int nAudioFrameSize = 0; //音频帧的大小
  1023. int nVideoFrameSize = 0; //视频帧的大小
  1024. int nWidth = 0;
  1025. int nHeight = 0;
  1026. int nPerSecBufferLens = 0; //每秒音频的长度
  1027. int nAudioFps = 0; //音频频率
  1028. int nRemoteAudioFps = 0; //远端音频频率
  1029. int nRemoteAudioNum = 0; //远端音频数量
  1030. char*pRemoteAudioBufTmp = NULL; //远端音频BUFFER
  1031. // 添加字幕所需变量定义 add by ly
  1032. //char subTitles[3][256];
  1033. char timeSubtitle[MAX_PATH];
  1034. const char *weekDict[] = {"日","一","二","三","四","五","六"};
  1035. SYSTEMTIME st;
  1036. DWORD nSubTitleTime = 0; //本段视频添加字幕耗时
  1037. #ifdef RVC_OS_WIN
  1038. string TtcDir = "C:\\Windows\\Fonts\\";
  1039. #else
  1040. //string TtcDir = "/usr/share/fonts/";
  1041. string TtcDir = "./";
  1042. #endif
  1043. string fontfile = "";
  1044. CvxText *pText = NULL;
  1045. bool bSwitchCam = true;
  1046. int nInitActiveCam = 0;
  1047. m_pHostApi->Debug("FindMatchedFile");
  1048. #ifdef RVC_OS_WIN
  1049. if (FindMatchedFile(TtcDir.c_str(),"msyh.",fontfile))
  1050. #else
  1051. if (FindMatchedFile(TtcDir.c_str(), "wryh.", fontfile))
  1052. #endif
  1053. {
  1054. m_pHostApi->Debug("Fontfile:%s", fontfile.c_str());
  1055. TtcDir += fontfile;
  1056. pText = new CvxText(m_pHostApi, TtcDir.c_str());
  1057. int type = 0;
  1058. CvScalar scalar = {12, 0.2, 0.1, 0}; //字体大小/空白比例/间隔比例/旋转角度
  1059. bool underline = false;
  1060. float diaphaneity = 1.0;
  1061. pText->setFont(&type, &scalar, &underline, &diaphaneity); //字体类型/size/下滑线/透明度
  1062. }
  1063. m_pHostApi->Debug("setFont success");
  1064. if (!pText) // 若字体为空 edit by ly 2018/02/27
  1065. {
  1066. if (m_SubtitleParam.bSubtitle)
  1067. {
  1068. m_pHostApi->Debug("Font is null, subtitles cannot be added!");
  1069. m_bStopRecord = TRUE;
  1070. m_pHostApi->OnRecordFailed();
  1071. }
  1072. }
  1073. int nVideoSizeFailedTimes = 0, nAudioSizeFailedTimes = 0; // add by ly 20160713
  1074. if (m_bWholeSection || !m_bSessionManage){
  1075. sprintf_s(m_WmvFileName,MAX_PATH,"%s%s_%d.wmv",m_PathName,m_FileName,WmvFileSerialNum);
  1076. }
  1077. else{
  1078. sprintf_s(m_WmvFileName,MAX_PATH,"%s%s_%d_end.wmv",m_PathName,m_FileName,WmvFileSerialNum);
  1079. }
  1080. m_pHostApi->Debug("m_WmvFileName = %s",m_WmvFileName);
  1081. m_bCloseVideo = FALSE;
  1082. m_bReNameVideo = FALSE;
  1083. while (!m_bStopRecord)
  1084. {
  1085. try
  1086. {
  1087. if (!bInitWmvParam)
  1088. {
  1089. //获取视频帧大小
  1090. if (nVideoFrameSize <= 0)
  1091. {
  1092. if (nVideoSizeFailedTimes >= 5000)
  1093. {
  1094. m_bStopRecord = TRUE;
  1095. m_pHostApi->OnRecordFailed(true);
  1096. continue;
  1097. }
  1098. m_pHostApi->Debug("Get Video FrameSize,record type is %s.",record_type_table[m_eRecordType]);
  1099. nVideoFrameSize = GetVideoFrameSizeNew(nWidth,nHeight);
  1100. if(nVideoFrameSize <= 0)
  1101. {
  1102. ++nVideoSizeFailedTimes;
  1103. m_pHostApi->Debug("nVideoFrameSize = 0");
  1104. FSleep(5);
  1105. continue;
  1106. }
  1107. }
  1108. //获取音频帧大小
  1109. if (nAudioFrameSize <= 0)
  1110. {
  1111. if (nAudioSizeFailedTimes >= 5000)
  1112. {
  1113. m_bStopRecord = TRUE;
  1114. m_pHostApi->OnRecordFailed(true);
  1115. continue;
  1116. }
  1117. nAudioFrameSize = m_audioqueue->GetFrameSize();
  1118. if (nAudioFrameSize <=0)
  1119. {
  1120. ++nAudioSizeFailedTimes;
  1121. //m_pHostApi->Debug("nAudioFrameSize = 0");
  1122. //nAudioFrameSize = m_remoteaudioqueue->GetFrameSize();
  1123. //m_pHostApi->Debug("use m_remoteaudioqueue audio frame size init audio frame size.");
  1124. FSleep(10);
  1125. continue;
  1126. }
  1127. //m_pHostApi->Debug("Local Audio Frame Size = %d", nAudioFrameSize);
  1128. }
  1129. //初始化视频buffer
  1130. if (videoframe->data == NULL)
  1131. {
  1132. videoframe->data = new unsigned char[nVideoFrameSize];
  1133. videoframe->framesize = nVideoFrameSize;
  1134. videoframe->height = nHeight;
  1135. videoframe->width = nWidth;
  1136. videoframe->format = VIDEO_FORMAT_RGB24;
  1137. }
  1138. //初始化WMV文件
  1139. audio_frame*audio = new audio_frame;
  1140. audio->data = new char[nAudioFrameSize];
  1141. if (!m_audioqueue->GetAudio(audio))
  1142. {
  1143. delete audio->data;
  1144. delete audio;
  1145. FSleep(5);
  1146. continue;
  1147. }
  1148. 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);
  1149. //计算每秒音频的长度
  1150. nPerSecBufferLens = audio->samplespersec*audio->bitspersample/8;
  1151. m_iAudioBufferSize = nPerSecBufferLens;
  1152. m_pHostApi->Debug("nPerSecBufferLens=%d", nPerSecBufferLens);
  1153. //计算音频帧的帧频,
  1154. nAudioFps = nPerSecBufferLens/audio->framesize;
  1155. m_pHostApi->Debug("音频帧的帧频 nAudioFps=%d", nAudioFps);
  1156. if((m_audioqueue != NULL)&&((m_remoteaudioqueue != NULL)&& eSingleSide != m_eRecordType))
  1157. {
  1158. //计算远端音频包的封包频率,本地50个包,远端为手机端使用1秒100个音频包分包,远端为可视柜台坐席使用1秒50个包分包 ,新增渠道时需要检查此处
  1159. if(ePhone2Pad == m_eRecordType) //手机渠道的双向录像
  1160. {
  1161. nRemoteAudioFps = 100;
  1162. }
  1163. else if(ePad2Agent == m_eRecordType || eStand2Agent == m_eRecordType) //可视柜台渠道的双向录像,本地端和坐席端声音采用左右声道方式录制
  1164. {
  1165. nRemoteAudioFps = 50;
  1166. audio->nchannels = 2;
  1167. if (CAPTURE_CLOCK*2 == audio->samplespersec){
  1168. audio->samplespersec = CAPTURE_CLOCK;
  1169. nPerSecBufferLens = CAPTURE_CLOCK*audio->bitspersample/8;
  1170. }
  1171. m_iAudioBufferSize = nPerSecBufferLens*2;
  1172. }
  1173. else
  1174. {
  1175. nRemoteAudioFps = 0;
  1176. }
  1177. m_pHostApi->Debug("calc remote audio fps = %d",nRemoteAudioFps);
  1178. }
  1179. m_pHostApi->Debug("audio fps = %d, audio buffer size = %d.",nAudioFps, m_iAudioBufferSize);
  1180. //初始化音频buffer
  1181. if (m_pAudioBufferTmp == NULL){
  1182. m_pAudioBufferTmp = new char[m_iAudioBufferSize];
  1183. memset(m_pAudioBufferTmp, 0, m_iAudioBufferSize);
  1184. }
  1185. if (pRemoteAudioBufTmp == NULL){
  1186. pRemoteAudioBufTmp = new char[nPerSecBufferLens];
  1187. memset(pRemoteAudioBufTmp,0,nPerSecBufferLens);
  1188. }
  1189. size_t uOutPutBitRate = GetAudioOutPutBitRate(audio->samplespersec,audio->nchannels,m_eAudioType);
  1190. if (m_bIsAudioNsOn && NULL != m_pAudioNsObj){
  1191. if (m_pAudioNsObj->SetNsParams(audio->samplespersec, CAPTURE_FRAME_TIME, m_iNsPolicy)){
  1192. m_pHostApi->Debug("Ns audio set params failed!");
  1193. }
  1194. }
  1195. m_pHostApi->Debug("InitWmvWriter audio param samplespersec=%d,audioOutChannels=%d,audioOutBitRate=%d bps.",audio->samplespersec,audio->nchannels,uOutPutBitRate);
  1196. m_pHostApi->Debug("InitWmvWriter video param Width=%d,Height=%d,Fps=%d.", nWidth, nHeight, m_nFps);
  1197. bResult = m_pWriter->InitWmvWriter(m_WmvFileName,nWidth,nHeight,24,m_nFps,audio->samplespersec,
  1198. audio->nchannels,audio->bitspersample,6,m_videoquality,uOutPutBitRate, (int)m_eAudioType);
  1199. delete audio->data;
  1200. delete audio;
  1201. if(!bResult)
  1202. {
  1203. m_pHostApi->Debug("WMV初始化失败!");
  1204. *m_bResult = FALSE;
  1205. // add by ly at 20160729
  1206. m_bStopRecord = TRUE;
  1207. m_pHostApi->OnRecordFailed();
  1208. break;
  1209. }
  1210. //开始录像
  1211. bResult = m_pWriter->StartWrite();
  1212. if(!bResult)
  1213. {
  1214. m_pHostApi->Debug("开始录像失败!");
  1215. *m_bResult = FALSE;
  1216. // add by ly at 20160729
  1217. m_bStopRecord = TRUE;
  1218. m_pHostApi->OnRecordFailed();
  1219. break;
  1220. }
  1221. bInitWmvParam = TRUE;
  1222. //当第一次记录时删除当前音频只剩下1帧,使音视频数据能够同步
  1223. m_audioqueue->ClearAudioQueue();
  1224. nRecordStartTime = timeGetTime();//本段录像开始时间
  1225. continue;
  1226. }
  1227. //控制音视频的同步,精确控制录制频率,确保暂停足够时间
  1228. int sleeptime = 0;
  1229. nRecordTime = timeGetTime() - nRecordStartTime - nSubTitleTime;
  1230. //m_pHostApi->Debug("nRecordStartTime=%ld, nAudioFromQueNum=%ld, nRecordTime=%ld .", nRecordStartTime, nAudioFromQueNum, nRecordTime);
  1231. if(m_nFps<nAudioFps)
  1232. {
  1233. if((m_audioqueue != NULL)&&((m_remoteaudioqueue != NULL)&& eSingleSide != m_eRecordType))//如果双向混音,则sleeptime以远端为准
  1234. {
  1235. if ((nRemoteAudioFps!=0)&&(nRemoteAudioFps>nAudioFps))
  1236. {
  1237. sleeptime = (int)((nRemoteAudioNum+1)*1000/nRemoteAudioFps-nRecordTime);
  1238. }
  1239. else
  1240. {
  1241. sleeptime = (int)((nAudioFromQueNum+1)*1000/nAudioFps-nRecordTime);
  1242. }
  1243. }
  1244. else
  1245. {
  1246. sleeptime = (int)((nAudioFromQueNum+1)*1000/nAudioFps-nRecordTime);
  1247. }
  1248. }
  1249. else
  1250. {
  1251. sleeptime = (int)((noldvideoNum+1)*1000/m_nFps-nRecordTime);
  1252. }
  1253. if(sleeptime>=0)
  1254. {
  1255. FSleep(sleeptime);
  1256. }
  1257. //计算经过的时间,计算已经经过几帧时间y
  1258. nRecordTime = timeGetTime() - nRecordStartTime - nSubTitleTime;
  1259. nVideoNum = (DWORD)(((double) nRecordTime /1000.0 ) * (double)m_nFps);
  1260. //如果是第1帧,或者又经过了1帧的时间,则录制视频,对视频进行压缩
  1261. if (((nVideoNum==0)&&(noldvideoNum == 0))||(nVideoNum>noldvideoNum))
  1262. {
  1263. BOOL bVideoLens = (m_videoqueue2 == NULL)?(m_videoqueue->GetVideoLens()>0):((m_videoqueue->GetVideoLens()>0)||(m_videoqueue2->GetVideoLens()>0));
  1264. if (bVideoLens)
  1265. {
  1266. BOOL bGetvideo = FALSE;
  1267. //bGetvideo = GetVideoFrame(videoframe,1,bSalesRecord);
  1268. //获取拼接后的视频帧数据
  1269. if (eStand2Agent == m_eRecordType && bSwitchCam){ //大机双向录像,只做一次摄像头选择 add by clp 20191025
  1270. bSwitchCam = false;
  1271. nInitActiveCam = m_pHostApi->GetActiveCamera();
  1272. m_pHostApi->Debug("Stand2S Device Double Record local use %d CAMERA_TYPE_ENV[0] CAMERA_TYPE_OPT[1] Camera.", nInitActiveCam);
  1273. }
  1274. bGetvideo = GetVideoFrameNew(videoframe,1, bSwitchCam, nInitActiveCam);
  1275. if (!bGetvideo)
  1276. {
  1277. FSleep(2);
  1278. continue;
  1279. }
  1280. // 加入字幕
  1281. if (m_SubtitleParam.bSubtitle)
  1282. {
  1283. DWORD nStartTime = timeGetTime();
  1284. IplImage *pImg = cvCreateImage(cvSize(nWidth, nHeight), IPL_DEPTH_8U, 3);
  1285. memcpy(pImg->imageData, videoframe->data, nVideoFrameSize);
  1286. //cvFlip(pImg); // 先把图像翻转
  1287. if (strlen(m_SubtitleParam.topSubtitleData) <= 0)
  1288. {
  1289. if (m_SubtitleParam.bSubtitleSection)
  1290. {
  1291. GetLocalTimeRVC(st);
  1292. sprintf(timeSubtitle, "%4d年%02d月%02d日 星期%s %02d:%02d:%02d",st.wYear,
  1293. st.wMonth,st.wDay,weekDict[st.wDayOfWeek],st.wHour,st.wMinute, st.wSecond);
  1294. }
  1295. else
  1296. {
  1297. GetLocalTimeRVC(st);
  1298. sprintf(timeSubtitle, "%4d/%02d/%02d %02d:%02d:%02d",st.wYear,
  1299. st.wMonth,st.wDay,st.wHour,st.wMinute, st.wSecond);
  1300. }
  1301. pText->putText(pImg, timeSubtitle, cvPoint(10, 12));
  1302. }
  1303. else
  1304. {
  1305. pText->putText(pImg, m_SubtitleParam.topSubtitleData, cvPoint(10, 12));
  1306. }
  1307. if (strlen(m_SubtitleParam.bottomSubtitleData1) > 0)
  1308. {
  1309. pText->putText(pImg, m_SubtitleParam.bottomSubtitleData1, cvPoint(10, pImg->height - 17));
  1310. }
  1311. if (strlen(m_SubtitleParam.bottomSubtitleData2) > 0)
  1312. {
  1313. pText->putText(pImg, m_SubtitleParam.bottomSubtitleData2, cvPoint(10, pImg->height - 3));
  1314. }
  1315. //拼接后的右侧视频添加坐席信息
  1316. if (strlen(m_SubtitleParam.strRightAgentInfo) > 0)
  1317. {
  1318. if (eStand2Agent == m_eRecordType){
  1319. pText->putText(pImg, m_SubtitleParam.strRightAgentInfo, cvPoint(10+640, pImg->height - 17));
  1320. }
  1321. else{
  1322. pText->putText(pImg, m_SubtitleParam.strRightAgentInfo, cvPoint(10+320, pImg->height - 17));
  1323. }
  1324. }
  1325. //cvFlip(pImg); // 添完字幕再翻转
  1326. memcpy(videoframe->data, pImg->imageData, nVideoFrameSize);
  1327. cvReleaseImage(&pImg);
  1328. nSubTitleTime += (timeGetTime()- nStartTime);
  1329. }
  1330. bResult = m_pWriter->ReceiveVideoData((BYTE*)videoframe->data,nVideoFrameSize);
  1331. if (!bResult)
  1332. {
  1333. m_pHostApi->Debug("视频流写入失败!");
  1334. *m_bResult = FALSE;
  1335. // add by ly at 20160729
  1336. m_bStopRecord = TRUE;
  1337. m_pHostApi->OnRecordFailed();
  1338. break;
  1339. }
  1340. noldvideoNum++;
  1341. }
  1342. }
  1343. //本地音频不为空,远端为空或者远端不为空,但不是双向则只录制本地音频
  1344. if((m_audioqueue != NULL)&&((m_remoteaudioqueue == NULL)||((m_remoteaudioqueue != NULL) && eSingleSide == m_eRecordType)))
  1345. {
  1346. //取音频数据,合并成1S的音频
  1347. int nAudioLens = m_audioqueue->GetAudioLens();
  1348. //m_pHostApi->Debug("AudioLens: %d!", nAudioLens);
  1349. if (nAudioLens>0)
  1350. {
  1351. if (nAudioBufferLens < nPerSecBufferLens)
  1352. {
  1353. //取出音频
  1354. audioframe->data = m_pAudioBufferTmp+nAudioBufferLens;
  1355. BOOL bGetAudio = FALSE;
  1356. if (nAudioLens < MAX_AUDIOQUEUE_LENS){
  1357. bGetAudio = m_audioqueue->GetAudioAndDel(audioframe);
  1358. }
  1359. else{
  1360. bGetAudio = m_audioqueue->GetAudio(audioframe);
  1361. }
  1362. //m_pHostApi->Debug("bGetAudio: %d!", bGetAudio);
  1363. if (bGetAudio){
  1364. if (m_bIsAudioNsOn && NULL != m_pAudioNsObj){
  1365. char pOutAudio[MAX_PATH*3] = {0};
  1366. int iRet = m_pAudioNsObj->NsProcess(pOutAudio,audioframe->framesize, audioframe->data, audioframe->framesize);
  1367. if (0 == iRet){
  1368. memset(m_pAudioBufferTmp+nAudioBufferLens, 0, audioframe->framesize);
  1369. memcpy(m_pAudioBufferTmp+nAudioBufferLens, pOutAudio, audioframe->framesize);
  1370. }
  1371. }
  1372. nAudioBufferLens+=audioframe->framesize;
  1373. //m_pHostApi->Debug("audio get samplespersec=%d,framesize=%d,channels=%d,format=%d,bitspersample=%d,series number=%d.",
  1374. // audioframe->samplespersec, audioframe->framesize, audioframe->nchannels, audioframe->format, audioframe->bitspersample, audioframe->iseriesnumber);
  1375. nAudioFromQueNum++;
  1376. if ((0 == nAudioFromQueNum%100) && 0 != audioframe->iseriesnumber){
  1377. //m_pHostApi->Debug("single side record current audio queue len is %d and audio series number is: %d.",nAudioLens, audioframe->iseriesnumber);
  1378. }
  1379. if (m_bIsAudioTransOn && NULL != m_salestransqueue){
  1380. if (m_salestransqueue->Enqueue(audioframe)){
  1381. //if (0 == audioframe->iseriesnumber%100){
  1382. //m_pHostApi->Debug("audio frame enqueue sales trans queue, and frame series number is %d.", audioframe->iseriesnumber);
  1383. //}
  1384. }
  1385. else{
  1386. //m_pHostApi->Debug("audio frame enqueue sales trans queue failed, and frame series number is %d.", audioframe->iseriesnumber);
  1387. }
  1388. }
  1389. }
  1390. else{
  1391. //m_pHostApi->Debug("get audio from m_audioqueue failed.");
  1392. FSleep(2); //huchen add ,避免不停获取音频数据导致cpu占用高
  1393. }
  1394. }
  1395. }
  1396. else {
  1397. //m_pHostApi->Debug("get audio length from m_audioqueue failed.");
  1398. FSleep(2); //huchen add ,避免不停获取音频数据导致cpu占用高
  1399. }
  1400. }
  1401. else if((m_audioqueue != NULL)&&((m_remoteaudioqueue != NULL)&& eSingleSide != m_eRecordType)) //客户端坐席端混音
  1402. {
  1403. //取音频数据,合并成1S的音频
  1404. int nAudioLens = m_audioqueue->GetAudioLens();
  1405. int nRemoteAudioLens = m_remoteaudioqueue->GetAudioLens();
  1406. if((nAudioLens>0)&&(nRemoteAudioLens<=0)) //远端无数据不混音
  1407. {
  1408. if (nAudioBufferLens < nPerSecBufferLens)
  1409. { //取出音频
  1410. audioframe->data = m_pAudioBufferTmp+nAudioBufferLens;
  1411. BOOL bGetAudio = FALSE;
  1412. if (nAudioLens < MAX_AUDIOQUEUE_LENS){
  1413. bGetAudio = m_audioqueue->GetAudioAndDel(audioframe);
  1414. }
  1415. else{
  1416. bGetAudio = m_audioqueue->GetAudio(audioframe);
  1417. }
  1418. if (bGetAudio){
  1419. //if (m_bIsAudioNsOn && NULL != m_pAudioNsObj){
  1420. // char pOutAudio[MAX_PATH*3] = {0};
  1421. // int iRet = m_pAudioNsObj->NsProcess(pOutAudio,audioframe->framesize, audioframe->data, audioframe->framesize);
  1422. // if (0 == iRet){
  1423. // memset(m_pAudioBufferTmp+nAudioBufferLens, 0, audioframe->framesize);
  1424. // memcpy(m_pAudioBufferTmp+nAudioBufferLens, pOutAudio, audioframe->framesize);
  1425. // }
  1426. //}
  1427. nAudioBufferLens += audioframe->framesize;
  1428. nAudioFromQueNum++;
  1429. if ((0 == nAudioFromQueNum%100) && (0 != audioframe->iseriesnumber)){
  1430. //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);
  1431. }
  1432. }
  1433. else{
  1434. //m_pHostApi->Debug("get audio from m_audioqueue failed.");
  1435. FSleep(2);
  1436. }
  1437. }
  1438. }
  1439. else if ((nAudioLens>0)&&(nRemoteAudioLens>0))//混音
  1440. {
  1441. if(nRemoteAudioBufferLens < nPerSecBufferLens) //收到的远端音频不够1秒
  1442. {
  1443. //取远端音频
  1444. BOOL bGetRemoteAudio = FALSE;
  1445. audio_frame*RemoteAudio = new audio_frame;
  1446. RemoteAudio->data = pRemoteAudioBufTmp+nRemoteAudioBufferLens;
  1447. if (nRemoteAudioLens < MAX_AUDIOQUEUE_LENS){
  1448. bGetRemoteAudio = m_remoteaudioqueue->GetAudioAndDel(RemoteAudio);
  1449. }
  1450. else{
  1451. bGetRemoteAudio = m_remoteaudioqueue->GetAudio(RemoteAudio);
  1452. }
  1453. if (bGetRemoteAudio){
  1454. //if (m_bIsAudioNsOn && NULL != m_pAudioNsObj){
  1455. // char pOutAudio[MAX_PATH*3] = {0};
  1456. // int iRet = m_pAudioNsObj->NsProcess(pOutAudio,RemoteAudio->framesize, RemoteAudio->data, RemoteAudio->framesize);
  1457. // if (0 == iRet){
  1458. // memset(pRemoteAudioBufTmp + nRemoteAudioBufferLens, 0, RemoteAudio->framesize);
  1459. // memcpy(pRemoteAudioBufTmp + nRemoteAudioBufferLens, pOutAudio, RemoteAudio->framesize);
  1460. // }
  1461. //}
  1462. nRemoteAudioBufferLens += RemoteAudio->framesize;
  1463. nRemoteAudioNum++;
  1464. }
  1465. else {
  1466. //m_pHostApi->Debug("get audio from m_remoteaudioqueue failed.");
  1467. FSleep(2);
  1468. }
  1469. delete RemoteAudio;
  1470. }
  1471. int ncurtime = timeGetTime() - nRecordStartTime - nSubTitleTime;
  1472. if(( nAudioBufferLens< nPerSecBufferLens)&&((ncurtime-nAudioFromQueNum*1000/nAudioFps)>=(1000/nAudioFps)))
  1473. {
  1474. //本地音频
  1475. audioframe->data = m_pAudioBufferTmp+nAudioBufferLens;
  1476. BOOL bGetAudio = FALSE;
  1477. if (nAudioLens < MAX_AUDIOQUEUE_LENS){
  1478. bGetAudio = m_audioqueue->GetAudioAndDel(audioframe);
  1479. }
  1480. else{
  1481. bGetAudio = m_audioqueue->GetAudio(audioframe);
  1482. }
  1483. if (bGetAudio){
  1484. if (CAPTURE_CLOCK == audioframe->samplespersec){
  1485. //if (m_bIsAudioNsOn && NULL != m_pAudioNsObj){
  1486. // char pOutAudio[MAX_PATH*3] = {0};
  1487. // int iRet = m_pAudioNsObj->NsProcess(pOutAudio,audioframe->framesize, audioframe->data, audioframe->framesize);
  1488. // if (0 == iRet){
  1489. // memset(m_pAudioBufferTmp + nAudioBufferLens, 0, audioframe->framesize);
  1490. // memcpy(m_pAudioBufferTmp + nAudioBufferLens, pOutAudio, audioframe->framesize);
  1491. // }
  1492. //}
  1493. nAudioBufferLens += audioframe->framesize;
  1494. }
  1495. else if(CAPTURE_CLOCK*2 == audioframe->samplespersec){
  1496. spx_uint32_t uInLen = audioframe->framesize;
  1497. spx_uint32_t uOutLen = 512;
  1498. spx_int16_t pOutAudio[512] = {0};
  1499. int iRet = speex_resampler_process_int(m_16kto8kst, 0, (spx_int16_t*)audioframe->data, &uInLen, pOutAudio, &uOutLen);
  1500. if (RESAMPLER_ERR_SUCCESS == iRet){
  1501. memset(m_pAudioBufferTmp+nAudioBufferLens, 0, audioframe->framesize);
  1502. memcpy(m_pAudioBufferTmp+nAudioBufferLens, pOutAudio, uOutLen);
  1503. nAudioBufferLens += uOutLen;
  1504. }
  1505. //m_pHostApi->Debug("16kto8k inLen is %d, outLen is %d.",audioframe->framesize, uOutLen);
  1506. }
  1507. else{
  1508. m_pHostApi->Debug("not support audio sample rate.");
  1509. m_pHostApi->OnRecordFailed();
  1510. }
  1511. nAudioFromQueNum++;
  1512. if ((0 == nAudioFromQueNum%100) && (0 != audioframe->iseriesnumber)){
  1513. //m_pHostApi->Debug("not single side record,and current local audio queue len is %d audio series number is: %d.",nAudioLens, audioframe->iseriesnumber);
  1514. }
  1515. }
  1516. else{
  1517. //m_pHostApi->Debug("get audio from m_audioqueue failed.");
  1518. FSleep(2);
  1519. }
  1520. }
  1521. //混音
  1522. if ((nRemoteAudioBufferLens==nPerSecBufferLens)&&(nAudioBufferLens == nPerSecBufferLens))
  1523. {
  1524. if(ePhone2Pad == m_eRecordType){
  1525. MixtureAudio(nAudioBufferLens,m_pAudioBufferTmp,pRemoteAudioBufTmp);
  1526. }
  1527. else{//可视远程双录场景,本地和坐席端分别录制到左右声道
  1528. uint32_t uOutlen = BindPCMAudioData(m_iAudioBufferSize, m_pAudioBufferTmp, nAudioBufferLens, pRemoteAudioBufTmp, nRemoteAudioBufferLens, audioframe->bitspersample/8, eLocalLeft);
  1529. //m_pHostApi->Debug("double channel buffer length is %d, local buffer len is %d, remote buffer len is %d.",m_iAudioBufferSize, nAudioBufferLens, nRemoteAudioBufferLens);
  1530. nAudioBufferLens = uOutlen;
  1531. }
  1532. }
  1533. }
  1534. else if((nAudioLens<=0)&&(nRemoteAudioLens>0)) //本地端无数据不混音,双录语音播报场景 add by clp 20191104
  1535. {
  1536. if (nAudioBufferLens < nPerSecBufferLens)
  1537. { //取出音频
  1538. audioframe->data = m_pAudioBufferTmp+nAudioBufferLens;
  1539. BOOL bGetAudio = FALSE;
  1540. if (nAudioLens < MAX_AUDIOQUEUE_LENS)
  1541. {
  1542. bGetAudio = m_remoteaudioqueue->GetAudioAndDel(audioframe);
  1543. }
  1544. else
  1545. {
  1546. bGetAudio = m_remoteaudioqueue->GetAudio(audioframe);
  1547. }
  1548. if (bGetAudio)
  1549. {
  1550. nAudioBufferLens+=audioframe->framesize;
  1551. nAudioFromQueNum++;
  1552. }
  1553. else {
  1554. //m_pHostApi->Debug("get audio from m_remoteaudioqueue failed.");
  1555. FSleep(2);
  1556. }
  1557. }
  1558. }
  1559. else {
  1560. //m_pHostApi->Debug("get audio length from m_remoteaudioqueue and m_audioqueue failed.");
  1561. FSleep(2);
  1562. }
  1563. }
  1564. //录制音频
  1565. //计算经过的时间,计算已经经过几帧时间
  1566. nRecordTime = timeGetTime() - nRecordStartTime - nSubTitleTime;
  1567. nAudioNum = (DWORD)((double) nRecordTime /1000.0 );
  1568. if ((nAudioNum > noldAudioNum)/*&&(noldvideoNum >= (m_nFps*(noldAudioNum+1)))*/&&(nAudioBufferLens >= nPerSecBufferLens))
  1569. {
  1570. if (m_pWriter->ReceiveAudioData((BYTE*)m_pAudioBufferTmp,nAudioBufferLens))
  1571. {
  1572. noldAudioNum++;
  1573. nAudioBufferLens = 0;
  1574. nRemoteAudioBufferLens = 0;
  1575. memset(m_pAudioBufferTmp,0,m_iAudioBufferSize);
  1576. memset(pRemoteAudioBufTmp,0,nPerSecBufferLens);
  1577. }
  1578. else
  1579. {
  1580. m_pHostApi->Debug("音频流写入失败!");
  1581. *m_bResult = FALSE;
  1582. // add by ly at 20160729
  1583. m_bStopRecord = TRUE;
  1584. m_pHostApi->OnRecordFailed();
  1585. break;
  1586. }
  1587. }
  1588. //计算文件大小,如果文件大小大于10分钟则建立新文件记录
  1589. nRecordTime = timeGetTime() - nRecordStartTime - nSubTitleTime;
  1590. if (!m_bWholeSection)
  1591. {
  1592. if((nRecordTime > WMVREC_MAX_FILE)||((m_bCloseVideo||m_bReNameVideo)&&m_bSessionManage))
  1593. {
  1594. m_pWriter->End();
  1595. m_pHostApi->OnASectionFinished(m_WmvFileName); // add by ly at 2017/03/25
  1596. m_bStopRecord = FALSE;
  1597. bInitWmvParam = FALSE;
  1598. nRecordTime = 0; //本段视频持续时间
  1599. nRecordStartTime = timeGetTime(); //本段录像开始时间
  1600. nTimeExpended = 0; //录制经过的时间
  1601. nVideoNum = 0; //当前时间的视频数
  1602. noldvideoNum = 0; //已经录制的视频数
  1603. nAudioNum = 0; //当前时间的音频数
  1604. noldAudioNum = 0; //已经录制的音频数
  1605. nAudioFromQueNum = 0; //从音频队列中取出的音频数量
  1606. nAudioBufferLens = 0; //音频录制拼接BUFFER的当前长度
  1607. nRemoteAudioBufferLens = 0; //远端音频拼接BUFFER的当前长度
  1608. nRemoteAudioNum=0; //远端音频数量
  1609. nSubTitleTime = 0; //本段视频添加字幕耗时
  1610. memset(videoframe->data,0,nVideoFrameSize);
  1611. memset(m_pAudioBufferTmp,0,m_iAudioBufferSize);
  1612. memset(pRemoteAudioBufTmp,0,nPerSecBufferLens);
  1613. if (m_bSessionManage)
  1614. {
  1615. //sessionid切换,重命名录像文件
  1616. if (m_bReNameVideo)
  1617. {
  1618. if (WmvFileSerialNum>0)//如果当前文件序号大于0,说明前一个文件与当前文件是同一个SESSION,需要修改前一个文件名+END,否则直接改当前文件的文件名
  1619. {
  1620. //sessionid切换,前一个文件需要加上END
  1621. char m_NewFileName[MAX_PATH];
  1622. char m_OldFileName[MAX_PATH];
  1623. string FileName(m_WmvFileName);
  1624. string name = FileName.substr(0,FileName.find_first_of('_')+1);
  1625. //m_pHostApi->Debug("renamefile1 name = %s",name.c_str());
  1626. //前一个文件名
  1627. sprintf_s(m_OldFileName,MAX_PATH,"%s%d.wmv",name.c_str(),WmvFileSerialNum-1);
  1628. sprintf_s(m_NewFileName,MAX_PATH,"%s%d_end.wmv",name.c_str(),WmvFileSerialNum-1);
  1629. m_pHostApi->Debug("renamefile1 from %s to %s",m_OldFileName,m_NewFileName);
  1630. ReNameFile(m_OldFileName,m_NewFileName);
  1631. }
  1632. //sessionid切换,当前文件需要重命名
  1633. char m_NewFileName[MAX_PATH];
  1634. WmvFileSerialNum = 0;
  1635. sprintf_s(m_NewFileName,MAX_PATH,"%s%s_%d.wmv",m_PathName,m_FileName,WmvFileSerialNum);
  1636. m_pHostApi->Debug("renamefile2 from %s to %s",m_WmvFileName,m_NewFileName);
  1637. if (ReNameFile(m_WmvFileName,m_NewFileName))
  1638. {
  1639. WmvFileSerialNum++;
  1640. }
  1641. m_bReNameVideo = FALSE;
  1642. }
  1643. else
  1644. {
  1645. //继续录像,删除当前文件的"END"
  1646. char m_NewFileName[MAX_PATH];
  1647. sprintf_s(m_NewFileName,MAX_PATH,"%s%s_%d.wmv",m_PathName,m_FileName,WmvFileSerialNum);
  1648. m_pHostApi->Debug("renamefile3 from %s to %s",m_WmvFileName,m_NewFileName);
  1649. ReNameFile(m_WmvFileName,m_NewFileName);
  1650. //WMV文件的序列号
  1651. WmvFileSerialNum++;
  1652. }
  1653. //新文件增加END
  1654. sprintf_s(m_WmvFileName,MAX_PATH,"%s%s_%d_end.wmv",m_PathName,m_FileName,WmvFileSerialNum);
  1655. m_pHostApi->Debug("generate new file name %s",m_WmvFileName);
  1656. m_bCloseVideo = FALSE;
  1657. }
  1658. else
  1659. {
  1660. //WMV文件的序列号
  1661. WmvFileSerialNum++;
  1662. sprintf_s(m_WmvFileName,MAX_PATH,"%s%s_%d.wmv",m_PathName,m_FileName,WmvFileSerialNum);
  1663. }
  1664. }
  1665. }
  1666. }
  1667. catch (...)
  1668. {
  1669. m_pHostApi->Debug("IPP copy error or stop record.");
  1670. Sleep(50);
  1671. continue;
  1672. }
  1673. }
  1674. if (pRemoteAudioBufTmp){
  1675. delete pRemoteAudioBufTmp;
  1676. pRemoteAudioBufTmp = NULL;
  1677. }
  1678. m_pHostApi->OnRecordFinished();
  1679. EndRecord();
  1680. if (pText)
  1681. {
  1682. delete pText;
  1683. pText = NULL;
  1684. }
  1685. return 0;
  1686. }
  1687. #ifdef RVC_OS_WIN
  1688. // 寻找某目录下与通配符匹配的文件 add by ly at 20170215
  1689. BOOL FindMatchedFile(LPCSTR sFindPath, LPCSTR sFindFileName, std::string &sMatchedFile)
  1690. {
  1691. char sPath[MAX_PATH];
  1692. memset(sPath, 0, MAX_PATH);
  1693. char sFormatFileName[MAX_PATH+2] = "*";
  1694. WIN32_FIND_DATA FindFileData;
  1695. HANDLE hFind;
  1696. strcpy(sFormatFileName, sFindPath);
  1697. if(sFindPath[strlen(sFindPath)-1] != SPLIT_SLASH)
  1698. {
  1699. strcat(sFormatFileName, SPLIT_SLASH_STR);
  1700. strcat(sFormatFileName, "*");
  1701. }
  1702. else
  1703. {
  1704. strcat(sFormatFileName, "*");
  1705. }
  1706. strcat(sFormatFileName, sFindFileName);
  1707. strcat(sFormatFileName, "*");
  1708. hFind = FindFirstFile(sFormatFileName, &FindFileData);
  1709. if (hFind == INVALID_HANDLE_VALUE)
  1710. {
  1711. sMatchedFile = "";
  1712. return FALSE;
  1713. }
  1714. else
  1715. {
  1716. sMatchedFile = FindFileData.cFileName;
  1717. FindClose(hFind);
  1718. }
  1719. return TRUE;
  1720. }
  1721. #else
  1722. BOOL FindMatchedFile(LPCSTR sFindPath, LPCSTR sFindFileName, std::string& sMatchedFile)
  1723. {
  1724. DIR* pDir = NULL;
  1725. struct dirent* pFile = NULL;
  1726. BOOL result = false;
  1727. pDir = opendir(sFindPath);
  1728. if (pDir == NULL) return result;
  1729. while ((pFile = readdir(pDir)) != NULL) {
  1730. if (pFile->d_type & DT_DIR) {
  1731. //m_pHostApi->Debug("FindMatchedFile DIR name: %s.", pFile->d_name);
  1732. if (strcmp(pFile->d_name, ".") == 0
  1733. || strcmp(pFile->d_name, "..") == 0) continue;
  1734. char Path[256];
  1735. int len = strlen(sFindPath);
  1736. strncpy(Path, sFindPath, len + 1);
  1737. if (sFindPath[len - 1] != '/') strncat(Path, "/", 2);
  1738. strncat(Path, pFile->d_name, strlen(pFile->d_name) + 1);
  1739. //m_pHostApi->Debug("FindMatchedFile Path: %s.", Path);
  1740. result = FindMatchedFile(Path, sFindFileName, sMatchedFile);
  1741. if (result) {
  1742. break;
  1743. }
  1744. }
  1745. else {
  1746. //m_pHostApi->Debug("FindMatchedFile FILE name: %s.", pFile->d_name);
  1747. if (strstr(pFile->d_name, sFindFileName) != NULL) {
  1748. char Path[256];
  1749. int len = strlen(sFindPath);
  1750. strncpy(Path, sFindPath, len + 1);
  1751. if (sFindPath[len - 1] != '/') strncat(Path, "/", 2);
  1752. strncat(Path, pFile->d_name, strlen(pFile->d_name) + 1);
  1753. //m_pHostApi->Debug("FindMatchedFile Finded: %s.", Path);
  1754. sMatchedFile = Path;
  1755. result = true;
  1756. break;
  1757. }
  1758. }
  1759. }
  1760. closedir(pDir);
  1761. return result;
  1762. }
  1763. #endif
  1764. #ifdef RVC_OS_WIN
  1765. static void DeleteDumpFiles()
  1766. {
  1767. CSimpleStringA sourPath = ".\\";
  1768. if (sourPath.GetLength() > 0 && sourPath[sourPath.GetLength()-1] != '\\') {
  1769. sourPath += "\\";
  1770. }
  1771. char srcFilePath[MAX_PATH]={0};
  1772. WIN32_FIND_DATA FindFileData;
  1773. HANDLE hFind;
  1774. BOOL fFinished = FALSE;
  1775. sprintf_s(srcFilePath, MAX_PATH, "%s*.dmp", sourPath);
  1776. hFind = FindFirstFile(srcFilePath, &FindFileData);
  1777. if (INVALID_HANDLE_VALUE != hFind)
  1778. {
  1779. while (!fFinished)
  1780. {
  1781. if (FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes)
  1782. {
  1783. continue;
  1784. }
  1785. CSimpleStringA sourFileName = CSimpleStringA::Format("%s%s", (LPCSTR)sourPath, FindFileData.cFileName);
  1786. if(!DeleteFile((LPCSTR)sourFileName)) {
  1787. Dbg("Error Code %lu while delete %s ", GetLastError(), (LPCSTR)sourFileName);
  1788. }
  1789. else{
  1790. Dbg("Delete file %s success.", (LPCSTR)sourFileName);
  1791. }
  1792. if (!FindNextFile(hFind, &FindFileData))
  1793. {
  1794. if (GetLastError() == ERROR_NO_MORE_FILES)
  1795. {
  1796. fFinished = TRUE;
  1797. }
  1798. else
  1799. {
  1800. break;
  1801. }
  1802. }
  1803. }
  1804. FindClose(hFind);
  1805. }
  1806. }
  1807. static void wmvrecorddump_exception(PEXCEPTION_POINTERS ExceptionInfo, libwmvrecord_impl* pWmvRecord)
  1808. {
  1809. if (NULL != pWmvRecord){
  1810. pWmvRecord->EndRecord();
  1811. if (NULL != pWmvRecord->m_pHostApi){
  1812. pWmvRecord->m_pHostApi->Debug("VideoRecord Thread Exception.");
  1813. pWmvRecord->m_pHostApi->OnRecordEntityExcption();
  1814. }
  1815. }
  1816. DeleteDumpFiles();
  1817. char tmp[MAX_PATH] = {0};
  1818. HANDLE hDumpFile;
  1819. sprintf(tmp, ".\\wmvrecord_%d.dmp", GetCurrentProcessId());
  1820. hDumpFile = CreateFileA( tmp, GENERIC_READ | GENERIC_WRITE,
  1821. 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
  1822. if( ( hDumpFile != NULL ) && ( hDumpFile != INVALID_HANDLE_VALUE ) )
  1823. {
  1824. MINIDUMP_EXCEPTION_INFORMATION mdei;
  1825. MINIDUMP_TYPE mdt;
  1826. mdei.ThreadId = GetCurrentThreadId();
  1827. mdei.ExceptionPointers = ExceptionInfo;
  1828. mdei.ClientPointers = FALSE;
  1829. mdt = MiniDumpWithFullMemory;
  1830. MiniDumpWriteDump( GetCurrentProcess(), GetCurrentProcessId(),
  1831. hDumpFile, mdt, (ExceptionInfo != 0) ? &mdei : 0, 0, 0 );
  1832. CloseHandle( hDumpFile );
  1833. }
  1834. }
  1835. #endif
  1836. //视频录制线程
  1837. static void* VideoRecordThread(void* pParam)
  1838. {
  1839. libwmvrecord_impl* WmvRecord = (libwmvrecord_impl*)pParam;
  1840. int iRet = -1;
  1841. #ifdef _WIN32
  1842. __try
  1843. {
  1844. iRet = WmvRecord->VideoRecord();
  1845. }
  1846. __except(wmvrecorddump_exception(GetExceptionInformation(),WmvRecord), EXCEPTION_EXECUTE_HANDLER) {
  1847. }
  1848. #else
  1849. iRet = WmvRecord->VideoRecord();
  1850. #endif
  1851. return &iRet;
  1852. }
  1853. public:
  1854. //开始录制 ly@2018/06/05
  1855. BOOL StartWmvRecord(int fps,int videoquality,const SubtitleParam *subtitleParam,BOOL bWholeSection,
  1856. BOOL bSessionManage,const char *pathname,int pathlength,const char *wmvfilename,int filenamelength, Rvc_RecordAudioParam_t* pAudioParam)//开始记录
  1857. {
  1858. m_salestransqueue = NULL;
  1859. m_audioqueue = NULL;
  1860. m_localaudioqueue = new Clibaudioqueue((char *)m_audioqueuename);
  1861. m_salesaudioqueue = NULL;
  1862. if (strlen(m_salesaudioqueuename) != 0)
  1863. {
  1864. m_salesaudioqueue = new Clibaudioqueue((char*)m_salesaudioqueuename);
  1865. }
  1866. m_videoqueue = NULL;
  1867. if (strlen(m_videoqueuename) != 0)
  1868. {
  1869. m_videoqueue = new Clibvideoqueue((char*)m_videoqueuename);
  1870. }
  1871. m_videoqueue2 = NULL;
  1872. if (strlen(m_videoqueue2name) != 0)
  1873. {
  1874. m_videoqueue2 = new Clibvideoqueue((char*)m_videoqueue2name);
  1875. }
  1876. m_remotevideoqueue = NULL;
  1877. if (strlen(m_remotevideoqueuename) != 0)
  1878. {
  1879. m_remotevideoqueue = new Clibvideoqueue((char*)m_remotevideoqueuename);
  1880. }
  1881. m_remoteaudioqueue = NULL;
  1882. if (strlen(m_remoteaudioqueuename) != 0)
  1883. {
  1884. m_remoteaudioqueue = new Clibaudioqueue((char*)m_remoteaudioqueuename);
  1885. }
  1886. memset(m_FileName,0,MAX_PATH);
  1887. memset(m_PathName,0,MAX_PATH);
  1888. if (filenamelength == 0){
  1889. SYSTEMTIME nowTime; // 系统时间结构体
  1890. GetLocalTimeRVC(nowTime);
  1891. sprintf_s(m_FileName,MAX_PATH,"%4d%02d%02d%02d%02d%02d",nowTime.wYear,nowTime.wMonth,nowTime.wDay,nowTime.wHour,nowTime.wMinute,nowTime.wSecond);
  1892. }
  1893. else{
  1894. memcpy(m_FileName,wmvfilename,filenamelength);
  1895. }
  1896. if (pathlength!=0){
  1897. memcpy(m_PathName,pathname,pathlength);
  1898. }
  1899. m_nFps = fps;
  1900. if (videoquality > 0 || videoquality <= 100){
  1901. m_videoquality = videoquality;
  1902. }
  1903. else{
  1904. m_pHostApi->Debug("warn: videoquality is illegal, will use default instead!");
  1905. m_videoquality = 75;
  1906. }
  1907. //if (audioOutBitRate < 8 || audioOutBitRate > 256){
  1908. // //m_pHostApi->Debug("warn: audioOutBitRate is illegal, will use default instead!");
  1909. // //m_audioOutBitRate = 8000;
  1910. //}
  1911. //else
  1912. //{
  1913. // m_audioOutBitRate = audioOutBitRate*1000;
  1914. //}
  1915. //远程双录双声道,输出码率是原来的2倍
  1916. //if (eStand2Agent == eRecordType || ePad2Agent == eRecordType){
  1917. // m_audioOutBitRate *= 2;
  1918. //}
  1919. memset(&m_SubtitleParam, 0, sizeof(SubtitleParam));
  1920. if(subtitleParam != NULL) // 接收字幕信息 add by ly
  1921. {
  1922. memcpy(&m_SubtitleParam, subtitleParam, sizeof(SubtitleParam));
  1923. }
  1924. m_bWholeSection = bWholeSection;
  1925. m_bSessionManage = bSessionManage;
  1926. m_eRecordType = pAudioParam->eRecordType;
  1927. m_eAudioType = pAudioParam->eOutPutType;
  1928. m_bIsAudioNsOn = pAudioParam->bIsNsOn;
  1929. m_iNsPolicy = pAudioParam->iNsPolicy;
  1930. m_bIsAudioTransOn = pAudioParam->bIsTransOn;
  1931. m_audioqueue = m_localaudioqueue;
  1932. if (m_salesaudioqueue && (eSingleSide == m_eRecordType || eStand2Agent == m_eRecordType)) //当大机进行双录拼接时使用本地音频队列会出现抢占导致取音频失败,此处选择使用销售音频队列
  1933. {
  1934. m_audioqueue = m_salesaudioqueue;
  1935. }
  1936. //Dbg("m_audioOutBitRate = %d.", m_audioOutBitRate);
  1937. 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");
  1938. //m_salestransqueue = new Clibaudiotransqueue(m_FileName);
  1939. if (m_bIsAudioTransOn){
  1940. m_salestransqueue = new Clibaudiotransqueue(m_FileName);
  1941. if (NULL != m_salestransqueue){
  1942. Dbg("get sales trans audio queue success, queue name is %s and addr is 0x%08x.", m_FileName, m_salestransqueue);
  1943. }
  1944. }
  1945. if (m_hRecordThread == NULL)
  1946. {
  1947. return StartRecord();
  1948. }
  1949. else
  1950. {
  1951. return FALSE;
  1952. }
  1953. }
  1954. BOOL StopWmvRecord() //退出
  1955. {
  1956. m_bStopRecord = TRUE;
  1957. sem_post(&m_semt);
  1958. if (0 == pthread_join(m_nRecordthreadId, NULL)) {
  1959. m_pHostApi->Debug("thread join video record thread %u success!", m_nRecordthreadId);
  1960. m_nRecordthreadId = 0;
  1961. }
  1962. else {
  1963. m_pHostApi->Debug("thread join video record thread failed!");
  1964. }
  1965. m_eRecordType = eSingleSide;
  1966. return TRUE;
  1967. }
  1968. void ChangeFilename(const char*newfilename)
  1969. {
  1970. if (!m_bWholeSection)
  1971. {
  1972. memset(m_FileName,0,MAX_PATH);
  1973. memcpy(m_FileName,newfilename,strlen(newfilename));
  1974. m_bReNameVideo = TRUE;
  1975. }
  1976. }
  1977. BOOL setRightWaterMark(char* strWaterMark, size_t ulen)
  1978. {
  1979. BOOL bRet = FALSE;
  1980. memset(m_SubtitleParam.strRightAgentInfo,0,MAX_PATH);
  1981. if (ulen < MAX_PATH){
  1982. memcpy(m_SubtitleParam.strRightAgentInfo,strWaterMark,ulen);
  1983. bRet = TRUE;
  1984. }
  1985. return bRet;
  1986. }
  1987. BOOL StopAudioStreamEnTransQueue() //停止音频包入传输队列
  1988. {
  1989. m_bIsAudioTransOn = false;
  1990. return TRUE;
  1991. }
  1992. void Debug(const char* fmt, ...)
  1993. {
  1994. va_list arg;
  1995. va_start(arg, fmt);
  1996. char buf[512];
  1997. int n = _vscprintf(fmt, arg);
  1998. if (n+1 > 512)
  1999. n = 511;
  2000. vsnprintf(buf, n+1, fmt, arg);
  2001. m_pHostApi->Debug(buf);
  2002. va_end(arg);
  2003. }
  2004. BOOL m_bCloseVideo; //是否关闭当前录像
  2005. BOOL m_bReNameVideo; //是否重命名录像文件
  2006. };
  2007. // 这是已导出类的构造函数。
  2008. // 有关类定义的信息,请参阅 libwmvrecord.h
  2009. Clibwmvrecord::Clibwmvrecord(BOOL *pResult, CWmvHostApi *pHostAPI,LPCTSTR audioqueuename,LPCTSTR videoqueuename,LPCTSTR videoqueue2name,LPCTSTR salesaudioqueuename,LPCTSTR remotevideoqueuename,LPCTSTR remoteaudioqueuename)
  2010. {
  2011. m_pImpl = new libwmvrecord_impl(pResult,pHostAPI,audioqueuename,videoqueuename,videoqueue2name,salesaudioqueuename,remotevideoqueuename,remoteaudioqueuename);
  2012. return;
  2013. }
  2014. Clibwmvrecord::~Clibwmvrecord()
  2015. {
  2016. delete m_pImpl;
  2017. return;
  2018. }
  2019. // ly@2018/06/05
  2020. BOOL Clibwmvrecord::StartWmvRecord(
  2021. int fps,
  2022. int videoquality,
  2023. Rvc_RecordAudioParam_t* pAudioParam,
  2024. SubtitleParam *subtitleParam /* = NULL */,
  2025. BOOL bWholeSection /* = FALSE */,
  2026. BOOL bSessionManage /* = FALSE */,
  2027. const char *pathname /* = NULL */,
  2028. int pathlength /* = 0 */,
  2029. const char *wmvfilename /* = NULL */,
  2030. int filenamelength /* = 0 */
  2031. )
  2032. {
  2033. return m_pImpl->StartWmvRecord(fps,videoquality,subtitleParam,bWholeSection,bSessionManage,pathname,pathlength,wmvfilename,filenamelength,pAudioParam);
  2034. }
  2035. BOOL Clibwmvrecord::StopWmvRecord() //退出
  2036. {
  2037. return m_pImpl->StopWmvRecord();
  2038. }
  2039. void Clibwmvrecord::CloseVideoFile() //结束当前录像
  2040. {
  2041. m_pImpl->m_bCloseVideo = TRUE;
  2042. }
  2043. BOOL Clibwmvrecord::ReNameVideoFile(const char*newfilename) //session变化,修改当前录像文件名
  2044. {
  2045. m_pImpl->ChangeFilename(newfilename);
  2046. //m_pImpl->m_bReNameVideo = TRUE;
  2047. return TRUE;
  2048. }
  2049. BOOL Clibwmvrecord::SetRightVideoWaterMark(char* strWaterMark)
  2050. {
  2051. BOOL bRet = FALSE;
  2052. if (NULL == strWaterMark){
  2053. return bRet;
  2054. }
  2055. size_t ulen = strlen(strWaterMark);
  2056. m_pImpl->setRightWaterMark(strWaterMark, ulen);
  2057. return bRet;
  2058. }
  2059. BOOL Clibwmvrecord::StopAudioEnTransQueue()
  2060. {
  2061. return m_pImpl->StopAudioStreamEnTransQueue();
  2062. }