libvideorecord_impl.cpp 63 KB

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