libvideorecord_impl.cpp 64 KB

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