capture.cpp 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348
  1. #include "capture.h"
  2. #ifdef RVC_OS_WIN
  3. #include "stdafx.h"
  4. #include <ipp.h>
  5. #include <objbase.h>
  6. #include <intrin.h>
  7. #include "videohorflip.h"
  8. #include <locale.h>
  9. #include "video_common/ffmpeg_api_cpp_adapter.h"
  10. #else
  11. #include "imediadeviceinfo.h"
  12. #include <unistd.h>
  13. #endif // RVC_OS_WIN
  14. #include <memutil.h>
  15. #include <md5.h>
  16. #include "y2k_time.h"
  17. #include "Event.h"
  18. using namespace MediaController;
  19. #ifndef av_always_inline
  20. #define av_always_inline __inline
  21. #endif // !av_always_inline
  22. #ifndef inline
  23. #define inline __inline
  24. #endif // !inline
  25. #ifndef RVC_AUDIO_BUFFER_LEN
  26. #define RVC_AUDIO_BUFFER_LEN 320
  27. #endif
  28. #ifndef MAX_PATH
  29. #define MAX_PATH 260
  30. #endif
  31. static void __dbg(const char *fmt, va_list arg)
  32. {
  33. int n = vsnprintf(NULL, 0, fmt, arg);
  34. if (n >= MAX_PATH) {
  35. char* buf = (char*)malloc((size_t)(n + 1));
  36. vsnprintf(buf, n + 1, fmt, arg);
  37. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf);
  38. free(buf);
  39. }
  40. else{
  41. char strlog[MAX_PATH] = {0};
  42. vsnprintf(strlog, MAX_PATH, fmt, arg);
  43. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog);
  44. }
  45. }
  46. static void __logevent(int itype, const char* strmessage)
  47. {
  48. if (0 == itype){
  49. LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_CAMERA_MATCHED_FORMAT, strmessage);
  50. }
  51. else if(1 == itype){
  52. LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_CAMERA_OUTPUT_FORMAT, strmessage);
  53. }
  54. else if (2 == itype) {
  55. LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_CAMOPEN_FAILED_INFO, strmessage);
  56. }
  57. else{
  58. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("unknown type event.");
  59. }
  60. }
  61. static int Bin2Str(unsigned char *x, int xlen, char *str, int str_size)
  62. {
  63. static const char *hex2char = "0123456789ABCDEF";
  64. int i, k = 0;
  65. if (str_size <= xlen * 2)
  66. return -1;
  67. for (i = 0; i < xlen; ++i) {
  68. int h = x[i] >> 4;
  69. int l = x[i] & 0xf;
  70. str[k++] = hex2char[h];
  71. str[k++] = hex2char[l];
  72. }
  73. str[k] = 0;
  74. return k;
  75. }
  76. static void __audiomgrlog(void* user_data, const char* fmt, va_list arg)
  77. {
  78. int n = vsnprintf(NULL, 0, fmt, arg);
  79. if (n >= MAX_PATH) {
  80. char* buf = (char*)malloc((size_t)(n + 1));
  81. vsnprintf(buf, n + 1, fmt, arg);
  82. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf);
  83. free(buf);
  84. }
  85. else {
  86. char strlog[MAX_PATH] = { 0 };
  87. vsnprintf(strlog, MAX_PATH, fmt, arg);
  88. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog);
  89. }
  90. }
  91. static void rvc_sleep(uint32_t umicroseconds)
  92. {
  93. #ifdef RVC_OS_WIN
  94. ::Sleep(umicroseconds);
  95. #else
  96. usleep(umicroseconds * 1000);
  97. #endif // RVC_OS_WIN
  98. }
  99. #ifdef RVC_OS_WIN
  100. static int translate_id(bool in_direction, int idx)
  101. {
  102. int i, n, ii;
  103. n = Pa_GetDeviceCount();
  104. for (i = 0, ii = 0; i < n; ++i) {
  105. const PaDeviceInfo* info = Pa_GetDeviceInfo(i);
  106. if (in_direction) {
  107. if (info->maxInputChannels) {
  108. if (ii == idx) {
  109. return i;
  110. }
  111. ii++;
  112. }
  113. }
  114. else {
  115. if (info->maxOutputChannels) {
  116. if (ii == idx) {
  117. return i;
  118. }
  119. ii++;
  120. }
  121. }
  122. }
  123. return -1;
  124. }
  125. static int audio_get_dev_count(int* in_cnt, int* out_cnt)
  126. {
  127. int icnt = 0, ocnt = 0;
  128. int cnt = Pa_GetDeviceCount();
  129. for (int i = 0; i < cnt; ++i) {
  130. const PaDeviceInfo* info = Pa_GetDeviceInfo(i);
  131. if (info->maxInputChannels)
  132. icnt++;
  133. if (info->maxOutputChannels)
  134. ocnt++;
  135. }
  136. if (in_cnt)
  137. *in_cnt = icnt;
  138. if (out_cnt)
  139. *out_cnt = ocnt;
  140. return 0;
  141. }
  142. static CSimpleStringA audio_get_dev_name(bool in_direction, int idx)
  143. {
  144. int cnt = Pa_GetDeviceCount();
  145. int ii, i;
  146. for (i = 0, ii = 0; i < cnt; ++i) {
  147. const PaDeviceInfo* info = Pa_GetDeviceInfo(i);
  148. if (in_direction) {
  149. if (info->maxInputChannels) {
  150. if (idx == ii) {
  151. CSimpleStringA strInDevice = CSimpleStringA(info->name);
  152. return strInDevice;
  153. }
  154. ii++;
  155. }
  156. }
  157. else {
  158. if (info->maxOutputChannels) {
  159. if (idx == ii) {
  160. CSimpleStringA strOutDevice = CSimpleStringA(info->name);
  161. return strOutDevice;
  162. }
  163. ii++;
  164. }
  165. }
  166. }
  167. return CSimpleStringA();
  168. }
  169. int capture_get_audio_device_id(bool in_direction, const char* dev_name)
  170. {
  171. if (NULL == dev_name) {
  172. return -1;
  173. }
  174. int cnt = Pa_GetDeviceCount();
  175. int ii, i;
  176. for (i = 0, ii = 0; i < cnt; ++i) {
  177. const PaDeviceInfo* info = Pa_GetDeviceInfo(i);
  178. if (in_direction) {
  179. if (info->maxInputChannels) {
  180. if (strstr(info->name, dev_name) != NULL) {
  181. return ii;
  182. }
  183. ii++;
  184. }
  185. }
  186. else {
  187. if (info->maxOutputChannels) {
  188. if (strstr(info->name, dev_name) != NULL) {
  189. return ii;
  190. }
  191. ii++;
  192. }
  193. }
  194. }
  195. return -1;
  196. }
  197. static int StreamCallback(const void *input,
  198. void *output,
  199. unsigned long frameCount,
  200. const PaStreamCallbackTimeInfo* timeInfo,
  201. PaStreamCallbackFlags statusFlags,
  202. void *userData)
  203. {
  204. audio_capture_t *audio_cap = (audio_capture_t*)userData;
  205. if (input) {
  206. audio_frame frm;
  207. frm.bitspersample = 16;
  208. frm.format = 1;
  209. frm.data = (char*)const_cast<void*>(input);
  210. frm.framesize = frameCount << 1;
  211. frm.nchannels = 1;
  212. frm.samplespersec = CAPTURE_CLOCK;
  213. if (INT_MAX == audio_cap->iseriesnumber++) {
  214. audio_cap->iseriesnumber = 0;
  215. }
  216. frm.iseriesnumber = audio_cap->iseriesnumber;
  217. if (!audio_cap->shm_queue->InsertAudio(&frm)) {
  218. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[StreamCallback] InsertAudio to shm_queue failed! frameCount:%d", frameCount);
  219. }
  220. else {
  221. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d InsertAudio to shm_queue success! and framesize is :%d, and shm_queue length is %d, frm.iseriesnumber = %d.", __FUNCTION__, __LINE__, frm.framesize, audio_cap->shm_queue->GetAudioLens(), frm.iseriesnumber);
  222. }
  223. if (!audio_cap->salesol_shm_queue->InsertAudio(&frm)) {
  224. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[StreamCallback] InsertAudio to salesol_shm_queue failed! frameCount:%d", frameCount);
  225. }
  226. else {
  227. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d InsertAudio to salesol_shm_queue success! and framesize is :%d, and salesol_shm_queue length is %d, frm.iseriesnumber = %d.", __FUNCTION__, __LINE__, frm.framesize, audio_cap->salesol_shm_queue->GetAudioLens(), frm.iseriesnumber);
  228. }
  229. }
  230. if (output) {
  231. memset(output, 0, frameCount<<1);
  232. }
  233. return paContinue;
  234. }
  235. static int Sales_StreamCallback(const void *input,
  236. void *output,
  237. unsigned long frameCount,
  238. const PaStreamCallbackTimeInfo* timeInfo,
  239. PaStreamCallbackFlags statusFlags,
  240. void *userData)
  241. {
  242. rvc_audio_capture_t *audio_cap = (rvc_audio_capture_t*)userData;
  243. if ((NULL != audio_cap) && (NULL != audio_cap->audio_shm_queue) && (NULL != input)){
  244. audio_frame frm;
  245. frm.bitspersample = 16;
  246. frm.format = 1;
  247. frm.data = (char*)const_cast<void*>(input);
  248. frm.framesize = frameCount << 1;
  249. frm.nchannels = 1;
  250. frm.samplespersec = audio_cap->iaudio_capture_samplerate;
  251. if (INT_MAX == audio_cap->iseriesnumber++){
  252. audio_cap->iseriesnumber = 0;
  253. }
  254. frm.iseriesnumber = audio_cap->iseriesnumber;
  255. if (!audio_cap->audio_shm_queue->InsertAudio(&frm)) {
  256. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[Sales_StreamCallback] InsertAudio failed! frameCount:%d", frameCount);
  257. }
  258. else {
  259. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d InsertAudio to audio_shm_queue success! and framesize is :%d, and shm_queue length is %d, frm.iseriesnumber = %d.", __FUNCTION__, __LINE__, frm.framesize, audio_cap->audio_shm_queue->GetAudioLens(), frm.iseriesnumber);
  260. }
  261. }
  262. if (output) {
  263. memset(output, 0, frameCount<<1);
  264. }
  265. return paContinue;
  266. }
  267. static int portaudio_capture_start(audio_capture_t* audio_cap)
  268. {
  269. capture_t *cap = audio_cap->parent;
  270. PaStreamParameters inParam = {0};
  271. PaStreamParameters salesInParam = {0};
  272. PaStreamParameters outParam = {0};
  273. PaError paError;
  274. const PaDeviceInfo *info;
  275. int nId = capture_get_audio_device_id(true, cap->config.strAudioIn.GetData());
  276. if (nId == -1)
  277. {
  278. LogWarn(Severity_Middle, Error_Debug,ERROR_MOD_MEDIACONTROLLER_HANDFREEIN_INITFAIL,"hand free in device config error,please check");
  279. return Error_AudioIN;
  280. }
  281. int in_dev_id = translate_id(true, nId);
  282. if (in_dev_id < 0) {
  283. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("audio in device translate failed!");
  284. return Error_AudioIN;
  285. }
  286. info = Pa_GetDeviceInfo(in_dev_id);
  287. if (!info) {
  288. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("get device info failed!");
  289. return Error_AudioIN;
  290. }
  291. inParam.channelCount = 1;
  292. inParam.device = in_dev_id;
  293. inParam.suggestedLatency = info->defaultLowInputLatency;
  294. inParam.sampleFormat = paInt16;
  295. inParam.hostApiSpecificStreamInfo = NULL;
  296. if (Pa_IsFormatSupported(&inParam, NULL, CAPTURE_CLOCK) != paNoError) {
  297. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("audio capture create error, cannot open audio input device");
  298. return Error_AudioIN;
  299. }
  300. nId = capture_get_audio_device_id(false, cap->config.strAudioOut.GetData());
  301. if (nId == -1)
  302. {
  303. LogWarn(Severity_Middle, Error_Debug,ERROR_MOD_MEDIACONTROLLER_HANDFREEOUT_INITFAIL,"hand free out device config error,please check");
  304. return Error_AudioOut;
  305. }
  306. int out_dev_id = translate_id(false, nId);
  307. if (out_dev_id < 0) {
  308. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("audio out device translate failed!");
  309. return Error_AudioOut;
  310. }
  311. info = Pa_GetDeviceInfo(out_dev_id);
  312. if (!info) {
  313. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("get device info failed!");
  314. return Error_AudioOut;
  315. }
  316. outParam.channelCount = 1;
  317. outParam.device = out_dev_id;
  318. outParam.suggestedLatency = info->defaultLowOutputLatency;
  319. outParam.sampleFormat = paInt16;
  320. outParam.hostApiSpecificStreamInfo = NULL;
  321. if (Pa_IsFormatSupported(NULL, &outParam, CAPTURE_CLOCK) != paNoError) {
  322. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("audio capture create error, cannot open audio input device");
  323. return Error_AudioOut;
  324. }
  325. paError = Pa_OpenStream(&audio_cap->stream, &inParam, NULL, CAPTURE_CLOCK,
  326. CAPTURE_FRAME_TIME * CAPTURE_CLOCK/1000, paClipOff|paDitherOff, &StreamCallback, audio_cap);
  327. if (paError != paNoError) {
  328. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("port audio open stream failed! paError = %d", paError);
  329. return Error_AudioIN;
  330. }
  331. paError = Pa_StartStream(audio_cap->stream);
  332. if (paError != paNoError) {
  333. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("port audio start stream failed! paError = %d", paError);
  334. return Error_AudioIN;
  335. }
  336. return Error_Succeed;
  337. }
  338. static void portaudio_capture_stop(audio_capture_t* audio_cap)
  339. {
  340. if (audio_cap->stream) {
  341. Pa_AbortStream(audio_cap->stream);
  342. Pa_CloseStream(audio_cap->stream);
  343. audio_cap->stream = NULL;
  344. }
  345. }
  346. static int record_portaudio_capture_start(rvc_audio_capture_t* audio_cap)
  347. {
  348. rvc_sales_audio_capture_t* cap = audio_cap->parent;
  349. PaStreamParameters salesInParam = { 0 };
  350. PaError paError;
  351. const PaDeviceInfo* info;
  352. int nId = capture_get_audio_device_id(true, cap->rvc_audio_config.strAudioIn.GetData());
  353. if (nId == -1)
  354. {
  355. LogWarn(Severity_Middle, Error_Debug,ERROR_MOD_MEDIACONTROLLER_HANDFREEIN_INITFAIL,"hand free in device config error,please check");
  356. return Error_AudioIN;
  357. }
  358. int in_dev_id = translate_id(true, nId);
  359. if (in_dev_id < 0) {
  360. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("sales audio in device translate failed!");
  361. return Error_AudioIN;
  362. }
  363. info = Pa_GetDeviceInfo(in_dev_id);
  364. if (!info) {
  365. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("get device info failed!");
  366. return Error_AudioIN;
  367. }
  368. salesInParam.channelCount = 1;
  369. salesInParam.device = in_dev_id;
  370. salesInParam.suggestedLatency = info->defaultLowInputLatency;
  371. salesInParam.sampleFormat = paInt16;
  372. salesInParam.hostApiSpecificStreamInfo = NULL;
  373. if (Pa_IsFormatSupported(&salesInParam, NULL, audio_cap->iaudio_capture_samplerate) != paNoError) {
  374. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("sales audio capture create error, cannot open audio input device, and current capture sample rate is %d.",audio_cap->iaudio_capture_samplerate);
  375. return Error_AudioIN;
  376. }
  377. paError = Pa_OpenStream(&audio_cap->stream, &salesInParam, NULL, audio_cap->iaudio_capture_samplerate,
  378. audio_cap->iaudio_capture_peroid * audio_cap->iaudio_capture_samplerate/1000, paClipOff|paDitherOff, &Sales_StreamCallback, audio_cap);
  379. if (paError != paNoError) {
  380. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("port audio open sales stream failed! paError = %d", paError);
  381. return Error_AudioIN;
  382. }
  383. paError = Pa_StartStream(audio_cap->stream);
  384. if (paError != paNoError) {
  385. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("port audio start sales stream failed! paError = %d", paError);
  386. return Error_AudioIN;
  387. }
  388. return Error_Succeed;
  389. }
  390. static void record_portaudio_capture_stop(rvc_audio_capture_t* audio_cap)
  391. {
  392. if (NULL != audio_cap) {
  393. if (audio_cap->stream) {
  394. PaError Error = Pa_AbortStream(audio_cap->stream);
  395. if (paNoError == Error) {
  396. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Pa_AbortStream no error.");
  397. }
  398. Error = Pa_CloseStream(audio_cap->stream);
  399. if (paNoError == Error) {
  400. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Pa_CloseStream no error.");
  401. }
  402. audio_cap->stream = NULL;
  403. }
  404. audio_cap->iseriesnumber = 0;
  405. }
  406. }
  407. #else
  408. static void audio_data_callback(const void* input, unsigned long audiolen, void* userdata)
  409. {
  410. audio_capture_t* audio_cap = (audio_capture_t*)userdata;
  411. if (input) {
  412. int ileft = audiolen;
  413. int index = 0;
  414. while (audio_cap->uaudiolen + ileft >= RVC_AUDIO_BUFFER_LEN)
  415. {
  416. int ineed = RVC_AUDIO_BUFFER_LEN - audio_cap->uaudiolen;
  417. memcpy(audio_cap->paudio_buffer + audio_cap->uaudiolen, input + index, ineed);
  418. ileft -= ineed;
  419. audio_cap->uaudiolen += ineed;
  420. index += ineed;
  421. audio_frame frm;
  422. frm.bitspersample = 16;
  423. frm.format = 1;
  424. frm.data = audio_cap->paudio_buffer;
  425. frm.framesize = RVC_AUDIO_BUFFER_LEN;
  426. frm.nchannels = 1;
  427. frm.samplespersec = 8000;
  428. if (INT_MAX == audio_cap->iseriesnumber++) {
  429. audio_cap->iseriesnumber = 0;
  430. }
  431. frm.iseriesnumber = audio_cap->iseriesnumber;
  432. if (!audio_cap->shm_queue->InsertAudio(&frm)) {
  433. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d InsertAudio to shm_queue failed!", __FUNCTION__, __LINE__);
  434. }
  435. //else {
  436. // DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d InsertAudio to shm_queue success! and framesize is :%d, and shm_queue length is %d, frm.iseriesnumber = %d.", __FUNCTION__, __LINE__, frm.framesize, audio_cap->shm_queue->GetAudioLens(), frm.iseriesnumber);
  437. //}
  438. audio_cap->uaudiolen -= RVC_AUDIO_BUFFER_LEN;
  439. }
  440. if (ileft > 0){
  441. memcpy(audio_cap->paudio_buffer + audio_cap->uaudiolen, input + index, ileft);
  442. audio_cap->uaudiolen += ileft;
  443. }
  444. }
  445. return;
  446. }
  447. static int pulseaudio_capture_start(audio_capture_t* audio_cap)
  448. {
  449. int iret = -1;
  450. capture_t* cap = audio_cap->parent;
  451. if (NULL != audio_cap->paudiocap){
  452. audiocap_param_t param = { 0 };
  453. int nId = audio_cap->paudiocap->audio_get_device_id(cap->config.strAudioIn.GetData(), true);
  454. if (nId == -1)
  455. {
  456. LogWarn(Severity_Middle, Error_Debug, ERROR_MOD_MEDIACONTROLLER_HANDFREEIN_INITFAIL, "hand free in device config error,please check");
  457. return Error_AudioIN;
  458. }
  459. param.ideviceid = nId;
  460. param.ichannels = 1;
  461. param.isampleformat = 3 /*PA_SAMPLE_S16LE*/;
  462. param.isamprate = 8000;
  463. param.flatency = 0.02;
  464. param.on_audio_callback = &audio_data_callback;
  465. param.user_data = audio_cap;
  466. audio_cap->paudiocap->set_audio_capture_params(&param);
  467. iret = audio_cap->paudiocap->start_audio_capture();
  468. }
  469. return iret;
  470. }
  471. static void pulseaudio_capture_stop(audio_capture_t* audio_cap)
  472. {
  473. if (audio_cap->paudiocap) {
  474. audio_cap->paudiocap->stop_audio_capture();
  475. }
  476. }
  477. static void record_audio_data_callback(const void* input, unsigned long audiolen, void* userdata)
  478. {
  479. rvc_audio_capture_t* audio_cap = (rvc_audio_capture_t*)userdata;
  480. if (input) {
  481. int ileft = audiolen;
  482. int index = 0;
  483. while (audio_cap->uaudiolen + ileft >= RVC_AUDIO_BUFFER_LEN)
  484. {
  485. int ineed = RVC_AUDIO_BUFFER_LEN - audio_cap->uaudiolen;
  486. memcpy(audio_cap->paudio_buffer + audio_cap->uaudiolen, input + index, ineed);
  487. ileft -= ineed;
  488. audio_cap->uaudiolen += ineed;
  489. index += ineed;
  490. audio_frame frm;
  491. frm.bitspersample = 16;
  492. frm.format = 1;
  493. frm.data = audio_cap->paudio_buffer;
  494. frm.framesize = RVC_AUDIO_BUFFER_LEN;
  495. frm.nchannels = 1;
  496. frm.samplespersec = 8000;
  497. frm.iseriesnumber = audio_cap->iseriesnumber++;
  498. if (audio_cap->iseriesnumber >= INT_MAX) {
  499. audio_cap->iseriesnumber = 0;
  500. }
  501. if (!audio_cap->audio_shm_queue->InsertAudio(&frm)) {
  502. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d InsertAudio to audio_shm_queue failed!", __FUNCTION__, __LINE__);
  503. }
  504. else {
  505. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d InsertAudio to audio_shm_queue success!", __FUNCTION__, __LINE__);
  506. }
  507. audio_cap->uaudiolen -= RVC_AUDIO_BUFFER_LEN;
  508. }
  509. if (ileft > 0) {
  510. memcpy(audio_cap->paudio_buffer + audio_cap->uaudiolen, input + index, ileft);
  511. audio_cap->uaudiolen += ileft;
  512. }
  513. }
  514. return;
  515. }
  516. static int record_pulseaudio_capture_start(rvc_audio_capture_t* audio_cap)
  517. {
  518. int iret = -1;
  519. rvc_sales_audio_capture_t* cap = audio_cap->parent;
  520. if (NULL != audio_cap->paudiocap) {
  521. audiocap_param_t param = { 0 };
  522. int nId = audio_cap->paudiocap->audio_get_device_id(cap->rvc_audio_config.strAudioIn.GetData(), true);
  523. if (nId == -1){
  524. LogWarn(Severity_Middle, Error_Debug, ERROR_MOD_MEDIACONTROLLER_HANDFREEIN_INITFAIL, "hand free in device config error,please check");
  525. return Error_AudioIN;
  526. }
  527. param.ideviceid = nId;
  528. param.ichannels = 1;
  529. param.isampleformat = 3 /*PA_SAMPLE_S16LE*/;
  530. param.isamprate = 8000;
  531. param.flatency = 0.02;
  532. param.on_audio_callback = &record_audio_data_callback;
  533. param.user_data = audio_cap;
  534. audio_cap->paudiocap->set_audio_capture_params(&param);
  535. iret = audio_cap->paudiocap->start_audio_capture();
  536. }
  537. return iret;
  538. }
  539. static void record_pulseaudio_capture_stop(rvc_audio_capture_t* audio_cap)
  540. {
  541. if (audio_cap->paudiocap) {
  542. audio_cap->paudiocap->stop_audio_capture();
  543. }
  544. }
  545. #endif
  546. static rvc_audio_capture_t *salesrecord_audio_capture_create(rvc_sales_audio_capture_t *cap)
  547. {
  548. rvc_audio_capture_t *audio_cap = ZALLOC_T(rvc_audio_capture_t);
  549. if (audio_cap) {
  550. audio_cap->parent = cap;
  551. audio_cap->audio_shm_queue = new Clibaudioqueue(REC_COMMON_AUDIO_SALES_SHM_QUEUE);
  552. //audio_cap->remote_audio_queue = new Clibaudioqueue(REC_COMMON_REMOTEAUDIO_SHM_QUEUE);
  553. audio_cap->iseriesnumber = 0;
  554. }
  555. #ifdef RVC_OS_LINUX
  556. audio_cap->paudio_buffer = new char[RVC_AUDIO_BUFFER_LEN];
  557. audio_cap->uaudiolen = 0;
  558. #endif
  559. return audio_cap;
  560. }
  561. static void salesrecord_audio_capture_destroy(rvc_audio_capture_t *audio_cap)
  562. {
  563. if (NULL != audio_cap){
  564. if (NULL != audio_cap->audio_shm_queue){
  565. delete audio_cap->audio_shm_queue;
  566. audio_cap->audio_shm_queue = NULL;
  567. }
  568. #ifdef RVC_OS_LINUX
  569. if (NULL != audio_cap->paudio_buffer){
  570. delete audio_cap->paudio_buffer;
  571. audio_cap->paudio_buffer = NULL;
  572. audio_cap->uaudiolen = 0;
  573. }
  574. #endif
  575. FREE(audio_cap);
  576. }
  577. }
  578. static audio_capture_t *audio_capture_create(capture_t *cap)
  579. {
  580. audio_capture_t *audio_cap = ZALLOC_T(audio_capture_t);
  581. if (audio_cap) {
  582. audio_cap->parent = cap;
  583. audio_cap->shm_queue = new Clibaudioqueue(REC_COMMON_AUDIO_SHM_QUEUE);
  584. audio_cap->salesol_shm_queue = new Clibaudioqueue(REC_COMMON_AUDIO_SALESOL_SHM_QUEUE);
  585. audio_cap->iseriesnumber = 0;
  586. #ifdef RVC_OS_LINUX
  587. audio_cap->paudio_buffer = new char[RVC_AUDIO_BUFFER_LEN];
  588. audio_cap->uaudiolen = 0;
  589. #endif
  590. }
  591. return audio_cap;
  592. }
  593. static void audio_capture_destroy(audio_capture_t *audio_cap)
  594. {
  595. delete audio_cap->shm_queue;
  596. audio_cap->shm_queue = NULL;
  597. delete audio_cap->salesol_shm_queue;
  598. audio_cap->salesol_shm_queue = NULL;
  599. #ifdef RVC_OS_LINUX
  600. if (NULL != audio_cap->paudio_buffer) {
  601. delete audio_cap->paudio_buffer;
  602. audio_cap->paudio_buffer = NULL;
  603. audio_cap->uaudiolen = 0;
  604. }
  605. #endif
  606. FREE(audio_cap);
  607. }
  608. static int audio_capture_start(audio_capture_t* audio_cap)
  609. {
  610. #ifdef RVC_OS_WIN
  611. return portaudio_capture_start(audio_cap);
  612. #else
  613. return pulseaudio_capture_start(audio_cap);
  614. #endif
  615. }
  616. static void audio_capture_stop(audio_capture_t* audio_cap)
  617. {
  618. #ifdef RVC_OS_WIN
  619. return portaudio_capture_stop(audio_cap);
  620. #else
  621. return pulseaudio_capture_stop(audio_cap);
  622. #endif
  623. }
  624. static int record_audio_capture_start(rvc_audio_capture_t* audio_cap)
  625. {
  626. #ifdef RVC_OS_WIN
  627. return record_portaudio_capture_start(audio_cap);
  628. #else
  629. return record_pulseaudio_capture_start(audio_cap);
  630. #endif
  631. }
  632. static void record_audio_capture_stop(rvc_audio_capture_t* audio_cap)
  633. {
  634. #ifdef RVC_OS_WIN
  635. return record_portaudio_capture_stop(audio_cap);
  636. #else
  637. return record_pulseaudio_capture_stop(audio_cap);
  638. #endif
  639. }
  640. static int calc_capture_mode(int width, int height, int *mode)
  641. {
  642. const struct {
  643. int mode;
  644. int width;
  645. int height;
  646. } modes [] = {
  647. {VIDEOCAP_FRAME_SQCIF, VIDEOCAP_SQCIF_WIDTH, VIDEOCAP_SQCIF_HEIGHT},
  648. {VIDEOCAP_FRAME_QQVGA, VIDEOCAP_QQVGA_WIDTH, VIDEOCAP_QQVGA_HEIGHT},
  649. {VIDEOCAP_FRAME_QCIF, VIDEOCAP_QCIF_WIDTH, VIDEOCAP_QCIF_HEIGHT},
  650. {VIDEOCAP_FRAME_QVGA, VIDEOCAP_QVGA_WIDTH, VIDEOCAP_QVGA_HEIGHT},
  651. {VIDEOCAP_FRAME_CIF, VIDEOCAP_CIF_WIDTH, VIDEOCAP_CIF_HEIGHT},
  652. {VIDEOCAP_FRAME_VGA, VIDEOCAP_VGA_WIDTH, VIDEOCAP_VGA_HEIGHT},
  653. {VIDEOCAP_FRAME_4CIF, VIDEOCAP_4CIF_WIDTH, VIDEOCAP_4CIF_HEIGHT},
  654. {VIDEOCAP_FRAME_SVGA, VIDEOCAP_SVGA_WIDTH, VIDEOCAP_SVGA_HEIGHT},
  655. {VIDEOCAP_FRAME_NHD, VIDEOCAP_NHD_WIDTH, VIDEOCAP_NHD_HEIGHT},
  656. {VIDEOCAP_FRAME_SXGA, VIDEOCAP_SXGA_WIDTH, VIDEOCAP_SXGA_HEIGHT},
  657. {VIDEOCAP_FRAME_720P, VIDEOCAP_720P_WIDTH, VIDEOCAP_720P_HEIGHT},
  658. {VIDEOCAP_FRAME_1080P, VIDEOCAP_1080P_WIDTH, VIDEOCAP_1080P_HEIGHT},
  659. };
  660. int i;
  661. for (i = 0; i < array_size(modes); ++i) {
  662. if (modes[i].width == width && modes[i].height == height) {
  663. *mode = modes[i].mode;
  664. return 0;
  665. }
  666. }
  667. return Error_NotExist;
  668. }
  669. static int video_shm_enqueue(Clibvideoqueue *shm_queue, video_frame *frame, unsigned int ucaptime, int flags, int iframeid)
  670. {
  671. videoq_frame tmp_frm;
  672. tmp_frm.data = frame->data[0];
  673. if (VIDEO_FORMAT_RGB24 == frame->format){
  674. tmp_frm.framesize = frame->width * frame->height * 3;
  675. tmp_frm.format = VIDEOQ_FORMAT_RGB24;
  676. }
  677. else {
  678. tmp_frm.framesize = frame->width * frame->height * 3/2;
  679. tmp_frm.format = VIDEOQ_FORMAT_I420;
  680. }
  681. tmp_frm.width = frame->width;
  682. tmp_frm.height = frame->height;
  683. tmp_frm.iframeid = iframeid;
  684. //unsigned int nowtime = y2k_time_now();
  685. if (!shm_queue->InsertVideo(&tmp_frm, flags, ucaptime)) {
  686. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("caution: insert shm video failed!");
  687. return Error_Unexpect;
  688. } else {
  689. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("insert shm video ok, and video frame id is %d.", tmp_frm.iframeid);
  690. return Error_Succeed;
  691. }
  692. }
  693. static void env_cap_on_frame(void *user_data, video_frame *frame)
  694. {
  695. video_capture_t *video_cap = (video_capture_t *)user_data;
  696. capture_t *cap = video_cap->parent;
  697. int rc;
  698. video_cap->frame_id++;
  699. video_cap->ulastcaptime = y2k_time_now();
  700. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("******************* env on frame, id=%d, ulastcaptime=%u", video_cap->frame_id, video_cap->ulastcaptime);
  701. //IplImage*img = NULL;
  702. //img = cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,3);
  703. //img->imageData = (char*)frame->data[0];
  704. //cvSaveImage("./env.jpg", img, 0);
  705. //cvReleaseImageHeader(&img);
  706. rc = video_shm_enqueue(video_cap->snapshot_shm_queue, frame, video_cap->ulastcaptime, VIDEOQUEUE_FLAG_VERTICAL_FLIP, video_cap->frame_id);
  707. //static int isnapshot_shm_queue = 0;
  708. //if (isnapshot_shm_queue == 0) {
  709. // video_frame_save_bmpfile("snapshot_shm_queue_1.bmp", frame);
  710. // isnapshot_shm_queue++;
  711. //}
  712. if (rc != Error_Succeed)
  713. {
  714. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("env snapshot queue enqueue failed! Error = %d, camera_type=%d.", rc, video_cap->camera_type);
  715. }
  716. else {
  717. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("env snapshot queue enqueue[%d] success and camera_type=%d.", video_cap->frame_id, video_cap->camera_type);
  718. }
  719. // snapshot
  720. if (rc==Error_Succeed)
  721. {
  722. if (*cap->config.ref_env_capture_count)
  723. {
  724. #ifdef RVC_OS_WIN
  725. InterlockedDecrement(cap->config.ref_env_capture_count);
  726. #else
  727. pthread_mutex_lock(cap->config.env_mutex);
  728. *(cap->config.ref_env_capture_count) -= 1;
  729. pthread_mutex_unlock(cap->config.env_mutex);
  730. #endif
  731. LogEvent(Severity_Middle, MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_ENV, "agent capture env ok, and capture env finished!");
  732. }
  733. else if (*cap->config.ref_envopt_capture_count & 2)
  734. {
  735. #ifdef RVC_OS_WIN
  736. _InterlockedAnd(cap->config.ref_envopt_capture_count, 0xfffffffD);
  737. #else
  738. pthread_mutex_lock(cap->config.envopt_mutex);
  739. LONG lcount = *(cap->config.ref_envopt_capture_count);
  740. *(cap->config.ref_envopt_capture_count) = (lcount & 0xfffffffD);
  741. pthread_mutex_unlock(cap->config.envopt_mutex);
  742. #endif // RVC_OS_WIN
  743. if (*cap->config.ref_envopt_capture_count == 0)
  744. {
  745. LogEvent(Severity_Middle, MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_ENVOPT, "agent capture env ok, and capture env opt finished!");
  746. }
  747. }
  748. }
  749. // preview
  750. {
  751. video_frame preview_frame;
  752. video_frame_alloc(REC_COMMON_VIDEO_PREVIEW_WIDTH, REC_COMMON_VIDEO_PREVIEW_HEIGHT, VIDEO_FORMAT_RGB24, &preview_frame);
  753. uint8_t *src_data[4] = {frame->data[0] + 80*3, 0, 0, 0};
  754. sws_scale(video_cap->preview_sws_ctx, src_data, frame->linesize, 0, frame->height, preview_frame.data, preview_frame.linesize);
  755. video_shm_enqueue(video_cap->preview_shm_queue, &preview_frame, video_cap->ulastcaptime, 0, video_cap->frame_id);
  756. if (rc != Error_Succeed){
  757. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("env preview_shm_queue enqueue failed, error = %d", rc);
  758. }
  759. else {
  760. //if (0 == video_cap->frame_id%10){
  761. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("env preview_shm_queue enqueue[%d] success, and current video queue len is %d.", video_cap->frame_id, video_cap->preview_shm_queue->GetVideoLens());
  762. //}
  763. }
  764. video_frame_free(&preview_frame);
  765. //static int ipreview_shm_queue = 0;
  766. //char strname[MAX_PATH] = { 0 };
  767. //snprintf(strname, MAX_PATH, "env_preview_shm_queue_%d.bmp", ipreview_shm_queue++);
  768. //video_frame_save_bmpfile(strname, &preview_frame);
  769. }
  770. // rtp
  771. {
  772. video_frame rtp_frame;
  773. video_frame_alloc(REC_COMMON_VIDEO_RTP_ENV_WIDTH, REC_COMMON_VIDEO_RTP_ENV_HEIGHT, VIDEO_FORMAT_RGB24, &rtp_frame);
  774. uint8_t *src_data[4] = {frame->data[0] + (frame->height-1) * frame->linesize[0], 0, 0, 0};
  775. int src_linesize[4] = {-frame->linesize[0], 0, 0, 0};
  776. sws_scale(video_cap->rtp_sws_ctx, src_data, src_linesize, 0, frame->height, rtp_frame.data, rtp_frame.linesize);
  777. video_shm_enqueue(video_cap->rtp_shm_queue, &rtp_frame, video_cap->ulastcaptime, 0, video_cap->frame_id);
  778. if (rc != Error_Succeed) {
  779. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("env rtp_shm_queue enqueue failed! Error = %d", rc);
  780. }
  781. else {
  782. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("env rtp_shm_queue enqueue[%d] success, and current video queue len is %d.", video_cap->frame_id, video_cap->rtp_shm_queue->GetVideoLens());
  783. }
  784. //static int irtp_frame = 0;
  785. //if (irtp_frame == 0){
  786. // video_frame_save_bmpfile("rtp_shm_queue.bmp", &rtp_frame);
  787. // irtp_frame++;
  788. //}
  789. #if 0
  790. static int i = 0;
  791. if (i == 0) {
  792. video_frame tmp_frame;
  793. video_frame_alloc(REC_COMMON_VIDEO_RTP_ENV_WIDTH, REC_COMMON_VIDEO_RTP_ENV_HEIGHT, VIDEO_FORMAT_RGB24, &tmp_frame);
  794. video_frame_fill_black(&tmp_frame);
  795. videoq_frame frm;
  796. frm.data = tmp_frame.data[0];
  797. video_cap->rtp_shm_queue->GetVideo(&frm, 0);
  798. video_frame_save_bmpfile("rtp_videoqueue_abc.bmp", &tmp_frame);
  799. video_frame_free(&tmp_frame);
  800. //video_frame_save_bmpfile("d:\\ab.bmp", &rtp_frame);
  801. i++;
  802. }
  803. #endif
  804. video_frame_free(&rtp_frame);
  805. }
  806. }
  807. static void opt_cap_on_frame(void *user_data, video_frame *frame)
  808. {
  809. video_capture_t *video_cap = (video_capture_t *)user_data;
  810. capture_t *cap = video_cap->parent;
  811. int rc;
  812. int rotate = 0;
  813. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("opt on frame!");
  814. if (cap->config.video_opt_rotate == 90) {
  815. rotate = 1;
  816. } else if (cap->config.video_opt_rotate == 270) {
  817. rotate = -1;
  818. } else {
  819. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("video_opt_rotate is not 90 or 270 return.");
  820. return;
  821. }
  822. video_cap->frame_id++;
  823. video_cap->ulastcaptime = y2k_time_now();
  824. #ifdef RVC_OS_WIN // use IPP, it's fast
  825. // prepare for rotation
  826. video_frame rframe;
  827. video_frame_alloc(frame->height, frame->width, frame->format, &rframe);
  828. {
  829. IppiSize srcSize;
  830. srcSize.width = frame->width;
  831. srcSize.height = frame->height;
  832. IppiRect srcROI;
  833. srcROI.width = frame->width;
  834. srcROI.height = frame->height;
  835. srcROI.x = 0;
  836. srcROI.y = 0;
  837. IppiRect dstROI;
  838. dstROI.width = frame->height;
  839. dstROI.height = frame->width;
  840. dstROI.x = 0;
  841. dstROI.y = 0;
  842. ippiTranspose_8u_C3R(frame->data[0], frame->linesize[0], rframe.data[0], rframe.linesize[0], srcSize);
  843. IppiAxis flip;
  844. IppiSize Size;
  845. Size.width = rframe.width;
  846. Size.height = rframe.height;
  847. if (rotate == 1) {
  848. flip = ippAxsHorizontal;
  849. ippiMirror_8u_C3IR(rframe.data[0], rframe.linesize[0], Size, flip);
  850. }
  851. else {
  852. flip = ippAxsVertical;
  853. ippiMirror_8u_C3IR(rframe.data[0], rframe.linesize[0], Size, flip);
  854. }
  855. }
  856. rc = video_shm_enqueue(video_cap->snapshot_shm_queue, &rframe, video_cap->ulastcaptime, 0, video_cap->frame_id);
  857. #else
  858. rc = video_shm_enqueue(video_cap->snapshot_shm_queue, frame, video_cap->ulastcaptime, 0, video_cap->frame_id);
  859. #endif
  860. if (rc != Error_Succeed)
  861. {
  862. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("opt snapshot queue enqueue shm failed! Error = %d, camera_type=%d", rc, video_cap->camera_type);
  863. }
  864. else {
  865. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("opt snapshot queue enqueue[%d] success, and current video queue len is %d.", video_cap->frame_id, video_cap->snapshot_shm_queue->GetVideoLens());
  866. }
  867. //static int isnapshot_shm_queue = 0;
  868. //if (isnapshot_shm_queue == 0) {
  869. // video_frame_save_bmpfile("opt_snapshot_shm_queue.bmp", &rframe);
  870. // isnapshot_shm_queue++;
  871. //}
  872. // snapshot
  873. if (rc==Error_Succeed)
  874. {
  875. if (*cap->config.ref_opt_capture_count)
  876. {
  877. #ifdef RVC_OS_WIN
  878. InterlockedDecrement(cap->config.ref_opt_capture_count);
  879. #else
  880. pthread_mutex_lock(cap->config.opt_mutex);
  881. *(cap->config.ref_opt_capture_count) -= 1;
  882. pthread_mutex_unlock(cap->config.opt_mutex);
  883. #endif
  884. LogEvent(Severity_Middle, MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_OPT, "agent capture opt ok, and capture opt finished!");
  885. }
  886. else if (*cap->config.ref_envopt_capture_count&1)
  887. {
  888. #ifdef RVC_OS_WIN
  889. if (InterlockedDecrement(cap->config.ref_envopt_capture_count) == 0)
  890. #else
  891. pthread_mutex_lock(cap->config.envopt_mutex);
  892. *(cap->config.ref_envopt_capture_count) -= 1;
  893. pthread_mutex_unlock(cap->config.envopt_mutex);
  894. if (0 == *cap->config.ref_envopt_capture_count)
  895. #endif
  896. {
  897. LogEvent(Severity_Middle, MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_ENVOPT, "agent capture opt ok, and capture envopt finished!");
  898. }
  899. }
  900. }
  901. // rtp
  902. {
  903. video_frame rtp_frame = {0};
  904. video_frame_alloc(REC_COMMON_VIDEO_RTP_OPT_WIDTH, REC_COMMON_VIDEO_RTP_OPT_HEIGHT, VIDEO_FORMAT_RGB24, &rtp_frame);
  905. #ifdef RVC_OS_WIN
  906. sws_scale(video_cap->rtp_sws_ctx, rframe.data, rframe.linesize, 0, rframe.height, rtp_frame.data, rtp_frame.linesize);
  907. #else
  908. sws_scale(video_cap->rtp_sws_ctx, frame->data, frame->linesize, 0, frame->height, rtp_frame.data, rtp_frame.linesize);
  909. #endif
  910. rc = video_shm_enqueue(video_cap->rtp_shm_queue, &rtp_frame, video_cap->ulastcaptime, 0, video_cap->frame_id);
  911. if (rc != Error_Succeed){
  912. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("opt rtp_shm_queue enqueue shm failed! Error = %d, camera_type=%d", rc, video_cap->camera_type);
  913. }
  914. //static int irtp_frame = 0;
  915. //if (irtp_frame == 0){
  916. // video_frame_save_bmpfile("opt_rtp_shm_queue.bmp", &rtp_frame);
  917. // irtp_frame++;
  918. //}
  919. video_frame_free(&rtp_frame);
  920. }
  921. // preview
  922. {
  923. video_frame preview_frame;
  924. video_frame_alloc(REC_COMMON_VIDEO_PREVIEW_HEIGHT, REC_COMMON_VIDEO_PREVIEW_WIDTH, VIDEO_FORMAT_RGB24, &preview_frame);
  925. #ifdef RVC_OS_WIN
  926. uint8_t *src_data[4] = {rframe.data[0] + rframe.linesize[0]*80, 0, 0, 0};
  927. sws_scale(video_cap->preview_sws_ctx, src_data, rframe.linesize, 0, REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_WIDTH, preview_frame.data, preview_frame.linesize);
  928. #else
  929. uint8_t* src_data[4] = { frame->data[0] + frame->linesize[0] * 80, 0, 0, 0 };
  930. sws_scale(video_cap->preview_sws_ctx, src_data, frame->linesize, 0, REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_WIDTH, preview_frame.data, preview_frame.linesize);
  931. #endif
  932. video_shm_enqueue(video_cap->preview_shm_queue, &preview_frame, video_cap->ulastcaptime, 1, video_cap->frame_id);
  933. //static int ipreview_shm_queue = 0;
  934. //char strname[MAX_PATH] = { 0 };
  935. //snprintf(strname, MAX_PATH, "opt_preview_shm_queue_%d.bmp", ipreview_shm_queue++);
  936. //video_frame_save_bmpfile(strname, &preview_frame);
  937. video_frame_free(&preview_frame);
  938. }
  939. #ifdef RVC_OS_WIN
  940. video_frame_free(&rframe);
  941. #endif
  942. }
  943. static video_capture_t *video_capture_create(capture_t *cap, int camera_type)
  944. {
  945. video_capture_t *video_cap = ZALLOC_T(video_capture_t);
  946. if (video_cap) {
  947. video_cap->parent = cap;
  948. video_cap->camera_type = camera_type;
  949. video_cap->frame_id = 0;
  950. if (camera_type == CAMERA_TYPE_ENV) {
  951. video_cap->snapshot_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_SNAPSHOT_QUEUE);
  952. video_cap->rtp_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_RTP_QUEUE);
  953. video_cap->rtp_sws_ctx = sws_getContext(REC_COMMON_VIDEO_SNAPSHOT_WIDTH,
  954. REC_COMMON_VIDEO_SNAPSHOT_HEIGHT,
  955. AV_PIX_FMT_BGR24,
  956. REC_COMMON_VIDEO_RTP_ENV_WIDTH,
  957. REC_COMMON_VIDEO_RTP_ENV_HEIGHT,
  958. AV_PIX_FMT_BGR24,
  959. SWS_POINT, NULL, NULL, NULL);
  960. video_cap->preview_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_PREVIEW_QUEUE);
  961. video_cap->preview_sws_ctx = sws_getContext(REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_WIDTH,
  962. REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_HEIGHT,
  963. AV_PIX_FMT_BGR24,
  964. REC_COMMON_VIDEO_PREVIEW_WIDTH,
  965. REC_COMMON_VIDEO_PREVIEW_HEIGHT,
  966. AV_PIX_FMT_BGR24,
  967. SWS_FAST_BILINEAR, NULL, NULL, NULL);
  968. }
  969. else {
  970. video_cap->snapshot_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_SNAPSHOT_QUEUE);
  971. video_cap->rtp_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_RTP_QUEUE);
  972. video_cap->rtp_sws_ctx = sws_getContext(REC_COMMON_VIDEO_SNAPSHOT_HEIGHT,
  973. REC_COMMON_VIDEO_SNAPSHOT_WIDTH,
  974. AV_PIX_FMT_BGR24,
  975. REC_COMMON_VIDEO_RTP_OPT_WIDTH,
  976. REC_COMMON_VIDEO_RTP_OPT_HEIGHT,
  977. AV_PIX_FMT_BGR24,
  978. SWS_POINT, NULL, NULL, NULL);
  979. video_cap->preview_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_PREVIEW_QUEUE);
  980. video_cap->preview_sws_ctx = sws_getContext(REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_HEIGHT,
  981. REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_WIDTH,
  982. AV_PIX_FMT_BGR24,
  983. REC_COMMON_VIDEO_PREVIEW_HEIGHT,
  984. REC_COMMON_VIDEO_PREVIEW_WIDTH,
  985. AV_PIX_FMT_BGR24,
  986. SWS_FAST_BILINEAR, NULL, NULL, NULL);
  987. }
  988. }
  989. return video_cap;
  990. }
  991. static void video_capture_destroy(video_capture_t *video_cap)
  992. {
  993. if (video_cap) {
  994. if (video_cap->preview_sws_ctx) {
  995. sws_freeContext(video_cap->preview_sws_ctx);
  996. video_cap->preview_sws_ctx = NULL;
  997. }
  998. if (video_cap->rtp_sws_ctx) {
  999. sws_freeContext(video_cap->rtp_sws_ctx);
  1000. video_cap->rtp_sws_ctx = NULL;
  1001. }
  1002. if (video_cap->snapshot_shm_queue) {
  1003. delete video_cap->snapshot_shm_queue;
  1004. video_cap->snapshot_shm_queue = NULL;
  1005. }
  1006. if (video_cap->rtp_shm_queue) {
  1007. delete video_cap->rtp_shm_queue;
  1008. video_cap->rtp_shm_queue = NULL;
  1009. }
  1010. if (video_cap->preview_shm_queue) {
  1011. delete video_cap->preview_shm_queue;
  1012. video_cap->preview_shm_queue = NULL;
  1013. }
  1014. FREE(video_cap);
  1015. }
  1016. }
  1017. static int get_video_capture_fps(int icapfps)
  1018. {
  1019. int ifps = REC_COMMON_VIDEO_RAW_FPS;
  1020. if (icapfps <=30 && icapfps >=5){
  1021. ifps = icapfps;
  1022. }
  1023. return ifps;
  1024. }
  1025. #ifdef RVC_OS_WIN
  1026. static int video_capture_start_win(video_capture_t* video_cap)
  1027. {
  1028. if (NULL == video_cap) {
  1029. return -1;
  1030. }
  1031. capture_config_t *conf = &video_cap->parent->config;
  1032. if (NULL == conf) {
  1033. return -1;
  1034. }
  1035. int dev_id;
  1036. if (video_cap->camera_type == CAMERA_TYPE_ENV)
  1037. {
  1038. dev_id = capture_get_video_device_id(conf->strVideoEnv);
  1039. if (dev_id == -1)
  1040. {
  1041. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("No environment camera,please check config file or device!");
  1042. return -1;
  1043. }
  1044. }
  1045. else
  1046. {
  1047. dev_id = capture_get_video_device_id(conf->strVideoOpt);
  1048. if (dev_id == -1)
  1049. {
  1050. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("No operation camera,please check config file or device!");
  1051. return -1;
  1052. }
  1053. }
  1054. videocap_param param = {0};
  1055. int cap_mode;
  1056. int rc = -1;
  1057. rc = calc_capture_mode(REC_COMMON_VIDEO_SNAPSHOT_WIDTH, REC_COMMON_VIDEO_SNAPSHOT_HEIGHT, &cap_mode);
  1058. if (rc != 0)
  1059. {
  1060. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("calc cap_mode failed!");
  1061. return rc;
  1062. }
  1063. param.cap_mode = cap_mode;
  1064. param.dev_id = dev_id;
  1065. param.frame_fmt = VIDEO_FORMAT_RGB24;
  1066. param.cap_frame_format = VIDEO_FORMAT_YUY2;
  1067. param.fps = REC_COMMON_VIDEO_RAW_FPS;
  1068. param.on_frame = (video_cap->camera_type == CAMERA_TYPE_ENV ? &env_cap_on_frame : &opt_cap_on_frame);
  1069. param.user_data = video_cap;
  1070. param.option = 0;
  1071. param.dbg = &__dbg;
  1072. param.logevent = &__logevent;
  1073. rc = videocap_create(&video_cap->cap, &param);
  1074. if (rc != 0)
  1075. {
  1076. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("videocap create failed!");
  1077. return rc;
  1078. }
  1079. rc = videocap_start(video_cap->cap);
  1080. if (rc != 0)
  1081. {
  1082. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("videocap start failed!");
  1083. videocap_destroy(video_cap->cap);
  1084. video_cap->cap = NULL;
  1085. return rc;
  1086. }
  1087. return 0;
  1088. }
  1089. #else
  1090. static void __videocaplog(void* user_data, const char* fmt, va_list arg)
  1091. {
  1092. int n = vsnprintf(NULL, 0, fmt, arg);
  1093. if (n >= MAX_PATH) {
  1094. char* buf = (char*)malloc((size_t)(n + 1));
  1095. vsnprintf(buf, n + 1, fmt, arg);
  1096. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf);
  1097. free(buf);
  1098. }
  1099. else {
  1100. char strlog[MAX_PATH] = { 0 };
  1101. vsnprintf(strlog, MAX_PATH, fmt, arg);
  1102. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog);
  1103. }
  1104. }
  1105. static int video_capture_start_linux(video_capture_t* video_cap)
  1106. {
  1107. int iret = -1;
  1108. capture_config_t* conf = &video_cap->parent->config;
  1109. int dev_id = -1;
  1110. if (video_cap->camera_type == CAMERA_TYPE_ENV){
  1111. dev_id = rvc_videocap_get_video_device_id(conf->strVideoEnv.GetData());
  1112. if (-1 == dev_id){
  1113. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("No environment camera[%s], please check config file or device!", conf->strVideoEnv.GetData());
  1114. return iret;
  1115. }
  1116. }
  1117. else{
  1118. dev_id = rvc_videocap_get_video_device_id(conf->strVideoOpt.GetData());
  1119. if (-1 == dev_id){
  1120. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("No operation camera[%s], please check config file or device!", conf->strVideoOpt.GetData());
  1121. return iret;
  1122. }
  1123. }
  1124. videocap_param_t t_param = { 0 };
  1125. int cap_mode;
  1126. int rc = calc_capture_mode(REC_COMMON_VIDEO_SNAPSHOT_WIDTH, REC_COMMON_VIDEO_SNAPSHOT_HEIGHT, &cap_mode);
  1127. if (rc != 0){
  1128. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("calc cap_mode failed!");
  1129. return iret;
  1130. }
  1131. t_param.cap_mode = cap_mode;
  1132. t_param.dev_id = dev_id;
  1133. t_param.frame_fmt = VIDEO_FORMAT_RGB24;
  1134. t_param.fps = get_video_capture_fps(video_cap->camera_type == CAMERA_TYPE_ENV ? conf->video_env_fps : conf->video_opt_fps);
  1135. t_param.irotate = (video_cap->camera_type == CAMERA_TYPE_ENV ? conf->video_env_rotate : conf->video_opt_rotate);
  1136. t_param.on_frame = (video_cap->camera_type == CAMERA_TYPE_ENV ? &env_cap_on_frame : &opt_cap_on_frame);
  1137. //t_param.on_frame_i420 = (video_cap->camera_type == CAMERA_TYPE_ENV ? &env_cap_on_frame_i420 : &opt_cap_on_frame_i420);
  1138. t_param.user_data = video_cap;
  1139. t_param.option = 0;
  1140. videocap_callback_t t_callback = { 0 };
  1141. t_callback.debug = &__videocaplog;
  1142. t_callback.logevent = &__logevent;
  1143. video_cap->pVideoCap = CreateVideoCaptureObj(&t_callback);
  1144. if (NULL != video_cap->pVideoCap){
  1145. if (0 == video_cap->pVideoCap->VideoCaptureSetParam(&t_param)) {
  1146. if (0 == video_cap->pVideoCap->StartVideoCapture()) {
  1147. iret = 0;
  1148. }
  1149. else {
  1150. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("VideoCap StartVideoCapture failed!");
  1151. DestroyVideoCaptureObj(video_cap->pVideoCap);
  1152. video_cap->pVideoCap = NULL;
  1153. }
  1154. }
  1155. else {
  1156. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("VideoCap SetVideoCaptureParam failed!");
  1157. DestroyVideoCaptureObj(video_cap->pVideoCap);
  1158. video_cap->pVideoCap = NULL;
  1159. }
  1160. }
  1161. else{
  1162. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)(" CreateVideoCaptureObj failed!");
  1163. }
  1164. return iret;
  1165. }
  1166. #endif // RVC_OS_WIN
  1167. static int video_capture_start(video_capture_t *video_cap)
  1168. {
  1169. #ifdef RVC_OS_WIN
  1170. return video_capture_start_win(video_cap);
  1171. #else
  1172. return video_capture_start_linux(video_cap);
  1173. #endif
  1174. }
  1175. static void video_capture_stop(video_capture_t *video_cap)
  1176. {
  1177. #ifdef RVC_OS_WIN
  1178. if (video_cap->cap) {
  1179. videocap_stop(video_cap->cap);
  1180. videocap_destroy(video_cap->cap);
  1181. video_cap->cap = NULL;
  1182. }
  1183. #else
  1184. if (video_cap->pVideoCap) {
  1185. video_cap->pVideoCap->StopVideoCapture();
  1186. DestroyVideoCaptureObj(video_cap->pVideoCap);
  1187. video_cap->pVideoCap = NULL;
  1188. }
  1189. #endif // RVC_OS_WIN
  1190. }
  1191. namespace MediaController {
  1192. DeviceTypeEnum g_eDeviceType;
  1193. int capture_create(const capture_config_t *config, capture_t **p_cap)
  1194. {
  1195. capture_t *cap = ZALLOC_T(capture_t);
  1196. cap->handfree_audio = NULL;
  1197. cap->env_video = NULL;
  1198. cap->opt_video = NULL;
  1199. memcpy(&cap->config, config, sizeof(capture_config_t));
  1200. cap->handfree_audio = audio_capture_create(cap);
  1201. if (!cap->handfree_audio) {
  1202. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("create handfree audio capture object failed!");
  1203. return Error_Unexpect;
  1204. }
  1205. int dev_id_env = capture_get_video_device_id(config->strVideoEnv);
  1206. if (dev_id_env != -1) {
  1207. cap->env_video = video_capture_create(cap, CAMERA_TYPE_ENV);
  1208. if (!cap->env_video) {
  1209. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("create env video object failed!");
  1210. return Error_Unexpect;
  1211. }
  1212. }
  1213. int dev_id_opt = -1;
  1214. if (eStand2sType == g_eDeviceType){
  1215. dev_id_opt = capture_get_video_device_id(config->strVideoOpt);
  1216. }
  1217. if (dev_id_opt != -1){
  1218. cap->opt_video = video_capture_create(cap, CAMERA_TYPE_OPT);
  1219. if (!cap->opt_video) {
  1220. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("create opt video object failed!");
  1221. return Error_Unexpect;
  1222. }
  1223. }
  1224. if((dev_id_env == -1)&&(dev_id_opt == -1)){
  1225. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("all camera device id error!");
  1226. capture_destroy(cap);
  1227. return Error_AllCamera;
  1228. }
  1229. else{
  1230. *p_cap = cap;
  1231. return Error_Succeed;
  1232. }
  1233. }
  1234. ErrorCodeEnum capture_create(const capture_config_t *config,capture_t *cap,int nCamera)
  1235. {
  1236. int dev_id1 = capture_get_video_device_id(config->strVideoEnv);
  1237. int dev_id2 = capture_get_video_device_id(config->strVideoOpt);
  1238. if((dev_id1 != -1)&&(nCamera==ENVCAMERA)&&(cap->env_video == NULL))
  1239. {
  1240. cap->env_video = video_capture_create(cap, CAMERA_TYPE_ENV);
  1241. if (!cap->env_video)
  1242. {
  1243. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("create env video object failed!");
  1244. return Error_Unexpect;
  1245. }
  1246. }
  1247. else if((dev_id2 != -1)&&(nCamera == OPTCAMERA)&&(cap->opt_video == NULL))
  1248. {
  1249. cap->opt_video = video_capture_create(cap, CAMERA_TYPE_OPT);
  1250. if (!cap->opt_video)
  1251. {
  1252. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("create opt video object failed!");
  1253. return Error_Unexpect;
  1254. }
  1255. }
  1256. else
  1257. {
  1258. return Error_Unexpect;
  1259. }
  1260. return Error_Succeed;
  1261. }
  1262. int salesaudio_capture_create(rvc_audio_capture_config_t *config, rvc_sales_audio_capture_t **p_cap)
  1263. {
  1264. rvc_sales_audio_capture_t *cap = ZALLOC_T(rvc_sales_audio_capture_t);
  1265. cap->rvc_audio = NULL;
  1266. memcpy(&cap->rvc_audio_config, config, sizeof(rvc_audio_capture_config_t));
  1267. cap->rvc_audio = salesrecord_audio_capture_create(cap);
  1268. if (!cap->rvc_audio) {
  1269. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("create sales record audio capture object failed!");
  1270. salesaudio_capture_destroy(cap);
  1271. cap = NULL;
  1272. return Error_Unexpect;
  1273. }
  1274. else
  1275. {
  1276. cap->rvc_audio->iaudio_capture_peroid = config->audio_capture_period;
  1277. cap->rvc_audio->iaudio_capture_samplerate = config->audio_capture_samplerate;
  1278. *p_cap = cap;
  1279. return 0;
  1280. }
  1281. }
  1282. void salesaudio_capture_destroy(rvc_sales_audio_capture_t *cap)
  1283. {
  1284. if (NULL != cap){
  1285. if (cap->rvc_audio) {
  1286. salesrecord_audio_capture_destroy(cap->rvc_audio);
  1287. cap->rvc_audio = NULL;
  1288. }
  1289. FREE(cap);
  1290. }
  1291. }
  1292. void capture_destroy(capture_t *cap)
  1293. {
  1294. if (cap) {
  1295. if (cap->env_video) {
  1296. video_capture_destroy(cap->env_video);
  1297. cap->env_video = NULL;
  1298. }
  1299. if (cap->opt_video) {
  1300. video_capture_destroy(cap->opt_video);
  1301. cap->opt_video = NULL;
  1302. }
  1303. if (cap->handfree_audio) {
  1304. audio_capture_destroy(cap->handfree_audio);
  1305. cap->handfree_audio = NULL;
  1306. }
  1307. FREE(cap);
  1308. }
  1309. }
  1310. void capture_destroy(capture_t *cap,int nCamera)
  1311. {
  1312. if (cap)
  1313. {
  1314. if((cap->env_video)&&(nCamera == ENVCAMERA))
  1315. {
  1316. video_capture_destroy(cap->env_video);
  1317. cap->env_video = NULL;
  1318. }
  1319. else if((cap->opt_video)&&(nCamera==OPTCAMERA))
  1320. {
  1321. video_capture_destroy(cap->opt_video);
  1322. cap->opt_video = NULL;
  1323. }
  1324. }
  1325. }
  1326. ErrorCodeEnum start_audio_capture(audio_capture_t *paudio)
  1327. {
  1328. ErrorCodeEnum rslt = Error_Succeed;
  1329. int rc = 0;
  1330. if (NULL != paudio) {
  1331. rc = audio_capture_start(paudio);
  1332. if (rc != Error_Succeed) {
  1333. rslt = (ErrorCodeEnum)rc;
  1334. if (rslt == Error_AudioIN){
  1335. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("start audio In object failed! rc:%d", rc);
  1336. LogWarn(Severity_Middle, Error_Debug,ERROR_MOD_MEDIACONTROLLER_HANDFREE_OPENFAIL,"open audioIn device fail,please check device");
  1337. }
  1338. else{
  1339. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("start audio Out object failed! rc:%d", rc);
  1340. LogWarn(Severity_Middle, Error_Debug,ERROR_MOD_MEDIACONTROLLER_HANDFREE_OPENFAIL,"open audioOut device fail,please check device");
  1341. }
  1342. }
  1343. }
  1344. else{
  1345. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("audio_capture_t is null.");
  1346. rslt = Error_AudioIN;
  1347. }
  1348. return rslt;
  1349. }
  1350. static ErrorCodeEnum capture_start_envopt(capture_t *cap)
  1351. {
  1352. int rc = 0;
  1353. if (cap->env_video)
  1354. {
  1355. rc = video_capture_start(cap->env_video);
  1356. if (rc != Error_Succeed)
  1357. {
  1358. cap->env_video->ustarttime = 0;
  1359. char strMessage[MAX_PATH*2] = {0};
  1360. get_camera_exception_message(strMessage, MAX_PATH*2, cap->config.strVideoEnv, "open environ camera fail, please check device.");
  1361. LogWarn(Severity_Middle,Error_NotInit,ERROR_MOD_MEDIACONTROLLER_ENVCAM_OPEN,strMessage);
  1362. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER).setAPI("OpenEnvCam").setLogCode("QLR040220801").setResultCode("RTA2803")("上摄像头故障,打开摄像头失败");
  1363. if (cap->opt_video)
  1364. {
  1365. if (cap->env_video)
  1366. {
  1367. rvc_sleep(cap->config.uintervaltime);
  1368. }
  1369. rc = video_capture_start(cap->opt_video);
  1370. if (rc != Error_Succeed)
  1371. {
  1372. cap->opt_video->ustarttime = 0;
  1373. char strMessage[MAX_PATH*2] = {0};
  1374. get_camera_exception_message(strMessage, MAX_PATH*2, cap->config.strVideoOpt, "open operate camera fail, please check device.");
  1375. LogWarn(Severity_Middle,Error_NotInit,ERROR_MOD_MEDIACONTROLLER_OPTCAM_OPEN, strMessage);
  1376. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER).setAPI("OpenOptCam").setLogCode("QLR040220801").setResultCode("RTA2804")("下摄像头故障,打开摄像头失败");
  1377. return Error_AllCamera;
  1378. }
  1379. else
  1380. {
  1381. cap->opt_video->ustarttime = y2k_time_now();
  1382. LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_OPTCAM_OPEN, "start optcam success.");
  1383. return Error_EnvCamera;
  1384. }
  1385. }
  1386. else
  1387. {
  1388. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("start all video capture object failed!");
  1389. return Error_AllCamera;
  1390. }
  1391. }
  1392. else
  1393. {
  1394. cap->env_video->ustarttime = y2k_time_now();
  1395. LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_ENVCAM_OPEN, "start envcam success.");
  1396. if (cap->opt_video)
  1397. {
  1398. if (cap->env_video)
  1399. {
  1400. rvc_sleep(cap->config.uintervaltime);
  1401. }
  1402. rc = video_capture_start(cap->opt_video);
  1403. if (rc != Error_Succeed)
  1404. {
  1405. cap->opt_video->ustarttime = 0;
  1406. char strMessage[MAX_PATH*2] = {0};
  1407. get_camera_exception_message(strMessage, MAX_PATH*2, cap->config.strVideoOpt, "open operate camera fail, please check device.");
  1408. LogWarn(Severity_Middle,Error_NotInit,ERROR_MOD_MEDIACONTROLLER_OPTCAM_OPEN,strMessage);
  1409. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER).setAPI("OpenOptCam").setLogCode("QLR040220801").setResultCode("RTA2804")("下摄像头故障,打开摄像头失败");
  1410. return Error_OptCamera;
  1411. }
  1412. else
  1413. {
  1414. cap->opt_video->ustarttime = y2k_time_now();
  1415. LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_OPTCAM_OPEN, "start optcam success.");
  1416. return Error_Succeed;
  1417. }
  1418. }
  1419. else
  1420. {
  1421. return Error_OptCamera;
  1422. }
  1423. }
  1424. }
  1425. else
  1426. {
  1427. if (cap->opt_video)
  1428. {
  1429. if (cap->env_video)
  1430. {
  1431. rvc_sleep(cap->config.uintervaltime);
  1432. }
  1433. rc = video_capture_start(cap->opt_video);
  1434. if (rc != Error_Succeed)
  1435. {
  1436. cap->opt_video->ustarttime = 0;
  1437. char strMessage[MAX_PATH*2] = {0};
  1438. get_camera_exception_message(strMessage, MAX_PATH*2, cap->config.strVideoOpt, "open operate camera fail,please check device.");
  1439. LogWarn(Severity_Middle,Error_NotInit,ERROR_MOD_MEDIACONTROLLER_OPTCAM_OPEN,strMessage);
  1440. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER).setAPI("OpenOptCam").setLogCode("QLR040220801").setResultCode("RTA2804")("下摄像头故障,打开摄像头失败");
  1441. return Error_AllCamera;
  1442. }
  1443. else
  1444. {
  1445. cap->opt_video->ustarttime = y2k_time_now();
  1446. LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_OPTCAM_OPEN, "start optcam success.");
  1447. return Error_EnvCamera;
  1448. }
  1449. }
  1450. else
  1451. {
  1452. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("start env video Error_AllCamera");
  1453. return Error_AllCamera;
  1454. }
  1455. }
  1456. }
  1457. ErrorCodeEnum capture_start(capture_t *cap)
  1458. {
  1459. int rc = 0;
  1460. if ('N' == cap->config.strAudioState[0] || 'P' == cap->config.strAudioState[0]) {
  1461. ErrorCodeEnum rslt = start_audio_capture(cap->handfree_audio);
  1462. if (Error_Succeed != rslt) {
  1463. return rslt;
  1464. }
  1465. }
  1466. else {
  1467. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("handfree audio is error, not start it.");
  1468. }
  1469. return capture_start_envopt(cap);
  1470. }
  1471. ErrorCodeEnum capture_start(capture_t *cap,int nCamera)
  1472. {
  1473. int rc = 0;
  1474. if (cap->env_video&&(nCamera==ENVCAMERA))
  1475. {
  1476. rc = video_capture_start(cap->env_video);
  1477. if (rc != Error_Succeed) {
  1478. cap->env_video->ustarttime = 0;
  1479. return Error_Hardware;
  1480. }
  1481. else{
  1482. cap->env_video->ustarttime = y2k_time_now();
  1483. LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_ENVCAM_OPEN, "start envcam success.");
  1484. return Error_Succeed;
  1485. }
  1486. }
  1487. else if(cap->opt_video&&(nCamera==OPTCAMERA))
  1488. {
  1489. rc = video_capture_start(cap->opt_video);
  1490. if (rc != Error_Succeed){
  1491. cap->opt_video->ustarttime = 0;
  1492. return Error_Hardware;
  1493. }
  1494. else{
  1495. cap->opt_video->ustarttime = y2k_time_now();
  1496. LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_OPTCAM_OPEN, "start optcam success.");
  1497. return Error_Succeed;
  1498. }
  1499. }
  1500. else{
  1501. return Error_Unexpect;
  1502. }
  1503. }
  1504. ErrorCodeEnum salesrecord_audio_capture_start(rvc_sales_audio_capture_t *cap)
  1505. {
  1506. auto rc = Error_Param;
  1507. if (NULL == cap){
  1508. return rc;
  1509. }
  1510. if (cap->rvc_audio) {
  1511. #ifdef RVC_OS_LINUX
  1512. audiomgr_callback_t t_callback = { 0 };
  1513. t_callback.debug = &__audiomgrlog;
  1514. cap->rvc_audio->paudiocap = CreateAudioMgrObj(&t_callback);
  1515. if (0 != cap->rvc_audio->paudiocap->audio_mgr_initialize()) {
  1516. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("audio manager initialize failed!");
  1517. return Error_Null;
  1518. }
  1519. #endif
  1520. int rslt = record_audio_capture_start(cap->rvc_audio);
  1521. if (rslt != Error_Succeed) {
  1522. if (Error_AudioIN == rslt){
  1523. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("start audio In object failed! rc:%d", rc);
  1524. LogWarn(Severity_Middle, Error_Debug,ERROR_MOD_MEDIACONTROLLER_HANDFREE_OPENFAIL,"open audioIn device fail,please check device");
  1525. }
  1526. rc = (ErrorCodeEnum)rslt;
  1527. }
  1528. else{
  1529. rc = Error_Succeed;
  1530. }
  1531. }
  1532. return rc;
  1533. }
  1534. void salesrecord_audio_capture_stop(rvc_sales_audio_capture_t *cap)
  1535. {
  1536. if (NULL != cap){
  1537. if (cap->rvc_audio){
  1538. record_audio_capture_stop(cap->rvc_audio);
  1539. #ifdef RVC_OS_LINUX
  1540. if (NULL != cap->rvc_audio->paudiocap) {
  1541. DestroyIAudioMgrObj(cap->rvc_audio->paudiocap);
  1542. cap->rvc_audio->paudiocap = NULL;
  1543. }
  1544. #endif // RVC_OS_LINUX
  1545. }
  1546. }
  1547. else{
  1548. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("sales record audio capture stop failed for param error.");
  1549. }
  1550. }
  1551. void capture_stop(capture_t *cap)
  1552. {
  1553. if (cap->handfree_audio) {
  1554. audio_capture_stop(cap->handfree_audio);
  1555. }
  1556. if (cap->env_video) {
  1557. if (0 != cap->env_video->ustarttime) {
  1558. int ienvtime = y2k_time_now() - cap->env_video->ustarttime;
  1559. LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_ENVCAM_CLOSE, CSimpleStringA::Format("stop envcam, and camera open time is %us.", ienvtime).GetData());
  1560. LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_ENVCAM_FRAME_INFO, CSimpleStringA::Format("env camera frame info is dest frame number is %d(%us), real frame number is %d, difference is %d.", ienvtime*cap->config.video_env_fps, ienvtime, cap->env_video->frame_id, ienvtime * cap->config.video_env_fps - cap->env_video->frame_id).GetData());
  1561. }
  1562. video_capture_stop(cap->env_video);
  1563. }
  1564. if (cap->opt_video) {
  1565. if (0 != cap->opt_video->ustarttime) {
  1566. int iopttime = y2k_time_now() - cap->opt_video->ustarttime;
  1567. LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_OPTCAM_CLOSE, CSimpleStringA::Format("stop optcam, and camera open time is %us.", iopttime).GetData());
  1568. LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_OPTCAM_FRAME_INFO, CSimpleStringA::Format("opt camera frame info is dest frame number is %d(%us), real frame number is %d, difference is %d.", iopttime * cap->config.video_opt_fps, iopttime, cap->opt_video->frame_id, iopttime * cap->config.video_opt_fps - cap->opt_video->frame_id).GetData());
  1569. }
  1570. video_capture_stop(cap->opt_video);
  1571. }
  1572. }
  1573. int capture_detect_camera_bug(capture_t *cap, int *env_n, int *opt_n, bool bsinglecam)
  1574. {
  1575. *env_n = 0;
  1576. *opt_n = 0;
  1577. if (cap->env_video)
  1578. {
  1579. if (cap->env_video->rtp_shm_queue)
  1580. {
  1581. *env_n = cap->env_video->rtp_shm_queue->GetVideoLens();
  1582. }
  1583. }
  1584. else
  1585. {
  1586. *env_n = -1;
  1587. }
  1588. if (cap->opt_video)
  1589. {
  1590. if (cap->opt_video->rtp_shm_queue)
  1591. {
  1592. *opt_n = cap->opt_video->rtp_shm_queue->GetVideoLens();
  1593. }
  1594. }
  1595. else
  1596. {
  1597. if (!bsinglecam)
  1598. {
  1599. *opt_n = -1;
  1600. }
  1601. else
  1602. {
  1603. *opt_n = 1;
  1604. }
  1605. }
  1606. return 0;
  1607. }
  1608. //摄像头没打开返回0,其他情况返回视频队列中最后一幅图像时间
  1609. int capture_get_last_frametime(capture_t *cap, unsigned int*env_n, unsigned int*opt_n)
  1610. {
  1611. *env_n = 0;
  1612. *opt_n = 0;
  1613. if (cap->env_video)
  1614. {
  1615. if (cap->env_video->rtp_shm_queue)
  1616. {
  1617. *env_n = cap->env_video->rtp_shm_queue->GetLastFrameTime();
  1618. }
  1619. }
  1620. if (cap->opt_video)
  1621. {
  1622. if (cap->opt_video->rtp_shm_queue)
  1623. {
  1624. *opt_n = cap->opt_video->rtp_shm_queue->GetLastFrameTime();
  1625. }
  1626. }
  1627. return 0;
  1628. }
  1629. int capture_get_video_device_id(const char *dev_name)
  1630. {
  1631. #ifdef RVC_OS_WIN
  1632. int n = videocap_get_device_count();
  1633. for (int i = 0; i < n; ++i) {
  1634. WCHAR tmp[256];
  1635. char t[256];
  1636. WCHAR tmp1[256];
  1637. char t1[256];
  1638. videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  1639. WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  1640. videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
  1641. WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
  1642. char t2[256];
  1643. strcpy(t2,t1);
  1644. for (int j = 0; j < strlen(t2); ++j)
  1645. {
  1646. t2[j] = toupper(t2[j]);
  1647. if (t2[j] == '#') t2[j] = '\\';
  1648. }
  1649. {
  1650. unsigned char x[MD5_DIGESTSIZE];
  1651. md5_ctx_t ctx;
  1652. md5_init(&ctx);
  1653. md5(x, t1, strlen(t1));
  1654. Bin2Str(x, sizeof(x), t1, sizeof(t1));
  1655. }
  1656. if (dev_name != NULL && strlen(dev_name) > 1 &&
  1657. strstr(dev_name, ";") == NULL)
  1658. {
  1659. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[dbg] device_path: %s",t2);
  1660. if (strstr(t2,dev_name) != NULL)
  1661. {
  1662. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[dbg] %s founded in %d cameras.", dev_name, n);
  1663. return i;
  1664. }
  1665. if (strcmp(dev_name, t) == 0)
  1666. return i;
  1667. }
  1668. else
  1669. {
  1670. strcat(t, ";");
  1671. strcat(t, t1);
  1672. if (strcmp(dev_name, t) == 0)
  1673. return i;
  1674. }
  1675. }
  1676. return -1; // not found
  1677. #else
  1678. return rvc_videocap_get_video_device_id(dev_name);
  1679. #endif // RVC_OS_WIN
  1680. }
  1681. int capture_lib_init(int *ivideonum)
  1682. {
  1683. int icamnum = 0;
  1684. CSimpleStringA strCamInfoJson("");
  1685. #ifdef RVC_OS_WIN
  1686. HRESULT hr = CoInitialize(NULL);
  1687. int rc;
  1688. {
  1689. HMODULE hModule = GetModuleHandleA("MSVCR100.dll");
  1690. if (hModule) {
  1691. typedef char* (*f_setlocale)(int, const char*);
  1692. f_setlocale f = (f_setlocale)GetProcAddress(hModule, "setlocale");
  1693. (*f)(LC_ALL, "chs");
  1694. }
  1695. }
  1696. if (SUCCEEDED(hr)) {
  1697. PaError Error;
  1698. Error = Pa_Initialize();
  1699. if (Error == paNoError) {
  1700. rc = videoframework_init();
  1701. if (rc != 0) {
  1702. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("videoframework_init failed, rc=%d", rc);
  1703. return Error_Resource;
  1704. }
  1705. }
  1706. else {
  1707. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("PaInitialize failed, rc=%d", Error);
  1708. return Error_Resource;
  1709. }
  1710. }
  1711. else {
  1712. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("coinitialze failed! hr:%d", hr);
  1713. return Error_Resource;
  1714. }
  1715. {
  1716. icamnum = videocap_get_device_count();
  1717. for (int i = 0; i < icamnum; ++i) {
  1718. WCHAR tmp[256] = {0};
  1719. char t[256] = {0};
  1720. WCHAR tmp1[256] ={0};
  1721. char t1[256] = {0};
  1722. videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  1723. WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  1724. videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
  1725. WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
  1726. {
  1727. unsigned char x[MD5_DIGESTSIZE];
  1728. md5_ctx_t ctx;
  1729. md5_init(&ctx);
  1730. md5(x, t1, strlen(t1));
  1731. Bin2Str(x, sizeof(x), t1, sizeof(t1));
  1732. }
  1733. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%d = %s;%s", i, t, t1);
  1734. strCamInfoJson += CSimpleStringA::Format("\"%d\":\"%s;%s\",", i, t, t1);
  1735. }
  1736. }
  1737. {
  1738. int icnt, ocnt;
  1739. rc = audio_get_dev_count(&icnt, &ocnt);
  1740. if (rc == 0) {
  1741. int i;
  1742. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("audio input devices(%d):", icnt);
  1743. for (i = 0; i < icnt; ++i) {
  1744. CSimpleStringA str = audio_get_dev_name(true, i);
  1745. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%d = %s", i, str.GetData());
  1746. }
  1747. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("audio output devices(%d):", ocnt);
  1748. for (i = 0; i < ocnt; ++i) {
  1749. CSimpleStringA str = audio_get_dev_name(false, i);
  1750. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%d = %s", i, str.GetData());
  1751. }
  1752. }
  1753. }
  1754. #else
  1755. {
  1756. int inumber = 0;
  1757. icamnum = rvc_videocap_get_device_count();
  1758. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("camera count is %d.", icamnum);
  1759. for (int i = 0; i < 64 && inumber < icamnum; ++i) {
  1760. char strcamera[2 * MAX_PATH] = { 0 };
  1761. char strpath[MAX_PATH] = { 0 };
  1762. if (0 == rvc_videocap_get_device_fullpathname(i, strcamera, 2 * MAX_PATH)){
  1763. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%d = %s", inumber, strcamera);
  1764. strCamInfoJson += CSimpleStringA::Format("\"%d\":\"%s\",", inumber, strcamera);
  1765. inumber++;
  1766. }
  1767. }
  1768. }
  1769. #endif // RVC_OS_WIN
  1770. if (icamnum > 0) {
  1771. LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_CAMERA_COUNT, CSimpleStringA::Format("camera number is %d.", icamnum).GetData());
  1772. if (strCamInfoJson.GetLength() > 0) {
  1773. strCamInfoJson[strCamInfoJson.GetLength() - 1] = '\0';
  1774. }
  1775. LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_GETCAMERA_INFOS, CSimpleStringA::Format("[{%s}]", strCamInfoJson.GetData()).GetData());
  1776. }
  1777. *ivideonum = icamnum;
  1778. return Error_Succeed;
  1779. }
  1780. void capture_lib_term()
  1781. {
  1782. #ifdef RVC_OS_WIN
  1783. Pa_Terminate();
  1784. videoframework_term();
  1785. CoUninitialize();
  1786. #else
  1787. #endif // RVC_OS_WIN
  1788. }
  1789. bool capture_adj_brightness(capture_t *cap,int nvalue,ErrorCodeEnum nCode)
  1790. {
  1791. #ifdef RVC_OS_WIN
  1792. HRESULT rst = S_OK;
  1793. if (cap && cap->env_video&&(nCode!=Error_EnvCamera)&&(nCode!=Error_AllCamera))
  1794. {
  1795. rst = videocap_adj_brightness(cap->env_video->cap,nvalue);
  1796. }
  1797. HRESULT rst2 = S_OK;
  1798. if (cap && cap->opt_video&&(nCode!=Error_OptCamera)&&(nCode!=Error_AllCamera))
  1799. {
  1800. rst2 = videocap_adj_brightness(cap->opt_video->cap,nvalue);
  1801. }
  1802. if (SUCCEEDED(rst) && SUCCEEDED(rst2)) {
  1803. return true;
  1804. }
  1805. else {
  1806. return false;
  1807. }
  1808. #else
  1809. int ienv = -1;
  1810. if (cap && cap->env_video && cap->env_video->pVideoCap && (nCode != Error_EnvCamera) && (nCode != Error_AllCamera)){
  1811. ienv = cap->env_video->pVideoCap->SetCamBrightness(nvalue);
  1812. }
  1813. int iopt = -1;
  1814. if (cap && cap->opt_video && cap->opt_video->pVideoCap && (nCode != Error_OptCamera) && (nCode != Error_AllCamera)){
  1815. iopt = cap->opt_video->pVideoCap->SetCamBrightness(nvalue);
  1816. }
  1817. if (0 == ienv && 0 == iopt){
  1818. return true;
  1819. }
  1820. else{
  1821. return false;
  1822. }
  1823. #endif // RVC_OS_WIN
  1824. }
  1825. bool capture_set_autobrightness(capture_t *cap,ErrorCodeEnum nCode)
  1826. {
  1827. #ifdef RVC_OS_WIN
  1828. HRESULT rst = S_OK;
  1829. HRESULT rst2 = S_OK;
  1830. if (cap && cap->env_video && (nCode != Error_EnvCamera) && (nCode != Error_AllCamera)){
  1831. rst = videocap_set_autobrightness(cap->env_video->cap);
  1832. }
  1833. if (cap && cap->opt_video && (nCode != Error_OptCamera) && (nCode != Error_AllCamera)){
  1834. rst2 = videocap_set_autobrightness(cap->opt_video->cap);
  1835. }
  1836. if (SUCCEEDED(rst) && SUCCEEDED(rst2)) {
  1837. return true;
  1838. }
  1839. else {
  1840. return false;
  1841. }
  1842. #else
  1843. int ienv = -1;
  1844. int iopt = -1;
  1845. if (cap && cap->env_video && cap->env_video->pVideoCap && (nCode != Error_EnvCamera) && (nCode != Error_AllCamera))
  1846. {
  1847. ienv = cap->env_video->pVideoCap->SetCamAutoBrightness();
  1848. }
  1849. if (cap && cap->opt_video && cap->opt_video->pVideoCap && (nCode != Error_OptCamera) && (nCode != Error_AllCamera))
  1850. {
  1851. iopt = cap->opt_video->pVideoCap->SetCamAutoBrightness();
  1852. }
  1853. if (0 == ienv && 0 == iopt) {
  1854. return true;
  1855. }
  1856. else {
  1857. return false;
  1858. }
  1859. #endif // RVC_OS_WIN
  1860. }
  1861. int capture_get_brightness(capture_t *cap, ErrorCodeEnum nCode)
  1862. {
  1863. #ifdef RVC_OS_WIN
  1864. int nValue1 = 0;
  1865. int nValue2 = 0;
  1866. HRESULT rst = S_OK;
  1867. if (cap && cap->env_video && (nCode != Error_EnvCamera) && (nCode != Error_AllCamera))
  1868. {
  1869. HRESULT rst = videocap_get_brightness(cap->env_video->cap, &nValue1);
  1870. }
  1871. HRESULT rst2 = S_OK;
  1872. if (cap && cap->opt_video && (nCode != Error_OptCamera) && (nCode != Error_AllCamera))
  1873. {
  1874. rst2 = videocap_get_brightness(cap->opt_video->cap, &nValue2);
  1875. }
  1876. else
  1877. {
  1878. return -1;
  1879. }
  1880. if (cap && (cap->opt_video == NULL) && cap->env_video)
  1881. {
  1882. return nValue1;
  1883. }
  1884. else if (cap && (cap->env_video == NULL) && cap->opt_video)
  1885. {
  1886. return nValue2;
  1887. }
  1888. else
  1889. {
  1890. if (SUCCEEDED(rst) && SUCCEEDED(rst2)) {
  1891. return (nValue1 <= nValue2) ? nValue1 : nValue2;
  1892. }
  1893. else {
  1894. return -1;
  1895. }
  1896. }
  1897. #else
  1898. int nValue1 = 0;
  1899. int nValue2 = 0;
  1900. int ienv = -1;
  1901. if (cap && cap->env_video && cap->env_video->pVideoCap && (nCode != Error_EnvCamera) && (nCode != Error_AllCamera)){
  1902. ienv = cap->env_video->pVideoCap->GetCamBrightness(&nValue1);
  1903. }
  1904. int iopt = -1;
  1905. if (cap && cap->opt_video && cap->opt_video->pVideoCap && (nCode != Error_OptCamera) && (nCode != Error_AllCamera)){
  1906. iopt = cap->opt_video->pVideoCap->GetCamBrightness(&nValue2);
  1907. }
  1908. else{
  1909. return -1;
  1910. }
  1911. if (cap && (cap->opt_video == NULL) && cap->env_video)
  1912. {
  1913. return nValue1;
  1914. }
  1915. else if (cap && (cap->env_video == NULL) && cap->opt_video)
  1916. {
  1917. return nValue2;
  1918. }
  1919. else
  1920. {
  1921. if (0 == ienv && 0 == iopt) {
  1922. return (nValue1 <= nValue2) ? nValue1 : nValue2;
  1923. }
  1924. else
  1925. {
  1926. return -1;
  1927. }
  1928. }
  1929. #endif // RVC_OS_WIN
  1930. }
  1931. int StopCamera(capture_t *cap,int nCamera)
  1932. {
  1933. if((nCamera == ENVCAMERA)&&cap->env_video)
  1934. {
  1935. video_capture_stop(cap->env_video);
  1936. if (0 != cap->env_video->ustarttime) {
  1937. int ienvtime = y2k_time_now() - cap->env_video->ustarttime;
  1938. LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_ENVCAM_CLOSE, CSimpleStringA::Format("stop envcam, and camera open time is %us.", ienvtime).GetData());
  1939. LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_ENVCAM_FRAME_INFO, CSimpleStringA::Format("env camera frame info is dest frame number is %d(%us), real frame number is %d, difference is %d.", ienvtime * cap->config.video_env_fps, ienvtime, cap->env_video->frame_id, ienvtime * cap->config.video_env_fps - cap->env_video->frame_id).GetData());
  1940. }
  1941. video_capture_destroy(cap->env_video);
  1942. cap->env_video = NULL;
  1943. return 0;
  1944. }
  1945. else if((nCamera == OPTCAMERA)&&cap->opt_video)
  1946. {
  1947. video_capture_stop(cap->opt_video);
  1948. if (0 != cap->opt_video->ustarttime) {
  1949. int iopttime = y2k_time_now() - cap->opt_video->ustarttime;
  1950. LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_OPTCAM_CLOSE, CSimpleStringA::Format("stop optcam, and camera open time is %us.", iopttime).GetData());
  1951. LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_OPTCAM_FRAME_INFO, CSimpleStringA::Format("opt camera frame info is dest frame number is %d(%us), real frame number is %d, difference is %d.", iopttime * cap->config.video_opt_fps, iopttime, cap->opt_video->frame_id, iopttime * cap->config.video_opt_fps - cap->opt_video->frame_id).GetData());
  1952. }
  1953. video_capture_destroy(cap->opt_video);
  1954. cap->opt_video = NULL;
  1955. return 0;
  1956. }
  1957. else
  1958. {
  1959. return -1;
  1960. }
  1961. }
  1962. void capture_clearsnapshotvideo(capture_t *cap,int nCamera)
  1963. {
  1964. if((nCamera == ENVCAMERA)&&cap->env_video)
  1965. {
  1966. cap->env_video->snapshot_shm_queue->ClearVideoQueue();
  1967. return;
  1968. }
  1969. else if((nCamera == OPTCAMERA)&&cap->opt_video)
  1970. {
  1971. cap->opt_video->snapshot_shm_queue->ClearVideoQueue();
  1972. return;
  1973. }
  1974. else if(nCamera == AlLCAMERA)
  1975. {
  1976. if (cap->env_video)
  1977. {
  1978. cap->env_video->snapshot_shm_queue->ClearVideoQueue();
  1979. }
  1980. if (cap->opt_video)
  1981. {
  1982. cap->opt_video->snapshot_shm_queue->ClearVideoQueue();
  1983. }
  1984. return;
  1985. }
  1986. else
  1987. {
  1988. return ;
  1989. }
  1990. }
  1991. int get_camera_exception_message(char* pBuffer, size_t uLen, CSimpleStringA strDeviceName, const char* strErrorMessage)
  1992. {
  1993. int iRet = 0;
  1994. if (strDeviceName.GetLength() > 0){
  1995. const char* strCameraName = strDeviceName.GetData();
  1996. char strBuffer[MAX_PATH] = {0};
  1997. if (snprintf(strBuffer, MAX_PATH, "%s", strCameraName) > 0){
  1998. char *pIndex = NULL;
  1999. if (pIndex = (char*)strstr(strBuffer, ";")){
  2000. *pIndex = '\0';
  2001. }
  2002. }
  2003. if (NULL != strErrorMessage){
  2004. size_t uDataLen = strlen(strBuffer);
  2005. size_t uErrorLen = strlen(strErrorMessage);
  2006. if (uLen > uDataLen + uErrorLen + 10){
  2007. iRet = snprintf(pBuffer, uLen, "[%s] %s", strBuffer, strErrorMessage);
  2008. }
  2009. }
  2010. }
  2011. if (0 == iRet){
  2012. if (NULL != strErrorMessage){
  2013. iRet = snprintf(pBuffer, uLen, "%s", strErrorMessage);
  2014. }
  2015. }
  2016. return iRet;
  2017. }
  2018. int capture_get_audio_device_list(bool in_direction, CSimpleStringA& strdevicelist)
  2019. {
  2020. int iret = 0;
  2021. #ifdef RVC_OS_WIN
  2022. int icnt = 0, ocnt = 0;
  2023. int rc = audio_get_dev_count(&icnt, &ocnt);
  2024. if (rc == 0) {
  2025. strdevicelist = "[";
  2026. if (in_direction) {
  2027. iret = icnt;
  2028. for (int i = 0; i < icnt; ++i) {
  2029. CSimpleStringA str = CSimpleStringA::Format("\"%s\"", audio_get_dev_name(true, i).GetData());
  2030. if (i > 0) {
  2031. strdevicelist += ",";
  2032. }
  2033. strdevicelist += str;
  2034. }
  2035. }
  2036. else {
  2037. iret = ocnt;
  2038. for (int i = 0; i < ocnt; ++i) {
  2039. CSimpleStringA str = CSimpleStringA::Format("\"%s\"",audio_get_dev_name(false, i).GetData());
  2040. if (i > 0) {
  2041. strdevicelist += ",";
  2042. }
  2043. strdevicelist += str;
  2044. }
  2045. }
  2046. strdevicelist += "]";
  2047. }
  2048. #endif
  2049. return iret;
  2050. }
  2051. int capture_get_video_device_list(CSimpleStringA& strdevicelist)
  2052. {
  2053. int icount = 0;
  2054. #ifdef RVC_OS_WIN
  2055. strdevicelist = "[";
  2056. icount = videocap_get_device_count();
  2057. for (int i = 0; i < icount; ++i) {
  2058. wchar_t tmp[MAX_PATH] = { 0 };
  2059. char t[MAX_PATH] = { 0 };
  2060. wchar_t tmp1[MAX_PATH] = { 0 };
  2061. char t1[MAX_PATH] = { 0 };
  2062. videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  2063. WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  2064. videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
  2065. WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
  2066. {
  2067. unsigned char x[MD5_DIGESTSIZE] = {0};
  2068. md5_ctx_t ctx;
  2069. md5_init(&ctx);
  2070. md5(x, t1, strlen(t1));
  2071. Bin2Str(x, sizeof(x), t1, sizeof(t1));
  2072. }
  2073. CSimpleStringA str = CSimpleStringA::Format("\"%s;%s\"", t, t1);
  2074. if (i > 0) {
  2075. strdevicelist += ",";
  2076. }
  2077. strdevicelist += str;
  2078. }
  2079. strdevicelist += "]";
  2080. #else
  2081. int ifound = 0;
  2082. strdevicelist = "[";
  2083. icount = rvc_videocap_get_device_count();
  2084. for (int i = 0; i < 64 && ifound < icount; ++i) {
  2085. char strcamera[2 * MAX_PATH] = { 0 };
  2086. if (0 == rvc_videocap_get_device_fullpathname(i, strcamera, 2 * MAX_PATH)) {
  2087. CSimpleStringA str = CSimpleStringA::Format("\"%s\"", strcamera);
  2088. if (ifound > 0) {
  2089. strdevicelist += ",";
  2090. }
  2091. strdevicelist += str;
  2092. ifound++;
  2093. }
  2094. }
  2095. strdevicelist += "]";
  2096. #endif
  2097. return icount;
  2098. }
  2099. }