capture.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847
  1. #include "stdafx.h"
  2. #include <locale.h>
  3. #include <memutil.h>
  4. #include <objbase.h>
  5. #include <md5.h>
  6. #include <ipp.h>
  7. #include <intrin.h>
  8. #include "y2k_time.h"
  9. #include "../../Other/include/rvc_media_common.h"
  10. #include "../../Other/libvideohorflip/videohorflip.h"
  11. #include "Event.h"
  12. #include "capture.h"
  13. using namespace MediaController;
  14. #define av_always_inline __inline
  15. #define inline __inline
  16. #include "../../Other/libvideoframework/video_common/ffmpeg_api_cpp_adapter.h"
  17. static int Bin2Str(unsigned char *x, int xlen, char *str, int str_size)
  18. {
  19. static const char *hex2char = "0123456789ABCDEF";
  20. int i, k = 0;
  21. if (str_size <= xlen * 2)
  22. return -1;
  23. for (i = 0; i < xlen; ++i) {
  24. int h = x[i] >> 4;
  25. int l = x[i] & 0xf;
  26. str[k++] = hex2char[h];
  27. str[k++] = hex2char[l];
  28. }
  29. str[k] = 0;
  30. return k;
  31. }
  32. static int translate_id(int in_direction, int idx)
  33. {
  34. int i, n, ii;
  35. n = Pa_GetDeviceCount();
  36. for (i = 0, ii = 0; i < n; ++i) {
  37. const PaDeviceInfo *info = Pa_GetDeviceInfo(i);
  38. if (in_direction) {
  39. if (info->maxInputChannels) {
  40. if (ii == idx) {
  41. return i;
  42. }
  43. ii++;
  44. }
  45. } else {
  46. if (info->maxOutputChannels) {
  47. if (ii == idx) {
  48. return i;
  49. }
  50. ii++;
  51. }
  52. }
  53. }
  54. return -1;
  55. }
  56. static int StreamCallback(const void *input,
  57. void *output,
  58. unsigned long frameCount,
  59. const PaStreamCallbackTimeInfo* timeInfo,
  60. PaStreamCallbackFlags statusFlags,
  61. void *userData)
  62. {
  63. audio_capture_t *audio_cap = (audio_capture_t*)userData;
  64. if (input) {
  65. audio_frame frm;
  66. frm.bitspersample = 16;
  67. frm.format = 1;
  68. frm.data = (char*)const_cast<void*>(input);
  69. frm.framesize = frameCount << 1;
  70. frm.nchannels = 1;
  71. frm.samplespersec = CAPTURE_CLOCK;
  72. frm.iseriesnumber = 0;
  73. if (!audio_cap->shm_queue->InsertAudio(&frm)) {
  74. Dbg("[StreamCallback] InsertAudio to shm_queue failed! frameCount:%d", frameCount);
  75. }
  76. if (!audio_cap->salesol_shm_queue->InsertAudio(&frm)) {
  77. Dbg("[StreamCallback] InsertAudio to salesol_shm_queue failed! frameCount:%d", frameCount);
  78. }
  79. }
  80. if (output) {
  81. memset(output, 0, frameCount<<1);
  82. }
  83. return paContinue;
  84. }
  85. static int Sales_StreamCallback(const void *input,
  86. void *output,
  87. unsigned long frameCount,
  88. const PaStreamCallbackTimeInfo* timeInfo,
  89. PaStreamCallbackFlags statusFlags,
  90. void *userData)
  91. {
  92. rvc_audio_capture_t *audio_cap = (rvc_audio_capture_t*)userData;
  93. if ((NULL != audio_cap) && (NULL != audio_cap->audio_shm_queue) && (NULL != input)){
  94. audio_frame frm;
  95. frm.bitspersample = 16;
  96. frm.format = 1;
  97. frm.data = (char*)const_cast<void*>(input);
  98. frm.framesize = frameCount << 1;
  99. frm.nchannels = 1;
  100. frm.samplespersec = audio_cap->iaudio_capture_samplerate;
  101. frm.iseriesnumber = audio_cap->iseriesnumber++;
  102. if (!audio_cap->audio_shm_queue->InsertAudio(&frm)) {
  103. Dbg("[Sales_StreamCallback] InsertAudio failed! frameCount:%d", frameCount);
  104. }
  105. else{
  106. if ((audio_cap->iaudio_capture_peroid) > 0 && (0 == frm.iseriesnumber%audio_cap->iaudio_capture_peroid)){
  107. if (audio_cap->iseriesnumber > INT_MAX){
  108. audio_cap->iseriesnumber = 0;
  109. }
  110. //Dbg("current audio frame series number is %d.", frm.iseriesnumber);
  111. }
  112. }
  113. if (eSingleWriteLocal == audio_cap->eType){
  114. fwrite(frm.data, frm.framesize, 1,(FILE*)(audio_cap->pdata));
  115. }
  116. }
  117. if (output) {
  118. memset(output, 0, frameCount<<1);
  119. }
  120. return paContinue;
  121. }
  122. static rvc_audio_capture_t *salesrecord_audio_capture_create(rvc_sales_audio_capture_t *cap)
  123. {
  124. rvc_audio_capture_t *audio_cap = ZALLOC_T(rvc_audio_capture_t);
  125. if (audio_cap) {
  126. audio_cap->parent = cap;
  127. audio_cap->audio_shm_queue = new Clibaudioqueue(REC_COMMON_AUDIO_SALES_SHM_QUEUE);
  128. audio_cap->iseriesnumber = 0;
  129. audio_cap->eType = eUnKnown;
  130. audio_cap->pdata = NULL;
  131. }
  132. return audio_cap;
  133. }
  134. static void salesrecord_audio_capture_destroy(rvc_audio_capture_t *audio_cap)
  135. {
  136. if (NULL != audio_cap){
  137. if (NULL != audio_cap->audio_shm_queue){
  138. delete audio_cap->audio_shm_queue;
  139. audio_cap->audio_shm_queue = NULL;
  140. Dbg("set audio_cap audio_shm_queue null");
  141. }
  142. free(audio_cap);
  143. }
  144. }
  145. static audio_capture_t *audio_capture_create(capture_t *cap)
  146. {
  147. audio_capture_t *audio_cap = ZALLOC_T(audio_capture_t);
  148. if (audio_cap) {
  149. audio_cap->parent = cap;
  150. audio_cap->shm_queue = new Clibaudioqueue(REC_COMMON_AUDIO_SHM_QUEUE);
  151. audio_cap->salesol_shm_queue = new Clibaudioqueue(REC_COMMON_AUDIO_SALESOL_SHM_QUEUE);
  152. //audio_cap->sales_shm_queue = new Clibaudioqueue(REC_COMMON_AUDIO_SALES_SHM_QUEUE);
  153. }
  154. return audio_cap;
  155. }
  156. static void audio_capture_destroy(audio_capture_t *audio_cap)
  157. {
  158. delete audio_cap->shm_queue;
  159. delete audio_cap->salesol_shm_queue;
  160. //delete audio_cap->sales_shm_queue;
  161. free(audio_cap);
  162. }
  163. static int audio_capture_start(audio_capture_t *audio_cap)
  164. {
  165. capture_t *cap = audio_cap->parent;
  166. PaStreamParameters inParam = {0};
  167. PaStreamParameters salesInParam = {0};
  168. PaStreamParameters outParam = {0};
  169. PaError paError;
  170. const PaDeviceInfo *info;
  171. int nId = capture_get_audio_device_id(true, cap->config.strAudioIn);
  172. if (nId == -1)
  173. {
  174. //需要立即处理的告警使用Severity_High
  175. LogError(Severity_High,Error_DevMedia,ERROR_MOD_MEDIACONTROLLER_HANDFREEIN_INITFAIL,"hand free in device config error,please check");
  176. return Error_AudioIN;
  177. }
  178. int in_dev_id = translate_id(TRUE, nId);
  179. if (in_dev_id < 0) {
  180. Dbg("audio in device translate failed!");
  181. return Error_AudioIN;
  182. }
  183. info = Pa_GetDeviceInfo(in_dev_id);
  184. if (!info) {
  185. Dbg("get device info failed!");
  186. return Error_AudioIN;
  187. }
  188. inParam.channelCount = 1;
  189. inParam.device = in_dev_id;
  190. inParam.suggestedLatency = info->defaultLowInputLatency;
  191. inParam.sampleFormat = paInt16;
  192. inParam.hostApiSpecificStreamInfo = NULL;
  193. if (Pa_IsFormatSupported(&inParam, NULL, CAPTURE_CLOCK) != paNoError) {
  194. Dbg("audio capture create error, cannot open audio input device");
  195. return Error_AudioIN;
  196. }
  197. nId = capture_get_audio_device_id(false, cap->config.strAudioOut);
  198. if (nId == -1)
  199. {
  200. //需要立即处理的告警使用Severity_High
  201. LogError(Severity_High,Error_DevMedia,ERROR_MOD_MEDIACONTROLLER_HANDFREEOUT_INITFAIL,"hand free out device config error,please check");
  202. return Error_AudioOut;
  203. }
  204. int out_dev_id = translate_id(FALSE, nId);
  205. if (out_dev_id < 0) {
  206. Dbg("audio out device translate failed!");
  207. return Error_AudioOut;
  208. }
  209. info = Pa_GetDeviceInfo(out_dev_id);
  210. if (!info) {
  211. Dbg("get device info failed!");
  212. return Error_AudioOut;
  213. }
  214. outParam.channelCount = 1;
  215. outParam.device = out_dev_id;
  216. outParam.suggestedLatency = info->defaultLowOutputLatency;
  217. outParam.sampleFormat = paInt16;
  218. outParam.hostApiSpecificStreamInfo = NULL;
  219. if (Pa_IsFormatSupported(NULL, &outParam, CAPTURE_CLOCK) != paNoError) {
  220. Dbg("audio capture create error, cannot open audio input device");
  221. return Error_AudioOut;
  222. }
  223. //打开流设备,可以用以下代码替换paError = Pa_OpenStream(&audio_cap->stream, &inParam, &outParam, CAPTURE_CLOCK,
  224. //CAPTURE_FRAME_TIME * CAPTURE_CLOCK/1000, paClipOff|paDitherOff, &StreamCallback, audio_cap);
  225. paError = Pa_OpenStream(&audio_cap->stream, &inParam, NULL, CAPTURE_CLOCK,
  226. CAPTURE_FRAME_TIME * CAPTURE_CLOCK/1000, paClipOff|paDitherOff, &StreamCallback, audio_cap);
  227. if (paError != paNoError) {
  228. Dbg("port audio open stream failed! paError = %d", paError);
  229. return Error_AudioIN;
  230. }
  231. paError = Pa_StartStream(audio_cap->stream);
  232. if (paError != paNoError) {
  233. Dbg("port audio start stream failed! paError = %d", paError);
  234. return Error_AudioIN;
  235. }
  236. return Error_Succeed;
  237. }
  238. static void audio_capture_stop(audio_capture_t *audio_cap)
  239. {
  240. if (audio_cap->stream) {
  241. Pa_AbortStream(audio_cap->stream);
  242. Pa_CloseStream(audio_cap->stream);
  243. audio_cap->stream = NULL;
  244. }
  245. //if (audio_cap->sales_stream) {
  246. // Pa_AbortStream(audio_cap->sales_stream);
  247. // Pa_CloseStream(audio_cap->sales_stream);
  248. // audio_cap->sales_stream = NULL;
  249. //}
  250. }
  251. static int record_audio_capture_start(rvc_audio_capture_t *audio_cap)
  252. {
  253. rvc_sales_audio_capture_t *cap = audio_cap->parent;
  254. PaStreamParameters salesInParam = {0};
  255. PaError paError;
  256. const PaDeviceInfo *info;
  257. int nId = capture_get_audio_device_id(true, cap->rvc_audio_config.strAudioIn);
  258. if (nId == -1)
  259. {
  260. //需要立即处理的告警使用Severity_High
  261. LogError(Severity_High,Error_DevMedia,ERROR_MOD_MEDIACONTROLLER_HANDFREEIN_INITFAIL,"hand free in device config error,please check");
  262. return Error_AudioIN;
  263. }
  264. int in_dev_id = translate_id(TRUE, nId);
  265. if (in_dev_id < 0) {
  266. Dbg("sales audio in device translate failed!");
  267. return Error_AudioIN;
  268. }
  269. info = Pa_GetDeviceInfo(in_dev_id);
  270. if (!info) {
  271. Dbg("get device info failed!");
  272. return Error_AudioIN;
  273. }
  274. salesInParam.channelCount = 1;
  275. salesInParam.device = in_dev_id;
  276. salesInParam.suggestedLatency = info->defaultLowInputLatency;
  277. salesInParam.sampleFormat = paInt16;
  278. salesInParam.hostApiSpecificStreamInfo = NULL;
  279. if (Pa_IsFormatSupported(&salesInParam, NULL, audio_cap->iaudio_capture_samplerate) != paNoError) {
  280. Dbg("sales audio capture create error, cannot open audio input device, and current capture sample rate is %d.",audio_cap->iaudio_capture_samplerate);
  281. return Error_AudioIN;
  282. }
  283. paError = Pa_OpenStream(&audio_cap->stream, &salesInParam, NULL, audio_cap->iaudio_capture_samplerate,
  284. audio_cap->iaudio_capture_peroid * audio_cap->iaudio_capture_samplerate/1000, paClipOff|paDitherOff, &Sales_StreamCallback, audio_cap);
  285. if (paError != paNoError) {
  286. Dbg("port audio open sales stream failed! paError = %d", paError);
  287. return Error_AudioIN;
  288. }
  289. paError = Pa_StartStream(audio_cap->stream);
  290. if (paError != paNoError) {
  291. Dbg("port audio start sales stream failed! paError = %d", paError);
  292. return Error_AudioIN;
  293. }
  294. return Error_Succeed;
  295. }
  296. static void record_audio_capture_stop(rvc_audio_capture_t *audio_cap)
  297. {
  298. if (NULL != audio_cap){
  299. if (audio_cap->stream) {
  300. PaError Error = Pa_AbortStream(audio_cap->stream);
  301. if (paNoError == Error){
  302. Dbg("Pa_AbortStream no error.");
  303. }
  304. Error = Pa_CloseStream(audio_cap->stream);
  305. if (paNoError == Error){
  306. Dbg("Pa_CloseStream no error.");
  307. }
  308. audio_cap->stream = NULL;
  309. }
  310. audio_cap->iseriesnumber = 0;
  311. }
  312. }
  313. static int calc_capture_mode(int width, int height, int *mode)
  314. {
  315. const struct {
  316. int mode;
  317. int width;
  318. int height;
  319. } modes [] = {
  320. {VIDEOCAP_FRAME_SQCIF, VIDEOCAP_SQCIF_WIDTH, VIDEOCAP_SQCIF_HEIGHT},
  321. {VIDEOCAP_FRAME_QQVGA, VIDEOCAP_QQVGA_WIDTH, VIDEOCAP_QQVGA_HEIGHT},
  322. {VIDEOCAP_FRAME_QCIF, VIDEOCAP_QCIF_WIDTH, VIDEOCAP_QCIF_HEIGHT},
  323. {VIDEOCAP_FRAME_QVGA, VIDEOCAP_QVGA_WIDTH, VIDEOCAP_QVGA_HEIGHT},
  324. {VIDEOCAP_FRAME_CIF, VIDEOCAP_CIF_WIDTH, VIDEOCAP_CIF_HEIGHT},
  325. {VIDEOCAP_FRAME_VGA, VIDEOCAP_VGA_WIDTH, VIDEOCAP_VGA_HEIGHT},
  326. {VIDEOCAP_FRAME_4CIF, VIDEOCAP_4CIF_WIDTH, VIDEOCAP_4CIF_HEIGHT},
  327. {VIDEOCAP_FRAME_SVGA, VIDEOCAP_SVGA_WIDTH, VIDEOCAP_SVGA_HEIGHT},
  328. {VIDEOCAP_FRAME_NHD, VIDEOCAP_NHD_WIDTH, VIDEOCAP_NHD_HEIGHT},
  329. {VIDEOCAP_FRAME_SXGA, VIDEOCAP_SXGA_WIDTH, VIDEOCAP_SXGA_HEIGHT},
  330. {VIDEOCAP_FRAME_720P, VIDEOCAP_720P_WIDTH, VIDEOCAP_720P_HEIGHT},
  331. {VIDEOCAP_FRAME_1080P, VIDEOCAP_1080P_WIDTH, VIDEOCAP_1080P_HEIGHT},
  332. };
  333. int i;
  334. for (i = 0; i < array_size(modes); ++i) {
  335. if (modes[i].width == width && modes[i].height == height) {
  336. *mode = modes[i].mode;
  337. return 0;
  338. }
  339. }
  340. return Error_NotExist;
  341. }
  342. static int video_shm_enqueue(Clibvideoqueue *shm_queue, video_frame *frame, int flags)
  343. {
  344. videoq_frame tmp_frm;
  345. tmp_frm.data = frame->data[0];
  346. tmp_frm.framesize = frame->width * frame->height * 3;
  347. tmp_frm.format = VIDEOQ_FORMAT_RGB24;
  348. tmp_frm.width = frame->width;
  349. tmp_frm.height = frame->height;
  350. unsigned int nowtime = y2k_time_now();
  351. if (!shm_queue->InsertVideo(&tmp_frm, flags,nowtime)) {
  352. Dbg("caution: insert shm video failed!");
  353. return Error_Unexpect;
  354. } else {
  355. //Dbg("insert shm video ok!");
  356. return Error_Succeed;
  357. }
  358. }
  359. static void env_cap_on_frame(void *user_data, video_frame *frame)
  360. {
  361. video_capture_t *video_cap = (video_capture_t *)user_data;
  362. capture_t *cap = video_cap->parent;
  363. int rc;
  364. video_cap->frame_id++;
  365. //Dbg("start env on frame, id=%d, tick=%d", video_cap->frame_id, GetTickCount());;
  366. //IplImage*img = NULL;
  367. //img = cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,3);
  368. //img->imageData = (char*)frame->data[0];
  369. //cvSaveImage("d:\\env.jpg", img,0);
  370. //cvReleaseImageHeader(&img);
  371. rc = video_shm_enqueue(video_cap->snapshot_shm_queue, frame, VIDEOQUEUE_FLAG_VERTICAL_FLIP);
  372. if (rc != Error_Succeed)
  373. {
  374. Dbg("env snapshot queue enqueue shm failed! Error = %d, camera_type=%d", rc, video_cap->camera_type);
  375. }
  376. // snapshot
  377. if (rc==Error_Succeed)
  378. {
  379. if (*cap->config.ref_env_capture_count)
  380. {
  381. Dbg("env camera ref_env_capture_count=%d",*cap->config.ref_env_capture_count);
  382. InterlockedDecrement(cap->config.ref_env_capture_count);
  383. LogEvent(Severity_Middle, MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_ENV, "agent capture env ok, and capture env finished!");
  384. }
  385. else if (*cap->config.ref_envopt_capture_count & 2)
  386. {
  387. Dbg("env camera ref_envopt_capture_count=%d",*cap->config.ref_envopt_capture_count);
  388. _InterlockedAnd(cap->config.ref_envopt_capture_count, 0xfffffffD);
  389. if (*cap->config.ref_envopt_capture_count == 0)
  390. {
  391. LogEvent(Severity_Middle, MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_ENVOPT, "agent capture env ok, and capture envopt finished!");
  392. }
  393. }
  394. }
  395. // preview
  396. {
  397. video_frame preview_frame;
  398. video_frame_alloc(REC_COMMON_VIDEO_PREVIEW_WIDTH, REC_COMMON_VIDEO_PREVIEW_HEIGHT, VIDEO_FORMAT_RGB24, &preview_frame);
  399. uint8_t *src_data[4] = {frame->data[0] + 80*3, 0, 0, 0};
  400. sws_scale(video_cap->preview_sws_ctx, src_data, frame->linesize, 0, frame->height, preview_frame.data, preview_frame.linesize);
  401. video_shm_enqueue(video_cap->preview_shm_queue, &preview_frame, 0);
  402. video_frame_free(&preview_frame);
  403. }
  404. // rtp
  405. {
  406. video_frame rtp_frame;
  407. video_frame_alloc(REC_COMMON_VIDEO_RTP_ENV_WIDTH, REC_COMMON_VIDEO_RTP_ENV_HEIGHT, VIDEO_FORMAT_RGB24, &rtp_frame);
  408. uint8_t *src_data[4] = {frame->data[0] + (frame->height-1) * frame->linesize[0], 0, 0, 0};
  409. int src_linesize[4] = {-frame->linesize[0], 0, 0, 0};
  410. sws_scale(video_cap->rtp_sws_ctx, src_data, src_linesize, 0, frame->height, rtp_frame.data, rtp_frame.linesize);
  411. video_shm_enqueue(video_cap->rtp_shm_queue, &rtp_frame, 0);
  412. #if 0
  413. static int i = 0;
  414. if (i == 0 && 0) {
  415. video_frame tmp_frame;
  416. video_frame_alloc(320, 180, VIDEO_FORMAT_RGB24, &tmp_frame);
  417. video_frame_fill_black(&tmp_frame);
  418. videoq_frame frm;
  419. frm.data = tmp_frame.data[0];
  420. video_cap->rtp_shm_queue->GetVideo(&frm, 0);
  421. video_frame_save_bmpfile("d:\\abc.bmp", &tmp_frame);
  422. video_frame_free(&tmp_frame);
  423. //video_frame_save_bmpfile("d:\\ab.bmp", &rtp_frame);
  424. }
  425. #endif
  426. video_frame_free(&rtp_frame);
  427. }
  428. //Dbg("end env on frame, id=%d, tick=%d", video_cap->frame_id, GetTickCount());;
  429. }
  430. static void opt_cap_on_frame(void *user_data, video_frame *frame)
  431. {
  432. video_capture_t *video_cap = (video_capture_t *)user_data;
  433. capture_t *cap = video_cap->parent;
  434. int rc;
  435. video_frame rframe;
  436. int rotate = 0;
  437. //Dbg("opt on frame!");
  438. if (cap->config.video_opt_rotate == 90) {
  439. rotate = 1;
  440. } else if (cap->config.video_opt_rotate == 270) {
  441. rotate = -1;
  442. } else {
  443. return;
  444. }
  445. video_cap->frame_id++;
  446. // prepare for rotation
  447. video_frame_alloc(frame->height, frame->width, frame->format, &rframe);
  448. #if 1 // use IPP, it's fast
  449. {
  450. IppiSize srcSize;
  451. srcSize.width = frame->width;
  452. srcSize.height = frame->height;
  453. IppiRect srcROI;
  454. srcROI.width = frame->width;
  455. srcROI.height = frame->height;
  456. srcROI.x = 0;
  457. srcROI.y = 0;
  458. IppiRect dstROI;
  459. dstROI.width = frame->height;
  460. dstROI.height = frame->width;
  461. dstROI.x = 0;
  462. dstROI.y = 0;
  463. ippiTranspose_8u_C3R(frame->data[0], frame->linesize[0], rframe.data[0], rframe.linesize[0], srcSize);
  464. IppiAxis flip;
  465. if (rotate == 1)
  466. {
  467. flip = ippAxsVertical;
  468. ippiMirror_8u_C3IR(rframe.data[0], rframe.linesize[0], srcSize, flip);
  469. }
  470. else
  471. {
  472. IppiSize Size;
  473. Size.width = rframe.width;
  474. Size.height = rframe.height;
  475. //flip = ippAxsHorizontal;
  476. flip = ippAxsBoth;
  477. ippiMirror_8u_C3IR(rframe.data[0], rframe.linesize[0], Size, flip);
  478. }
  479. }
  480. #else
  481. videohorflip_rotate(frame->data[0], frame->width, frame->height, rframe.data[0], rotate);
  482. #endif
  483. rc = video_shm_enqueue(video_cap->snapshot_shm_queue, &rframe, 0);
  484. if (rc != Error_Succeed)
  485. {
  486. Dbg("opt snapshot queue enqueue shm failed! Error = %d, camera_type=%d", rc, video_cap->camera_type);
  487. }
  488. // snapshot
  489. if (rc==Error_Succeed)
  490. {
  491. if (*cap->config.ref_opt_capture_count)
  492. {
  493. Dbg("opt camera ref_opt_capture_count=%d",*cap->config.ref_opt_capture_count);
  494. InterlockedDecrement(cap->config.ref_opt_capture_count);
  495. LogEvent(Severity_Middle, MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_OPT, "agent capture opt ok, and capture opt finished!");
  496. }
  497. else if (*cap->config.ref_envopt_capture_count&1)
  498. {
  499. Dbg("opt camera ref_envopt_capture_count=%d",*cap->config.ref_envopt_capture_count);
  500. if (InterlockedDecrement(cap->config.ref_envopt_capture_count) == 0)
  501. {
  502. LogEvent(Severity_Middle, MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_ENVOPT, "agent capture opt ok, and capture envopt finished!");
  503. }
  504. }
  505. }
  506. // rtp
  507. {
  508. video_frame rtp_frame = {0};
  509. video_frame_alloc(REC_COMMON_VIDEO_RTP_OPT_WIDTH, REC_COMMON_VIDEO_RTP_OPT_HEIGHT, VIDEO_FORMAT_RGB24, &rtp_frame);
  510. sws_scale(video_cap->rtp_sws_ctx, rframe.data, rframe.linesize, 0, rframe.height, rtp_frame.data, rtp_frame.linesize);
  511. video_shm_enqueue(video_cap->rtp_shm_queue, &rtp_frame, 0);
  512. video_frame_free(&rtp_frame);
  513. }
  514. video_frame_free(&rframe);
  515. }
  516. static video_capture_t *video_capture_create(capture_t *cap, int camera_type)
  517. {
  518. video_capture_t *video_cap = ZALLOC_T(video_capture_t);
  519. if (video_cap) {
  520. video_cap->parent = cap;
  521. video_cap->camera_type = camera_type;
  522. video_cap->frame_id = 0;
  523. if (camera_type == CAMERA_TYPE_ENV) {
  524. video_cap->snapshot_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_SNAPSHOT_QUEUE);
  525. video_cap->rtp_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_RTP_QUEUE);
  526. video_cap->rtp_sws_ctx = sws_getContext(REC_COMMON_VIDEO_SNAPSHOT_WIDTH,
  527. REC_COMMON_VIDEO_SNAPSHOT_HEIGHT,
  528. PIX_FMT_BGR24,
  529. REC_COMMON_VIDEO_RTP_ENV_WIDTH,
  530. REC_COMMON_VIDEO_RTP_ENV_HEIGHT,
  531. PIX_FMT_BGR24,
  532. SWS_POINT, NULL, NULL, NULL);
  533. video_cap->preview_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_PREVIEW_QUEUE);
  534. video_cap->preview_sws_ctx = sws_getContext(REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_WIDTH,
  535. REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_HEIGHT,
  536. PIX_FMT_BGR24,
  537. REC_COMMON_VIDEO_PREVIEW_WIDTH,
  538. REC_COMMON_VIDEO_PREVIEW_HEIGHT,
  539. PIX_FMT_BGR24,
  540. SWS_FAST_BILINEAR, NULL, NULL, NULL);
  541. } else {
  542. video_cap->snapshot_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_SNAPSHOT_QUEUE);
  543. video_cap->rtp_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_RTP_QUEUE);
  544. video_cap->rtp_sws_ctx = sws_getContext(REC_COMMON_VIDEO_SNAPSHOT_HEIGHT,
  545. REC_COMMON_VIDEO_SNAPSHOT_WIDTH,
  546. PIX_FMT_BGR24,
  547. REC_COMMON_VIDEO_RTP_OPT_WIDTH,
  548. REC_COMMON_VIDEO_RTP_OPT_HEIGHT,
  549. PIX_FMT_BGR24,
  550. SWS_POINT, NULL, NULL, NULL);
  551. //video_cap->preview_shm_queue = NULL;
  552. //video_cap->preview_sws_ctx = NULL;
  553. }
  554. }
  555. return video_cap;
  556. }
  557. static void video_capture_destroy(video_capture_t *video_cap)
  558. {
  559. if (video_cap) {
  560. if (video_cap->preview_sws_ctx) {
  561. sws_freeContext(video_cap->preview_sws_ctx);
  562. video_cap->preview_sws_ctx = NULL;
  563. }
  564. if (video_cap->rtp_sws_ctx) {
  565. sws_freeContext(video_cap->rtp_sws_ctx);
  566. video_cap->rtp_sws_ctx = NULL;
  567. }
  568. if (video_cap->snapshot_shm_queue) {
  569. delete video_cap->snapshot_shm_queue;
  570. video_cap->snapshot_shm_queue = NULL;
  571. }
  572. if (video_cap->rtp_shm_queue) {
  573. delete video_cap->rtp_shm_queue;
  574. video_cap->rtp_shm_queue = NULL;
  575. }
  576. if (video_cap->preview_shm_queue) {
  577. delete video_cap->preview_shm_queue;
  578. video_cap->preview_shm_queue = NULL;
  579. }
  580. free(video_cap);
  581. }
  582. }
  583. static int video_capture_start(video_capture_t *video_cap)
  584. {
  585. capture_config_t *conf = &video_cap->parent->config;
  586. int dev_id;
  587. if (video_cap->camera_type == CAMERA_TYPE_ENV)
  588. {
  589. dev_id = capture_get_video_device_id(conf->strVideoEnv);
  590. if (dev_id == -1)
  591. {
  592. Dbg("No environment camera,please check config file or device!");
  593. return -1;
  594. }
  595. }
  596. else
  597. {
  598. dev_id = capture_get_video_device_id(conf->strVideoOpt);
  599. if (dev_id == -1)
  600. {
  601. Dbg("No operation camera,please check config file or device!");
  602. return -1;
  603. }
  604. }
  605. videocap_param param = {0};
  606. int cap_mode;
  607. int rc = -1;
  608. rc = calc_capture_mode(REC_COMMON_VIDEO_SNAPSHOT_WIDTH, REC_COMMON_VIDEO_SNAPSHOT_HEIGHT, &cap_mode);
  609. if (rc != 0)
  610. {
  611. Dbg("calc cap_mode failed!");
  612. return rc;
  613. }
  614. param.cap_mode = cap_mode;
  615. param.dev_id = dev_id;
  616. param.frame_fmt = VIDEO_FORMAT_RGB24;
  617. if ((ePadtype == g_eDeviceType)||(eMobilePadType == g_eDeviceType)||(eDesk2SType == g_eDeviceType)||(eDesk1SType == g_eDeviceType)||(eDesk2SIntegratedType == g_eDeviceType))
  618. {
  619. param.fps = REC_COMMON_VIDEO_PADRAW_FPS;
  620. }
  621. else
  622. {
  623. param.fps = REC_COMMON_VIDEO_RAW_FPS;
  624. }
  625. param.on_frame = (video_cap->camera_type == CAMERA_TYPE_ENV ? &env_cap_on_frame : &opt_cap_on_frame);
  626. param.user_data = video_cap;
  627. param.option = 0;
  628. rc = videocap_create(&video_cap->cap, &param);
  629. if (rc != 0)
  630. {
  631. Dbg("videocap create failed!");
  632. return rc;
  633. }
  634. rc = videocap_start(video_cap->cap);
  635. if (rc != 0)
  636. {
  637. Dbg("videocap start failed!");
  638. videocap_destroy(video_cap->cap);
  639. video_cap->cap = NULL;
  640. return rc;
  641. }
  642. else{
  643. Dbg("videocap start success!");
  644. }
  645. return 0;
  646. }
  647. static void video_capture_stop(video_capture_t *video_cap)
  648. {
  649. if (video_cap->cap) {
  650. videocap_stop(video_cap->cap);
  651. videocap_destroy(video_cap->cap);
  652. video_cap->cap = NULL;
  653. }
  654. }
  655. static int audio_get_dev_count(int *in_cnt, int *out_cnt)
  656. {
  657. int icnt = 0, ocnt = 0;
  658. int cnt = Pa_GetDeviceCount();
  659. for (int i = 0; i < cnt; ++i) {
  660. const PaDeviceInfo *info = Pa_GetDeviceInfo(i);
  661. if (info->maxInputChannels)
  662. icnt ++;
  663. if (info->maxOutputChannels)
  664. ocnt ++;
  665. }
  666. if (in_cnt)
  667. *in_cnt = icnt;
  668. if (out_cnt)
  669. *out_cnt = ocnt;
  670. return 0;
  671. }
  672. static CSimpleStringA audio_get_dev_name(bool in_direction, int idx)
  673. {
  674. int cnt = Pa_GetDeviceCount();
  675. int ii, i;
  676. for (i = 0, ii = 0; i < cnt; ++i) {
  677. const PaDeviceInfo *info = Pa_GetDeviceInfo(i);
  678. if (in_direction) {
  679. if (info->maxInputChannels) {
  680. if (idx == ii) {
  681. return CSimpleStringA(info->name);
  682. }
  683. ii++;
  684. }
  685. } else {
  686. if (info->maxOutputChannels) {
  687. if (idx == ii) {
  688. return CSimpleStringA(info->name);
  689. }
  690. ii++;
  691. }
  692. }
  693. }
  694. return CSimpleStringA();
  695. }
  696. namespace MediaController {
  697. DeviceTypeEnum g_eDeviceType;
  698. int capture_create(const capture_config_t *config, capture_t **p_cap)
  699. {
  700. capture_t *cap = ZALLOC_T(capture_t);
  701. cap->audio = NULL;
  702. cap->env_video = NULL;
  703. cap->opt_video = NULL;
  704. memcpy(&cap->config, config, sizeof(capture_config_t));
  705. cap->audio = audio_capture_create(cap);
  706. if (!cap->audio) {
  707. Dbg("create audio capture object failed!");
  708. return Error_Unexpect;
  709. }
  710. int dev_id_env = capture_get_video_device_id(config->strVideoEnv);
  711. if (dev_id_env != -1)
  712. {
  713. cap->env_video = video_capture_create(cap, CAMERA_TYPE_ENV);
  714. if (!cap->env_video) {
  715. Dbg("create env video object failed!");
  716. return Error_Unexpect;
  717. }
  718. }
  719. int dev_id_opt = -1;
  720. if (eStand2sType == g_eDeviceType)
  721. {
  722. dev_id_opt = capture_get_video_device_id(config->strVideoOpt);
  723. }
  724. if (dev_id_opt != -1)
  725. {
  726. cap->opt_video = video_capture_create(cap, CAMERA_TYPE_OPT);
  727. if (!cap->opt_video)
  728. {
  729. Dbg("create opt video object failed!");
  730. return Error_Unexpect;
  731. }
  732. }
  733. if((dev_id_env == -1)&&(dev_id_opt == -1))
  734. {
  735. Dbg("all camera device id error!");
  736. capture_destroy(cap);
  737. return Error_Unexpect;
  738. }
  739. else
  740. {
  741. *p_cap = cap;
  742. return 0;
  743. }
  744. }
  745. ErrorCodeEnum capture_create(const capture_config_t *config,capture_t *cap,int nCamera)
  746. {
  747. int dev_id1 = capture_get_video_device_id(config->strVideoEnv);
  748. int dev_id2 = capture_get_video_device_id(config->strVideoOpt);
  749. if((dev_id1 != -1)&&(nCamera==ENVCAMERA)&&(cap->env_video == NULL))
  750. {
  751. cap->env_video = video_capture_create(cap, CAMERA_TYPE_ENV);
  752. if (!cap->env_video)
  753. {
  754. Dbg("create env video object failed!");
  755. return Error_Unexpect;
  756. }
  757. }
  758. else if((dev_id2 != -1)&&(nCamera == OPTCAMERA)&&(cap->opt_video == NULL))
  759. {
  760. cap->opt_video = video_capture_create(cap, CAMERA_TYPE_OPT);
  761. if (!cap->opt_video)
  762. {
  763. Dbg("create opt video object failed!");
  764. return Error_Unexpect;
  765. }
  766. }
  767. else
  768. {
  769. return Error_Unexpect;
  770. }
  771. return Error_Succeed;
  772. }
  773. int salesaudio_capture_create(rvc_audio_capture_config_t *config, rvc_sales_audio_capture_t **p_cap)
  774. {
  775. rvc_sales_audio_capture_t *cap = ZALLOC_T(rvc_sales_audio_capture_t);
  776. cap->rvc_audio = NULL;
  777. memcpy(&cap->rvc_audio_config, config, sizeof(rvc_audio_capture_config_t));
  778. cap->rvc_audio = salesrecord_audio_capture_create(cap);
  779. if (!cap->rvc_audio) {
  780. Dbg("create sales record audio capture object failed!");
  781. salesaudio_capture_destroy(cap);
  782. cap = NULL;
  783. return Error_Unexpect;
  784. }
  785. else
  786. {
  787. cap->rvc_audio->iaudio_capture_peroid = config->audio_capture_period;
  788. cap->rvc_audio->iaudio_capture_samplerate = config->audio_capture_samplerate;
  789. *p_cap = cap;
  790. Dbg("create sales record audio capture object(%0x) success, capture sample rate is %d, and rvc audio(%0x) in device is %s.",
  791. cap, cap->rvc_audio->iaudio_capture_samplerate, cap->rvc_audio, cap->rvc_audio_config.strAudioIn.GetData());
  792. return 0;
  793. }
  794. }
  795. void salesaudio_capture_destroy(rvc_sales_audio_capture_t *cap)
  796. {
  797. if (NULL != cap){
  798. Dbg("sales audio capture destroy, and cap addr is %0x.", cap);
  799. if (cap->rvc_audio) {
  800. Dbg("sales audio capture rvc_audio destroy, and rvc_audio addr is %0x.", cap->rvc_audio);
  801. salesrecord_audio_capture_destroy(cap->rvc_audio);
  802. cap->rvc_audio = NULL;
  803. Dbg("set rvc_audio to null.");
  804. }
  805. free(cap);
  806. }
  807. }
  808. void capture_destroy(capture_t *cap)
  809. {
  810. if (cap) {
  811. if (cap->env_video) {
  812. video_capture_destroy(cap->env_video);
  813. cap->env_video = NULL;
  814. }
  815. if (cap->opt_video) {
  816. video_capture_destroy(cap->opt_video);
  817. cap->opt_video = NULL;
  818. }
  819. if (cap->audio) {
  820. audio_capture_destroy(cap->audio);
  821. cap->audio = NULL;
  822. }
  823. free(cap);
  824. }
  825. }
  826. void capture_destroy(capture_t *cap,int nCamera)
  827. {
  828. if (cap)
  829. {
  830. if((cap->env_video)&&(nCamera == ENVCAMERA))
  831. {
  832. video_capture_destroy(cap->env_video);
  833. cap->env_video = NULL;
  834. }
  835. else if((cap->opt_video)&&(nCamera==OPTCAMERA))
  836. {
  837. video_capture_destroy(cap->opt_video);
  838. cap->opt_video = NULL;
  839. }
  840. }
  841. }
  842. ErrorCodeEnum start_audio_capture(audio_capture_t *paudio)
  843. {
  844. ErrorCodeEnum rslt = Error_Succeed;
  845. int rc = 0;
  846. if (NULL != paudio) {
  847. rc = audio_capture_start(paudio);
  848. if (rc != Error_Succeed) {
  849. rslt = (ErrorCodeEnum)rc;
  850. if (rslt == Error_AudioIN){
  851. Dbg("start audio In object failed! rc:%d", rc);
  852. LogError(Severity_High,Error_NotInit,ERROR_MOD_MEDIACONTROLLER_HANDFREE_OPENFAIL,"open audioIn device fail,please check device");
  853. }
  854. else{
  855. Dbg("start audio Out object failed! rc:%d", rc);
  856. LogError(Severity_High,Error_NotInit,ERROR_MOD_MEDIACONTROLLER_HANDFREE_OPENFAIL,"open audioOut device fail,please check device");
  857. }
  858. }
  859. }
  860. else{
  861. Dbg("audio_capture_t is null.");
  862. rslt = Error_AudioIN;
  863. }
  864. return rslt;
  865. }
  866. ErrorCodeEnum capture_start(capture_t *cap)
  867. {
  868. int rc = 0;
  869. ErrorCodeEnum rslt = start_audio_capture(cap->audio);
  870. if (Error_Succeed != rslt){
  871. return rslt;
  872. }
  873. if (cap->env_video)
  874. {
  875. rc = video_capture_start(cap->env_video);
  876. if (rc != Error_Succeed)
  877. {
  878. Dbg("start env video capture object failed! rc:%d", rc);
  879. char strMessage[MAX_PATH*2] = {0};
  880. get_camera_exception_message(strMessage, MAX_PATH*2, cap->config.strVideoEnv, "open environ camera fail,please check device.");
  881. LogError(Severity_High,Error_NotInit,ERROR_MOD_MEDIACONTROLLER_ENVCAM_OPEN,strMessage);
  882. if (cap->opt_video)
  883. {
  884. if (cap->env_video)
  885. {
  886. ::Sleep(3000);
  887. }
  888. rc = video_capture_start(cap->opt_video);
  889. if (rc != Error_Succeed)
  890. {
  891. Dbg("start opt video capture object failed! rc:%d", rc);
  892. char strMessage[MAX_PATH*2] = {0};
  893. get_camera_exception_message(strMessage, MAX_PATH*2, cap->config.strVideoOpt, "open operate camera fail,please check device.");
  894. LogError(Severity_High,Error_NotInit,ERROR_MOD_MEDIACONTROLLER_OPTCAM_OPEN, strMessage);
  895. return Error_AllCamera;
  896. }
  897. else
  898. {
  899. Dbg("start env video capture object failed!");
  900. return Error_EnvCamera;
  901. }
  902. }
  903. else
  904. {
  905. Dbg("start all video capture object failed!");
  906. return Error_AllCamera;
  907. }
  908. }
  909. else
  910. {
  911. if (cap->opt_video)
  912. {
  913. if (cap->env_video)
  914. {
  915. ::Sleep(3000);
  916. }
  917. rc = video_capture_start(cap->opt_video);
  918. if (rc != Error_Succeed)
  919. {
  920. Dbg("start opt video capture object failed! rc:%d", rc);
  921. char strMessage[MAX_PATH*2] = {0};
  922. get_camera_exception_message(strMessage, MAX_PATH*2, cap->config.strVideoOpt, "open operate camera fail,please check device.");
  923. LogError(Severity_High,Error_NotInit,ERROR_MOD_MEDIACONTROLLER_OPTCAM_OPEN,strMessage);
  924. return Error_OptCamera;
  925. }
  926. else
  927. {
  928. return Error_Succeed;
  929. }
  930. }
  931. else
  932. {
  933. Dbg("opt_video = Null");
  934. return Error_OptCamera;
  935. }
  936. }
  937. }
  938. else
  939. {
  940. if (cap->opt_video)
  941. {
  942. if (cap->env_video)
  943. {
  944. ::Sleep(3000);
  945. }
  946. rc = video_capture_start(cap->opt_video);
  947. if (rc != Error_Succeed)
  948. {
  949. Dbg("start opt video capture object failed! rc:%d", rc);
  950. char strMessage[MAX_PATH*2] = {0};
  951. get_camera_exception_message(strMessage, MAX_PATH*2, cap->config.strVideoOpt, "open operate camera fail,please check device.");
  952. LogError(Severity_High,Error_NotInit,ERROR_MOD_MEDIACONTROLLER_OPTCAM_OPEN,strMessage);
  953. return Error_AllCamera;
  954. }
  955. else
  956. {
  957. Dbg("start env video capture object failed!");
  958. return Error_EnvCamera;
  959. }
  960. }
  961. else
  962. {
  963. Dbg("start env video Error_AllCamera");
  964. return Error_AllCamera;
  965. }
  966. }
  967. }
  968. ErrorCodeEnum capture_start(capture_t *cap,int nCamera)
  969. {
  970. int rc = 0;
  971. if (cap->env_video&&(nCamera==ENVCAMERA))
  972. {
  973. rc = video_capture_start(cap->env_video);
  974. if (rc != Error_Succeed)
  975. {
  976. return Error_Hardware;
  977. }
  978. else
  979. {
  980. return Error_Succeed;
  981. }
  982. }
  983. else if(cap->opt_video&&(nCamera==OPTCAMERA))
  984. {
  985. rc = video_capture_start(cap->opt_video);
  986. if (rc != Error_Succeed)
  987. {
  988. return Error_Hardware;
  989. }
  990. else
  991. {
  992. return Error_Succeed;
  993. }
  994. }
  995. else
  996. {
  997. return Error_Unexpect;
  998. }
  999. }
  1000. ErrorCodeEnum salesrecord_audio_capture_start(rvc_sales_audio_capture_t *cap)
  1001. {
  1002. auto rc = Error_Param;
  1003. if (NULL == cap){
  1004. return rc;
  1005. }
  1006. if (cap->rvc_audio) {
  1007. int rslt = record_audio_capture_start(cap->rvc_audio);
  1008. if (rslt != Error_Succeed) {
  1009. if (Error_AudioIN == rslt){
  1010. Dbg("start audio In object failed! rc:%d", rc);
  1011. LogError(Severity_High,Error_NotInit,ERROR_MOD_MEDIACONTROLLER_HANDFREE_OPENFAIL,"open audioIn device fail,please check device");
  1012. }
  1013. rc = (ErrorCodeEnum)rslt;
  1014. }
  1015. else{
  1016. rc = Error_Succeed;
  1017. }
  1018. }
  1019. return rc;
  1020. }
  1021. void salesrecord_audio_capture_stop(rvc_sales_audio_capture_t *cap)
  1022. {
  1023. if (NULL != cap){
  1024. if (cap->rvc_audio){
  1025. if (cap->rvc_audio->pdata){
  1026. if (eSingleWriteLocal == cap->rvc_audio->eType){
  1027. fclose((FILE*)cap->rvc_audio->pdata);
  1028. cap->rvc_audio->pdata = NULL;
  1029. }
  1030. }
  1031. record_audio_capture_stop(cap->rvc_audio);
  1032. }
  1033. }
  1034. else{
  1035. Dbg("sales record audio capture stop failed for param error.");
  1036. }
  1037. }
  1038. void capture_stop(capture_t *cap)
  1039. {
  1040. if (cap->audio) {
  1041. audio_capture_stop(cap->audio);
  1042. }
  1043. if (cap->env_video) {
  1044. video_capture_stop(cap->env_video);
  1045. }
  1046. if (cap->opt_video) {
  1047. video_capture_stop(cap->opt_video);
  1048. }
  1049. }
  1050. int capture_detect_camera_bug(capture_t *cap, int *env_n, int *opt_n,BOOL IsPad)
  1051. {
  1052. *env_n = 0;
  1053. *opt_n = 0;
  1054. if (cap->env_video)
  1055. {
  1056. if (cap->env_video->rtp_shm_queue)
  1057. {
  1058. *env_n = cap->env_video->rtp_shm_queue->GetVideoLens();
  1059. }
  1060. }
  1061. else
  1062. {
  1063. *env_n = -1;
  1064. }
  1065. if (cap->opt_video)
  1066. {
  1067. if (cap->opt_video->rtp_shm_queue)
  1068. {
  1069. *opt_n = cap->opt_video->rtp_shm_queue->GetVideoLens();
  1070. }
  1071. }
  1072. else
  1073. {
  1074. if (!IsPad)
  1075. {
  1076. *opt_n = -1;
  1077. }
  1078. else
  1079. {
  1080. *opt_n = 1;
  1081. }
  1082. }
  1083. return 0;
  1084. }
  1085. int capture_get_last_frametime(capture_t *cap, DWORD *env_n, DWORD *opt_n,BOOL IsPad)
  1086. {
  1087. *env_n = 0;
  1088. *opt_n = 0;
  1089. if (cap->env_video)
  1090. {
  1091. if (cap->env_video->rtp_shm_queue)
  1092. {
  1093. *env_n = cap->env_video->rtp_shm_queue->GetLastFrameTime();
  1094. }
  1095. }
  1096. else
  1097. {
  1098. *env_n = 0;
  1099. }
  1100. if (cap->opt_video)
  1101. {
  1102. if (cap->opt_video->rtp_shm_queue)
  1103. {
  1104. *opt_n = cap->opt_video->rtp_shm_queue->GetLastFrameTime();
  1105. }
  1106. }
  1107. else
  1108. {
  1109. *opt_n = 0;
  1110. }
  1111. return 0;
  1112. }
  1113. int capture_get_audio_device_id(bool in_direction, const char *dev_name)
  1114. {
  1115. int cnt = Pa_GetDeviceCount();
  1116. int ii, i;
  1117. for (i = 0, ii = 0; i < cnt; ++i) {
  1118. const PaDeviceInfo *info = Pa_GetDeviceInfo(i);
  1119. if (in_direction) {
  1120. if (info->maxInputChannels) {
  1121. if (strstr(info->name, dev_name) != NULL) {
  1122. return ii;
  1123. }
  1124. ii++;
  1125. }
  1126. } else {
  1127. if (info->maxOutputChannels) {
  1128. if (strstr(info->name, dev_name) != NULL) {
  1129. return ii;
  1130. }
  1131. ii++;
  1132. }
  1133. }
  1134. }
  1135. return -1;
  1136. }
  1137. int capture_get_video_device_id(const char *dev_name)
  1138. {
  1139. int i, n;
  1140. n = videocap_get_device_count();
  1141. for (i = 0; i < n; ++i) {
  1142. WCHAR tmp[256];
  1143. char t[256];
  1144. WCHAR tmp1[256];
  1145. char t1[256];
  1146. videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  1147. WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  1148. videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
  1149. WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
  1150. // save DevicePath (add by ly at 20160725)
  1151. char t2[256];
  1152. strcpy(t2,t1);
  1153. for (int j = 0; j < strlen(t2); ++j)
  1154. {
  1155. t2[j] = toupper(t2[j]);
  1156. if (t2[j] == '#') t2[j] = '\\';
  1157. }
  1158. {
  1159. unsigned char x[MD5_DIGESTSIZE];
  1160. md5_ctx_t ctx;
  1161. md5_init(&ctx);
  1162. md5(x, t1, strlen(t1));
  1163. Bin2Str(x, sizeof(x), t1, sizeof(t1));
  1164. }
  1165. if (dev_name != NULL && strlen(dev_name) > 1 &&
  1166. strstr(dev_name, ";") == NULL) // 外接摄像头 add by ly at 20160531
  1167. {
  1168. /*if (strstr(t, dev_name) != NULL)
  1169. {
  1170. Dbg("[dbg] %s founded in %d cameras.", dev_name, n);
  1171. return i;
  1172. }*/
  1173. Dbg("[dbg] device_path: %s",t2);
  1174. if (strstr(t2,dev_name) != NULL) // 判断外接摄像头DeviceLocationPaths是否是DevicePath的子串
  1175. {
  1176. Dbg("[dbg] %s founded in %d cameras.", dev_name, n);
  1177. return i;
  1178. }
  1179. if (strcmp(dev_name, t) == 0) // 如果是直接用友好名称查询(适用于高拍仪) add by ly 2017/11/08
  1180. return i;
  1181. }
  1182. else
  1183. {
  1184. strcat(t, ";");
  1185. strcat(t, t1);
  1186. if (strcmp(dev_name, t) == 0)
  1187. return i;
  1188. }
  1189. }
  1190. return -1; // not found
  1191. }
  1192. int capture_lib_init() // edit by ly at 20160401
  1193. {
  1194. HRESULT hr = CoInitialize(NULL);
  1195. int rc;
  1196. {
  1197. HMODULE hModule = GetModuleHandleA("MSVCR100.dll");
  1198. if (hModule) {
  1199. typedef char *(*f_setlocale)(int, const char*);
  1200. f_setlocale f = (f_setlocale)GetProcAddress(hModule, "setlocale");
  1201. (*f)(LC_ALL, "chs");
  1202. }
  1203. }
  1204. if (SUCCEEDED(hr)) {
  1205. PaError Error;
  1206. Error = Pa_Initialize();
  1207. if (Error == paNoError) {
  1208. rc = videoframework_init();
  1209. if (rc != 0) {
  1210. Dbg("videoframework_init failed, rc=%d", rc);
  1211. return Error_Resource;
  1212. }
  1213. } else {
  1214. Dbg("PaInitialize failed, rc=%d", Error);
  1215. return Error_Resource;
  1216. }
  1217. } else {
  1218. Dbg("coinitialze failed! hr:%d", hr);
  1219. return Error_Resource;
  1220. }
  1221. {
  1222. int i, n;
  1223. n = videocap_get_device_count();
  1224. for (i = 0; i < n; ++i) {
  1225. WCHAR tmp[256];
  1226. char t[256];
  1227. WCHAR tmp1[256];
  1228. char t1[256];
  1229. videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  1230. WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  1231. videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
  1232. WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
  1233. {
  1234. unsigned char x[MD5_DIGESTSIZE];
  1235. md5_ctx_t ctx;
  1236. md5_init(&ctx);
  1237. md5(x, t1, strlen(t1));
  1238. Bin2Str(x, sizeof(x), t1, sizeof(t1));
  1239. }
  1240. Dbg("%d = %s;%s", i, t, t1);
  1241. }
  1242. }
  1243. {
  1244. int icnt, ocnt;
  1245. rc = audio_get_dev_count(&icnt, &ocnt);
  1246. if (rc == 0) {
  1247. int i;
  1248. Dbg("audio input devices(%d):", icnt);
  1249. for (i = 0; i < icnt; ++i) {
  1250. CSimpleStringA str = audio_get_dev_name(true, i);
  1251. Dbg("%d = %s", i, (LPCSTR)str);
  1252. }
  1253. Dbg("audio output devices(%d):", ocnt);
  1254. for (i = 0; i < ocnt; ++i) {
  1255. CSimpleStringA str = audio_get_dev_name(false, i);
  1256. Dbg("%d = %s", i, (LPCSTR)str);
  1257. }
  1258. }
  1259. }
  1260. return Error_Succeed;
  1261. }
  1262. int videocap_innerdev_fetch(CSimpleStringA&frontcam,CSimpleStringA&rearcam) // edit by ly at 20160401
  1263. {
  1264. int i, n;
  1265. frontcam = "$", rearcam = "$";
  1266. n = videocap_get_device_count();
  1267. if (n == 2) // 未插入外接时
  1268. {
  1269. for (i = 0; i < n; ++i) {
  1270. WCHAR tmp[256];
  1271. char t[256];
  1272. WCHAR tmp1[256];
  1273. char t1[256];
  1274. videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  1275. WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  1276. videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
  1277. WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
  1278. {
  1279. unsigned char x[MD5_DIGESTSIZE];
  1280. md5_ctx_t ctx;
  1281. md5_init(&ctx);
  1282. md5(x, t1, strlen(t1));
  1283. Bin2Str(x, sizeof(x), t1, sizeof(t1));
  1284. }
  1285. strcat(t, ";");
  1286. strcat(t, t1);
  1287. if (i==0)
  1288. {
  1289. frontcam = t;
  1290. }
  1291. else if(i==1)
  1292. {
  1293. rearcam = t;
  1294. }
  1295. }
  1296. }
  1297. else
  1298. {
  1299. Dbg("[dbg] detect %d cameras.", n);
  1300. }
  1301. return n;
  1302. }
  1303. int videocap_outerdev_fetch( CSimpleStringA envcam,CSimpleStringA optcam,CSimpleStringA ewscam,CAutoArray<CSimpleStringA> &hspcams,CSimpleStringA &outercam )
  1304. {
  1305. int i, n;
  1306. outercam = "$";
  1307. n = videocap_get_device_count();
  1308. int m = hspcams.GetCount(); // 高拍仪的个数 add by ly 2017/11/07
  1309. if (ewscam.GetLength() <= 1) // 如果外部广角摄像头未配置
  1310. {
  1311. if (n <= m || n > 3+m)
  1312. {
  1313. Dbg("[dbg] [videocap_outerdev_fetch] detect %d cameras.", n);
  1314. }
  1315. else
  1316. {
  1317. int cnt = 0;
  1318. for (i = 0; i < n; ++i) {
  1319. WCHAR tmp[256];
  1320. char t[256];
  1321. WCHAR tmp1[256];
  1322. char t1[256];
  1323. videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  1324. WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  1325. videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
  1326. WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
  1327. // save DevicePath (add by ly at 20160725)
  1328. char t2[256];
  1329. strcpy(t2,t1);
  1330. for (int j = 0; j < strlen(t2); ++j)
  1331. {
  1332. t2[j] = toupper(t2[j]);
  1333. if (t2[j] == '#') t2[j] = '\\';
  1334. }
  1335. {
  1336. unsigned char x[MD5_DIGESTSIZE];
  1337. md5_ctx_t ctx;
  1338. md5_init(&ctx);
  1339. md5(x, t1, strlen(t1));
  1340. Bin2Str(x, sizeof(x), t1, sizeof(t1));
  1341. }
  1342. // 检查是否为高拍仪摄像头,若是则直接枚举下一个摄像头
  1343. bool isHspCam = false;
  1344. for (int k = 0; k < m; ++k) {
  1345. if (!strcmp((LPCTSTR)hspcams[k],t)) {
  1346. isHspCam = true;
  1347. break;
  1348. }
  1349. }
  1350. if (isHspCam) {
  1351. continue;
  1352. }
  1353. strcat(t, ";");
  1354. strcat(t, t1);
  1355. if(strcmp(t,(LPCTSTR)envcam) && strcmp(t,(LPCTSTR)optcam))
  1356. {
  1357. if (0 == cnt)
  1358. {
  1359. outercam = t2;
  1360. }
  1361. ++cnt;
  1362. }
  1363. }
  1364. if(cnt != 1)
  1365. {
  1366. outercam = "$";
  1367. }
  1368. }
  1369. }
  1370. else // 如果外部广角摄像头已配置
  1371. {
  1372. if (n <= m || n > 4+m)
  1373. {
  1374. Dbg("[dbg] [videocap_outerdev_fetch] detect %d cameras.", n);
  1375. }
  1376. else
  1377. {
  1378. int cnt = 0;
  1379. for (i = 0; i < n; ++i) {
  1380. WCHAR tmp[256];
  1381. char t[256];
  1382. WCHAR tmp1[256];
  1383. char t1[256];
  1384. videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  1385. WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  1386. videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
  1387. WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
  1388. // save DevicePath (add by ly at 20160725)
  1389. char t2[256];
  1390. strcpy(t2,t1);
  1391. for (int j = 0; j < strlen(t2); ++j)
  1392. {
  1393. t2[j] = toupper(t2[j]);
  1394. if (t2[j] == '#') t2[j] = '\\';
  1395. }
  1396. {
  1397. unsigned char x[MD5_DIGESTSIZE];
  1398. md5_ctx_t ctx;
  1399. md5_init(&ctx);
  1400. md5(x, t1, strlen(t1));
  1401. Bin2Str(x, sizeof(x), t1, sizeof(t1));
  1402. }
  1403. // 检查是否为高拍仪摄像头,若是则直接枚举下一个摄像头
  1404. bool isHspCam = false;
  1405. for (int k = 0; k < m; ++k) {
  1406. if (!strcmp((LPCTSTR)hspcams[k],t)) {
  1407. isHspCam = true;
  1408. break;
  1409. }
  1410. }
  1411. if (isHspCam) {
  1412. continue;
  1413. }
  1414. strcat(t, ";");
  1415. strcat(t, t1);
  1416. if(strcmp(t,(LPCTSTR)envcam) && strcmp(t,(LPCTSTR)optcam) && !strstr(t2,(LPCTSTR)ewscam))
  1417. {
  1418. if (0 == cnt)
  1419. {
  1420. outercam = t2;
  1421. }
  1422. ++cnt;
  1423. }
  1424. }
  1425. if(cnt != 1)
  1426. {
  1427. outercam = "$";
  1428. }
  1429. }
  1430. }
  1431. return n;
  1432. }
  1433. void capture_lib_term()
  1434. {
  1435. videoframework_term();
  1436. Pa_Terminate();
  1437. CoUninitialize();
  1438. }
  1439. bool capture_adj_brightness(capture_t *cap,int nvalue,ErrorCodeEnum nCode)
  1440. {
  1441. HRESULT rst = S_OK;
  1442. if (cap->env_video&&(nCode!=Error_EnvCamera)&&(nCode!=Error_AllCamera))
  1443. {
  1444. rst = videocap_adj_brightness(cap->env_video->cap,nvalue);
  1445. }
  1446. HRESULT rst2 = S_OK;
  1447. if (cap->opt_video->cap&&(nCode!=Error_OptCamera)&&(nCode!=Error_AllCamera))
  1448. {
  1449. rst2 = videocap_adj_brightness(cap->opt_video->cap,nvalue);
  1450. }
  1451. if (SUCCEEDED(rst)&&SUCCEEDED(rst2))
  1452. return true;
  1453. else
  1454. return false;
  1455. }
  1456. bool capture_set_autobrightness(capture_t *cap,ErrorCodeEnum nCode)
  1457. {
  1458. HRESULT rst = S_OK;
  1459. HRESULT rst2 = S_OK;
  1460. if (cap->env_video&&(nCode!=Error_EnvCamera)&&(nCode!=Error_AllCamera))
  1461. {
  1462. rst = videocap_set_autobrightness(cap->env_video->cap);
  1463. }
  1464. if (cap->opt_video&&(nCode!=Error_OptCamera)&&(nCode!=Error_AllCamera))
  1465. {
  1466. rst2 = videocap_set_autobrightness(cap->opt_video->cap);
  1467. }
  1468. if (SUCCEEDED(rst)&&SUCCEEDED(rst2))
  1469. return true;
  1470. else
  1471. return false;
  1472. }
  1473. int capture_get_brightness(capture_t *cap,ErrorCodeEnum nCode)
  1474. {
  1475. int nValue1=0;
  1476. int nValue2=0;
  1477. HRESULT rst = S_OK;
  1478. if (cap->env_video&&(nCode!=Error_EnvCamera)&&(nCode!=Error_AllCamera))
  1479. {
  1480. HRESULT rst = videocap_get_brightness(cap->env_video->cap,&nValue1);
  1481. }
  1482. HRESULT rst2 = S_OK;
  1483. if (cap->opt_video&&(nCode!=Error_OptCamera)&&(nCode!=Error_AllCamera))
  1484. {
  1485. rst2 = videocap_get_brightness(cap->opt_video->cap,&nValue2);
  1486. }
  1487. else
  1488. {
  1489. return -1;
  1490. }
  1491. if ((cap->opt_video == NULL)&&cap->env_video)
  1492. {
  1493. return nValue1;
  1494. }
  1495. else if ((cap->env_video == NULL)&&cap->opt_video)
  1496. {
  1497. return nValue2;
  1498. }
  1499. else
  1500. {
  1501. if (SUCCEEDED(rst)&&SUCCEEDED(rst2))
  1502. return (nValue1<=nValue2)?nValue1:nValue2;
  1503. else
  1504. return -1;
  1505. }
  1506. }
  1507. int StopCamera(capture_t *cap,int nCamera)
  1508. {
  1509. if((nCamera == ENVCAMERA)&&cap->env_video)
  1510. {
  1511. video_capture_stop(cap->env_video);
  1512. video_capture_destroy(cap->env_video);
  1513. cap->env_video = NULL;
  1514. return 0;
  1515. }
  1516. else if((nCamera == OPTCAMERA)&&cap->opt_video)
  1517. {
  1518. video_capture_stop(cap->opt_video);
  1519. video_capture_destroy(cap->opt_video);
  1520. cap->opt_video = NULL;
  1521. return 0;
  1522. }
  1523. else
  1524. {
  1525. return -1;
  1526. }
  1527. }
  1528. //根据frontcam完整名(包含设备名+逗号+设备路径MD5)、ewscam设备路径、hspcams设备名,找到rearcam的完整名
  1529. //排除以上设备后的唯一设备即为rearcam
  1530. int videocap_optdev_fetch( CSimpleStringA frontcam,CSimpleStringA ewscam,CAutoArray<CSimpleStringA> &hspcams,CSimpleStringA &rearcam )
  1531. {
  1532. int i, n;
  1533. rearcam = "$";
  1534. n = videocap_get_device_count();
  1535. int m = hspcams.GetCount(); // 高拍仪的个数 add by ly 2017/11/07
  1536. if (ewscam.GetLength() <= 1) // 如果外部广角摄像头未配置
  1537. {
  1538. if (n <= 2+m)
  1539. {
  1540. int cnt = 0;
  1541. for (i = 0; i < n; ++i) {
  1542. WCHAR tmp[256];
  1543. char t[256];
  1544. WCHAR tmp1[256];
  1545. char t1[256];
  1546. videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  1547. WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  1548. videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
  1549. WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
  1550. {
  1551. unsigned char x[MD5_DIGESTSIZE];
  1552. md5_ctx_t ctx;
  1553. md5_init(&ctx);
  1554. md5(x, t1, strlen(t1));
  1555. Bin2Str(x, sizeof(x), t1, sizeof(t1));
  1556. }
  1557. // 检查是否为高拍仪摄像头,若是则直接枚举下一个摄像头
  1558. bool isHspCam = false;
  1559. for (int k = 0; k < m; ++k) {
  1560. if (!strcmp((LPCTSTR)hspcams[k],t)) {
  1561. isHspCam = true;
  1562. break;
  1563. }
  1564. }
  1565. if (isHspCam) {
  1566. continue;
  1567. }
  1568. strcat(t, ";");
  1569. strcat(t, t1);
  1570. if (strcmp((LPCTSTR)frontcam, t))
  1571. {
  1572. if (0 == cnt)
  1573. {
  1574. rearcam = t;
  1575. }
  1576. cnt++;
  1577. }
  1578. }
  1579. if (1 != cnt) // 未找到或无法识别内置后摄像头
  1580. {
  1581. rearcam = "$";
  1582. }
  1583. }
  1584. else
  1585. {
  1586. Dbg("[dbg] [videocap_optdev_fetch] detect %d cameras.", n);
  1587. }
  1588. }
  1589. else // 如果外部广角摄像头已配置
  1590. {
  1591. if (n <= 3+m)
  1592. {
  1593. int cnt = 0;
  1594. for (i = 0; i < n; ++i) {
  1595. WCHAR tmp[256];
  1596. char t[256];
  1597. WCHAR tmp1[256];
  1598. char t1[256];
  1599. videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  1600. WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  1601. videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
  1602. WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
  1603. // 检查是否为高拍仪摄像头,若是则直接枚举下一个摄像头
  1604. bool isHspCam = false;
  1605. for (int k = 0; k < m; ++k) {
  1606. if (!strcmp((LPCTSTR)hspcams[k],t)) {
  1607. isHspCam = true;
  1608. break;
  1609. }
  1610. }
  1611. if (isHspCam) {
  1612. continue;
  1613. }
  1614. // save DevicePath (add by ly at 20160725)
  1615. char t2[256];
  1616. strcpy(t2,t1);
  1617. for (int j = 0; j < strlen(t2); ++j)
  1618. {
  1619. t2[j] = toupper(t2[j]);
  1620. if (t2[j] == '#') t2[j] = '\\';
  1621. }
  1622. {
  1623. unsigned char x[MD5_DIGESTSIZE];
  1624. md5_ctx_t ctx;
  1625. md5_init(&ctx);
  1626. md5(x, t1, strlen(t1));
  1627. Bin2Str(x, sizeof(x), t1, sizeof(t1));
  1628. }
  1629. strcat(t, ";");
  1630. strcat(t, t1);
  1631. if (strcmp((LPCTSTR)frontcam, t) && !strstr(t2,(LPCTSTR)ewscam))
  1632. {
  1633. if (0 == cnt)
  1634. {
  1635. rearcam = t;
  1636. }
  1637. cnt++;
  1638. }
  1639. }
  1640. if (1 != cnt) // 未找到或无法识别内置后摄像头
  1641. {
  1642. rearcam = "$";
  1643. }
  1644. }
  1645. else
  1646. {
  1647. Dbg("[dbg] [videocap_optdev_fetch] detect %d cameras.", n);
  1648. }
  1649. }
  1650. return n;
  1651. }
  1652. void capture_clearsnapshotvideo(capture_t *cap,int nCamera)
  1653. {
  1654. if((nCamera == ENVCAMERA)&&cap->env_video)
  1655. {
  1656. Dbg("clear ENV snapshot video!");
  1657. cap->env_video->snapshot_shm_queue->ClearVideoQueue();
  1658. return;
  1659. }
  1660. else if((nCamera == OPTCAMERA)&&cap->opt_video)
  1661. {
  1662. Dbg("clear OPT snapshot video!");
  1663. cap->opt_video->snapshot_shm_queue->ClearVideoQueue();
  1664. return;
  1665. }
  1666. else if(nCamera == AlLCAMERA)
  1667. {
  1668. if (cap->env_video)
  1669. {
  1670. Dbg("clear ENV snapshot video!");
  1671. cap->env_video->snapshot_shm_queue->ClearVideoQueue();
  1672. }
  1673. if (cap->opt_video)
  1674. {
  1675. Dbg("clear OPT snapshot video!");
  1676. cap->opt_video->snapshot_shm_queue->ClearVideoQueue();
  1677. }
  1678. return;
  1679. }
  1680. else
  1681. {
  1682. return ;
  1683. }
  1684. }
  1685. int get_camera_exception_message(char* pBuffer, size_t uLen, CSimpleStringA strDeviceName, const char* strErrorMessage)
  1686. {
  1687. int iRet = 0;
  1688. if (strDeviceName.GetLength() > 0){
  1689. const char* strCameraName = strDeviceName.GetData();
  1690. char strBuffer[MAX_PATH] = {0};
  1691. if (sprintf_s(strBuffer, MAX_PATH, "%s", strCameraName) > 0){
  1692. char *pIndex = NULL;
  1693. if (pIndex = (char*)strstr(strBuffer, ";")){
  1694. *pIndex = '\0';
  1695. }
  1696. }
  1697. if (NULL != strErrorMessage){
  1698. size_t uDataLen = strlen(strBuffer);
  1699. size_t uErrorLen = strlen(strErrorMessage);
  1700. if (uLen > uDataLen + uErrorLen + 10){
  1701. iRet = sprintf_s(pBuffer, uLen, "[%s] %s", strBuffer, strErrorMessage);
  1702. }
  1703. }
  1704. }
  1705. if (0 == iRet){
  1706. if (NULL != strErrorMessage){
  1707. iRet = sprintf_s(pBuffer, uLen, "%s", strErrorMessage);
  1708. }
  1709. }
  1710. return iRet;
  1711. }
  1712. }