capture.cpp 73 KB

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