capture.cpp 71 KB

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