capture.cpp 73 KB

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