capture.cpp 73 KB

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