capture.cpp 80 KB

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