capture.cpp 69 KB

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