capture.cpp 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628
  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. LogError(Severity_High, Error_DevMedia, 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. LogError(Severity_High, Error_DevMedia, 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. LogError(Severity_High, Error_DevMedia, 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. LogError(Severity_High, Error_DevMedia, 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. audio_cap->uaudiolen -= RVC_AUDIO_BUFFER_LEN;
  539. }
  540. if (ileft > 0) {
  541. memcpy(audio_cap->paudio_buffer + audio_cap->uaudiolen, input + index, ileft);
  542. audio_cap->uaudiolen += ileft;
  543. }
  544. }
  545. return;
  546. }
  547. static int record_pulseaudio_capture_start(rvc_audio_capture_t* audio_cap)
  548. {
  549. LOG_FUNCTION();
  550. int iret = -1;
  551. rvc_sales_audio_capture_t* cap = audio_cap->parent;
  552. if (NULL != audio_cap->paudiocap) {
  553. Dbg("%s:%d.", __FUNCTION__, __LINE__);
  554. audiocap_param_t param = { 0 };
  555. int nId = audio_cap->paudiocap->audio_get_device_id(cap->rvc_audio_config.strAudioIn.GetData(), true);
  556. if (nId == -1)
  557. {
  558. //需要立即处理的告警使用Severity_High
  559. LogError(Severity_High, Error_DevMedia, ERROR_MOD_MEDIACONTROLLER_HANDFREEIN_INITFAIL, "hand free in device config error,please check");
  560. return Error_AudioIN;
  561. }
  562. param.ideviceid = nId;
  563. param.ichannels = 1;
  564. param.isampleformat = 3 /*PA_SAMPLE_S16LE*/;
  565. param.isamprate = 8000;
  566. param.flatency = 0.02;
  567. param.on_audio_callback = &record_audio_data_callback;
  568. param.user_data = audio_cap;
  569. Dbg("%s:%d.", __FUNCTION__, __LINE__);
  570. audio_cap->paudiocap->set_audio_capture_params(&param);
  571. Dbg("%s:%d.", __FUNCTION__, __LINE__);
  572. iret = audio_cap->paudiocap->start_audio_capture();
  573. Dbg("%s:%d.", __FUNCTION__, __LINE__);
  574. }
  575. return iret;
  576. }
  577. static void record_pulseaudio_capture_stop(rvc_audio_capture_t* audio_cap)
  578. {
  579. if (audio_cap->paudiocap) {
  580. audio_cap->paudiocap->stop_audio_capture();
  581. }
  582. }
  583. #endif
  584. static rvc_audio_capture_t *salesrecord_audio_capture_create(rvc_sales_audio_capture_t *cap)
  585. {
  586. rvc_audio_capture_t *audio_cap = ZALLOC_T(rvc_audio_capture_t);
  587. if (audio_cap) {
  588. audio_cap->parent = cap;
  589. audio_cap->audio_shm_queue = new Clibaudioqueue(REC_COMMON_REMOTEAUDIO_SHM_QUEUE);
  590. audio_cap->iseriesnumber = 0;
  591. audio_cap->eType = eUnKnown;
  592. audio_cap->pdata = NULL;
  593. }
  594. #ifdef RVC_OS_LINUX
  595. audio_cap->paudio_buffer = new char[RVC_AUDIO_BUFFER_LEN];
  596. audio_cap->uaudiolen = 0;
  597. #endif
  598. return audio_cap;
  599. }
  600. static void salesrecord_audio_capture_destroy(rvc_audio_capture_t *audio_cap)
  601. {
  602. if (NULL != audio_cap){
  603. if (NULL != audio_cap->audio_shm_queue){
  604. delete audio_cap->audio_shm_queue;
  605. audio_cap->audio_shm_queue = NULL;
  606. Dbg("set audio_cap audio_shm_queue null");
  607. }
  608. free(audio_cap);
  609. #ifdef RVC_OS_LINUX
  610. delete audio_cap->paudio_buffer;
  611. audio_cap->paudio_buffer = NULL;
  612. audio_cap->uaudiolen = 0;
  613. #endif
  614. }
  615. }
  616. static audio_capture_t *audio_capture_create(capture_t *cap)
  617. {
  618. audio_capture_t *audio_cap = ZALLOC_T(audio_capture_t);
  619. if (audio_cap) {
  620. audio_cap->parent = cap;
  621. audio_cap->shm_queue = new Clibaudioqueue(REC_COMMON_AUDIO_SHM_QUEUE);
  622. audio_cap->salesol_shm_queue = new Clibaudioqueue(REC_COMMON_AUDIO_SALESOL_SHM_QUEUE);
  623. #ifdef RVC_OS_LINUX
  624. audio_cap->paudio_buffer = new char[RVC_AUDIO_BUFFER_LEN];
  625. audio_cap->uaudiolen = 0;
  626. #endif
  627. }
  628. return audio_cap;
  629. }
  630. static void audio_capture_destroy(audio_capture_t *audio_cap)
  631. {
  632. delete audio_cap->shm_queue;
  633. delete audio_cap->salesol_shm_queue;
  634. free(audio_cap);
  635. #ifdef RVC_OS_LINUX
  636. delete audio_cap->paudio_buffer;
  637. audio_cap->paudio_buffer = NULL;
  638. audio_cap->uaudiolen = 0;
  639. #endif
  640. }
  641. static int audio_capture_start(audio_capture_t* audio_cap)
  642. {
  643. #ifdef RVC_OS_WIN
  644. return portaudio_capture_start(audio_cap);
  645. #else
  646. return pulseaudio_capture_start(audio_cap);
  647. #endif
  648. }
  649. static void audio_capture_stop(audio_capture_t* audio_cap)
  650. {
  651. #ifdef RVC_OS_WIN
  652. return portaudio_capture_stop(audio_cap);
  653. #else
  654. return pulseaudio_capture_stop(audio_cap);
  655. #endif
  656. }
  657. static int record_audio_capture_start(rvc_audio_capture_t* audio_cap)
  658. {
  659. #ifdef RVC_OS_WIN
  660. return record_portaudio_capture_start(audio_cap);
  661. #else
  662. return record_pulseaudio_capture_start(audio_cap);
  663. #endif
  664. }
  665. static void record_audio_capture_stop(rvc_audio_capture_t* audio_cap)
  666. {
  667. #ifdef RVC_OS_WIN
  668. return record_portaudio_capture_stop(audio_cap);
  669. #else
  670. return record_pulseaudio_capture_stop(audio_cap);
  671. #endif
  672. }
  673. static int calc_capture_mode(int width, int height, int *mode)
  674. {
  675. const struct {
  676. int mode;
  677. int width;
  678. int height;
  679. } modes [] = {
  680. {VIDEOCAP_FRAME_SQCIF, VIDEOCAP_SQCIF_WIDTH, VIDEOCAP_SQCIF_HEIGHT},
  681. {VIDEOCAP_FRAME_QQVGA, VIDEOCAP_QQVGA_WIDTH, VIDEOCAP_QQVGA_HEIGHT},
  682. {VIDEOCAP_FRAME_QCIF, VIDEOCAP_QCIF_WIDTH, VIDEOCAP_QCIF_HEIGHT},
  683. {VIDEOCAP_FRAME_QVGA, VIDEOCAP_QVGA_WIDTH, VIDEOCAP_QVGA_HEIGHT},
  684. {VIDEOCAP_FRAME_CIF, VIDEOCAP_CIF_WIDTH, VIDEOCAP_CIF_HEIGHT},
  685. {VIDEOCAP_FRAME_VGA, VIDEOCAP_VGA_WIDTH, VIDEOCAP_VGA_HEIGHT},
  686. {VIDEOCAP_FRAME_4CIF, VIDEOCAP_4CIF_WIDTH, VIDEOCAP_4CIF_HEIGHT},
  687. {VIDEOCAP_FRAME_SVGA, VIDEOCAP_SVGA_WIDTH, VIDEOCAP_SVGA_HEIGHT},
  688. {VIDEOCAP_FRAME_NHD, VIDEOCAP_NHD_WIDTH, VIDEOCAP_NHD_HEIGHT},
  689. {VIDEOCAP_FRAME_SXGA, VIDEOCAP_SXGA_WIDTH, VIDEOCAP_SXGA_HEIGHT},
  690. {VIDEOCAP_FRAME_720P, VIDEOCAP_720P_WIDTH, VIDEOCAP_720P_HEIGHT},
  691. {VIDEOCAP_FRAME_1080P, VIDEOCAP_1080P_WIDTH, VIDEOCAP_1080P_HEIGHT},
  692. };
  693. int i;
  694. for (i = 0; i < array_size(modes); ++i) {
  695. if (modes[i].width == width && modes[i].height == height) {
  696. *mode = modes[i].mode;
  697. return 0;
  698. }
  699. }
  700. return Error_NotExist;
  701. }
  702. static int video_shm_enqueue(Clibvideoqueue *shm_queue, video_frame *frame, int flags)
  703. {
  704. videoq_frame tmp_frm;
  705. tmp_frm.data = frame->data[0];
  706. if (VIDEO_FORMAT_RGB24 == frame->format){
  707. tmp_frm.framesize = frame->width * frame->height * 3;
  708. tmp_frm.format = VIDEOQ_FORMAT_RGB24;
  709. }
  710. else {
  711. tmp_frm.framesize = frame->width * frame->height * 3/2;
  712. tmp_frm.format = VIDEOQ_FORMAT_I420;
  713. }
  714. tmp_frm.width = frame->width;
  715. tmp_frm.height = frame->height;
  716. unsigned int nowtime = y2k_time_now();
  717. if (!shm_queue->InsertVideo(&tmp_frm, flags,nowtime)) {
  718. Dbg("caution: insert shm video failed!");
  719. return Error_Unexpect;
  720. } else {
  721. //Dbg("insert shm video ok!");
  722. return Error_Succeed;
  723. }
  724. }
  725. //static int env_cap_on_frame_i420(void* user_data, video_frame* frame)
  726. //{
  727. // video_capture_t* video_cap = (video_capture_t*)user_data;
  728. // capture_t* cap = video_cap->parent;
  729. // return video_shm_enqueue(video_cap->render_shm_queue, frame, VIDEOQUEUE_FLAG_VERTICAL_FLIP);
  730. //}
  731. //
  732. //
  733. //static int opt_cap_on_frame_i420(void* user_data, video_frame* frame)
  734. //{
  735. // video_capture_t* video_cap = (video_capture_t*)user_data;
  736. // capture_t* cap = video_cap->parent;
  737. // return video_shm_enqueue(video_cap->opt_render_shm_queue, frame, VIDEOQUEUE_FLAG_VERTICAL_FLIP);
  738. //}
  739. static void env_cap_on_frame(void *user_data, video_frame *frame)
  740. {
  741. video_capture_t *video_cap = (video_capture_t *)user_data;
  742. capture_t *cap = video_cap->parent;
  743. int rc;
  744. video_cap->frame_id++;
  745. //Dbg("start env on frame, id=%d, tick=%d", video_cap->frame_id, GetTickCount());;
  746. //IplImage* img = NULL;
  747. //img = cvCreateImage(cvSize(frame->width, frame->height), IPL_DEPTH_8U, 3);
  748. //img->imageData = (char*)frame->data[0];
  749. //cvSaveImage("./env.jpg", img, 0);
  750. //cvReleaseImageHeader(&img);
  751. rc = video_shm_enqueue(video_cap->snapshot_shm_queue, frame, VIDEOQUEUE_FLAG_VERTICAL_FLIP);
  752. //static int isnapshot_shm_queue = 0;
  753. //if (isnapshot_shm_queue == 0) {
  754. // video_frame_save_bmpfile("snapshot_shm_queue_1.bmp", frame);
  755. // isnapshot_shm_queue++;
  756. //}
  757. if (rc != Error_Succeed)
  758. {
  759. Dbg("env snapshot queue enqueue failed! Error = %d, camera_type=%d", rc, video_cap->camera_type);
  760. }
  761. //else {
  762. // Dbg("env snapshot queue enqueue success! camera_type=%d", video_cap->camera_type);
  763. //}
  764. // snapshot
  765. if (rc==Error_Succeed)
  766. {
  767. if (*cap->config.ref_env_capture_count)
  768. {
  769. Dbg("env camera ref_env_capture_count=%d", *cap->config.ref_env_capture_count);
  770. #ifdef RVC_OS_WIN
  771. InterlockedDecrement(cap->config.ref_env_capture_count);
  772. #else
  773. //__sync_fetch_and_sub(cap->config.ref_env_capture_count, 1);
  774. pthread_mutex_lock(cap->config.env_mutex);
  775. *(cap->config.ref_env_capture_count) -= 1;
  776. pthread_mutex_unlock(cap->config.env_mutex);
  777. #endif
  778. LogEvent(Severity_Middle, MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_ENV, "agent capture env ok, and capture env finished!");
  779. }
  780. else if (*cap->config.ref_envopt_capture_count & 2)
  781. {
  782. Dbg("env camera ref_envopt_capture_count=%d", *cap->config.ref_envopt_capture_count);
  783. #ifdef RVC_OS_WIN
  784. _InterlockedAnd(cap->config.ref_envopt_capture_count, 0xfffffffD);
  785. #else
  786. //__sync_fetch_and_add(cap->config.ref_env_capture_count, 0xfffffffD);
  787. pthread_mutex_lock(cap->config.envopt_mutex);
  788. LONG lcount = *(cap->config.ref_envopt_capture_count);
  789. *(cap->config.ref_envopt_capture_count) = (lcount & 0xfffffffD);
  790. pthread_mutex_unlock(cap->config.envopt_mutex);
  791. #endif // RVC_OS_WIN
  792. if (*cap->config.ref_envopt_capture_count == 0)
  793. {
  794. LogEvent(Severity_Middle, MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_ENVOPT, "agent capture env ok, and capture env opt finished!");
  795. }
  796. }
  797. }
  798. // preview
  799. {
  800. video_frame preview_frame;
  801. video_frame_alloc(REC_COMMON_VIDEO_PREVIEW_WIDTH, REC_COMMON_VIDEO_PREVIEW_HEIGHT, VIDEO_FORMAT_RGB24, &preview_frame);
  802. uint8_t *src_data[4] = {frame->data[0] + 80*3, 0, 0, 0};
  803. sws_scale(video_cap->preview_sws_ctx, src_data, frame->linesize, 0, frame->height, preview_frame.data, preview_frame.linesize);
  804. rc= video_shm_enqueue(video_cap->preview_shm_queue, &preview_frame, 0);
  805. if (rc != Error_Succeed){
  806. Dbg("preview_shm_queue enqueue failed, error = %d", rc);
  807. }
  808. else {
  809. //if (0 == video_cap->frame_id%10){
  810. // Dbg("preview_shm_queue enqueue[%d] success, and current video queue len is %d.", video_cap->frame_id, video_cap->preview_shm_queue->GetVideoLens());
  811. //}
  812. }
  813. video_frame_free(&preview_frame);
  814. //static int ipreview_shm_queue = 0;
  815. //if (ipreview_shm_queue == 0) {
  816. // video_frame_save_bmpfile("preview_shm_queue_1.bmp", frame);
  817. // ipreview_shm_queue++;
  818. //}
  819. }
  820. // rtp
  821. {
  822. video_frame rtp_frame;
  823. video_frame_alloc(REC_COMMON_VIDEO_RTP_ENV_WIDTH, REC_COMMON_VIDEO_RTP_ENV_HEIGHT, VIDEO_FORMAT_RGB24, &rtp_frame);
  824. uint8_t *src_data[4] = {frame->data[0] + (frame->height-1) * frame->linesize[0], 0, 0, 0};
  825. int src_linesize[4] = {-frame->linesize[0], 0, 0, 0};
  826. sws_scale(video_cap->rtp_sws_ctx, src_data, src_linesize, 0, frame->height, rtp_frame.data, rtp_frame.linesize);
  827. rc = video_shm_enqueue(video_cap->rtp_shm_queue, &rtp_frame, 0);
  828. if (rc != Error_Succeed) {
  829. Dbg("rtp_shm_queue enqueue failed! Error = %d", rc);
  830. }
  831. //else {
  832. // Dbg("rtp_shm_queue enqueue success!");
  833. //}
  834. //static int irtp_frame = 0;
  835. //if (irtp_frame == 0){
  836. // video_frame_save_bmpfile("rtp_shm_queue.bmp", &rtp_frame);
  837. // irtp_frame++;
  838. //}
  839. #if 0
  840. static int i = 0;
  841. if (i == 0) {
  842. video_frame tmp_frame;
  843. video_frame_alloc(REC_COMMON_VIDEO_RTP_ENV_WIDTH, REC_COMMON_VIDEO_RTP_ENV_HEIGHT, VIDEO_FORMAT_RGB24, &tmp_frame);
  844. video_frame_fill_black(&tmp_frame);
  845. videoq_frame frm;
  846. frm.data = tmp_frame.data[0];
  847. video_cap->rtp_shm_queue->GetVideo(&frm, 0);
  848. video_frame_save_bmpfile("rtp_videoqueue_abc.bmp", &tmp_frame);
  849. video_frame_free(&tmp_frame);
  850. //video_frame_save_bmpfile("d:\\ab.bmp", &rtp_frame);
  851. i++;
  852. }
  853. #endif
  854. video_frame_free(&rtp_frame);
  855. }
  856. //Dbg("end env on frame, id=%d, tick=%d", video_cap->frame_id, GetTickCount());;
  857. }
  858. static void opt_cap_on_frame(void *user_data, video_frame *frame)
  859. {
  860. video_capture_t *video_cap = (video_capture_t *)user_data;
  861. capture_t *cap = video_cap->parent;
  862. int rc;
  863. int rotate = 0;
  864. //Dbg("opt on frame!");
  865. if (cap->config.video_opt_rotate == 90) {
  866. rotate = 1;
  867. } else if (cap->config.video_opt_rotate == 270) {
  868. rotate = -1;
  869. } else {
  870. Dbg("video_opt_rotate is not 90 or 270 return.");
  871. return;
  872. }
  873. video_cap->frame_id++;
  874. #ifdef RVC_OS_WIN // use IPP, it's fast
  875. // prepare for rotation
  876. video_frame rframe;
  877. video_frame_alloc(frame->height, frame->width, frame->format, &rframe);
  878. {
  879. IppiSize srcSize;
  880. srcSize.width = frame->width;
  881. srcSize.height = frame->height;
  882. IppiRect srcROI;
  883. srcROI.width = frame->width;
  884. srcROI.height = frame->height;
  885. srcROI.x = 0;
  886. srcROI.y = 0;
  887. IppiRect dstROI;
  888. dstROI.width = frame->height;
  889. dstROI.height = frame->width;
  890. dstROI.x = 0;
  891. dstROI.y = 0;
  892. ippiTranspose_8u_C3R(frame->data[0], frame->linesize[0], rframe.data[0], rframe.linesize[0], srcSize);
  893. IppiAxis flip;
  894. if (rotate == 1)
  895. {
  896. flip = ippAxsVertical;
  897. ippiMirror_8u_C3IR(rframe.data[0], rframe.linesize[0], srcSize, flip);
  898. }
  899. else
  900. {
  901. IppiSize Size;
  902. Size.width = rframe.width;
  903. Size.height = rframe.height;
  904. //flip = ippAxsHorizontal;
  905. flip = ippAxsBoth;
  906. ippiMirror_8u_C3IR(rframe.data[0], rframe.linesize[0], Size, flip);
  907. }
  908. }
  909. rc = video_shm_enqueue(video_cap->snapshot_shm_queue, &rframe, 0);
  910. #else
  911. rc = video_shm_enqueue(video_cap->snapshot_shm_queue, frame, 0);
  912. #endif
  913. if (rc != Error_Succeed)
  914. {
  915. Dbg("opt snapshot queue enqueue shm failed! Error = %d, camera_type=%d", rc, video_cap->camera_type);
  916. }
  917. else {
  918. //Dbg("opt snapshot queue enqueue[%d] success, and current video queue len is %d.", video_cap->frame_id, video_cap->snapshot_shm_queue->GetVideoLens());
  919. }
  920. //static int isnapshot_shm_queue = 0;
  921. //if (isnapshot_shm_queue == 0) {
  922. // video_frame_save_bmpfile("opt_snapshot_shm_queue.bmp", &rframe);
  923. // isnapshot_shm_queue++;
  924. //}
  925. // snapshot
  926. if (rc==Error_Succeed)
  927. {
  928. if (*cap->config.ref_opt_capture_count)
  929. {
  930. Dbg("opt camera ref_opt_capture_count=%d",*cap->config.ref_opt_capture_count);
  931. #ifdef RVC_OS_WIN
  932. InterlockedDecrement(cap->config.ref_opt_capture_count);
  933. #else
  934. //__sync_fetch_and_sub(cap->config.ref_opt_capture_count, 1);
  935. pthread_mutex_lock(cap->config.opt_mutex);
  936. *(cap->config.ref_opt_capture_count) -= 1;
  937. pthread_mutex_unlock(cap->config.opt_mutex);
  938. #endif
  939. LogEvent(Severity_Middle, MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_OPT, "agent capture opt ok, and capture opt finished!");
  940. }
  941. else if (*cap->config.ref_envopt_capture_count&1)
  942. {
  943. Dbg("opt camera ref_envopt_capture_count=%d", *cap->config.ref_envopt_capture_count);
  944. #ifdef RVC_OS_WIN
  945. if (InterlockedDecrement(cap->config.ref_envopt_capture_count) == 0)
  946. #else
  947. //__sync_fetch_and_sub(cap->config.ref_envopt_capture_count,1);
  948. pthread_mutex_lock(cap->config.envopt_mutex);
  949. *(cap->config.ref_envopt_capture_count) -= 1;
  950. pthread_mutex_unlock(cap->config.envopt_mutex);
  951. if (0 == *cap->config.ref_envopt_capture_count)
  952. #endif
  953. {
  954. LogEvent(Severity_Middle, MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_ENVOPT, "agent capture opt ok, and capture envopt finished!");
  955. }
  956. }
  957. }
  958. // rtp
  959. {
  960. video_frame rtp_frame = {0};
  961. video_frame_alloc(REC_COMMON_VIDEO_RTP_OPT_WIDTH, REC_COMMON_VIDEO_RTP_OPT_HEIGHT, VIDEO_FORMAT_RGB24, &rtp_frame);
  962. #ifdef RVC_OS_WIN
  963. sws_scale(video_cap->rtp_sws_ctx, rframe.data, rframe.linesize, 0, rframe.height, rtp_frame.data, rtp_frame.linesize);
  964. #else
  965. sws_scale(video_cap->rtp_sws_ctx, frame->data, frame->linesize, 0, frame->height, rtp_frame.data, rtp_frame.linesize);
  966. #endif
  967. rc = video_shm_enqueue(video_cap->rtp_shm_queue, &rtp_frame, 0);
  968. if (rc != Error_Succeed)
  969. {
  970. Dbg("rtp_shm_queue enqueue shm failed! Error = %d, camera_type=%d", rc, video_cap->camera_type);
  971. }
  972. else {
  973. //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());
  974. }
  975. //static int irtp_frame = 0;
  976. //if (irtp_frame == 0){
  977. // video_frame_save_bmpfile("opt_rtp_shm_queue.bmp", &rtp_frame);
  978. // irtp_frame++;
  979. //}
  980. video_frame_free(&rtp_frame);
  981. }
  982. #ifdef RVC_OS_WIN
  983. video_frame_free(&rframe);
  984. #endif
  985. }
  986. static video_capture_t *video_capture_create(capture_t *cap, int camera_type)
  987. {
  988. video_capture_t *video_cap = ZALLOC_T(video_capture_t);
  989. if (video_cap) {
  990. video_cap->parent = cap;
  991. video_cap->camera_type = camera_type;
  992. video_cap->frame_id = 0;
  993. if (camera_type == CAMERA_TYPE_ENV) {
  994. //video_cap->render_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_RENDER_QUEUE);
  995. //video_cap->opt_render_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_RENDER_QUEUE);
  996. video_cap->snapshot_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_SNAPSHOT_QUEUE);
  997. video_cap->rtp_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_RTP_QUEUE);
  998. video_cap->rtp_sws_ctx = sws_getContext(REC_COMMON_VIDEO_SNAPSHOT_WIDTH,
  999. REC_COMMON_VIDEO_SNAPSHOT_HEIGHT,
  1000. AV_PIX_FMT_BGR24,
  1001. REC_COMMON_VIDEO_RTP_ENV_WIDTH,
  1002. REC_COMMON_VIDEO_RTP_ENV_HEIGHT,
  1003. AV_PIX_FMT_BGR24,
  1004. SWS_POINT, NULL, NULL, NULL);
  1005. video_cap->preview_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_PREVIEW_QUEUE);
  1006. video_cap->preview_sws_ctx = sws_getContext(REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_WIDTH,
  1007. REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_HEIGHT,
  1008. AV_PIX_FMT_BGR24,
  1009. REC_COMMON_VIDEO_PREVIEW_WIDTH,
  1010. REC_COMMON_VIDEO_PREVIEW_HEIGHT,
  1011. AV_PIX_FMT_BGR24,
  1012. SWS_FAST_BILINEAR, NULL, NULL, NULL);
  1013. } else {
  1014. //video_cap->render_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_RENDER_QUEUE);
  1015. //video_cap->opt_render_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_RENDER_QUEUE);
  1016. video_cap->snapshot_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_SNAPSHOT_QUEUE);
  1017. video_cap->rtp_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_RTP_QUEUE);
  1018. video_cap->rtp_sws_ctx = sws_getContext(REC_COMMON_VIDEO_SNAPSHOT_HEIGHT,
  1019. REC_COMMON_VIDEO_SNAPSHOT_WIDTH,
  1020. AV_PIX_FMT_BGR24,
  1021. REC_COMMON_VIDEO_RTP_OPT_WIDTH,
  1022. REC_COMMON_VIDEO_RTP_OPT_HEIGHT,
  1023. AV_PIX_FMT_BGR24,
  1024. SWS_POINT, NULL, NULL, NULL);
  1025. //video_cap->preview_shm_queue = NULL;
  1026. //video_cap->preview_sws_ctx = NULL;
  1027. }
  1028. }
  1029. return video_cap;
  1030. }
  1031. static void video_capture_destroy(video_capture_t *video_cap)
  1032. {
  1033. if (video_cap) {
  1034. if (video_cap->preview_sws_ctx) {
  1035. sws_freeContext(video_cap->preview_sws_ctx);
  1036. video_cap->preview_sws_ctx = NULL;
  1037. }
  1038. if (video_cap->rtp_sws_ctx) {
  1039. sws_freeContext(video_cap->rtp_sws_ctx);
  1040. video_cap->rtp_sws_ctx = NULL;
  1041. }
  1042. if (video_cap->snapshot_shm_queue) {
  1043. delete video_cap->snapshot_shm_queue;
  1044. video_cap->snapshot_shm_queue = NULL;
  1045. }
  1046. if (video_cap->rtp_shm_queue) {
  1047. delete video_cap->rtp_shm_queue;
  1048. video_cap->rtp_shm_queue = NULL;
  1049. }
  1050. if (video_cap->preview_shm_queue) {
  1051. delete video_cap->preview_shm_queue;
  1052. video_cap->preview_shm_queue = NULL;
  1053. }
  1054. //if (video_cap->render_shm_queue) {
  1055. // delete video_cap->render_shm_queue;
  1056. // video_cap->render_shm_queue = NULL;
  1057. //}
  1058. //if (video_cap->opt_render_shm_queue) {
  1059. // delete video_cap->opt_render_shm_queue;
  1060. // video_cap->opt_render_shm_queue = NULL;
  1061. //}
  1062. free(video_cap);
  1063. }
  1064. }
  1065. static int get_video_capture_fps(int icapfps)
  1066. {
  1067. int ifps = REC_COMMON_VIDEO_PADRAW_FPS;
  1068. if ((ePadtype == g_eDeviceType) || (eMobilePadType == g_eDeviceType) || (eDesk2SType == g_eDeviceType)
  1069. || (eDesk1SType == g_eDeviceType) || (eDesk2SIntegratedType == g_eDeviceType)){
  1070. ifps = REC_COMMON_VIDEO_PADRAW_FPS;
  1071. }
  1072. else{
  1073. ifps = REC_COMMON_VIDEO_RAW_FPS;
  1074. }
  1075. if (icapfps <=30 && icapfps >=5){
  1076. ifps = icapfps;
  1077. }
  1078. return ifps;
  1079. }
  1080. #ifdef RVC_OS_WIN
  1081. static int video_capture_start_win(video_capture_t* video_cap)
  1082. {
  1083. capture_config_t* conf = &video_cap->parent->config;
  1084. int dev_id;
  1085. if (video_cap->camera_type == CAMERA_TYPE_ENV)
  1086. {
  1087. dev_id = capture_get_video_device_id(conf->strVideoEnv);
  1088. if (dev_id == -1)
  1089. {
  1090. Dbg("No environment camera,please check config file or device!");
  1091. return -1;
  1092. }
  1093. }
  1094. else
  1095. {
  1096. dev_id = capture_get_video_device_id(conf->strVideoOpt);
  1097. if (dev_id == -1)
  1098. {
  1099. Dbg("No operation camera,please check config file or device!");
  1100. return -1;
  1101. }
  1102. }
  1103. videocap_param param = { 0 };
  1104. int cap_mode;
  1105. int rc = -1;
  1106. rc = calc_capture_mode(REC_COMMON_VIDEO_SNAPSHOT_WIDTH, REC_COMMON_VIDEO_SNAPSHOT_HEIGHT, &cap_mode);
  1107. if (rc != 0)
  1108. {
  1109. Dbg("calc cap_mode failed!");
  1110. return rc;
  1111. }
  1112. param.cap_mode = cap_mode;
  1113. param.dev_id = dev_id;
  1114. param.frame_fmt = VIDEO_FORMAT_RGB24;
  1115. if ((ePadtype == g_eDeviceType) || (eMobilePadType == g_eDeviceType) || (eDesk2SType == g_eDeviceType) || (eDesk1SType == g_eDeviceType) || (eDesk2SIntegratedType == g_eDeviceType))
  1116. {
  1117. param.fps = REC_COMMON_VIDEO_PADRAW_FPS;
  1118. }
  1119. else
  1120. {
  1121. param.fps = REC_COMMON_VIDEO_RAW_FPS;
  1122. }
  1123. param.on_frame = (video_cap->camera_type == CAMERA_TYPE_ENV ? &env_cap_on_frame : &opt_cap_on_frame);
  1124. param.user_data = video_cap;
  1125. param.option = 0;
  1126. rc = videocap_create(&video_cap->cap, &param);
  1127. if (rc != 0)
  1128. {
  1129. Dbg("videocap create failed!");
  1130. return rc;
  1131. }
  1132. rc = videocap_start(video_cap->cap);
  1133. if (rc != 0)
  1134. {
  1135. Dbg("videocap start failed!");
  1136. videocap_destroy(video_cap->cap);
  1137. video_cap->cap = NULL;
  1138. return rc;
  1139. }
  1140. else {
  1141. Dbg("videocap start success!");
  1142. }
  1143. return 0;
  1144. }
  1145. #else
  1146. static void __videocaplog(void* user_data, const char* fmt, va_list arg)
  1147. {
  1148. vDbg(fmt, arg);
  1149. }
  1150. static void __logevent(int itype, const char* strmessage)
  1151. {
  1152. if (0 == itype) {
  1153. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_MEDIACONTROLLER_CAMERA_MATCHED_FORMAT, strmessage);
  1154. }
  1155. else if (1 == itype) {
  1156. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_MEDIACONTROLLER_CAMERA_OUTPUT_FORMAT, strmessage);
  1157. }
  1158. else {
  1159. Dbg("unknown type event.");
  1160. }
  1161. }
  1162. static int video_capture_start_linux(video_capture_t* video_cap)
  1163. {
  1164. LOG_FUNCTION();
  1165. int iret = -1;
  1166. capture_config_t* conf = &video_cap->parent->config;
  1167. int dev_id = -1;
  1168. if (video_cap->camera_type == CAMERA_TYPE_ENV){
  1169. dev_id = rvc_videocap_get_video_device_id(conf->strVideoEnv.GetData());
  1170. if (-1 == dev_id){
  1171. Dbg("No environment camera[%s], please check config file or device!", conf->strVideoEnv.GetData());
  1172. return iret;
  1173. }
  1174. }
  1175. else{
  1176. dev_id = rvc_videocap_get_video_device_id(conf->strVideoOpt.GetData());
  1177. if (-1 == dev_id){
  1178. Dbg("No operation camera[%s], please check config file or device!", conf->strVideoOpt.GetData());
  1179. return iret;
  1180. }
  1181. }
  1182. Dbg("video dev_id is %d", dev_id);
  1183. videocap_param_t t_param = { 0 };
  1184. int cap_mode;
  1185. int rc = calc_capture_mode(REC_COMMON_VIDEO_SNAPSHOT_WIDTH, REC_COMMON_VIDEO_SNAPSHOT_HEIGHT, &cap_mode);
  1186. if (rc != 0){
  1187. Dbg("calc cap_mode failed!");
  1188. return iret;
  1189. }
  1190. else{
  1191. Dbg("cap_mode == %d.",cap_mode);
  1192. }
  1193. t_param.cap_mode = cap_mode;
  1194. t_param.dev_id = dev_id;
  1195. t_param.frame_fmt = VIDEO_FORMAT_RGB24;
  1196. t_param.fps = get_video_capture_fps(video_cap->camera_type == CAMERA_TYPE_ENV ? conf->video_env_fps : conf->video_opt_fps);
  1197. t_param.irotate = (video_cap->camera_type == CAMERA_TYPE_ENV ? conf->video_env_rotate : conf->video_opt_rotate);
  1198. t_param.on_frame = (video_cap->camera_type == CAMERA_TYPE_ENV ? &env_cap_on_frame : &opt_cap_on_frame);
  1199. //t_param.on_frame_i420 = (video_cap->camera_type == CAMERA_TYPE_ENV ? &env_cap_on_frame_i420 : &opt_cap_on_frame_i420);
  1200. t_param.user_data = video_cap;
  1201. t_param.option = 0;
  1202. videocap_callback_t t_callback = { 0 };
  1203. t_callback.debug = &__videocaplog;
  1204. t_callback.logevent = &__logevent;
  1205. video_cap->pVideoCap = CreateVideoCaptureObj(&t_callback);
  1206. if (NULL != video_cap->pVideoCap){
  1207. if (0 == video_cap->pVideoCap->VideoCaptureSetParam(&t_param)) {
  1208. Dbg("VideoCap SetVideoCaptureParam success!");
  1209. if (0 == video_cap->pVideoCap->StartVideoCapture()) {
  1210. Dbg("VideoCap StartVideoCapture success!");
  1211. iret = 0;
  1212. }
  1213. else {
  1214. Dbg("VideoCap StartVideoCapture failed!");
  1215. DestroyVideoCaptureObj(video_cap->pVideoCap);
  1216. }
  1217. }
  1218. else {
  1219. Dbg("VideoCap SetVideoCaptureParam failed!");
  1220. DestroyVideoCaptureObj(video_cap->pVideoCap);
  1221. }
  1222. }
  1223. else{
  1224. Dbg(" CreateVideoCaptureObj failed!");
  1225. }
  1226. return iret;
  1227. }
  1228. #endif // RVC_OS_WIN
  1229. static int video_capture_start(video_capture_t *video_cap)
  1230. {
  1231. #ifdef RVC_OS_WIN
  1232. return video_capture_start_win(video_cap);
  1233. #else
  1234. return video_capture_start_linux(video_cap);
  1235. #endif
  1236. }
  1237. static void video_capture_stop(video_capture_t *video_cap)
  1238. {
  1239. #ifdef RVC_OS_WIN
  1240. if (video_cap->cap) {
  1241. videocap_stop(video_cap->cap);
  1242. videocap_destroy(video_cap->cap);
  1243. video_cap->cap = NULL;
  1244. }
  1245. #else
  1246. if (video_cap->pVideoCap) {
  1247. video_cap->pVideoCap->StopVideoCapture();
  1248. DestroyVideoCaptureObj(video_cap->pVideoCap);
  1249. video_cap->pVideoCap = NULL;
  1250. }
  1251. #endif // RVC_OS_WIN
  1252. }
  1253. namespace MediaController {
  1254. DeviceTypeEnum g_eDeviceType;
  1255. int capture_create(const capture_config_t *config, capture_t **p_cap)
  1256. {
  1257. capture_t *cap = ZALLOC_T(capture_t);
  1258. cap->audio = NULL;
  1259. cap->env_video = NULL;
  1260. cap->opt_video = NULL;
  1261. memcpy(&cap->config, config, sizeof(capture_config_t));
  1262. cap->audio = audio_capture_create(cap);
  1263. if (!cap->audio) {
  1264. Dbg("create audio capture object failed!");
  1265. return Error_Unexpect;
  1266. }
  1267. int dev_id_env = capture_get_video_device_id(config->strVideoEnv);
  1268. if (dev_id_env != -1) {
  1269. cap->env_video = video_capture_create(cap, CAMERA_TYPE_ENV);
  1270. if (!cap->env_video) {
  1271. Dbg("create env video object failed!");
  1272. return Error_Unexpect;
  1273. }
  1274. }
  1275. int dev_id_opt = -1;
  1276. if (eStand2sType == g_eDeviceType){
  1277. dev_id_opt = capture_get_video_device_id(config->strVideoOpt);
  1278. }
  1279. if (dev_id_opt != -1){
  1280. cap->opt_video = video_capture_create(cap, CAMERA_TYPE_OPT);
  1281. if (!cap->opt_video) {
  1282. Dbg("create opt video object failed!");
  1283. return Error_Unexpect;
  1284. }
  1285. }
  1286. if((dev_id_env == -1)&&(dev_id_opt == -1)){
  1287. Dbg("all camera device id error!");
  1288. capture_destroy(cap);
  1289. return Error_Unexpect;
  1290. }
  1291. else{
  1292. *p_cap = cap;
  1293. return 0;
  1294. }
  1295. }
  1296. ErrorCodeEnum capture_create(const capture_config_t *config,capture_t *cap,int nCamera)
  1297. {
  1298. int dev_id1 = capture_get_video_device_id(config->strVideoEnv);
  1299. int dev_id2 = capture_get_video_device_id(config->strVideoOpt);
  1300. if((dev_id1 != -1)&&(nCamera==ENVCAMERA)&&(cap->env_video == NULL))
  1301. {
  1302. cap->env_video = video_capture_create(cap, CAMERA_TYPE_ENV);
  1303. if (!cap->env_video)
  1304. {
  1305. Dbg("create env video object failed!");
  1306. return Error_Unexpect;
  1307. }
  1308. }
  1309. else if((dev_id2 != -1)&&(nCamera == OPTCAMERA)&&(cap->opt_video == NULL))
  1310. {
  1311. cap->opt_video = video_capture_create(cap, CAMERA_TYPE_OPT);
  1312. if (!cap->opt_video)
  1313. {
  1314. Dbg("create opt video object failed!");
  1315. return Error_Unexpect;
  1316. }
  1317. }
  1318. else
  1319. {
  1320. return Error_Unexpect;
  1321. }
  1322. return Error_Succeed;
  1323. }
  1324. int salesaudio_capture_create(rvc_audio_capture_config_t *config, rvc_sales_audio_capture_t **p_cap)
  1325. {
  1326. rvc_sales_audio_capture_t *cap = ZALLOC_T(rvc_sales_audio_capture_t);
  1327. cap->rvc_audio = NULL;
  1328. memcpy(&cap->rvc_audio_config, config, sizeof(rvc_audio_capture_config_t));
  1329. cap->rvc_audio = salesrecord_audio_capture_create(cap);
  1330. if (!cap->rvc_audio) {
  1331. Dbg("create sales record audio capture object failed!");
  1332. salesaudio_capture_destroy(cap);
  1333. cap = NULL;
  1334. return Error_Unexpect;
  1335. }
  1336. else
  1337. {
  1338. cap->rvc_audio->iaudio_capture_peroid = config->audio_capture_period;
  1339. cap->rvc_audio->iaudio_capture_samplerate = config->audio_capture_samplerate;
  1340. *p_cap = cap;
  1341. Dbg("create sales record audio capture object(0x%08x) success, capture sample rate is %d, and rvc audio(0x%08x) in device is %s.",
  1342. cap, cap->rvc_audio->iaudio_capture_samplerate, cap->rvc_audio, cap->rvc_audio_config.strAudioIn.GetData());
  1343. return 0;
  1344. }
  1345. }
  1346. void salesaudio_capture_destroy(rvc_sales_audio_capture_t *cap)
  1347. {
  1348. if (NULL != cap){
  1349. Dbg("sales audio capture destroy, and cap addr is 0x%08x.", cap);
  1350. if (cap->rvc_audio) {
  1351. Dbg("sales audio capture rvc_audio destroy, and rvc_audio addr is 0x%08x.", cap->rvc_audio);
  1352. salesrecord_audio_capture_destroy(cap->rvc_audio);
  1353. cap->rvc_audio = NULL;
  1354. Dbg("set rvc_audio to null.");
  1355. }
  1356. free(cap);
  1357. }
  1358. }
  1359. void capture_destroy(capture_t *cap)
  1360. {
  1361. if (cap) {
  1362. if (cap->env_video) {
  1363. video_capture_destroy(cap->env_video);
  1364. cap->env_video = NULL;
  1365. }
  1366. if (cap->opt_video) {
  1367. video_capture_destroy(cap->opt_video);
  1368. cap->opt_video = NULL;
  1369. }
  1370. if (cap->audio) {
  1371. audio_capture_destroy(cap->audio);
  1372. cap->audio = NULL;
  1373. }
  1374. free(cap);
  1375. }
  1376. }
  1377. void capture_destroy(capture_t *cap,int nCamera)
  1378. {
  1379. if (cap)
  1380. {
  1381. if((cap->env_video)&&(nCamera == ENVCAMERA))
  1382. {
  1383. video_capture_destroy(cap->env_video);
  1384. cap->env_video = NULL;
  1385. }
  1386. else if((cap->opt_video)&&(nCamera==OPTCAMERA))
  1387. {
  1388. video_capture_destroy(cap->opt_video);
  1389. cap->opt_video = NULL;
  1390. }
  1391. }
  1392. }
  1393. ErrorCodeEnum start_audio_capture(audio_capture_t *paudio)
  1394. {
  1395. ErrorCodeEnum rslt = Error_Succeed;
  1396. int rc = 0;
  1397. if (NULL != paudio) {
  1398. rc = audio_capture_start(paudio);
  1399. if (rc != Error_Succeed) {
  1400. rslt = (ErrorCodeEnum)rc;
  1401. if (rslt == Error_AudioIN){
  1402. Dbg("start audio In object failed! rc:%d", rc);
  1403. LogError(Severity_High,Error_NotInit,ERROR_MOD_MEDIACONTROLLER_HANDFREE_OPENFAIL,"open audioIn device fail,please check device");
  1404. }
  1405. else{
  1406. Dbg("start audio Out object failed! rc:%d", rc);
  1407. LogError(Severity_High,Error_NotInit,ERROR_MOD_MEDIACONTROLLER_HANDFREE_OPENFAIL,"open audioOut device fail,please check device");
  1408. }
  1409. }
  1410. else{
  1411. Dbg("audio capture start success.");
  1412. }
  1413. }
  1414. else{
  1415. Dbg("audio_capture_t is null.");
  1416. rslt = Error_AudioIN;
  1417. }
  1418. return rslt;
  1419. }
  1420. ErrorCodeEnum capture_start(capture_t *cap)
  1421. {
  1422. int rc = 0;
  1423. ErrorCodeEnum rslt = start_audio_capture(cap->audio);
  1424. if (Error_Succeed != rslt) {
  1425. return rslt;
  1426. }
  1427. if (cap->env_video)
  1428. {
  1429. rc = video_capture_start(cap->env_video);
  1430. if (rc != Error_Succeed)
  1431. {
  1432. Dbg("start env video capture object failed! rc:%d", rc);
  1433. char strMessage[MAX_PATH*2] = {0};
  1434. get_camera_exception_message(strMessage, MAX_PATH*2, cap->config.strVideoEnv, "open environ camera fail,please check device.");
  1435. LogWarn(Severity_Middle,Error_NotInit,ERROR_MOD_MEDIACONTROLLER_ENVCAM_OPEN,strMessage);
  1436. if (cap->opt_video)
  1437. {
  1438. if (cap->env_video)
  1439. {
  1440. ::Sleep(3000);
  1441. }
  1442. rc = video_capture_start(cap->opt_video);
  1443. if (rc != Error_Succeed)
  1444. {
  1445. Dbg("start opt video capture object failed! rc:%d", rc);
  1446. char strMessage[MAX_PATH*2] = {0};
  1447. get_camera_exception_message(strMessage, MAX_PATH*2, cap->config.strVideoOpt, "open operate camera fail,please check device.");
  1448. LogWarn(Severity_Middle,Error_NotInit,ERROR_MOD_MEDIACONTROLLER_OPTCAM_OPEN, strMessage);
  1449. return Error_AllCamera;
  1450. }
  1451. else
  1452. {
  1453. Dbg("start env video capture object failed!");
  1454. return Error_EnvCamera;
  1455. }
  1456. }
  1457. else
  1458. {
  1459. Dbg("start all video capture object failed!");
  1460. return Error_AllCamera;
  1461. }
  1462. }
  1463. else
  1464. {
  1465. if (cap->opt_video)
  1466. {
  1467. if (cap->env_video)
  1468. {
  1469. ::Sleep(3000);
  1470. }
  1471. rc = video_capture_start(cap->opt_video);
  1472. if (rc != Error_Succeed)
  1473. {
  1474. Dbg("start opt video capture object failed! rc:%d", rc);
  1475. char strMessage[MAX_PATH*2] = {0};
  1476. get_camera_exception_message(strMessage, MAX_PATH*2, cap->config.strVideoOpt, "open operate camera fail,please check device.");
  1477. LogWarn(Severity_Middle,Error_NotInit,ERROR_MOD_MEDIACONTROLLER_OPTCAM_OPEN,strMessage);
  1478. return Error_OptCamera;
  1479. }
  1480. else
  1481. {
  1482. return Error_Succeed;
  1483. }
  1484. }
  1485. else
  1486. {
  1487. Dbg("opt_video = Null");
  1488. return Error_OptCamera;
  1489. }
  1490. }
  1491. }
  1492. else
  1493. {
  1494. if (cap->opt_video)
  1495. {
  1496. if (cap->env_video)
  1497. {
  1498. ::Sleep(3000);
  1499. }
  1500. rc = video_capture_start(cap->opt_video);
  1501. if (rc != Error_Succeed)
  1502. {
  1503. Dbg("start opt video capture object failed! rc:%d", rc);
  1504. char strMessage[MAX_PATH*2] = {0};
  1505. get_camera_exception_message(strMessage, MAX_PATH*2, cap->config.strVideoOpt, "open operate camera fail,please check device.");
  1506. LogWarn(Severity_Middle,Error_NotInit,ERROR_MOD_MEDIACONTROLLER_OPTCAM_OPEN,strMessage);
  1507. return Error_AllCamera;
  1508. }
  1509. else
  1510. {
  1511. Dbg("start env video capture object failed!");
  1512. return Error_EnvCamera;
  1513. }
  1514. }
  1515. else
  1516. {
  1517. Dbg("start env video Error_AllCamera");
  1518. return Error_AllCamera;
  1519. }
  1520. }
  1521. }
  1522. ErrorCodeEnum capture_start(capture_t *cap,int nCamera)
  1523. {
  1524. int rc = 0;
  1525. if (cap->env_video&&(nCamera==ENVCAMERA))
  1526. {
  1527. rc = video_capture_start(cap->env_video);
  1528. if (rc != Error_Succeed)
  1529. {
  1530. return Error_Hardware;
  1531. }
  1532. else
  1533. {
  1534. return Error_Succeed;
  1535. }
  1536. }
  1537. else if(cap->opt_video&&(nCamera==OPTCAMERA))
  1538. {
  1539. rc = video_capture_start(cap->opt_video);
  1540. if (rc != Error_Succeed)
  1541. {
  1542. return Error_Hardware;
  1543. }
  1544. else
  1545. {
  1546. return Error_Succeed;
  1547. }
  1548. }
  1549. else
  1550. {
  1551. return Error_Unexpect;
  1552. }
  1553. }
  1554. ErrorCodeEnum salesrecord_audio_capture_start(rvc_sales_audio_capture_t *cap)
  1555. {
  1556. auto rc = Error_Param;
  1557. if (NULL == cap){
  1558. return rc;
  1559. }
  1560. if (cap->rvc_audio) {
  1561. audiomgr_callback_t t_callback = { 0 };
  1562. t_callback.debug = &__audiomgrlog;
  1563. cap->rvc_audio->paudiocap = CreateAudioMgrObj(&t_callback);
  1564. if (0 == cap->rvc_audio->paudiocap->audio_mgr_initialize()) {
  1565. Dbg("audio manager initialize success!");
  1566. }
  1567. else {
  1568. Dbg("audio manager initialize failed!");
  1569. return Error_Null;
  1570. }
  1571. int rslt = record_audio_capture_start(cap->rvc_audio);
  1572. if (rslt != Error_Succeed) {
  1573. if (Error_AudioIN == rslt){
  1574. Dbg("start audio In object failed! rc:%d", rc);
  1575. LogError(Severity_High,Error_NotInit,ERROR_MOD_MEDIACONTROLLER_HANDFREE_OPENFAIL,"open audioIn device fail,please check device");
  1576. }
  1577. rc = (ErrorCodeEnum)rslt;
  1578. }
  1579. else{
  1580. rc = Error_Succeed;
  1581. }
  1582. }
  1583. return rc;
  1584. }
  1585. void salesrecord_audio_capture_stop(rvc_sales_audio_capture_t *cap)
  1586. {
  1587. if (NULL != cap){
  1588. if (cap->rvc_audio){
  1589. if (cap->rvc_audio->pdata){
  1590. if (eSingleWriteLocal == cap->rvc_audio->eType){
  1591. fclose((FILE*)cap->rvc_audio->pdata);
  1592. cap->rvc_audio->pdata = NULL;
  1593. }
  1594. }
  1595. record_audio_capture_stop(cap->rvc_audio);
  1596. }
  1597. }
  1598. else{
  1599. Dbg("sales record audio capture stop failed for param error.");
  1600. }
  1601. }
  1602. void capture_stop(capture_t *cap)
  1603. {
  1604. if (cap->audio) {
  1605. audio_capture_stop(cap->audio);
  1606. }
  1607. if (cap->env_video) {
  1608. video_capture_stop(cap->env_video);
  1609. }
  1610. if (cap->opt_video) {
  1611. video_capture_stop(cap->opt_video);
  1612. }
  1613. }
  1614. int capture_detect_camera_bug(capture_t *cap, int *env_n, int *opt_n,BOOL IsPad)
  1615. {
  1616. *env_n = 0;
  1617. *opt_n = 0;
  1618. if (cap->env_video)
  1619. {
  1620. if (cap->env_video->rtp_shm_queue)
  1621. {
  1622. *env_n = cap->env_video->rtp_shm_queue->GetVideoLens();
  1623. }
  1624. }
  1625. else
  1626. {
  1627. *env_n = -1;
  1628. }
  1629. if (cap->opt_video)
  1630. {
  1631. if (cap->opt_video->rtp_shm_queue)
  1632. {
  1633. *opt_n = cap->opt_video->rtp_shm_queue->GetVideoLens();
  1634. }
  1635. }
  1636. else
  1637. {
  1638. if (!IsPad)
  1639. {
  1640. *opt_n = -1;
  1641. }
  1642. else
  1643. {
  1644. *opt_n = 1;
  1645. }
  1646. }
  1647. return 0;
  1648. }
  1649. int capture_get_last_frametime(capture_t *cap, DWORD *env_n, DWORD *opt_n,BOOL IsPad)
  1650. {
  1651. *env_n = 0;
  1652. *opt_n = 0;
  1653. if (cap->env_video)
  1654. {
  1655. if (cap->env_video->rtp_shm_queue)
  1656. {
  1657. *env_n = cap->env_video->rtp_shm_queue->GetLastFrameTime();
  1658. }
  1659. }
  1660. else
  1661. {
  1662. *env_n = 0;
  1663. }
  1664. if (cap->opt_video)
  1665. {
  1666. if (cap->opt_video->rtp_shm_queue)
  1667. {
  1668. *opt_n = cap->opt_video->rtp_shm_queue->GetLastFrameTime();
  1669. }
  1670. }
  1671. else
  1672. {
  1673. *opt_n = 0;
  1674. }
  1675. return 0;
  1676. }
  1677. int capture_get_video_device_id(const char *dev_name)
  1678. {
  1679. #ifdef RVC_OS_WIN
  1680. int n = videocap_get_device_count();
  1681. for (int i = 0; i < n; ++i) {
  1682. WCHAR tmp[256];
  1683. char t[256];
  1684. WCHAR tmp1[256];
  1685. char t1[256];
  1686. videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  1687. WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  1688. videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
  1689. WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
  1690. // save DevicePath (add by ly at 20160725)
  1691. char t2[256];
  1692. strcpy(t2,t1);
  1693. for (int j = 0; j < strlen(t2); ++j)
  1694. {
  1695. t2[j] = toupper(t2[j]);
  1696. if (t2[j] == '#') t2[j] = '\\';
  1697. }
  1698. {
  1699. unsigned char x[MD5_DIGESTSIZE];
  1700. md5_ctx_t ctx;
  1701. md5_init(&ctx);
  1702. md5(x, t1, strlen(t1));
  1703. Bin2Str(x, sizeof(x), t1, sizeof(t1));
  1704. }
  1705. if (dev_name != NULL && strlen(dev_name) > 1 &&
  1706. strstr(dev_name, ";") == NULL) // 外接摄像头 add by ly at 20160531
  1707. {
  1708. /*if (strstr(t, dev_name) != NULL)
  1709. {
  1710. Dbg("[dbg] %s founded in %d cameras.", dev_name, n);
  1711. return i;
  1712. }*/
  1713. Dbg("[dbg] device_path: %s",t2);
  1714. if (strstr(t2,dev_name) != NULL) // 判断外接摄像头DeviceLocationPaths是否是DevicePath的子串
  1715. {
  1716. Dbg("[dbg] %s founded in %d cameras.", dev_name, n);
  1717. return i;
  1718. }
  1719. if (strcmp(dev_name, t) == 0) // 如果是直接用友好名称查询(适用于高拍仪) add by ly 2017/11/08
  1720. return i;
  1721. }
  1722. else
  1723. {
  1724. strcat(t, ";");
  1725. strcat(t, t1);
  1726. if (strcmp(dev_name, t) == 0)
  1727. return i;
  1728. }
  1729. }
  1730. return -1; // not found
  1731. #else
  1732. return rvc_videocap_get_video_device_id(dev_name);
  1733. #endif // RVC_OS_WIN
  1734. }
  1735. int capture_lib_init() // edit by ly at 20160401
  1736. {
  1737. #ifdef RVC_OS_WIN
  1738. HRESULT hr = CoInitialize(NULL);
  1739. int rc;
  1740. {
  1741. HMODULE hModule = GetModuleHandleA("MSVCR100.dll");
  1742. if (hModule) {
  1743. typedef char* (*f_setlocale)(int, const char*);
  1744. f_setlocale f = (f_setlocale)GetProcAddress(hModule, "setlocale");
  1745. (*f)(LC_ALL, "chs");
  1746. }
  1747. }
  1748. if (SUCCEEDED(hr)) {
  1749. PaError Error;
  1750. Error = Pa_Initialize();
  1751. if (Error == paNoError) {
  1752. rc = videoframework_init();
  1753. if (rc != 0) {
  1754. Dbg("videoframework_init failed, rc=%d", rc);
  1755. return Error_Resource;
  1756. }
  1757. }
  1758. else {
  1759. Dbg("PaInitialize failed, rc=%d", Error);
  1760. return Error_Resource;
  1761. }
  1762. }
  1763. else {
  1764. Dbg("coinitialze failed! hr:%d", hr);
  1765. return Error_Resource;
  1766. }
  1767. {
  1768. int i, n;
  1769. n = videocap_get_device_count();
  1770. for (i = 0; i < n; ++i) {
  1771. WCHAR tmp[256];
  1772. char t[256];
  1773. WCHAR tmp1[256];
  1774. char t1[256];
  1775. videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  1776. WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  1777. videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
  1778. WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
  1779. {
  1780. unsigned char x[MD5_DIGESTSIZE];
  1781. md5_ctx_t ctx;
  1782. md5_init(&ctx);
  1783. md5(x, t1, strlen(t1));
  1784. Bin2Str(x, sizeof(x), t1, sizeof(t1));
  1785. }
  1786. Dbg("%d = %s;%s", i, t, t1);
  1787. }
  1788. }
  1789. {
  1790. int icnt, ocnt;
  1791. int rc = audio_get_dev_count(&icnt, &ocnt);
  1792. if (rc == 0) {
  1793. int i;
  1794. Dbg("audio input devices(%d):", icnt);
  1795. for (i = 0; i < icnt; ++i) {
  1796. CSimpleStringA str = audio_get_dev_name(true, i);
  1797. Dbg("%d = %s", i, (LPCSTR)str);
  1798. }
  1799. Dbg("audio output devices(%d):", ocnt);
  1800. for (i = 0; i < ocnt; ++i) {
  1801. CSimpleStringA str = audio_get_dev_name(false, i);
  1802. Dbg("%d = %s", i, (LPCSTR)str);
  1803. }
  1804. }
  1805. }
  1806. #else
  1807. {
  1808. int inumber = 0;
  1809. int icount = rvc_videocap_get_device_count();
  1810. for (int i = 0; i < 64 && inumber < icount; ++i) {
  1811. char strcamera[2 * MAX_PATH] = { 0 };
  1812. char strpath[MAX_PATH] = { 0 };
  1813. if (0 == rvc_videocap_get_device_fullpathname(i, strcamera, 2 * MAX_PATH)){
  1814. Dbg("%d = %s", inumber++, strcamera);
  1815. }
  1816. }
  1817. }
  1818. #endif // RVC_OS_WIN
  1819. return Error_Succeed;
  1820. }
  1821. int videocap_innerdev_fetch(CSimpleStringA&frontcam,CSimpleStringA&rearcam) // edit by ly at 20160401
  1822. {
  1823. int i = 0, n = 0;
  1824. #ifdef RVC_OS_WIN
  1825. frontcam = "$", rearcam = "$";
  1826. n = videocap_get_device_count();
  1827. if (n == 2) // 未插入外接时
  1828. {
  1829. for (i = 0; i < n; ++i) {
  1830. WCHAR tmp[256];
  1831. char t[256];
  1832. WCHAR tmp1[256];
  1833. char t1[256];
  1834. videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  1835. WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  1836. videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
  1837. WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
  1838. {
  1839. unsigned char x[MD5_DIGESTSIZE];
  1840. md5_ctx_t ctx;
  1841. md5_init(&ctx);
  1842. md5(x, t1, strlen(t1));
  1843. Bin2Str(x, sizeof(x), t1, sizeof(t1));
  1844. }
  1845. strcat(t, ";");
  1846. strcat(t, t1);
  1847. if (i == 0)
  1848. {
  1849. frontcam = t;
  1850. }
  1851. else if (i == 1)
  1852. {
  1853. rearcam = t;
  1854. }
  1855. }
  1856. }
  1857. else
  1858. {
  1859. Dbg("[dbg] detect %d cameras.", n);
  1860. }
  1861. #else
  1862. #endif // RVC_OS_WIN
  1863. return n;
  1864. }
  1865. int videocap_outerdev_fetch( CSimpleStringA envcam,CSimpleStringA optcam,CSimpleStringA ewscam,CAutoArray<CSimpleStringA> &hspcams,CSimpleStringA &outercam )
  1866. {
  1867. int i = 0, n = 0;
  1868. #ifdef RVC_OS_WIN
  1869. outercam = "$";
  1870. n = videocap_get_device_count();
  1871. int m = hspcams.GetCount(); // 高拍仪的个数 add by ly 2017/11/07
  1872. if (ewscam.GetLength() <= 1) // 如果外部广角摄像头未配置
  1873. {
  1874. if (n <= m || n > 3+m)
  1875. {
  1876. Dbg("[dbg] [videocap_outerdev_fetch] detect %d cameras.", n);
  1877. }
  1878. else
  1879. {
  1880. int cnt = 0;
  1881. for (i = 0; i < n; ++i) {
  1882. WCHAR tmp[256];
  1883. char t[256];
  1884. WCHAR tmp1[256];
  1885. char t1[256];
  1886. videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  1887. WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  1888. videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
  1889. WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
  1890. // save DevicePath (add by ly at 20160725)
  1891. char t2[256];
  1892. strcpy(t2,t1);
  1893. for (int j = 0; j < strlen(t2); ++j)
  1894. {
  1895. t2[j] = toupper(t2[j]);
  1896. if (t2[j] == '#') t2[j] = '\\';
  1897. }
  1898. {
  1899. unsigned char x[MD5_DIGESTSIZE];
  1900. md5_ctx_t ctx;
  1901. md5_init(&ctx);
  1902. md5(x, t1, strlen(t1));
  1903. Bin2Str(x, sizeof(x), t1, sizeof(t1));
  1904. }
  1905. // 检查是否为高拍仪摄像头,若是则直接枚举下一个摄像头
  1906. bool isHspCam = false;
  1907. for (int k = 0; k < m; ++k) {
  1908. if (!strcmp((LPCTSTR)hspcams[k],t)) {
  1909. isHspCam = true;
  1910. break;
  1911. }
  1912. }
  1913. if (isHspCam) {
  1914. continue;
  1915. }
  1916. strcat(t, ";");
  1917. strcat(t, t1);
  1918. if(strcmp(t,(LPCTSTR)envcam) && strcmp(t,(LPCTSTR)optcam))
  1919. {
  1920. if (0 == cnt)
  1921. {
  1922. outercam = t2;
  1923. }
  1924. ++cnt;
  1925. }
  1926. }
  1927. if(cnt != 1)
  1928. {
  1929. outercam = "$";
  1930. }
  1931. }
  1932. }
  1933. else // 如果外部广角摄像头已配置
  1934. {
  1935. if (n <= m || n > 4+m)
  1936. {
  1937. Dbg("[dbg] [videocap_outerdev_fetch] detect %d cameras.", n);
  1938. }
  1939. else
  1940. {
  1941. int cnt = 0;
  1942. for (i = 0; i < n; ++i) {
  1943. WCHAR tmp[256];
  1944. char t[256];
  1945. WCHAR tmp1[256];
  1946. char t1[256];
  1947. videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  1948. WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  1949. videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
  1950. WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
  1951. // save DevicePath (add by ly at 20160725)
  1952. char t2[256];
  1953. strcpy(t2,t1);
  1954. for (int j = 0; j < strlen(t2); ++j)
  1955. {
  1956. t2[j] = toupper(t2[j]);
  1957. if (t2[j] == '#') t2[j] = '\\';
  1958. }
  1959. {
  1960. unsigned char x[MD5_DIGESTSIZE];
  1961. md5_ctx_t ctx;
  1962. md5_init(&ctx);
  1963. md5(x, t1, strlen(t1));
  1964. Bin2Str(x, sizeof(x), t1, sizeof(t1));
  1965. }
  1966. // 检查是否为高拍仪摄像头,若是则直接枚举下一个摄像头
  1967. bool isHspCam = false;
  1968. for (int k = 0; k < m; ++k) {
  1969. if (!strcmp((LPCTSTR)hspcams[k],t)) {
  1970. isHspCam = true;
  1971. break;
  1972. }
  1973. }
  1974. if (isHspCam) {
  1975. continue;
  1976. }
  1977. strcat(t, ";");
  1978. strcat(t, t1);
  1979. if(strcmp(t,(LPCTSTR)envcam) && strcmp(t,(LPCTSTR)optcam) && !strstr(t2,(LPCTSTR)ewscam))
  1980. {
  1981. if (0 == cnt)
  1982. {
  1983. outercam = t2;
  1984. }
  1985. ++cnt;
  1986. }
  1987. }
  1988. if(cnt != 1)
  1989. {
  1990. outercam = "$";
  1991. }
  1992. }
  1993. }
  1994. #else
  1995. #endif // RVC_OS_WIN
  1996. return n;
  1997. }
  1998. void capture_enum_cameras(CAutoArray<CSimpleStringA>& cams)
  1999. {
  2000. #if defined(RVC_OS_LINUX)
  2001. cams.Clear();
  2002. CAutoArray<CSimpleStringA> tmp;
  2003. int inumber = 0;
  2004. int icount = rvc_videocap_get_device_count();
  2005. if (icount >= 64) icount = 64;
  2006. tmp.Init(icount);
  2007. for (int i = 0; inumber < icount; ++i) {
  2008. char strcamera[2 * MAX_PATH] = { 0 };
  2009. char strpath[MAX_PATH] = { 0 };
  2010. if (0 == rvc_videocap_get_device_fullpathname(i, strcamera, 2 * MAX_PATH)) {
  2011. tmp[inumber] = strcamera;
  2012. Dbg("%d = %s", inumber++, strcamera);
  2013. }
  2014. }
  2015. cams.Copy(tmp, 0, inumber);
  2016. #endif //RVC_OS_LINUX
  2017. }
  2018. void capture_lib_term()
  2019. {
  2020. #ifdef RVC_OS_WIN
  2021. Pa_Terminate();
  2022. videoframework_term();
  2023. CoUninitialize();
  2024. #else
  2025. #endif // RVC_OS_WIN
  2026. }
  2027. bool capture_adj_brightness(capture_t *cap,int nvalue,ErrorCodeEnum nCode)
  2028. {
  2029. #ifdef RVC_OS_WIN
  2030. HRESULT rst = S_OK;
  2031. if (cap->env_video&&(nCode!=Error_EnvCamera)&&(nCode!=Error_AllCamera))
  2032. {
  2033. rst = videocap_adj_brightness(cap->env_video->cap,nvalue);
  2034. }
  2035. HRESULT rst2 = S_OK;
  2036. if (cap->opt_video->cap&&(nCode!=Error_OptCamera)&&(nCode!=Error_AllCamera))
  2037. {
  2038. rst2 = videocap_adj_brightness(cap->opt_video->cap,nvalue);
  2039. }
  2040. if (SUCCEEDED(rst)&&SUCCEEDED(rst2))
  2041. return true;
  2042. else
  2043. return false;
  2044. #else
  2045. int ienv = -1;
  2046. if (cap->env_video && cap->env_video->pVideoCap && (nCode != Error_EnvCamera) && (nCode != Error_AllCamera)){
  2047. ienv = cap->env_video->pVideoCap->SetCamBrightness(nvalue);
  2048. }
  2049. int iopt = -1;
  2050. if (cap->opt_video && cap->opt_video->pVideoCap && (nCode != Error_OptCamera) && (nCode != Error_AllCamera)){
  2051. iopt = cap->opt_video->pVideoCap->SetCamBrightness(nvalue);
  2052. }
  2053. if (0 == ienv && 0 == iopt){
  2054. return true;
  2055. }
  2056. else{
  2057. return false;
  2058. }
  2059. #endif // RVC_OS_WIN
  2060. }
  2061. bool capture_set_autobrightness(capture_t *cap,ErrorCodeEnum nCode)
  2062. {
  2063. #ifdef RVC_OS_WIN
  2064. HRESULT rst = S_OK;
  2065. HRESULT rst2 = S_OK;
  2066. if (cap->env_video && (nCode != Error_EnvCamera) && (nCode != Error_AllCamera))
  2067. {
  2068. rst = videocap_set_autobrightness(cap->env_video->cap);
  2069. }
  2070. if (cap->opt_video && (nCode != Error_OptCamera) && (nCode != Error_AllCamera))
  2071. {
  2072. rst2 = videocap_set_autobrightness(cap->opt_video->cap);
  2073. }
  2074. if (SUCCEEDED(rst) && SUCCEEDED(rst2))
  2075. return true;
  2076. else
  2077. return false;
  2078. #else
  2079. int ienv = -1;
  2080. int iopt = -1;
  2081. if (cap->env_video && cap->env_video->pVideoCap && (nCode != Error_EnvCamera) && (nCode != Error_AllCamera))
  2082. {
  2083. ienv = cap->env_video->pVideoCap->SetCamAutoBrightness();
  2084. }
  2085. if (cap->opt_video && cap->opt_video->pVideoCap && (nCode != Error_OptCamera) && (nCode != Error_AllCamera))
  2086. {
  2087. iopt = cap->opt_video->pVideoCap->SetCamAutoBrightness();
  2088. }
  2089. if (0 == ienv && 0 == iopt) {
  2090. return true;
  2091. }
  2092. else {
  2093. return false;
  2094. }
  2095. #endif // RVC_OS_WIN
  2096. }
  2097. int capture_get_brightness(capture_t *cap,ErrorCodeEnum nCode)
  2098. {
  2099. #ifdef RVC_OS_WIN
  2100. int nValue1 = 0;
  2101. int nValue2 = 0;
  2102. HRESULT rst = S_OK;
  2103. if (cap->env_video && (nCode != Error_EnvCamera) && (nCode != Error_AllCamera))
  2104. {
  2105. HRESULT rst = videocap_get_brightness(cap->env_video->cap, &nValue1);
  2106. }
  2107. HRESULT rst2 = S_OK;
  2108. if (cap->opt_video && (nCode != Error_OptCamera) && (nCode != Error_AllCamera))
  2109. {
  2110. rst2 = videocap_get_brightness(cap->opt_video->cap, &nValue2);
  2111. }
  2112. else
  2113. {
  2114. return -1;
  2115. }
  2116. if ((cap->opt_video == NULL) && cap->env_video)
  2117. {
  2118. return nValue1;
  2119. }
  2120. else if ((cap->env_video == NULL) && cap->opt_video)
  2121. {
  2122. return nValue2;
  2123. }
  2124. else
  2125. {
  2126. if (SUCCEEDED(rst) && SUCCEEDED(rst2))
  2127. return (nValue1 <= nValue2) ? nValue1 : nValue2;
  2128. else
  2129. return -1;
  2130. }
  2131. #else
  2132. int nValue1 = 0;
  2133. int nValue2 = 0;
  2134. int ienv = -1;
  2135. if (cap->env_video && cap->env_video->pVideoCap && (nCode != Error_EnvCamera) && (nCode != Error_AllCamera)){
  2136. ienv = cap->env_video->pVideoCap->GetCamBrightness(&nValue1);
  2137. }
  2138. int iopt = -1;
  2139. if (cap->opt_video && cap->opt_video->pVideoCap && (nCode != Error_OptCamera) && (nCode != Error_AllCamera)){
  2140. iopt = cap->opt_video->pVideoCap->GetCamBrightness(&nValue2);
  2141. }
  2142. else{
  2143. return -1;
  2144. }
  2145. if ((cap->opt_video == NULL) && cap->env_video)
  2146. {
  2147. return nValue1;
  2148. }
  2149. else if ((cap->env_video == NULL) && cap->opt_video)
  2150. {
  2151. return nValue2;
  2152. }
  2153. else
  2154. {
  2155. if (0 == ienv && 0 == iopt) {
  2156. return (nValue1 <= nValue2) ? nValue1 : nValue2;
  2157. }
  2158. else
  2159. {
  2160. return -1;
  2161. }
  2162. }
  2163. #endif // RVC_OS_WIN
  2164. }
  2165. int StopCamera(capture_t *cap,int nCamera)
  2166. {
  2167. if((nCamera == ENVCAMERA)&&cap->env_video)
  2168. {
  2169. video_capture_stop(cap->env_video);
  2170. video_capture_destroy(cap->env_video);
  2171. cap->env_video = NULL;
  2172. return 0;
  2173. }
  2174. else if((nCamera == OPTCAMERA)&&cap->opt_video)
  2175. {
  2176. video_capture_stop(cap->opt_video);
  2177. video_capture_destroy(cap->opt_video);
  2178. cap->opt_video = NULL;
  2179. return 0;
  2180. }
  2181. else
  2182. {
  2183. return -1;
  2184. }
  2185. }
  2186. //根据frontcam完整名(包含设备名+逗号+设备路径MD5)、ewscam设备路径、hspcams设备名,找到rearcam的完整名
  2187. //排除以上设备后的唯一设备即为rearcam
  2188. int videocap_optdev_fetch( CSimpleStringA frontcam,CSimpleStringA ewscam,CAutoArray<CSimpleStringA> &hspcams,CSimpleStringA &rearcam )
  2189. {
  2190. int i = 0, n = 0;
  2191. #ifdef RVC_OS_WIN
  2192. rearcam = "$";
  2193. n = videocap_get_device_count();
  2194. int m = hspcams.GetCount(); // 高拍仪的个数 add by ly 2017/11/07
  2195. if (ewscam.GetLength() <= 1) // 如果外部广角摄像头未配置
  2196. {
  2197. if (n <= 2+m)
  2198. {
  2199. int cnt = 0;
  2200. for (i = 0; i < n; ++i) {
  2201. WCHAR tmp[256];
  2202. char t[256];
  2203. WCHAR tmp1[256];
  2204. char t1[256];
  2205. videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  2206. WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  2207. videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
  2208. WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
  2209. {
  2210. unsigned char x[MD5_DIGESTSIZE];
  2211. md5_ctx_t ctx;
  2212. md5_init(&ctx);
  2213. md5(x, t1, strlen(t1));
  2214. Bin2Str(x, sizeof(x), t1, sizeof(t1));
  2215. }
  2216. // 检查是否为高拍仪摄像头,若是则直接枚举下一个摄像头
  2217. bool isHspCam = false;
  2218. for (int k = 0; k < m; ++k) {
  2219. if (!strcmp((LPCTSTR)hspcams[k],t)) {
  2220. isHspCam = true;
  2221. break;
  2222. }
  2223. }
  2224. if (isHspCam) {
  2225. continue;
  2226. }
  2227. strcat(t, ";");
  2228. strcat(t, t1);
  2229. if (strcmp((LPCTSTR)frontcam, t))
  2230. {
  2231. if (0 == cnt)
  2232. {
  2233. rearcam = t;
  2234. }
  2235. cnt++;
  2236. }
  2237. }
  2238. if (1 != cnt) // 未找到或无法识别内置后摄像头
  2239. {
  2240. rearcam = "$";
  2241. }
  2242. }
  2243. else
  2244. {
  2245. Dbg("[dbg] [videocap_optdev_fetch] detect %d cameras.", n);
  2246. }
  2247. }
  2248. else // 如果外部广角摄像头已配置
  2249. {
  2250. if (n <= 3+m)
  2251. {
  2252. int cnt = 0;
  2253. for (i = 0; i < n; ++i) {
  2254. WCHAR tmp[256];
  2255. char t[256];
  2256. WCHAR tmp1[256];
  2257. char t1[256];
  2258. videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  2259. WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  2260. videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
  2261. WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
  2262. // 检查是否为高拍仪摄像头,若是则直接枚举下一个摄像头
  2263. bool isHspCam = false;
  2264. for (int k = 0; k < m; ++k) {
  2265. if (!strcmp((LPCTSTR)hspcams[k],t)) {
  2266. isHspCam = true;
  2267. break;
  2268. }
  2269. }
  2270. if (isHspCam) {
  2271. continue;
  2272. }
  2273. // save DevicePath (add by ly at 20160725)
  2274. char t2[256];
  2275. strcpy(t2,t1);
  2276. for (int j = 0; j < strlen(t2); ++j)
  2277. {
  2278. t2[j] = toupper(t2[j]);
  2279. if (t2[j] == '#') t2[j] = '\\';
  2280. }
  2281. {
  2282. unsigned char x[MD5_DIGESTSIZE];
  2283. md5_ctx_t ctx;
  2284. md5_init(&ctx);
  2285. md5(x, t1, strlen(t1));
  2286. Bin2Str(x, sizeof(x), t1, sizeof(t1));
  2287. }
  2288. strcat(t, ";");
  2289. strcat(t, t1);
  2290. if (strcmp((LPCTSTR)frontcam, t) && !strstr(t2,(LPCTSTR)ewscam))
  2291. {
  2292. if (0 == cnt)
  2293. {
  2294. rearcam = t;
  2295. }
  2296. cnt++;
  2297. }
  2298. }
  2299. if (1 != cnt) // 未找到或无法识别内置后摄像头
  2300. {
  2301. rearcam = "$";
  2302. }
  2303. }
  2304. else
  2305. {
  2306. Dbg("[dbg] [videocap_optdev_fetch] detect %d cameras.", n);
  2307. }
  2308. }
  2309. #else
  2310. #endif // RVC_OS_WIN
  2311. return n;
  2312. }
  2313. void capture_clearsnapshotvideo(capture_t *cap,int nCamera)
  2314. {
  2315. if((nCamera == ENVCAMERA)&&cap->env_video)
  2316. {
  2317. Dbg("clear ENV snapshot video!");
  2318. cap->env_video->snapshot_shm_queue->ClearVideoQueue();
  2319. return;
  2320. }
  2321. else if((nCamera == OPTCAMERA)&&cap->opt_video)
  2322. {
  2323. Dbg("clear OPT snapshot video!");
  2324. cap->opt_video->snapshot_shm_queue->ClearVideoQueue();
  2325. return;
  2326. }
  2327. else if(nCamera == AlLCAMERA)
  2328. {
  2329. if (cap->env_video)
  2330. {
  2331. Dbg("clear ENV snapshot video!");
  2332. cap->env_video->snapshot_shm_queue->ClearVideoQueue();
  2333. }
  2334. if (cap->opt_video)
  2335. {
  2336. Dbg("clear OPT snapshot video!");
  2337. cap->opt_video->snapshot_shm_queue->ClearVideoQueue();
  2338. }
  2339. return;
  2340. }
  2341. else
  2342. {
  2343. return ;
  2344. }
  2345. }
  2346. int get_camera_exception_message(char* pBuffer, size_t uLen, CSimpleStringA strDeviceName, const char* strErrorMessage)
  2347. {
  2348. int iRet = 0;
  2349. if (strDeviceName.GetLength() > 0){
  2350. const char* strCameraName = strDeviceName.GetData();
  2351. char strBuffer[MAX_PATH] = {0};
  2352. if (sprintf_s(strBuffer, MAX_PATH, "%s", strCameraName) > 0){
  2353. char *pIndex = NULL;
  2354. if (pIndex = (char*)strstr(strBuffer, ";")){
  2355. *pIndex = '\0';
  2356. }
  2357. }
  2358. if (NULL != strErrorMessage){
  2359. size_t uDataLen = strlen(strBuffer);
  2360. size_t uErrorLen = strlen(strErrorMessage);
  2361. if (uLen > uDataLen + uErrorLen + 10){
  2362. iRet = sprintf_s(pBuffer, uLen, "[%s] %s", strBuffer, strErrorMessage);
  2363. }
  2364. }
  2365. }
  2366. if (0 == iRet){
  2367. if (NULL != strErrorMessage){
  2368. iRet = sprintf_s(pBuffer, uLen, "%s", strErrorMessage);
  2369. }
  2370. }
  2371. return iRet;
  2372. }
  2373. }