capture.cpp 64 KB

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