libvideorecord_impl.cpp 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382
  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. if (m_audioqueue) {
  590. m_audioqueue->ClearAudioQueue();
  591. }
  592. nRecordStartTime = timeGetTime();//本段录像开始时间
  593. }
  594. //控制音视频的同步,精确控制录制频率,确保暂停足够时间
  595. m_iRecordedTime = timeGetTime() - nRecordStartTime - m_iSubTitleTime;
  596. MediaSynchronous();
  597. //计算经过的时间,计算已经经过几帧时间
  598. m_iRecordedTime = timeGetTime() - nRecordStartTime - m_iSubTitleTime;
  599. int nVideoNum = (int)(((double)m_iRecordedTime / 1000.0) * (double)m_nFps);
  600. //如果是第1帧,或者又经过了1帧的时间,则录制视频,对视频进行压缩
  601. if (((nVideoNum == 0) && (m_iRecordVideoNum == 0)) || (nVideoNum > m_iRecordVideoNum)) {
  602. int iRet = WriteVideoFrame();
  603. if (-1 == iRet) {
  604. break;
  605. }
  606. }
  607. if (false == GetRecordAudioFrame()) {
  608. if (++iGetAudioFailedTimes > 200) {
  609. LogFailedEvent(eLocalAudioGetFailed, "连续5s获取本地音频失败");
  610. break;
  611. }
  612. }
  613. else {
  614. iGetAudioFailedTimes = 0;
  615. }
  616. //录制音频,计算经过的时间,计算已经经过几帧时间
  617. m_iRecordedTime = timeGetTime() - nRecordStartTime - m_iSubTitleTime;
  618. int nAudioNum = (int)((double)m_iRecordedTime / 1000.0);
  619. if ((nAudioNum > m_iRecordAudioNum) && (m_iAudioBufferLen >= m_iAudioPerSecBufLen)) {
  620. int iRet = WriteAudioFrame();
  621. if (-1 == iRet) {
  622. break;
  623. }
  624. }
  625. //计算文件大小,如果文件大小大于10分钟则建立新文件记录
  626. m_iRecordedTime = timeGetTime() - nRecordStartTime - m_iSubTitleTime;
  627. if (!m_bWholeSection) {
  628. if ((m_iRecordedTime > REC_MAX_FILE) || ((m_bCloseVideo || m_bReNameVideo) && m_bSessionManage)) {
  629. EndRecordWrite();
  630. bInitRecordParam = false;
  631. nRecordStartTime = timeGetTime(); //本段录像开始时间
  632. ReSetRecordParams();
  633. ReNameVideo();
  634. }
  635. }
  636. if (m_bPauseRecord) {
  637. m_pHostApi->Debug(RECORD_LOG_DEBUG, "reset media queue.");
  638. ResetMediaQueue();
  639. }
  640. }
  641. else{
  642. if (eStand2Agent == m_eRecordType) {
  643. HandleLeftAudioData();
  644. }
  645. m_pHostApi->Debug(RECORD_LOG_INFO, "stop video record..");
  646. break;
  647. }
  648. }
  649. if (bInitRecordParam) {
  650. m_pHostApi->OnRecordFinished();
  651. EndRecord();
  652. m_pHostApi->OnASectionFinished(m_VideoFileName, m_iFileSerialNum, true);
  653. }
  654. else {
  655. m_pHostApi->Debug(RECORD_LOG_INFO, "not Init Record Param, Exit.");
  656. }
  657. return 0;
  658. }
  659. int libvideorecord_impl::HandleLeftAudioData()
  660. {
  661. int nAudioLens = m_audioqueue->GetAudioLens();
  662. int nRemoteAudioLens = m_remote_audioqueue->GetAudioLens();
  663. int iRet = -1;
  664. while (nAudioLens > 0){
  665. GetRecordAudioFrame();
  666. if (m_iAudioBufferLen >= m_iAudioPerSecBufLen){
  667. iRet = WriteAudioFrame();
  668. if (-1 == iRet) {
  669. break;
  670. }
  671. }
  672. nAudioLens = m_audioqueue->GetAudioLens();
  673. nRemoteAudioLens = m_remote_audioqueue->GetAudioLens();
  674. m_pHostApi->Debug(RECORD_LOG_DEBUG, "HandleLeftAudioData local audio length is %d, and remote audio length is %d.", nAudioLens, nRemoteAudioLens);
  675. }
  676. if (m_iAudioBufferLen > 0) {
  677. iRet = WriteAudioFrame();
  678. }
  679. return iRet;
  680. }
  681. int libvideorecord_impl::GetDestTypeVideoFrameSize(int& iWidth, int& iHeight, eRvcRecordType eRecordType)
  682. {
  683. int iSize = 0;
  684. if (eSingleSide == eRecordType) {
  685. iSize = GetSingleSideVideoFrameSize(iWidth, iHeight);
  686. }
  687. else {
  688. iSize = GetDoubleSideVideoFrameSize(iWidth, iHeight);
  689. }
  690. return iSize;
  691. }
  692. bool libvideorecord_impl::GetRecordVideoFrameSize()
  693. {
  694. bool bRet = false;
  695. m_pHostApi->Debug(RECORD_LOG_DEBUG, "get video framesize, and record type is %s.", record_type_table[m_eRecordType]);
  696. for (int i = 0; i < RECORD_FAILED_MAX_TIMES; i++) {
  697. m_iVideoFrameSize = GetDestTypeVideoFrameSize(m_iWidth, m_iHeight, m_eRecordType);
  698. if (m_iVideoFrameSize <= 0) {
  699. if (Rvc_Timeout(50)) {
  700. if (0 != i && 0 == i % (RECORD_FAILED_MAX_TIMES / 10)) {
  701. m_pHostApi->Debug(RECORD_LOG_ERROR, "GetDestTypeVideoFrameSize failed %d times.", i);
  702. }
  703. }
  704. else {
  705. m_pHostApi->Debug(RECORD_LOG_INFO, "GetRecordVideoFrameSize return, user stop.");
  706. break;
  707. }
  708. }
  709. else {
  710. if (i > 0) {
  711. m_pHostApi->Debug(RECORD_LOG_DEBUG, "get video framesize success, and failed times is %d.", i);
  712. }
  713. bRet = true;
  714. break;
  715. }
  716. }
  717. //if (bRet) {
  718. // m_pHostApi->Debug(RECORD_LOG_DEBUG, "get video framesize is %d, and width is %d, height is %d.", m_iVideoFrameSize, m_iWidth, m_iHeight);
  719. //}
  720. return bRet;
  721. }
  722. bool libvideorecord_impl::GetRecordAudioFrameSize()
  723. {
  724. bool bRet = false;
  725. for (int i = 0; i < RECORD_FAILED_MAX_TIMES; i++) {
  726. m_iAudioFrameSize = m_audioqueue->GetFrameSize();
  727. if (m_iAudioFrameSize <= 0) {
  728. if (Rvc_Timeout(10)) {
  729. if (0 != i && 0 == i % (RECORD_FAILED_MAX_TIMES / 10)) {
  730. m_pHostApi->Debug(RECORD_LOG_ERROR, "get local audio frame size failed %d times.", i);
  731. }
  732. }
  733. else {
  734. m_pHostApi->Debug(RECORD_LOG_INFO, "GetRecordAudioFrameSize return, user stop.");
  735. break;
  736. }
  737. }
  738. else {
  739. if (i > 0) {
  740. m_pHostApi->Debug(RECORD_LOG_DEBUG, "get audio framesize success, and failed times is %d.", i);
  741. }
  742. bRet = true;
  743. break;
  744. }
  745. }
  746. //if (bRet) {
  747. // m_pHostApi->Debug(RECORD_LOG_DEBUG, "local audio frame size = %d.", m_iAudioFrameSize);
  748. //}
  749. return bRet;
  750. }
  751. bool libvideorecord_impl::GetRecordAudioParams(audio_frame** paudio)
  752. {
  753. bool bRet = false;
  754. (*paudio)->data = new char[m_iAudioFrameSize];
  755. for (int i = 0; i < RECORD_FAILED_MAX_TIMES; i++) {
  756. if (!m_audioqueue->GetAudio(*paudio)) {
  757. if (Rvc_Timeout(10)) {
  758. if (0 != i && 0 == i % (RECORD_FAILED_MAX_TIMES / 10)) {
  759. m_pHostApi->Debug(RECORD_LOG_ERROR, "GetAudio failed %d times.", i);
  760. }
  761. }
  762. else {
  763. m_pHostApi->Debug(RECORD_LOG_INFO, "GetAudio return, user stop.");
  764. break;
  765. }
  766. }
  767. else {
  768. bRet = true;
  769. break;
  770. }
  771. }
  772. RECORD_SAFE_DELETE((*paudio)->data);
  773. //if (bRet) {
  774. // 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);
  775. //}
  776. return bRet;
  777. }
  778. bool libvideorecord_impl::InitRecordVideoFrameBuffer()
  779. {
  780. bool bRet = false;
  781. if (m_videoframe->data == NULL) {
  782. m_videoframe->data = new unsigned char[m_iVideoFrameSize];
  783. m_videoframe->framesize = m_iVideoFrameSize;
  784. m_videoframe->height = m_iHeight;
  785. m_videoframe->width = m_iWidth;
  786. m_videoframe->format = VIDEO_FORMAT_RGB24;
  787. bRet = true;
  788. }
  789. return bRet;
  790. }
  791. bool libvideorecord_impl::CalcuRecordAudioParams(audio_frame* paudio)
  792. {
  793. bool bRet = false;
  794. //计算每秒音频的长度
  795. m_iAudioPerSecBufLen = paudio->samplespersec * paudio->bitspersample / 8;
  796. m_iRecordAudioBufSize = m_iAudioPerSecBufLen;
  797. //m_pHostApi->Debug(RECORD_LOG_DEBUG, "m_iAudioPerSecBufLen=%d", m_iAudioPerSecBufLen);
  798. //计算音频帧的帧频,
  799. m_iLocalAudioFps = m_iAudioPerSecBufLen / paudio->framesize;
  800. //m_pHostApi->Debug(RECORD_LOG_DEBUG, "音频帧的帧频 m_iLocalAudioFps=%d", m_iLocalAudioFps);
  801. if (eSingleSide != m_eRecordType) {
  802. if (eStand2Agent == m_eRecordType) { //可视柜台渠道的双向录像,本地端和坐席端声音采用左右声道方式录制
  803. m_iRemoteAudioFps = 50;
  804. m_iRecordAudioBufSize = m_iAudioPerSecBufLen * 2;
  805. }
  806. else {
  807. m_iRemoteAudioFps = 0;
  808. }
  809. m_pHostApi->Debug(RECORD_LOG_DEBUG, "calc remote audio fps = %d", m_iRemoteAudioFps);
  810. }
  811. else {
  812. if (2 == m_iAudioChannels) {
  813. m_iRecordAudioBufSize = m_iAudioPerSecBufLen * 2;
  814. }
  815. }
  816. //m_pHostApi->Debug(RECORD_LOG_DEBUG, "audio fps = %d, record audio buffer size = %d.", m_iLocalAudioFps, m_iRecordAudioBufSize);
  817. bRet = true;
  818. return bRet;
  819. }
  820. bool libvideorecord_impl::InitRecordAudioBuffer()
  821. {
  822. bool bRet = false;
  823. if (m_pRecordAudioBuffer == NULL) {
  824. m_pRecordAudioBuffer = new char[m_iRecordAudioBufSize];
  825. memset(m_pRecordAudioBuffer, 0, m_iRecordAudioBufSize);
  826. }
  827. if (eSingleSide != m_eRecordType) {
  828. if (m_pRemoteAudioBuffer == NULL) {
  829. m_pRemoteAudioBuffer = new char[m_iAudioPerSecBufLen];
  830. memset(m_pRemoteAudioBuffer, 0, m_iAudioPerSecBufLen);
  831. }
  832. }
  833. bRet = true;
  834. return bRet;
  835. }
  836. int libvideorecord_impl::GetSingleSideVideoFrameSize(int& iWidth, int& iHeight)
  837. {
  838. int iSize = 0;
  839. if (m_env_videoqueue == NULL) {
  840. m_pHostApi->Debug(RECORD_LOG_ERROR, "本地视频队列未初始化,不存在!");
  841. return iSize;
  842. }
  843. int env_width = 0;
  844. int env_height = 0;
  845. if (m_opt_videoqueue) {
  846. int opt_width = 0;
  847. int opt_height = 0;
  848. iSize = m_env_videoqueue->GetFrameSize(env_width, env_height);
  849. if (iSize) {
  850. (env_width > env_height) ? (iWidth = iHeight = env_width) : (iWidth = iHeight = env_height);
  851. }
  852. else {
  853. iSize = m_opt_videoqueue->GetFrameSize(opt_width, opt_height);
  854. (opt_width > opt_height) ? (iWidth = iHeight = opt_width) : (iWidth = iHeight = opt_height);
  855. }
  856. if (!m_SubtitleParam.bSubtitle || !m_SubtitleParam.bSubtitleSection) {
  857. iSize = iWidth * iHeight * 3;
  858. }
  859. else {
  860. iHeight += 50;
  861. iSize = iWidth * iHeight * 3;
  862. }
  863. }
  864. else {
  865. if (!m_SubtitleParam.bSubtitle || !m_SubtitleParam.bSubtitleSection) {
  866. iSize = m_env_videoqueue->GetFrameSize(iWidth, iHeight);
  867. }
  868. else
  869. {
  870. iSize = m_env_videoqueue->GetFrameSize(env_width, env_height);
  871. iWidth = env_width;
  872. iHeight = env_height + 50;
  873. iSize = iWidth * iHeight * 3;
  874. }
  875. }
  876. return iSize;
  877. }
  878. int libvideorecord_impl::GetDoubleSideVideoFrameSize(int& iWidth, int& iHeight)
  879. {
  880. int iSize = 0;
  881. if (m_env_videoqueue == NULL) {
  882. m_pHostApi->Debug(RECORD_LOG_ERROR, "本地视频队列未初始化,不存在!");
  883. return iSize;
  884. }
  885. //如果远端队列不为空,且录制双向视频
  886. if (m_remote_videoqueue) {
  887. if (eStand2Agent == m_eRecordType) //大机以终端的视频大小确定画布,终端为640*360,远端固定为320*240,则画布为640*640+320*240 = 960*640
  888. {
  889. //计算画布左边部分的分辨率,640*640
  890. int env_width = 0;
  891. int env_height = 0;
  892. int opt_width = 0;
  893. int opt_height = 0;
  894. iSize = m_env_videoqueue->GetFrameSize(env_width, env_height);
  895. if (iSize){
  896. (env_width > env_height) ? (iWidth = iHeight = env_width) : (iWidth = iHeight = env_height);
  897. }
  898. else{
  899. iSize = m_opt_videoqueue->GetFrameSize(opt_width, opt_height);
  900. (opt_width > opt_height) ? (iWidth = iHeight = opt_width) : (iWidth = iHeight = opt_height);
  901. }
  902. if (iWidth > 0 && iHeight > 0) {
  903. //远端视频320*240
  904. iWidth = iWidth + 480;
  905. if (!m_SubtitleParam.bSubtitle || !m_SubtitleParam.bSubtitleSection) {
  906. iSize = iWidth * iHeight * 3;
  907. }
  908. else {
  909. iHeight += 50;
  910. iSize = iWidth * iHeight * 3;
  911. }
  912. if (iSize > 0) {
  913. int remote_width = 0;
  914. int remote_height = 0;
  915. int iRemoteSize = m_remote_videoqueue->GetFrameSize(remote_width, remote_height);
  916. if (0 == iRemoteSize) {
  917. //m_pHostApi->Debug(RECORD_LOG_ERROR, "get remote video frame size failed!");
  918. iSize = 0;
  919. }
  920. }
  921. }
  922. }
  923. }
  924. return iSize;
  925. }
  926. //从指定队列读取video
  927. bool libvideorecord_impl::GetVideoFrame(video_frame* Video, int flags, Clibvideoqueue* queue)
  928. {
  929. return queue->GetVideo2(Video, flags);
  930. }
  931. int libvideorecord_impl::GetDestTypeVideoFrameData(videoq_frame* Video, int flags, const bool bSwitchCam, int iInitCam, eRvcRecordType eRecordType)
  932. {
  933. int iRet = 0;
  934. if (m_opt_videoqueue) {
  935. iRet = GetDoubleCameraVideoFrameData(Video, flags, bSwitchCam, iInitCam, eRecordType);
  936. }
  937. else {
  938. iRet = GetSingleCameraVideoFrameData(Video, flags, eRecordType);
  939. }
  940. return iRet;
  941. }
  942. int libvideorecord_impl::GetSingleCameraVideoFrameData(videoq_frame* Video, int flags, eRvcRecordType eRecordType)
  943. {
  944. int iRet = 0;
  945. if (eSingleSide == m_eRecordType) {
  946. if (!m_SubtitleParam.bSubtitle || !m_SubtitleParam.bSubtitleSection){
  947. return m_env_videoqueue->GetVideo(Video, flags) ? 0 : -1;
  948. }
  949. else{
  950. bool bRslt = false;
  951. int width = 0;
  952. int height = 0;
  953. m_env_videoqueue->GetFrameSize(width, height);
  954. memset(Video->data, 0, Video->framesize);
  955. videoq_frame* tmp_frm = new videoq_frame;
  956. tmp_frm->data = Video->data + Video->width * 30 * 3;
  957. bRslt = m_env_videoqueue->GetVideo(tmp_frm, flags);
  958. if (!bRslt){
  959. delete tmp_frm;
  960. m_pHostApi->Debug(RECORD_LOG_ERROR, "get env video fail!");
  961. return -1;
  962. }
  963. delete tmp_frm;
  964. }
  965. }
  966. else {
  967. m_pHostApi->Debug(RECORD_LOG_DEBUG, "Invalid record type, the device type cannot record both way video.");
  968. return -4;
  969. }
  970. return iRet;
  971. }
  972. //获取env摄像头数据失败返回-1
  973. //获取opt摄像头数据失败返回-2
  974. //获取远端视频数据失败返回-3
  975. int libvideorecord_impl::GetStand2SVideoFrameData(videoq_frame* Video, int flags, const bool bSwitchCam, int iInitCam, eRvcRecordType eRecordType)
  976. {
  977. if (!m_remote_videoqueue) {
  978. m_pHostApi->Debug(RECORD_LOG_ERROR, "remote video queue is null!");
  979. return -4;
  980. }
  981. //将大机摄像头和远端视频拼接到一张画布中
  982. memset(Video->data, 0, Video->framesize);
  983. bool bRslt = false;
  984. int width = 0;
  985. int height = 0;
  986. m_env_videoqueue->GetFrameSize(width, height);
  987. video_frame localtmp_frm = { 0 };
  988. localtmp_frm.data[0] = new unsigned char[640 * 640 * 3];
  989. localtmp_frm.linesize[0] = 640 * 3;
  990. localtmp_frm.format = m_videoframe->format;
  991. localtmp_frm.width = 640;
  992. localtmp_frm.height = 640;
  993. memset(localtmp_frm.data[0], 0, 640 * 640 * 3);
  994. int nActiveCam = iInitCam;
  995. if (bSwitchCam){
  996. nActiveCam = m_pHostApi->GetRecordCamera();
  997. }
  998. if (nActiveCam == 0) {// get env snapshot
  999. videoq_frame* tmp_frm = new videoq_frame;
  1000. tmp_frm->data = localtmp_frm.data[0] + width * (width - height) / 2 * 3; //在画布中加上偏移量,横向摄像头拼接
  1001. bRslt = m_env_videoqueue->GetVideo(tmp_frm, flags);
  1002. if (!bRslt) {
  1003. delete tmp_frm;
  1004. m_pHostApi->Debug(RECORD_LOG_ERROR, "stand device both way video, get env video fail!");
  1005. return -1;
  1006. }
  1007. //else {
  1008. // 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);
  1009. //}
  1010. delete tmp_frm;
  1011. }
  1012. else if (nActiveCam == 1) {// get opt snapshot
  1013. videoq_frame* tmp_frm = new videoq_frame;
  1014. tmp_frm->data = localtmp_frm.data[0] + (width - height) / 2 * 3; //在画布中加上偏移量
  1015. //竖向摄像头图像,拼接
  1016. bRslt = m_opt_videoqueue->GetVideo3(tmp_frm, flags);
  1017. if (!bRslt){
  1018. delete tmp_frm;
  1019. m_pHostApi->Debug(RECORD_LOG_ERROR, "stand device both way video, get opt video fail!");
  1020. return -2;
  1021. }
  1022. //else {
  1023. // 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);
  1024. //}
  1025. delete tmp_frm;
  1026. }
  1027. else{
  1028. m_pHostApi->Debug(RECORD_LOG_DEBUG, "nActiveCam value is not valid, and it's value is %d.", nActiveCam);
  1029. return -4;
  1030. }
  1031. {
  1032. //本地录像需要偏移量
  1033. int nOffset = 0;
  1034. if (!m_SubtitleParam.bSubtitle || !m_SubtitleParam.bSubtitleSection){
  1035. nOffset = 0;
  1036. }
  1037. else{
  1038. nOffset = 25 * Video->width * 3;
  1039. }
  1040. #ifdef _WIN32
  1041. for (int i = 0; i < localtmp_frm.height && i < Video->height; i++){
  1042. memcpy(Video->data + nOffset + i * Video->width * 3, localtmp_frm.data[0] + i * localtmp_frm.linesize[0], localtmp_frm.linesize[0]);
  1043. }
  1044. #else
  1045. int imaxheight = localtmp_frm.height > Video->height ? Video->height : localtmp_frm.height;
  1046. if (0 == nActiveCam) {
  1047. for (int i = imaxheight - 1, j = 0; i >= 0 && j < imaxheight; i--, j++){
  1048. memcpy(Video->data + nOffset + j * Video->width * 3, localtmp_frm.data[0] + i * localtmp_frm.linesize[0], localtmp_frm.linesize[0]);
  1049. }
  1050. }
  1051. else {
  1052. for (int i = 0; i < imaxheight; i++){
  1053. memcpy(Video->data + nOffset + i * Video->width * 3, localtmp_frm.data[0] + i * localtmp_frm.linesize[0], localtmp_frm.linesize[0]);
  1054. }
  1055. }
  1056. #endif
  1057. }
  1058. delete localtmp_frm.data[0];
  1059. //拼接远端视频,合成一个图像
  1060. {
  1061. int w = 0, h = 0;
  1062. int nsize = GetVideoFrameSize(w, h, m_remote_videoqueue);
  1063. video_frame tmp_frm = { 0 };
  1064. tmp_frm.data[0] = new unsigned char[nsize];
  1065. tmp_frm.linesize[0] = w * 3;
  1066. tmp_frm.format = m_videoframe->format;
  1067. tmp_frm.width = w;
  1068. tmp_frm.height = h;
  1069. bool bGetRemotevideo = false;
  1070. bGetRemotevideo = GetVideoFrame(&tmp_frm, 1, m_remote_videoqueue);
  1071. if (bGetRemotevideo){
  1072. #ifdef _WIN32
  1073. for (int i = 0; i < tmp_frm.height && i < Video->height; i++){
  1074. 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]);
  1075. }
  1076. #else
  1077. int imaxheight = tmp_frm.height > Video->height ? Video->height : tmp_frm.height;
  1078. for (int i = imaxheight - 1, j = 0; i >= 0 && j < imaxheight; i--, j++){
  1079. 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]);
  1080. }
  1081. #endif
  1082. }
  1083. delete tmp_frm.data[0];
  1084. if (!bGetRemotevideo){
  1085. m_pHostApi->Debug(RECORD_LOG_ERROR, "remote video queue get video failed!");
  1086. return -3;
  1087. }
  1088. //else {
  1089. // 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);
  1090. //}
  1091. }
  1092. return 0;
  1093. }
  1094. int libvideorecord_impl::GetSingleSideVideoFrameData(videoq_frame* Video, int flags, const bool bSwitchCam, int iInitCam)
  1095. {
  1096. bool bRslt = false;
  1097. int iwidth = 0;
  1098. int iheight = 0;
  1099. m_env_videoqueue->GetFrameSize(iwidth, iheight);
  1100. memset(Video->data, 0, Video->framesize);
  1101. int nActiveCam = iInitCam;
  1102. if (bSwitchCam) {
  1103. nActiveCam = m_pHostApi->GetRecordCamera();
  1104. }
  1105. if (0 == nActiveCam) { // get env snapshot
  1106. videoq_frame* tmp_frm = new videoq_frame;
  1107. if (!m_SubtitleParam.bSubtitle || !m_SubtitleParam.bSubtitleSection) {
  1108. tmp_frm->data = Video->data + Video->width * (iwidth - iheight) / 2 * 3;
  1109. }
  1110. else {
  1111. tmp_frm->data = Video->data + Video->width * ((iwidth - iheight) / 2 + 30) * 3;
  1112. }
  1113. bRslt = m_env_videoqueue->GetVideo(tmp_frm, flags);
  1114. delete tmp_frm;
  1115. if (!bRslt) {
  1116. m_pHostApi->Debug(RECORD_LOG_DEBUG, "get env video fail!");
  1117. return -1;
  1118. }
  1119. }
  1120. else if (1 == nActiveCam) { // get opt snapshot
  1121. videoq_frame* tmp_frm = new videoq_frame;
  1122. m_opt_videoqueue->GetFrameSize(iwidth, iheight);
  1123. if (!m_SubtitleParam.bSubtitle || !m_SubtitleParam.bSubtitleSection) {
  1124. tmp_frm->data = Video->data + (iheight - iwidth) / 2 * 3;
  1125. }
  1126. else {
  1127. tmp_frm->data = Video->data + (Video->width * 30 + (iheight - iwidth) / 2) * 3;
  1128. }
  1129. //横向摄像头图像,拼接
  1130. bRslt = m_opt_videoqueue->GetVideo3(tmp_frm, flags);
  1131. delete tmp_frm;
  1132. if (!bRslt) {
  1133. m_pHostApi->Debug(RECORD_LOG_DEBUG, "get opt video fail!");
  1134. return -2;
  1135. }
  1136. }
  1137. else {
  1138. m_pHostApi->Debug(RECORD_LOG_DEBUG, "nActiveCam value is not valid, and it's value is %d.", nActiveCam);
  1139. return -1;
  1140. }
  1141. return 0;
  1142. }
  1143. int libvideorecord_impl::GetDoubleCameraVideoFrameData(videoq_frame* Video, int flags, const bool bSwitchCam, int iInitCam, eRvcRecordType eRecordType)
  1144. {
  1145. int iRet = 0;
  1146. if (eStand2Agent == m_eRecordType) { //如果远端队列不为空,且录制双向视频
  1147. iRet = GetStand2SVideoFrameData(Video, flags, bSwitchCam, iInitCam, eRecordType);
  1148. }
  1149. else if(eSingleSide == eRecordType){
  1150. iRet = GetSingleSideVideoFrameData(Video, flags, bSwitchCam, iInitCam);
  1151. }
  1152. else {
  1153. m_pHostApi->Debug(RECORD_LOG_ERROR, "invalid record type.");
  1154. iRet = -1;
  1155. }
  1156. return iRet;
  1157. }
  1158. bool libvideorecord_impl::IsRecordingCamError(int iRecordingCam, eRvcRecordType eRecordType)
  1159. {
  1160. bool bRet = false;
  1161. if (eStand2Agent == eRecordType) {
  1162. if (0 == iRecordingCam) {
  1163. if ('E' == m_pHostApi->GetCameraState() || 'B' == m_pHostApi->GetCameraState()) {
  1164. bRet = true;
  1165. }
  1166. }
  1167. else {
  1168. if ('O' == m_pHostApi->GetCameraState() || 'B' == m_pHostApi->GetCameraState() || 'E' == m_pHostApi->GetCameraState()) {
  1169. bRet = true;
  1170. }
  1171. }
  1172. }
  1173. else {
  1174. if ('B' == m_pHostApi->GetCameraState()) {
  1175. bRet = true;
  1176. }
  1177. }
  1178. return bRet;
  1179. }
  1180. bool libvideorecord_impl::InitCvxText()
  1181. {
  1182. bool bRet = false;
  1183. #ifdef _WIN32
  1184. string TtcDir = "C:\\Windows\\Fonts\\";
  1185. #else
  1186. string TtcDir = "./";
  1187. #endif
  1188. string fontfile = "";
  1189. #ifdef _WIN32
  1190. if (FindMatchedFile(TtcDir.c_str(), "msyh.", fontfile))
  1191. #else
  1192. if (FindMatchedFile(TtcDir.c_str(), "wryh.", fontfile))
  1193. #endif
  1194. {
  1195. //m_pHostApi->Debug(RECORD_LOG_INFO, "Fontfile:%s", fontfile.c_str());
  1196. TtcDir += fontfile;
  1197. m_pText = new CvxText(TtcDir.c_str());
  1198. int type = 0;
  1199. #ifdef _WIN32
  1200. CvScalar scalar = { 12, 0.2, 0.1, 0 }; //字体大小/空白比例/间隔比例/旋转角度
  1201. #else
  1202. CvScalar scalar = { 16, 0.3, 0.2, 0 }; //字体大小/空白比例/间隔比例/旋转角度
  1203. #endif
  1204. bool underline = false;
  1205. float diaphaneity = 1.0;
  1206. m_pText->setFont(&type, &scalar, &underline, &diaphaneity); //字体类型/size/下滑线/透明度
  1207. bRet = true;
  1208. }
  1209. else {
  1210. if (!m_pText) {
  1211. if (m_SubtitleParam.bSubtitle) {
  1212. LogFailedEvent(eFontNULL, "Font is null, subtitles cannot be added!");
  1213. }
  1214. }
  1215. }
  1216. return bRet;
  1217. }
  1218. void libvideorecord_impl::GetVideoFullName()
  1219. {
  1220. if (m_bWholeSection || !m_bSessionManage) {
  1221. rvc_snprintf(m_VideoFileName, MAX_PATH, "%s%s_%d.%s", m_PathName, m_FileName, m_iFileSerialNum, m_VideoFomat);
  1222. }
  1223. else {
  1224. rvc_snprintf(m_VideoFileName, MAX_PATH, "%s%s_%d_end.%s", m_PathName, m_FileName, m_iFileSerialNum, m_VideoFomat);
  1225. }
  1226. m_pHostApi->Debug(RECORD_LOG_DEBUG, "m_VideoFileName = %s", m_VideoFileName);
  1227. }
  1228. bool libvideorecord_impl::AddCvxText(unsigned char** pData, int iDataSize, int iWidth, int iHeight)
  1229. {
  1230. bool bRet = false;
  1231. #ifdef _WIN32
  1232. char timeSubtitle[MAX_PATH] = { 0 };
  1233. const char* weekDict[] = { "日","一","二","三","四","五","六" };
  1234. SYSTEMTIME st;
  1235. IplImage* pImg = cvCreateImage(cvSize(iWidth, iHeight), IPL_DEPTH_8U, 3);
  1236. memcpy(pImg->imageData, *pData, iDataSize);
  1237. cvFlip(pImg); // 先把图像翻转
  1238. if (strlen(m_SubtitleParam.topSubtitleData) <= 0) {
  1239. if (m_SubtitleParam.bSubtitleSection) {
  1240. GetLocalTime(&st);
  1241. _snprintf(timeSubtitle, MAX_PATH, "%4d年%02d月%02d日 星期%s %02d:%02d:%02d", st.wYear,
  1242. st.wMonth, st.wDay, weekDict[st.wDayOfWeek], st.wHour, st.wMinute, st.wSecond);
  1243. }
  1244. else {
  1245. GetLocalTime(&st);
  1246. _snprintf(timeSubtitle, MAX_PATH, "%4d/%02d/%02d %02d:%02d:%02d", st.wYear,
  1247. st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
  1248. }
  1249. m_pText->putText(pImg, timeSubtitle, cvPoint(10, 12));
  1250. }
  1251. else {
  1252. m_pText->putText(pImg, m_SubtitleParam.topSubtitleData, cvPoint(10, 12));
  1253. }
  1254. if (strlen(m_SubtitleParam.bottomSubtitleData1) > 0) {
  1255. m_pText->putText(pImg, m_SubtitleParam.bottomSubtitleData1, cvPoint(10, pImg->height - 17));
  1256. }
  1257. if (strlen(m_SubtitleParam.bottomSubtitleData2) > 0) {
  1258. m_pText->putText(pImg, m_SubtitleParam.bottomSubtitleData2, cvPoint(10, pImg->height - 3));
  1259. }
  1260. //拼接后的右侧视频添加坐席信息
  1261. if (strlen(m_SubtitleParam.strRightAgentInfo) > 0) {
  1262. if (eStand2Agent == m_eRecordType) {
  1263. m_pText->putText(pImg, m_SubtitleParam.strRightAgentInfo, cvPoint(10 + 640, pImg->height - 17));
  1264. }
  1265. else {
  1266. m_pText->putText(pImg, m_SubtitleParam.strRightAgentInfo, cvPoint(10 + 320, pImg->height - 17));
  1267. }
  1268. }
  1269. cvFlip(pImg); // 添完字幕再翻转
  1270. #else
  1271. wchar_t timeSubtitle[MAX_PATH] = { 0 };
  1272. const char* weekDict[] = { "日","一","二","三","四","五","六" };
  1273. IplImage* pImg = cvCreateImage(cvSize(iWidth, iHeight), IPL_DEPTH_8U, 3);
  1274. memcpy(pImg->imageData, *pData, iDataSize);
  1275. //cvFlip(pImg); // 先把图像翻转
  1276. if (strlen(m_SubtitleParam.topSubtitleData) <= 0) {
  1277. struct tm* pst = NULL;
  1278. time_t t = time(NULL);
  1279. pst = localtime(&t);
  1280. if (m_SubtitleParam.bSubtitleSection) {
  1281. swprintf(timeSubtitle, MAX_PATH, L"%4d年%02d月%02d日 星期%s %02d:%02d:%02d", pst->tm_year + 1900,
  1282. pst->tm_mon + 1, pst->tm_mday, weekDict[pst->tm_wday], pst->tm_hour, pst->tm_min, pst->tm_sec);
  1283. }
  1284. else {
  1285. swprintf(timeSubtitle, MAX_PATH, L"%4d/%02d/%02d %02d:%02d:%02d", pst->tm_year + 1900,
  1286. pst->tm_mon + 1, pst->tm_mday, pst->tm_hour, pst->tm_min, pst->tm_sec);
  1287. }
  1288. m_pText->putText(pImg, timeSubtitle, cvPoint(10, 42));
  1289. //m_pHostApi->Debug(RECORD_LOG_INFO, "水印信息为:%s.", timeSubtitle);
  1290. }
  1291. else {
  1292. m_pText->putText(pImg, m_SubtitleParam.topSubtitleData, cvPoint(10, 42));
  1293. }
  1294. if (wcslen(m_SubtitleParam.bottomSubtitleData1) > 0) {
  1295. m_pText->putText(pImg, m_SubtitleParam.bottomSubtitleData1, cvPoint(10, pImg->height - 47));
  1296. }
  1297. if (wcslen(m_SubtitleParam.bottomSubtitleData2) > 0) {
  1298. m_pText->putText(pImg, m_SubtitleParam.bottomSubtitleData2, cvPoint(10, pImg->height - 23));
  1299. }
  1300. //拼接后的右侧视频添加坐席信息
  1301. if (wcslen(m_SubtitleParam.strRightAgentInfo) > 0) {
  1302. if (eStand2Agent == m_eRecordType) {
  1303. m_pText->putText(pImg, m_SubtitleParam.strRightAgentInfo, cvPoint(10 + 640, pImg->height - 47));
  1304. }
  1305. else {
  1306. m_pText->putText(pImg, m_SubtitleParam.strRightAgentInfo, cvPoint(10 + 320, pImg->height - 47));
  1307. }
  1308. }
  1309. #endif
  1310. memcpy(*pData, pImg->imageData, iDataSize);
  1311. cvReleaseImage(&pImg);
  1312. bRet = true;
  1313. return bRet;
  1314. }
  1315. bool libvideorecord_impl::ReNameVideo()
  1316. {
  1317. bool bRet = false;
  1318. if (m_bSessionManage) {
  1319. //sessionid切换,重命名录像文件
  1320. if (m_bReNameVideo) {
  1321. if (m_iFileSerialNum > 0)//如果当前文件序号大于0,说明前一个文件与当前文件是同一个SESSION,需要修改前一个文件名+END,否则直接改当前文件的文件名
  1322. {
  1323. //sessionid切换,前一个文件需要加上END
  1324. char m_NewFileName[MAX_PATH] = { 0 };
  1325. char m_OldFileName[MAX_PATH] = { 0 };
  1326. string FileName(m_VideoFileName);
  1327. string name = FileName.substr(0, FileName.find_first_of('_') + 1);
  1328. //m_pHostApi->Debug("renamefile1 name = %s",name.c_str());
  1329. //前一个文件名
  1330. rvc_snprintf(m_OldFileName, MAX_PATH, "%s%d.%s", name.c_str(), m_iFileSerialNum - 1, m_VideoFomat);
  1331. rvc_snprintf(m_NewFileName, MAX_PATH, "%s%d_end.%s", name.c_str(), m_iFileSerialNum - 1, m_VideoFomat);
  1332. m_pHostApi->Debug(RECORD_LOG_DEBUG, "renamefile1 from %s to %s", m_OldFileName, m_NewFileName);
  1333. ReNameFile(m_OldFileName, m_NewFileName);
  1334. }
  1335. //sessionid切换,当前文件需要重命名
  1336. char m_NewFileName[MAX_PATH] = { 0 };
  1337. m_iFileSerialNum = 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, "renamefile2 from %s to %s", m_VideoFileName, m_NewFileName);
  1340. if (ReNameFile(m_VideoFileName, m_NewFileName)) {
  1341. m_pHostApi->OnASectionFinished(m_NewFileName, m_iFileSerialNum, false);
  1342. m_iFileSerialNum++;
  1343. }
  1344. m_bReNameVideo = false;
  1345. }
  1346. else {
  1347. //继续录像,删除当前文件的"END"
  1348. char m_NewFileName[MAX_PATH] = { 0 };
  1349. rvc_snprintf(m_NewFileName, MAX_PATH, "%s%s_%d.%s", m_PathName, m_FileName, m_iFileSerialNum, m_VideoFomat);
  1350. m_pHostApi->Debug(RECORD_LOG_DEBUG, "renamefile3 from %s to %s", m_VideoFileName, m_NewFileName);
  1351. ReNameFile(m_VideoFileName, m_NewFileName);
  1352. m_pHostApi->OnASectionFinished(m_NewFileName, m_iFileSerialNum, false);
  1353. //WMV文件的序列号
  1354. m_iFileSerialNum++;
  1355. }
  1356. //新文件增加END
  1357. rvc_snprintf(m_VideoFileName, MAX_PATH, "%s%s_%d_end.%s", m_PathName, m_FileName, m_iFileSerialNum, m_VideoFomat);
  1358. m_pHostApi->Debug(RECORD_LOG_DEBUG, "generate new file name %s", m_VideoFileName);
  1359. m_bCloseVideo = false;
  1360. }
  1361. else {
  1362. //WMV文件的序列号
  1363. m_pHostApi->OnASectionFinished(m_VideoFileName, m_iFileSerialNum, false);
  1364. m_iFileSerialNum++;
  1365. rvc_snprintf(m_VideoFileName, MAX_PATH, "%s%s_%d.%s", m_PathName, m_FileName, m_iFileSerialNum, m_VideoFomat);
  1366. }
  1367. bRet = true;
  1368. return bRet;
  1369. }
  1370. bool libvideorecord_impl::InitAudioQueue()
  1371. {
  1372. if (strlen(m_audioqueuename) > 0) {
  1373. m_local_audioqueue = new Clibaudioqueue(m_audioqueuename);
  1374. }
  1375. else {
  1376. m_local_audioqueue = NULL;
  1377. }
  1378. if (strlen(m_salesaudioqueuename) > 0) {
  1379. m_sales_audioqueue = new Clibaudioqueue(m_salesaudioqueuename);
  1380. }
  1381. else {
  1382. m_sales_audioqueue = NULL;
  1383. }
  1384. if (strlen(m_remoteaudioqueuename) > 0) {
  1385. m_remote_audioqueue = new Clibaudioqueue(m_remoteaudioqueuename);
  1386. }
  1387. else {
  1388. m_remote_audioqueue = NULL;
  1389. }
  1390. return true;
  1391. }
  1392. bool libvideorecord_impl::InitVideoQueue()
  1393. {
  1394. if (strlen(m_env_videoqueuename) > 0) {
  1395. m_env_videoqueue = new Clibvideoqueue(m_env_videoqueuename);
  1396. }
  1397. else {
  1398. m_env_videoqueue = NULL;
  1399. }
  1400. if (strlen(m_opt_videoqueuename) > 0) {
  1401. m_opt_videoqueue = new Clibvideoqueue(m_opt_videoqueuename);
  1402. }
  1403. else {
  1404. m_opt_videoqueue = NULL;
  1405. }
  1406. if (strlen(m_remotevideoqueuename) > 0) {
  1407. m_remote_videoqueue = new Clibvideoqueue(m_remotevideoqueuename);
  1408. }
  1409. else {
  1410. m_remote_videoqueue = NULL;
  1411. }
  1412. return true;
  1413. }
  1414. bool libvideorecord_impl::InitRecordParams()
  1415. {
  1416. m_iFileSerialNum = 0;
  1417. m_iVideoFrameSize = 0;
  1418. m_iWidth = 0;
  1419. m_iHeight = 0;
  1420. m_iAudioPerSecBufLen = 0;
  1421. m_iLocalAudioFps = 0;
  1422. m_iRemoteAudioFps = 0;
  1423. m_pRemoteAudioBuffer = NULL;
  1424. m_pRecordAudioBuffer = NULL;
  1425. m_iRecordAudioBufSize = 0;
  1426. m_iRecordedTime = 0;
  1427. m_iRemoteAudioNum = 0;
  1428. m_iAudioFromQueNum = 0;
  1429. m_iRecordVideoNum = 0;
  1430. m_iRecordAudioNum = 0;
  1431. m_iSubTitleTime = 0;
  1432. m_iRemoteAudioBufLen = 0;
  1433. m_iAudioBufferLen = 0;
  1434. m_bStopRecord = false;
  1435. //每次启动录像重新初始化
  1436. m_bCloseVideo = false;
  1437. m_bReNameVideo = false;
  1438. m_iRemoteVideoFailedTimes = 0;
  1439. m_bSwitchCam = true;
  1440. return true;
  1441. }
  1442. bool libvideorecord_impl::InitRecordWriter(char* filename, int width, int height, int colorbit, int nfps,
  1443. int nSamplePsec, int nchannels, int nBitPerSample, int nmaxspacing, int nquality, int nOutBitRate, int iAudioType)
  1444. {
  1445. bool bResult = false;
  1446. bResult = m_pFFmpegWriter->InitWriter(filename, width, height, 24, nfps, nSamplePsec,
  1447. nchannels, nBitPerSample, 6, nquality, nOutBitRate, (int)iAudioType);
  1448. return bResult;
  1449. }
  1450. bool libvideorecord_impl::StartRecordWrite()
  1451. {
  1452. bool bResult = false;
  1453. bResult = m_pFFmpegWriter->StartWrite();
  1454. return bResult;
  1455. }
  1456. bool libvideorecord_impl::EndRecordWrite()
  1457. {
  1458. bool bResult = false;
  1459. bResult = m_pFFmpegWriter->StopWrite();
  1460. return bResult;
  1461. }
  1462. bool libvideorecord_impl::ReceiveRecordVideoData(unsigned char* pData, unsigned long len)
  1463. {
  1464. bool bResult = false;
  1465. bResult = m_pFFmpegWriter->ReceiveVideoData(pData, len);
  1466. return bResult;
  1467. }
  1468. bool libvideorecord_impl::ReceiveRecordAudioData(unsigned char* pData, unsigned long len)
  1469. {
  1470. bool bResult = false;
  1471. bResult = m_pFFmpegWriter->ReceiveAudioData(pData, len);
  1472. return bResult;
  1473. }
  1474. #ifdef _WIN32
  1475. bool libvideorecord_impl::setRightWaterMark(const char* strWaterMark, uint32_t ulen)
  1476. #else
  1477. bool libvideorecord_impl::setRightWaterMark(const wchar_t* strWaterMark, uint32_t ulen)
  1478. #endif
  1479. {
  1480. bool bRet = false;
  1481. memset(m_SubtitleParam.strRightAgentInfo, 0, MAX_PATH);
  1482. if (ulen < MAX_PATH) {
  1483. memcpy(m_SubtitleParam.strRightAgentInfo, strWaterMark, ulen);
  1484. bRet = true;
  1485. }
  1486. return bRet;
  1487. }
  1488. void libvideorecord_impl::Debug(const char* fmt, ...)
  1489. {
  1490. va_list arg;
  1491. va_start(arg, fmt);
  1492. m_pHostApi->vDebug(RECORD_LOG_DEBUG, fmt, arg);
  1493. va_end(arg);
  1494. }
  1495. void libvideorecord_impl::vDebug(const char* str, va_list list)
  1496. {
  1497. m_pHostApi->vDebug(RECORD_LOG_DEBUG, str, list);
  1498. }
  1499. bool libvideorecord_impl::ContinueRecord() //继续录像
  1500. {
  1501. m_pHostApi->Debug(RECORD_LOG_DEBUG, "continue record");
  1502. #ifdef _WIN32
  1503. #else
  1504. InitAudioQueue();
  1505. m_audioqueue = m_local_audioqueue;
  1506. InitVideoQueue();
  1507. #endif
  1508. m_bPauseRecord = false;
  1509. return true;
  1510. }
  1511. void libvideorecord_impl::ChangeFilename(const char* newfilename)
  1512. {
  1513. if (!m_bWholeSection) {
  1514. memset(m_FileName, 0, MAX_PATH);
  1515. memcpy(m_FileName, newfilename, strlen(newfilename));
  1516. m_bReNameVideo = true;
  1517. }
  1518. }
  1519. bool libvideorecord_impl::StopVideoRecord() //退出
  1520. {
  1521. m_bStopRecord = true;
  1522. #ifdef _WIN32
  1523. SetEvent(m_hEventWait);
  1524. WaitForSingleObject(m_hRecordThread, INFINITE);
  1525. CloseHandle(m_hRecordThread);
  1526. m_hRecordThread = NULL;
  1527. #else
  1528. sem_post(&m_semt);
  1529. if (0 == pthread_join(m_nRecordthreadId, NULL)) {
  1530. m_pHostApi->Debug(RECORD_LOG_DEBUG, "thread join video record thread %u success!", m_nRecordthreadId);
  1531. m_nRecordthreadId = 0;
  1532. }
  1533. else {
  1534. m_pHostApi->Debug(RECORD_LOG_INFO, "thread join video record thread failed!");
  1535. }
  1536. #endif
  1537. m_eRecordType = eSingleSide;
  1538. return true;
  1539. }
  1540. bool libvideorecord_impl::PauseRecord() //暂停录像
  1541. {
  1542. m_pHostApi->Debug(RECORD_LOG_DEBUG, "pause record");
  1543. m_bPauseRecord = true;
  1544. return true;
  1545. }
  1546. bool libvideorecord_impl::StartVideoRecord(int fps, int videoquality, eVideoFormat eFormat, const SubtitleParam* subtitleParam, bool bWholeSection,
  1547. bool bSessionManage, const char* pathname, int pathlength, const char* pfilename, int filenamelength, Rvc_RecordAudioParam_t* pAudioParam)//开始记录
  1548. {
  1549. m_audioqueue = NULL;
  1550. InitAudioQueue();
  1551. InitVideoQueue();
  1552. memset(m_FileName, 0, MAX_PATH);
  1553. memset(m_PathName, 0, MAX_PATH);
  1554. if (filenamelength == 0) {
  1555. #ifdef _WIN32
  1556. SYSTEMTIME nowTime; // 系统时间结构体
  1557. GetLocalTime(&nowTime);
  1558. rvc_snprintf(m_FileName, MAX_PATH, "%4d%02d%02d%02d%02d%02d", nowTime.wYear, nowTime.wMonth, nowTime.wDay, nowTime.wHour, nowTime.wMinute, nowTime.wSecond);
  1559. #else
  1560. struct tm* pst = NULL;
  1561. time_t t = time(NULL);
  1562. pst = localtime(&t);
  1563. rvc_snprintf(m_FileName, MAX_PATH, "%4d%02d%02d%02d%02d%02d", pst->tm_year + 1900,
  1564. pst->tm_mon + 1, pst->tm_mday, pst->tm_hour, pst->tm_min, pst->tm_sec);
  1565. #endif
  1566. }
  1567. else {
  1568. memcpy(m_FileName, pfilename, filenamelength);
  1569. }
  1570. m_eFormat = eFormat;
  1571. rvc_snprintf(m_VideoFomat, MAX_PATH, "%s", RECORD_MP4_SUFFIX);
  1572. if (pathlength != 0) {
  1573. memcpy(m_PathName, pathname, pathlength);
  1574. }
  1575. m_nFps = fps;
  1576. if (videoquality > 0 || videoquality <= 100) {
  1577. m_videoquality = videoquality;
  1578. }
  1579. else {
  1580. m_pHostApi->Debug(RECORD_LOG_ERROR, "warn: videoquality is illegal, will use default instead!");
  1581. m_videoquality = 75;
  1582. }
  1583. memset(&m_SubtitleParam, 0, sizeof(SubtitleParam));
  1584. if (subtitleParam != NULL) {
  1585. memcpy(&m_SubtitleParam, subtitleParam, sizeof(SubtitleParam));
  1586. }
  1587. m_bWholeSection = bWholeSection;
  1588. m_bSessionManage = bSessionManage;
  1589. m_eRecordType = pAudioParam->eRecordType;
  1590. m_eAudioType = pAudioParam->eOutPutType;
  1591. m_bIsAudioNsOn = pAudioParam->bIsNsOn;
  1592. m_iNsPolicy = pAudioParam->iNsPolicy;
  1593. m_bIsAudioTransOn = pAudioParam->bIsTransOn;
  1594. m_iAudioChannels = pAudioParam->iAudioChannels;
  1595. m_bMuteAudio = pAudioParam->bMuteAudioMode;
  1596. m_audioqueue = m_local_audioqueue;
  1597. if (m_sales_audioqueue && (eSingleSide == m_eRecordType || eStand2Agent == m_eRecordType)) //当大机进行双录拼接时使用本地音频队列会出现抢占导致取音频失败,此处选择使用销售音频队列
  1598. {
  1599. m_audioqueue = m_sales_audioqueue;
  1600. }
  1601. //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");
  1602. #ifdef _WIN32
  1603. if (m_hRecordThread == NULL) {
  1604. return StartRecord();
  1605. }
  1606. else {
  1607. return false;
  1608. }
  1609. #else
  1610. if (0 != m_nRecordthreadId) {
  1611. StopVideoRecord();
  1612. }
  1613. sem_init(&m_semt, 0, 0);
  1614. return StartRecord();
  1615. #endif
  1616. }
  1617. void libvideorecord_impl::LogFailedEvent(eRvcRecordFailedCase eCase, const char* pszMessage, bool bRecordDevFault)
  1618. {
  1619. *m_bResult = false;
  1620. m_bStopRecord = true;
  1621. m_pHostApi->OnRecordFailed(eCase, pszMessage, bRecordDevFault);
  1622. }
  1623. void libvideorecord_impl::MediaSynchronous()
  1624. {
  1625. int isleeptime = 0;
  1626. if (m_nFps < m_iLocalAudioFps) {
  1627. if (eSingleSide != m_eRecordType)//如果双向混音,则sleeptime以远端为准
  1628. {
  1629. if ((m_iRemoteAudioFps != 0) && (m_iRemoteAudioFps > m_iLocalAudioFps)) {
  1630. isleeptime = (int)((m_iRemoteAudioNum + 1) * 1000 / m_iRemoteAudioFps - m_iRecordedTime);
  1631. }
  1632. else {
  1633. isleeptime = (int)((m_iAudioFromQueNum + 1) * 1000 / m_iLocalAudioFps - m_iRecordedTime);
  1634. }
  1635. }
  1636. else {
  1637. isleeptime = (int)((m_iAudioFromQueNum + 1) * 1000 / m_iLocalAudioFps - m_iRecordedTime);
  1638. }
  1639. }
  1640. else {
  1641. isleeptime = (int)((m_iRecordVideoNum + 1) * 1000 / m_nFps - m_iRecordedTime);
  1642. }
  1643. if (isleeptime > 0) {
  1644. Rvc_Timeout(isleeptime);
  1645. }
  1646. }
  1647. int libvideorecord_impl::WriteVideoFrame()
  1648. {
  1649. int iret = 0;
  1650. static int nInitActiveCam = 0;
  1651. bool bVideoLens = (m_opt_videoqueue == NULL) ? (m_env_videoqueue->GetVideoLens() > 0) : ((m_env_videoqueue->GetVideoLens() > 0) || (m_opt_videoqueue->GetVideoLens() > 0));
  1652. if (bVideoLens) {
  1653. bool bGetvideo = false;
  1654. //获取拼接后的视频帧数据
  1655. if (eStand2Agent == m_eRecordType) {
  1656. if (m_bSwitchCam) {
  1657. nInitActiveCam = m_pHostApi->GetRecordCamera();
  1658. m_pHostApi->Debug(RECORD_LOG_INFO, "Stand2S Device Double Record local use %d CAMERA_TYPE_ENV[0] CAMERA_TYPE_OPT[1] Camera.", nInitActiveCam);
  1659. m_bSwitchCam = false;
  1660. }
  1661. }
  1662. int iGetvideo = GetDestTypeVideoFrameData(m_videoframe, VIDEOQUEUE_FLAG_VERTICAL_FLIP, m_bSwitchCam, nInitActiveCam, m_eRecordType);
  1663. //m_pHostApi->Debug(RECORD_LOG_DEBUG, "GetDestTypeVideoFrameData result = %d.", iGetvideo);
  1664. if (0 != iGetvideo) {
  1665. if (eStand2Agent == m_eRecordType) {
  1666. if (IsRecordingCamError(nInitActiveCam)) {
  1667. LogFailedEvent(eVideoGetFailed, "remote recording camera error!");
  1668. return -1;
  1669. }
  1670. else {
  1671. if (-3 == iGetvideo){
  1672. if (!m_bStopRecord) {
  1673. m_iRemoteVideoFailedTimes++;
  1674. m_pHostApi->Debug(RECORD_LOG_ERROR, "Get Remote Video Frame Failed time is %d.", m_iRemoteVideoFailedTimes);
  1675. if (GET_REMOTEVIDEO_FAILED_MAX_TIMES > m_iRemoteVideoFailedTimes) {
  1676. return 0;
  1677. }
  1678. else{
  1679. LogFailedEvent(eRemoteVideoGetFailed, "Get Remote Video Frame Failed.", false);
  1680. return -1;
  1681. }
  1682. }
  1683. }
  1684. }
  1685. }
  1686. else {
  1687. if (IsRecordingCamError(nInitActiveCam, eSingleSide)) {
  1688. LogFailedEvent(eVideoGetFailed, "remote recording camera error!");
  1689. return -1;
  1690. }
  1691. else {
  1692. Rvc_Timeout(2);
  1693. return -2;
  1694. }
  1695. }
  1696. }
  1697. // 加入字幕
  1698. if (m_SubtitleParam.bSubtitle) {
  1699. unsigned int nStartTime = timeGetTime();
  1700. AddCvxText(&m_videoframe->data, m_iVideoFrameSize, m_iWidth, m_iHeight);
  1701. m_iSubTitleTime += (timeGetTime() - nStartTime);
  1702. }
  1703. if (!ReceiveRecordVideoData((unsigned char*)m_videoframe->data, m_iVideoFrameSize)) {
  1704. int iremote_audio_length = 0;
  1705. if (m_remote_audioqueue) {
  1706. iremote_audio_length = m_remote_audioqueue->GetAudioLens();
  1707. }
  1708. char strmsg[MAX_PATH] = { 0 };
  1709. rvc_snprintf(strmsg, MAX_PATH, "视频流写入失败, remote_audio_length = %d, nVideoFrameSize = %d, noldvideoNum = %d", iremote_audio_length, m_iVideoFrameSize, m_iRecordVideoNum);
  1710. if (0 == iremote_audio_length) {
  1711. LogFailedEvent(eRemoteAudioGetFailed, strmsg);
  1712. }
  1713. else {
  1714. LogFailedEvent(eLocalAudioGetFailed, strmsg);
  1715. }
  1716. return -1;
  1717. }
  1718. else {
  1719. m_iRecordVideoNum++;
  1720. if (0 == m_iRecordVideoNum % 50) {
  1721. m_pHostApi->Debug(RECORD_LOG_DEBUG, "已成功写入视频帧数为(%d)", m_iRecordVideoNum);
  1722. }
  1723. }
  1724. }
  1725. else {
  1726. if (eStand2Agent == m_eRecordType) {
  1727. if (IsRecordingCamError(nInitActiveCam)) {
  1728. LogFailedEvent(eVideoGetFailed, "remote recording camera error!");
  1729. return -1;
  1730. }
  1731. }
  1732. else {
  1733. if (IsRecordingCamError(nInitActiveCam, eSingleSide)) {
  1734. LogFailedEvent(eVideoGetFailed, "recording camera error!");
  1735. return -1;
  1736. }
  1737. else {
  1738. Rvc_Timeout(2);
  1739. return -2;
  1740. }
  1741. }
  1742. }
  1743. return iret;
  1744. }
  1745. bool libvideorecord_impl::ReSetRecordParams()
  1746. {
  1747. m_bStopRecord = false;
  1748. m_iRecordedTime = 0; //本段视频持续时间
  1749. m_iRecordVideoNum = 0; //已经录制的视频数
  1750. m_iAudioFromQueNum = 0; //从音频队列中取出的音频数量
  1751. m_iRemoteAudioNum = 0; //远端音频数量
  1752. m_iSubTitleTime = 0; //本段视频添加字幕耗时
  1753. m_iRecordAudioNum = 0; //已录制音频数
  1754. m_iRemoteAudioBufLen = 0; //远端音频拼接BUFFER的当前长度
  1755. m_iAudioBufferLen = 0; //音频录制拼接BUFFER的当前长度
  1756. memset(m_videoframe->data, 0, m_iVideoFrameSize);
  1757. memset(m_pRecordAudioBuffer, 0, m_iRecordAudioBufSize);
  1758. if (eSingleSide != m_eRecordType) {
  1759. if (NULL != m_pRemoteAudioBuffer) {
  1760. memset(m_pRemoteAudioBuffer, 0, m_iAudioPerSecBufLen);
  1761. }
  1762. }
  1763. return true;
  1764. }
  1765. bool libvideorecord_impl::GetRemoteAudioFrame(int iAudioLens)
  1766. {
  1767. bool bGetRemoteAudio = false;
  1768. audio_frame* RemoteAudio = new audio_frame;
  1769. RemoteAudio->data = m_pRemoteAudioBuffer + m_iRemoteAudioBufLen;
  1770. if (iAudioLens <= MAX_AUDIOQUEUE_LENS && iAudioLens > 1) {
  1771. bGetRemoteAudio = m_remote_audioqueue->GetAudioAndDel(RemoteAudio);
  1772. }
  1773. else {
  1774. bGetRemoteAudio = m_remote_audioqueue->GetAudio(RemoteAudio);
  1775. }
  1776. if (bGetRemoteAudio) {
  1777. //if (m_bIsAudioNsOn && NULL != m_pAudioNsObj){
  1778. // char pOutAudio[MAX_PATH*3] = {0};
  1779. // int iRet = m_pAudioNsObj->NsProcess(pOutAudio,RemoteAudio->framesize, RemoteAudio->data, RemoteAudio->framesize);
  1780. // if (0 == iRet){
  1781. // memset(m_pRemoteAudioBuffer + m_iRemoteAudioBufLen, 0, RemoteAudio->framesize);
  1782. // memcpy(m_pRemoteAudioBuffer + m_iRemoteAudioBufLen, pOutAudio, RemoteAudio->framesize);
  1783. // }
  1784. //}
  1785. 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);
  1786. m_iRemoteAudioBufLen += RemoteAudio->framesize;
  1787. m_iRemoteAudioNum++;
  1788. m_pHostApi->Debug(RECORD_LOG_DEBUG, "m_iRemoteAudioNum is %d.", m_iRemoteAudioNum);
  1789. }
  1790. delete RemoteAudio;
  1791. return bGetRemoteAudio;
  1792. }
  1793. bool libvideorecord_impl::GetLocalAudioFrame(int iAudioLens)
  1794. {
  1795. bool bRet = false;
  1796. //取出音频
  1797. m_audioframe->data = m_pRecordAudioBuffer + m_iAudioBufferLen;
  1798. bool bGetAudio = false;
  1799. if (m_audioqueue && (iAudioLens <= MAX_AUDIOQUEUE_LENS)) {
  1800. bGetAudio = m_audioqueue->GetAudioAndDel(m_audioframe);
  1801. }
  1802. else {
  1803. bGetAudio = m_audioqueue->GetAudio(m_audioframe);
  1804. }
  1805. if (bGetAudio) {
  1806. if (m_bIsAudioNsOn && NULL != m_pAudioNsObj) {
  1807. char pOutAudio[CAPTURE_FRAME_MAX_LEN] = { 0 };
  1808. int iRet = m_pAudioNsObj->NsProcess(pOutAudio, CAPTURE_FRAME_MAX_LEN, m_audioframe->data, m_audioframe->framesize);
  1809. if (0 == iRet) {
  1810. memset(m_pRecordAudioBuffer + m_iAudioBufferLen, 0, m_audioframe->framesize);
  1811. memcpy(m_pRecordAudioBuffer + m_iAudioBufferLen, pOutAudio, m_audioframe->framesize);
  1812. }
  1813. }
  1814. if (CAPTURE_CLOCK == m_audioframe->samplespersec) {
  1815. m_iAudioBufferLen += m_audioframe->framesize;
  1816. m_iAudioFromQueNum++;
  1817. //if ((0 == m_iAudioFromQueNum % 100) && 0 != m_audioframe->iseriesnumber) {
  1818. // m_pHostApi->Debug(RECORD_LOG_DEBUG, "current audio queue len is %d and audio series number is: %d.", iAudioLens, m_audioframe->iseriesnumber);
  1819. //}
  1820. bRet = true;
  1821. }
  1822. else {
  1823. 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);
  1824. LogFailedEvent(eSampNotSupport, "not support audio sample rate.");
  1825. }
  1826. }
  1827. else {
  1828. m_pHostApi->Debug(RECORD_LOG_ERROR, "get audio from queue failed.");
  1829. }
  1830. return bRet;
  1831. }
  1832. bool libvideorecord_impl::GetSingleSideAudioFrame()
  1833. {
  1834. bool bRet = false;
  1835. //取音频数据,合并成1s的音频
  1836. int nAudioLens = 0;
  1837. if (m_audioqueue) {
  1838. nAudioLens = m_audioqueue->GetAudioLens();
  1839. }
  1840. if (nAudioLens > 0) {
  1841. if (m_iAudioBufferLen < m_iAudioPerSecBufLen) {
  1842. bRet = GetLocalAudioFrame(nAudioLens);
  1843. }
  1844. if (m_iAudioBufferLen >= m_iAudioPerSecBufLen) {//本地录像双声道录音
  1845. if (2 == m_iAudioChannels) {
  1846. if (m_iRecordAudioBufSize == m_iAudioBufferLen * 2) {
  1847. uint32_t uOutlen = ConstructStereoAudioData(m_iRecordAudioBufSize, m_pRecordAudioBuffer, m_iAudioBufferLen, m_audioframe->bitspersample / 8);
  1848. m_iAudioBufferLen = uOutlen;
  1849. }
  1850. }
  1851. }
  1852. }
  1853. return bRet;
  1854. }
  1855. bool libvideorecord_impl::AddMuteAudioFrame(bool bLocal, int iAudioFrameSize)
  1856. {
  1857. bool bRet = false;
  1858. if (0 == iAudioFrameSize) {
  1859. return bRet;
  1860. }
  1861. if (bLocal) {
  1862. memset(m_pRecordAudioBuffer + m_iAudioBufferLen, 0, iAudioFrameSize);
  1863. m_iAudioBufferLen += iAudioFrameSize;
  1864. m_iAudioFromQueNum++;
  1865. if (0 == m_iAudioFromQueNum % 100) {
  1866. m_pHostApi->Debug(RECORD_LOG_DEBUG, "m_iAudioFromQueNum is %d(Mute AudioFrame).", m_iAudioFromQueNum);
  1867. }
  1868. bRet = true;
  1869. }
  1870. else {
  1871. memset(m_pRemoteAudioBuffer + m_iRemoteAudioBufLen, 0, iAudioFrameSize);
  1872. m_iRemoteAudioBufLen += iAudioFrameSize;
  1873. m_iRemoteAudioNum++;
  1874. if (0 == m_iRemoteAudioNum % 100) {
  1875. m_pHostApi->Debug(RECORD_LOG_DEBUG, "m_iRemoteAudioNum is %d(Mute Audio Frame).", m_iRemoteAudioNum);
  1876. }
  1877. bRet = true;
  1878. }
  1879. return bRet;
  1880. }
  1881. bool libvideorecord_impl::InitDefaultAudioParams(audio_frame* paudio)
  1882. {
  1883. bool bRet = false;
  1884. if (NULL != paudio) {
  1885. paudio->samplespersec = 8000;
  1886. paudio->framesize = 320;
  1887. paudio->nchannels = 1;
  1888. paudio->format = 1;
  1889. paudio->bitspersample = 16;
  1890. paudio->iseriesnumber = 0;
  1891. bRet = true;
  1892. }
  1893. return bRet;
  1894. }
  1895. bool libvideorecord_impl::GetRecordAudioFrame()
  1896. {
  1897. bool bRet = false;
  1898. //单向只录制本地音频
  1899. if (eSingleSide == m_eRecordType) {
  1900. if (false == m_bMuteAudio) {
  1901. bRet = GetSingleSideAudioFrame();
  1902. if (!bRet) {
  1903. bRet = AddMuteAudioFrame(true, m_iAudioPerSecBufLen / 50);
  1904. }
  1905. }
  1906. else {
  1907. bRet = AddMuteAudioFrame(true, m_iAudioPerSecBufLen / 50);
  1908. }
  1909. }
  1910. else{
  1911. //取音频数据,合并成1秒的音频
  1912. int nAudioLens = m_audioqueue->GetAudioLens();
  1913. int nRemoteAudioLens = m_remote_audioqueue->GetAudioLens();
  1914. m_pHostApi->Debug(RECORD_LOG_DEBUG,"current local audio length is %d, and remote audio length is %d.", nAudioLens, nRemoteAudioLens);
  1915. //1. 取本地音频
  1916. if (m_iAudioBufferLen < m_iAudioPerSecBufLen) {
  1917. if (nAudioLens > 0) {//取到本地音频
  1918. bRet = GetLocalAudioFrame(nAudioLens);
  1919. }
  1920. else {//本地音频队列为空,用静音填充
  1921. bRet = AddMuteAudioFrame(true, m_iAudioPerSecBufLen/50);
  1922. }
  1923. }
  1924. //2. 取远端音频
  1925. if (m_iRemoteAudioBufLen < m_iAudioPerSecBufLen) {
  1926. if (nRemoteAudioLens > 0) {
  1927. bRet = GetRemoteAudioFrame(nRemoteAudioLens); //取远端音频
  1928. }
  1929. else{
  1930. bRet = AddMuteAudioFrame(false, m_iAudioPerSecBufLen / 50);
  1931. }
  1932. }
  1933. //3. 可视远程双录场景,本地和坐席端分别录制到左右声道
  1934. if ((m_iRemoteAudioBufLen == m_iAudioPerSecBufLen) && (m_iAudioBufferLen == m_iAudioPerSecBufLen)) {
  1935. uint32_t uOutlen = BindPCMAudioData(m_iRecordAudioBufSize, m_pRecordAudioBuffer, m_iAudioBufferLen, m_pRemoteAudioBuffer, m_iRemoteAudioBufLen, m_audioframe->bitspersample / 8, eLocalLeft);
  1936. 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);
  1937. m_iAudioBufferLen = uOutlen;
  1938. }
  1939. }
  1940. return bRet;
  1941. }
  1942. int libvideorecord_impl::WriteAudioFrame()
  1943. {
  1944. int iRet = 0;
  1945. if (ReceiveRecordAudioData((unsigned char*)m_pRecordAudioBuffer, m_iAudioBufferLen)) {
  1946. m_iRecordAudioNum++;
  1947. if (0 == m_iRecordAudioNum % 10) {
  1948. m_pHostApi->Debug(RECORD_LOG_DEBUG, "已成功写入音频帧数为(%d), 当前音频包大小为(%d).", m_iRecordAudioNum, m_iAudioBufferLen);
  1949. }
  1950. m_iAudioBufferLen = 0;
  1951. m_iRemoteAudioBufLen = 0;
  1952. memset((void*)m_pRecordAudioBuffer, 0, m_iRecordAudioBufSize);
  1953. if (NULL != m_pRemoteAudioBuffer) {
  1954. memset(m_pRemoteAudioBuffer, 0, m_iAudioPerSecBufLen);
  1955. }
  1956. }
  1957. else {
  1958. char strinfo[MAX_PATH] = { 0 };
  1959. rvc_snprintf(strinfo, MAX_PATH, "音频流写入失败, m_iAudioBufferLen = %d, m_iRecordAudioNum = %d.", m_iAudioBufferLen, m_iRecordAudioNum);
  1960. LogFailedEvent(eAudioStreamWriteFailed, strinfo);
  1961. return -1;
  1962. }
  1963. return iRet;
  1964. }
  1965. bool libvideorecord_impl::InitVideoRecordParams()
  1966. {
  1967. bool bRet = false;
  1968. //获取视频帧大小
  1969. if (!GetRecordVideoFrameSize()) {
  1970. if (false == m_bStopRecord) {
  1971. LogFailedEvent(eVideoGetFailed, "Get Video Frame Failed Max Times.", true);
  1972. }
  1973. return bRet;
  1974. }
  1975. // 静音模式录像
  1976. if (m_bMuteAudio) {
  1977. m_iAudioFrameSize = 320;
  1978. }
  1979. else {
  1980. //获取音频帧大小
  1981. if (!GetRecordAudioFrameSize()) {
  1982. if (eSingleSide == m_eRecordType) {
  1983. if (false == m_bStopRecord) {
  1984. m_bMuteAudio = true;
  1985. m_iAudioFrameSize = 320;
  1986. }
  1987. else {
  1988. return bRet;
  1989. }
  1990. }
  1991. else {
  1992. if (false == m_bStopRecord) {
  1993. LogFailedEvent(eLocalAudioGetFailed, "Get Audio Frame Failed Max Times.", true);
  1994. }
  1995. return bRet;
  1996. }
  1997. }
  1998. }
  1999. //初始化视频buffer
  2000. InitRecordVideoFrameBuffer();
  2001. //获取音频参数
  2002. audio_frame* audio = new audio_frame;
  2003. // 静音模式录像
  2004. if (m_bMuteAudio) {
  2005. InitDefaultAudioParams(audio);
  2006. }
  2007. else {
  2008. if (!GetRecordAudioParams(&audio)) {
  2009. RECORD_SAFE_DELETE(audio);
  2010. LogFailedEvent(eLocalAudioGetFailed, "Get Audio Frame Data Failed Max Times.", true);
  2011. return bRet;
  2012. }
  2013. }
  2014. CalcuRecordAudioParams(audio);
  2015. //初始化音频buffer
  2016. InitRecordAudioBuffer();
  2017. uint32_t uOutPutBitRate = GetAudioOutPutBitRate(audio->samplespersec, m_iAudioChannels, m_eAudioType);
  2018. if (m_bIsAudioNsOn && NULL != m_pAudioNsObj) {
  2019. if (m_pAudioNsObj->SetNsParams(audio->samplespersec, CAPTURE_FRAME_TIME, m_iNsPolicy)) {
  2020. m_bIsAudioNsOn = false;
  2021. m_pHostApi->Debug(RECORD_LOG_INFO, "Ns audio set params failed, set AudioNsOn false.");
  2022. }
  2023. }
  2024. //m_pHostApi->Debug(RECORD_LOG_DEBUG, "InitRecordWriter samplespersec=%d,audioOutChannels=%d,audioOutBitRate=%d bps.", audio->samplespersec, m_iAudioChannels, uOutPutBitRate);
  2025. if (!InitRecordWriter(m_VideoFileName, m_iWidth, m_iHeight, 24, m_nFps, audio->samplespersec,
  2026. m_iAudioChannels, audio->bitspersample, 6, m_videoquality, uOutPutBitRate, (int)m_eAudioType)) {
  2027. RECORD_SAFE_DELETE(audio);
  2028. LogFailedEvent(eInitialFailed, "初始化失败!");
  2029. return bRet;
  2030. }
  2031. RECORD_SAFE_DELETE(audio);
  2032. bRet = true;
  2033. return bRet;
  2034. }
  2035. bool libvideorecord_impl::ResetMediaQueue()
  2036. {
  2037. #ifdef _WIN32
  2038. #else
  2039. RECORD_SAFE_DELETE(m_local_audioqueue);
  2040. RECORD_SAFE_DELETE(m_env_videoqueue);
  2041. RECORD_SAFE_DELETE(m_opt_videoqueue);
  2042. #endif
  2043. return true;
  2044. }