capture.cpp 70 KB

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