capture.cpp 66 KB

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