capture.cpp 47 KB

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