capture.cpp 73 KB

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