capture.cpp 81 KB

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