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