capture.cpp 66 KB

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