1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734 |
- #ifdef RVC_OS_WIN
- #include "stdafx.h"
- #include <ipp.h>
- #include <objbase.h>
- #include <intrin.h>
- #include "../../Other/libvideohorflip/videohorflip.h"
- #include "../../Other/libvideoframework/video_common/ffmpeg_api_cpp_adapter.h"
- #else
- #endif // RVC_OS_WIN
- #include <locale.h>
- #include <memutil.h>
- #include <md5.h>
- #include "y2k_time.h"
- #include "../../Other/rvcmediacommon/rvc_media_common.h"
- #include "Event.h"
- #include "capture.h"
- using namespace MediaController;
- #ifndef av_always_inline
- #define av_always_inline __inline
- #endif // !av_always_inline
- #ifndef inline
- #define inline __inline
- #endif // !inline
- #ifndef RVC_AUDIO_BUFFER_LEN
- #define RVC_AUDIO_BUFFER_LEN 320
- #endif
- static void __dbg(const char *fmt, va_list arg)
- {
- int n = vsnprintf(NULL, 0, fmt, arg);
- if (n >= MAX_PATH) {
- char* buf = (char*)malloc((size_t)(n + 1));
- vsnprintf(buf, n + 1, fmt, arg);
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf);
- free(buf);
- }
- else{
- char strlog[MAX_PATH] = {0};
- vsnprintf(strlog, MAX_PATH, fmt, arg);
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog);
- }
- }
- static int Bin2Str(unsigned char *x, int xlen, char *str, int str_size)
- {
- static const char *hex2char = "0123456789ABCDEF";
- int i, k = 0;
- if (str_size <= xlen * 2)
- return -1;
- for (i = 0; i < xlen; ++i) {
- int h = x[i] >> 4;
- int l = x[i] & 0xf;
- str[k++] = hex2char[h];
- str[k++] = hex2char[l];
- }
- str[k] = 0;
- return k;
- }
- static void __audiomgrlog(void* user_data, const char* fmt, va_list arg)
- {
- int n = vsnprintf(NULL, 0, fmt, arg);
- if (n >= MAX_PATH) {
- char* buf = (char*)malloc((size_t)(n + 1));
- vsnprintf(buf, n + 1, fmt, arg);
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf);
- free(buf);
- }
- else {
- char strlog[MAX_PATH] = { 0 };
- vsnprintf(strlog, MAX_PATH, fmt, arg);
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog);
- }
- }
- #ifdef RVC_OS_WIN
- static int audio_get_dev_count(int* in_cnt, int* out_cnt)
- {
- int icnt = 0, ocnt = 0;
- int cnt = Pa_GetDeviceCount();
- for (int i = 0; i < cnt; ++i) {
- const PaDeviceInfo* info = Pa_GetDeviceInfo(i);
- if (info->maxInputChannels)
- icnt++;
- if (info->maxOutputChannels)
- ocnt++;
- }
- if (in_cnt)
- * in_cnt = icnt;
- if (out_cnt)
- * out_cnt = ocnt;
- return 0;
- }
- static CSimpleStringA audio_get_dev_name(bool in_direction, int idx)
- {
- int cnt = Pa_GetDeviceCount();
- int ii, i;
- for (i = 0, ii = 0; i < cnt; ++i) {
- const PaDeviceInfo* info = Pa_GetDeviceInfo(i);
- if (in_direction) {
- if (info->maxInputChannels) {
- if (idx == ii) {
- return CSimpleStringA(info->name);
- }
- ii++;
- }
- }
- else {
- if (info->maxOutputChannels) {
- if (idx == ii) {
- return CSimpleStringA(info->name);
- }
- ii++;
- }
- }
- }
- return CSimpleStringA();
- }
- static int translate_id(int in_direction, int idx)
- {
- int i, n, ii;
- n = Pa_GetDeviceCount();
- for (i = 0, ii = 0; i < n; ++i) {
- const PaDeviceInfo* info = Pa_GetDeviceInfo(i);
- if (in_direction) {
- if (info->maxInputChannels) {
- if (ii == idx) {
- return i;
- }
- ii++;
- }
- }
- else {
- if (info->maxOutputChannels) {
- if (ii == idx) {
- return i;
- }
- ii++;
- }
- }
- }
- return -1;
- }
- int capture_get_audio_device_id(bool in_direction, const char* dev_name)
- {
- int cnt = Pa_GetDeviceCount();
- int ii, i;
- for (i = 0, ii = 0; i < cnt; ++i) {
- const PaDeviceInfo* info = Pa_GetDeviceInfo(i);
- if (in_direction) {
- if (info->maxInputChannels) {
- if (strstr(info->name, dev_name) != NULL) {
- return ii;
- }
- ii++;
- }
- }
- else {
- if (info->maxOutputChannels) {
- if (strstr(info->name, dev_name) != NULL) {
- return ii;
- }
- ii++;
- }
- }
- }
- return -1;
- }
- static int StreamCallback(const void* input,
- void* output,
- unsigned long frameCount,
- const PaStreamCallbackTimeInfo* timeInfo,
- PaStreamCallbackFlags statusFlags,
- void* userData)
- {
- audio_capture_t* audio_cap = (audio_capture_t*)userData;
- if (input) {
- audio_frame frm;
- audio_frame dst_frm;
- frm.bitspersample = audio_cap->dev_bitspersample;
- frm.format = 1;
- frm.data = (char*)const_cast<void*>(input);
- frm.framesize = frameCount * (audio_cap->dev_bitspersample / 8);
- frm.nchannels = audio_cap->dev_channels;
- frm.samplespersec = audio_cap->dev_samplespersec;
- frm.iseriesnumber = 0;
- //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[StreamCallback] frameCount :%d and framesize :%d, nchannels :%d, bitspersample :%d.", frameCount, frm.framesize, frm.nchannels, frm.bitspersample);
- if (audio_cap->dev_bitspersample == 32) {
- dst_frm.bitspersample = 16;
- dst_frm.format = 1;
- dst_frm.data = (char*)malloc(frameCount * 16 / 8);
- dst_frm.framesize = frameCount * 16 / 8;
- dst_frm.nchannels = audio_cap->dev_channels;
- dst_frm.samplespersec = audio_cap->dev_samplespersec;
- dst_frm.iseriesnumber = 0;
- From32To16Bit(&frm, &dst_frm);
- //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[StreamCallback] From32To16Bit frameCount :%d and framesize :%d, nchannels :%d, bitspersample :%d.", frameCount, dst_frm.framesize, dst_frm.nchannels, dst_frm.bitspersample);
- if (!audio_cap->shm_queue->InsertAudio(&dst_frm)) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[StreamCallback] InsertAudio to shm_queue failed! frameCount:%d", frameCount);
- }
- //else {
- // DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[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());
- //}
- //if (!audio_cap->salesol_shm_queue->InsertAudio(&dst_frm)) {
- // DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[StreamCallback] InsertAudio to salesol_shm_queue failed! frameCount:%d", frameCount);
- //}
- //else {
- // DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[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());
- //}
- free(dst_frm.data);
- }
- else {
- if (!audio_cap->shm_queue->InsertAudio(&frm)) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[StreamCallback] InsertAudio to shm_queue failed! frameCount:%d", frameCount);
- }
- //else {
- // DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[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());
- //}
- //if (!audio_cap->salesol_shm_queue->InsertAudio(&frm)) {
- // DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[StreamCallback] InsertAudio to salesol_shm_queue failed! frameCount:%d", frameCount);
- //}
- //else {
- // DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[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());
- //}
- }
- }
- if (output) {
- memset(output, 0, frameCount << 1);
- }
- return paContinue;
- }
- static int Sales_StreamCallback(const void* input,
- void* output,
- unsigned long frameCount,
- const PaStreamCallbackTimeInfo* timeInfo,
- PaStreamCallbackFlags statusFlags,
- void* userData)
- {
- rvc_audio_capture_t* audio_cap = (rvc_audio_capture_t*)userData;
- if ((NULL != audio_cap) && (NULL != audio_cap->audio_shm_queue) && (NULL != input)) {
- audio_frame frm;
- audio_frame dst_frm;
- frm.bitspersample = audio_cap->dev_bitspersample;
- frm.format = 1;
- frm.data = (char*)const_cast<void*>(input);
- frm.framesize = frameCount * (audio_cap->dev_bitspersample / 8);
- frm.nchannels = audio_cap->dev_channels;
- frm.samplespersec = audio_cap->dev_samplespersec;
- frm.iseriesnumber = audio_cap->iseriesnumber++;
- //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[Sales_StreamCallback] frameCount :%d and framesize :%d, nchannels :%d, bitspersample :%d.", frameCount, frm.framesize, frm.nchannels, frm.bitspersample);
- if (audio_cap->dev_bitspersample == 32) {
- dst_frm.bitspersample = 16;
- dst_frm.format = 1;
- dst_frm.data = (char*)malloc(frameCount * 16 / 8);
- dst_frm.framesize = frameCount * 16 / 8;
- dst_frm.nchannels = audio_cap->dev_channels;
- dst_frm.samplespersec = audio_cap->dev_samplespersec;
- dst_frm.iseriesnumber = frm.iseriesnumber;
- From32To16Bit(&frm, &dst_frm);
- //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[Sales_StreamCallback] From32To16Bit frameCount :%d and framesize :%d, nchannels :%d, bitspersample :%d.", frameCount, dst_frm.framesize, dst_frm.nchannels, dst_frm.bitspersample);
- if (!audio_cap->audio_shm_queue->InsertAudio(&dst_frm)) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[Sales_StreamCallback] InsertAudio failed! frameCount:%d", frameCount);
- }
- else {
- if ((audio_cap->iaudio_capture_peroid) > 0 && (0 == dst_frm.iseriesnumber % audio_cap->iaudio_capture_peroid)) {
- if (audio_cap->iseriesnumber > INT_MAX) {
- audio_cap->iseriesnumber = 0;
- }
- //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("current audio frame series number is %d.", frm.iseriesnumber);
- }
- }
- free(dst_frm.data);
- }
- else {
- if (!audio_cap->audio_shm_queue->InsertAudio(&frm)) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[Sales_StreamCallback] InsertAudio failed! frameCount:%d", frameCount);
- }
- else {
- if ((audio_cap->iaudio_capture_peroid) > 0 && (0 == frm.iseriesnumber % audio_cap->iaudio_capture_peroid)) {
- if (audio_cap->iseriesnumber > INT_MAX) {
- audio_cap->iseriesnumber = 0;
- }
- //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("current audio frame series number is %d.", frm.iseriesnumber);
- }
- }
- }
- if (eSingleWriteLocal == audio_cap->eType) {
- fwrite(frm.data, frm.framesize, 1, (FILE*)(audio_cap->pdata));
- }
- }
- if (output) {
- memset(output, 0, frameCount << 1);
- }
- return paContinue;
- }
- static int portaudio_capture_start(audio_capture_t* audio_cap)
- {
- capture_t* cap = audio_cap->parent;
- PaStreamParameters outParam = { 0 };
- PaStreamParameters inParam = { 0 };
- PaStreamParameters salesInParam = { 0 };
- PaError paError = paNoError;
- int nId = capture_get_audio_device_id(true, cap->config.strAudioIn);
- if (nId == -1)
- {
- //需要立即处理的告警使用Severity_High
- LogWarn(Severity_Middle, Error_Debug,ERROR_MOD_MEDIACONTROLLER_HANDFREEIN_INITFAIL,"hand free in device config error,please check");
- return Error_AudioIN;
- }
- int in_dev_id = translate_id(TRUE, nId);
- if (in_dev_id < 0) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("audio in device translate failed!");
- return Error_AudioIN;
- }
- const PaDeviceInfo* in_info = Pa_GetDeviceInfo(in_dev_id);
- if (!in_info) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get device info failed!");
- return Error_AudioIN;
- }
- else
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[%d] in dev name is %s, defaultSampleRate = %f", in_dev_id, in_info->name, in_info->defaultSampleRate);
- }
- inParam.channelCount = 1;
- audio_cap->dev_channels = 1;
- inParam.device = in_dev_id;
- inParam.suggestedLatency = in_info->defaultLowInputLatency;
- inParam.sampleFormat = paInt16;
- audio_cap->dev_bitspersample = 16;
- inParam.hostApiSpecificStreamInfo = NULL;
- audio_cap->dev_samplespersec = in_info->defaultSampleRate;
- double inSampleRate = in_info->defaultSampleRate;
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("audio input device name is %s.", in_info->name);
- paError = Pa_IsFormatSupported(&inParam, NULL, inSampleRate);
- if (paNoError != paError) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[%d] in dev %s audio capture create error[%d], cannot open audio input device", in_dev_id, in_info->name, paError);
- return Error_AudioIN;
- }
- else
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[%d] in dev %s audio capture create success, sampleFormat = %d,inSampleRate = %f.", in_dev_id, in_info->name, inParam.sampleFormat, inSampleRate);
- }
- nId = capture_get_audio_device_id(false, cap->config.strAudioOut);
- if (nId == -1)
- {
- //需要立即处理的告警使用Severity_High
- LogWarn(Severity_Low, Error_Debug, ERROR_MOD_MEDIACONTROLLER_HANDFREEOUT_INITFAIL, "hand free out device config error,please check");
- return Error_AudioOut;
- }
- int out_dev_id = translate_id(FALSE, nId);
- if (out_dev_id < 0) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("audio out device translate failed!");
- return Error_AudioOut;
- }
- const PaDeviceInfo* out_info = Pa_GetDeviceInfo(out_dev_id);
- if (!out_info) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get output device info failed!");
- return Error_AudioOut;
- }
- outParam.channelCount = 1;
- outParam.device = out_dev_id;
- outParam.suggestedLatency = out_info->defaultLowOutputLatency;
- outParam.sampleFormat = paInt16;
- outParam.hostApiSpecificStreamInfo = NULL;
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("audio output device name is %s.", in_info->name);
- if (Pa_IsFormatSupported(NULL, &outParam, out_info->defaultSampleRate) != paNoError) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("audio capture create error, cannot open output audio device.");
- return Error_AudioOut;
- }
- //打开流设备,可以用以下代码替换paError = Pa_OpenStream(&audio_cap->stream, &inParam, &outParam, CAPTURE_CLOCK,
- //CAPTURE_FRAME_TIME * CAPTURE_CLOCK/1000, paClipOff|paDitherOff, &StreamCallback, audio_cap);
- paError = Pa_OpenStream(&audio_cap->stream, &inParam, NULL, inSampleRate,
- CAPTURE_FRAME_TIME * inSampleRate / 1000, paClipOff | paDitherOff, &StreamCallback, audio_cap);
- if (paNoError != paError) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[%d] in dev %s port audio open stream failed! paError = %d", in_dev_id, in_info->name, paError);
- return Error_AudioIN;
- }
- else
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[%d] in dev %s port audio open stream success, sampleFormat = %d,inSampleRate = %f.", in_dev_id, in_info->name, inParam.sampleFormat, inSampleRate);
- }
- paError = Pa_StartStream(audio_cap->stream);
- if (paNoError != paError) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[%d] in dev %s port audio start stream failed! paError = %d", in_dev_id, in_info->name, paError);
- return Error_AudioIN;
- }
- else
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[%d] in dev %s port audio start stream success, sampleFormat = %d,inSampleRate = %f.", in_dev_id, in_info->name, inParam.sampleFormat, inSampleRate);
- }
- return Error_Succeed;
- }
- static void portaudio_capture_stop(audio_capture_t* audio_cap)
- {
- if (audio_cap->stream) {
- Pa_AbortStream(audio_cap->stream);
- Pa_CloseStream(audio_cap->stream);
- audio_cap->stream = NULL;
- }
- }
- static int record_portaudio_capture_start(rvc_audio_capture_t* audio_cap)
- {
- rvc_sales_audio_capture_t* cap = audio_cap->parent;
- PaStreamParameters salesInParam = { 0 };
- PaError paError;
- const PaDeviceInfo* info;
- int nId = capture_get_audio_device_id(true, cap->rvc_audio_config.strAudioIn);
- if (nId == -1)
- {
- //需要立即处理的告警使用Severity_High
- LogWarn(Severity_Middle, Error_Debug,ERROR_MOD_MEDIACONTROLLER_HANDFREEIN_INITFAIL,"hand free in device config error,please check");
- return Error_AudioIN;
- }
- int in_dev_id = translate_id(TRUE, nId);
- if (in_dev_id < 0) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("sales audio in device translate failed!");
- return Error_AudioIN;
- }
- info = Pa_GetDeviceInfo(in_dev_id);
- if (!info) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get device info failed!");
- return Error_AudioIN;
- }
- else
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[%d] in dev name is %s, defaultSampleRate = %f", in_dev_id, info->name, info->defaultSampleRate);
- }
- salesInParam.channelCount = 1;
- audio_cap->dev_channels = 1;
- salesInParam.device = in_dev_id;
- salesInParam.suggestedLatency = info->defaultLowInputLatency;
- salesInParam.sampleFormat = paInt16;
- audio_cap->dev_bitspersample = 16;
- salesInParam.hostApiSpecificStreamInfo = NULL;
- audio_cap->dev_samplespersec = info->defaultSampleRate;
- if (Pa_IsFormatSupported(&salesInParam, NULL, info->defaultSampleRate) != paNoError) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("sales audio capture create error, cannot open audio input device, and current capture sample rate is %d.", info->defaultSampleRate);
- return Error_AudioIN;
- }
- paError = Pa_OpenStream(&audio_cap->stream, &salesInParam, NULL, info->defaultSampleRate,
- audio_cap->iaudio_capture_peroid * info->defaultSampleRate / 1000, paClipOff | paDitherOff, &Sales_StreamCallback, audio_cap);
- if (paError != paNoError) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("port audio open sales stream failed! paError = %d", paError);
- return Error_AudioIN;
- }
- paError = Pa_StartStream(audio_cap->stream);
- if (paError != paNoError) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("port audio start sales stream failed! paError = %d", paError);
- return Error_AudioIN;
- }
- return Error_Succeed;
- }
- static void record_portaudio_capture_stop(rvc_audio_capture_t* audio_cap)
- {
- if (NULL != audio_cap) {
- if (audio_cap->stream) {
- PaError Error = Pa_AbortStream(audio_cap->stream);
- if (paNoError == Error) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Pa_AbortStream no error.");
- }
- Error = Pa_CloseStream(audio_cap->stream);
- if (paNoError == Error) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Pa_CloseStream no error.");
- }
- audio_cap->stream = NULL;
- }
- audio_cap->iseriesnumber = 0;
- }
- }
- #else
- static void audio_data_callback(const void* input, unsigned long audiolen, void* userdata)
- {
- audio_capture_t* audio_cap = (audio_capture_t*)userdata;
- if (input) {
- int ileft = audiolen;
- int index = 0;
- while (audio_cap->uaudiolen + ileft >= RVC_AUDIO_BUFFER_LEN)
- {
- int ineed = RVC_AUDIO_BUFFER_LEN - audio_cap->uaudiolen;
- memcpy(audio_cap->paudio_buffer + audio_cap->uaudiolen, input + index, ineed);
- ileft -= ineed;
- audio_cap->uaudiolen += ineed;
- index += ineed;
- audio_frame frm;
- frm.bitspersample = 16;
- frm.format = 1;
- frm.data = audio_cap->paudio_buffer;
- frm.framesize = RVC_AUDIO_BUFFER_LEN;
- frm.nchannels = 1;
- frm.samplespersec = 8000;
- frm.iseriesnumber = audio_cap->iseriesnumber++;
- if (audio_cap->iseriesnumber >= INT_MAX) {
- audio_cap->iseriesnumber = 0;
- }
- if (!audio_cap->shm_queue->InsertAudio(&frm)) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d InsertAudio to shm_queue failed!", __FUNCTION__, __LINE__);
- }
- //else {
- // DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d InsertAudio to shm_queue success! and framesize is :%d, and shm_queue length is %d, frm.iseriesnumber = %d.", __FUNCTION__, __LINE__, frm.framesize, audio_cap->shm_queue->GetAudioLens(), frm.iseriesnumber);
- //}
- audio_cap->uaudiolen -= RVC_AUDIO_BUFFER_LEN;
- }
- if (ileft > 0){
- memcpy(audio_cap->paudio_buffer + audio_cap->uaudiolen, input + index, ileft);
- audio_cap->uaudiolen += ileft;
- }
- }
- return;
- }
- static int pulseaudio_capture_start(audio_capture_t* audio_cap)
- {
- int iret = -1;
- capture_t* cap = audio_cap->parent;
-
- if (NULL != audio_cap->paudiocap){
- audiocap_param_t param = { 0 };
- int nId = audio_cap->paudiocap->audio_get_device_id(cap->config.strAudioIn.GetData(), true);
- if (nId == -1)
- {
- //需要立即处理的告警使用Severity_High
- LogWarn(Severity_Low, Error_Debug, ERROR_MOD_MEDIACONTROLLER_HANDFREEIN_INITFAIL, "hand free in device config error,please check");
- return Error_AudioIN;
- }
- param.ideviceid = nId;
- param.ichannels = 1;
- param.isampleformat = 3 /*PA_SAMPLE_S16LE*/;
- param.isamprate = 8000;
- param.flatency = 0.02;
- param.on_audio_callback = &audio_data_callback;
- param.user_data = audio_cap;
- audio_cap->paudiocap->set_audio_capture_params(¶m);
- iret = audio_cap->paudiocap->start_audio_capture();
- }
- return iret;
- }
- static void pulseaudio_capture_stop(audio_capture_t* audio_cap)
- {
- if (audio_cap->paudiocap) {
- audio_cap->paudiocap->stop_audio_capture();
- }
- }
- static void record_audio_data_callback(const void* input, unsigned long audiolen, void* userdata)
- {
- rvc_audio_capture_t* audio_cap = (rvc_audio_capture_t*)userdata;
- if (input) {
- int ileft = audiolen;
- int index = 0;
- while (audio_cap->uaudiolen + ileft >= RVC_AUDIO_BUFFER_LEN)
- {
- int ineed = RVC_AUDIO_BUFFER_LEN - audio_cap->uaudiolen;
- memcpy(audio_cap->paudio_buffer + audio_cap->uaudiolen, input + index, ineed);
- ileft -= ineed;
- audio_cap->uaudiolen += ineed;
- index += ineed;
- audio_frame frm;
- frm.bitspersample = 16;
- frm.format = 1;
- frm.data = audio_cap->paudio_buffer;
- frm.framesize = RVC_AUDIO_BUFFER_LEN;
- frm.nchannels = 1;
- frm.samplespersec = 8000;
- frm.iseriesnumber = audio_cap->iseriesnumber++;
- if (audio_cap->iseriesnumber >= INT_MAX) {
- audio_cap->iseriesnumber = 0;
- }
- if (!audio_cap->audio_shm_queue->InsertAudio(&frm)) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d InsertAudio to audio_shm_queue failed!", __FUNCTION__, __LINE__);
- }
- else {
- //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d InsertAudio to audio_shm_queue success!", __FUNCTION__, __LINE__);
- }
-
- if (eSingleWriteLocal == audio_cap->eType) {
- fwrite(frm.data, frm.framesize, 1, (FILE*)(audio_cap->pdata));
- }
- audio_cap->uaudiolen -= RVC_AUDIO_BUFFER_LEN;
- }
- if (ileft > 0) {
- memcpy(audio_cap->paudio_buffer + audio_cap->uaudiolen, input + index, ileft);
- audio_cap->uaudiolen += ileft;
- }
- }
- return;
- }
- static int record_pulseaudio_capture_start(rvc_audio_capture_t* audio_cap)
- {
- LOG_FUNCTION();
-
- int iret = -1;
- rvc_sales_audio_capture_t* cap = audio_cap->parent;
- if (NULL != audio_cap->paudiocap) {
- audiocap_param_t param = { 0 };
- int nId = audio_cap->paudiocap->audio_get_device_id(cap->rvc_audio_config.strAudioIn.GetData(), true);
- if (nId == -1){
- //需要立即处理的告警使用Severity_High
- LogWarn(Severity_Low, Error_Debug, ERROR_MOD_MEDIACONTROLLER_HANDFREEIN_INITFAIL, "hand free in device config error,please check");
- return Error_AudioIN;
- }
- param.ideviceid = nId;
- param.ichannels = 1;
- param.isampleformat = 3 /*PA_SAMPLE_S16LE*/;
- param.isamprate = 8000;
- param.flatency = 0.02;
- param.on_audio_callback = &record_audio_data_callback;
- param.user_data = audio_cap;
- audio_cap->paudiocap->set_audio_capture_params(¶m);
- iret = audio_cap->paudiocap->start_audio_capture();
- }
- return iret;
- }
- static void record_pulseaudio_capture_stop(rvc_audio_capture_t* audio_cap)
- {
- if (audio_cap->paudiocap) {
- audio_cap->paudiocap->stop_audio_capture();
- }
- }
- #endif
- static rvc_audio_capture_t *salesrecord_audio_capture_create(rvc_sales_audio_capture_t *cap)
- {
- rvc_audio_capture_t *audio_cap = ZALLOC_T(rvc_audio_capture_t);
- if (audio_cap) {
- audio_cap->parent = cap;
- audio_cap->audio_shm_queue = new Clibaudioqueue(REC_COMMON_AUDIO_SALES_SHM_QUEUE);
- //audio_cap->remote_audio_queue = new Clibaudioqueue(REC_COMMON_REMOTEAUDIO_SHM_QUEUE);
- audio_cap->iseriesnumber = 0;
- audio_cap->eType = eUnKnown;
- audio_cap->pdata = NULL;
- }
- #ifdef RVC_OS_LINUX
- audio_cap->paudio_buffer = new char[RVC_AUDIO_BUFFER_LEN];
- audio_cap->uaudiolen = 0;
- #endif
- return audio_cap;
- }
- static void salesrecord_audio_capture_destroy(rvc_audio_capture_t *audio_cap)
- {
- if (NULL != audio_cap){
- if (NULL != audio_cap->audio_shm_queue){
- delete audio_cap->audio_shm_queue;
- audio_cap->audio_shm_queue = NULL;
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("set audio_cap audio_shm_queue null");
- }
- #ifdef RVC_OS_LINUX
- if (NULL != audio_cap->paudio_buffer){
- delete audio_cap->paudio_buffer;
- audio_cap->paudio_buffer = NULL;
- audio_cap->uaudiolen = 0;
- }
- #endif
- free(audio_cap);
- audio_cap = NULL;
- }
- }
- static audio_capture_t *audio_capture_create(capture_t *cap)
- {
- audio_capture_t *audio_cap = ZALLOC_T(audio_capture_t);
- if (audio_cap) {
- audio_cap->parent = cap;
- audio_cap->shm_queue = new Clibaudioqueue(REC_COMMON_AUDIO_SHM_QUEUE);
- audio_cap->salesol_shm_queue = new Clibaudioqueue(REC_COMMON_AUDIO_SALESOL_SHM_QUEUE);
- #ifdef RVC_OS_LINUX
- audio_cap->paudio_buffer = new char[RVC_AUDIO_BUFFER_LEN];
- audio_cap->uaudiolen = 0;
- #endif
- }
- return audio_cap;
- }
- static void audio_capture_destroy(audio_capture_t *audio_cap)
- {
- delete audio_cap->shm_queue;
- delete audio_cap->salesol_shm_queue;
- free(audio_cap);
- #ifdef RVC_OS_LINUX
- delete audio_cap->paudio_buffer;
- audio_cap->paudio_buffer = NULL;
- audio_cap->uaudiolen = 0;
- #endif
- }
- static int audio_capture_start(audio_capture_t* audio_cap)
- {
- #ifdef RVC_OS_WIN
- return portaudio_capture_start(audio_cap);
- #else
- return pulseaudio_capture_start(audio_cap);
- #endif
- }
- static void audio_capture_stop(audio_capture_t* audio_cap)
- {
- #ifdef RVC_OS_WIN
- return portaudio_capture_stop(audio_cap);
- #else
- return pulseaudio_capture_stop(audio_cap);
- #endif
- }
- static int record_audio_capture_start(rvc_audio_capture_t* audio_cap)
- {
- #ifdef RVC_OS_WIN
- return record_portaudio_capture_start(audio_cap);
- #else
- return record_pulseaudio_capture_start(audio_cap);
- #endif
- }
- static void record_audio_capture_stop(rvc_audio_capture_t* audio_cap)
- {
- #ifdef RVC_OS_WIN
- return record_portaudio_capture_stop(audio_cap);
- #else
- return record_pulseaudio_capture_stop(audio_cap);
- #endif
- }
- static int calc_capture_mode(int width, int height, int *mode)
- {
- const struct {
- int mode;
- int width;
- int height;
- } modes [] = {
- {VIDEOCAP_FRAME_SQCIF, VIDEOCAP_SQCIF_WIDTH, VIDEOCAP_SQCIF_HEIGHT},
- {VIDEOCAP_FRAME_QQVGA, VIDEOCAP_QQVGA_WIDTH, VIDEOCAP_QQVGA_HEIGHT},
- {VIDEOCAP_FRAME_QCIF, VIDEOCAP_QCIF_WIDTH, VIDEOCAP_QCIF_HEIGHT},
- {VIDEOCAP_FRAME_QVGA, VIDEOCAP_QVGA_WIDTH, VIDEOCAP_QVGA_HEIGHT},
- {VIDEOCAP_FRAME_CIF, VIDEOCAP_CIF_WIDTH, VIDEOCAP_CIF_HEIGHT},
- {VIDEOCAP_FRAME_VGA, VIDEOCAP_VGA_WIDTH, VIDEOCAP_VGA_HEIGHT},
- {VIDEOCAP_FRAME_4CIF, VIDEOCAP_4CIF_WIDTH, VIDEOCAP_4CIF_HEIGHT},
- {VIDEOCAP_FRAME_SVGA, VIDEOCAP_SVGA_WIDTH, VIDEOCAP_SVGA_HEIGHT},
- {VIDEOCAP_FRAME_NHD, VIDEOCAP_NHD_WIDTH, VIDEOCAP_NHD_HEIGHT},
- {VIDEOCAP_FRAME_SXGA, VIDEOCAP_SXGA_WIDTH, VIDEOCAP_SXGA_HEIGHT},
- {VIDEOCAP_FRAME_720P, VIDEOCAP_720P_WIDTH, VIDEOCAP_720P_HEIGHT},
- {VIDEOCAP_FRAME_1080P, VIDEOCAP_1080P_WIDTH, VIDEOCAP_1080P_HEIGHT},
- };
- int i;
- for (i = 0; i < array_size(modes); ++i) {
- if (modes[i].width == width && modes[i].height == height) {
- *mode = modes[i].mode;
- return 0;
- }
- }
- return Error_NotExist;
- }
- static int video_shm_enqueue(Clibvideoqueue *shm_queue, video_frame *frame, int flags, int iframeid)
- {
- videoq_frame tmp_frm;
- tmp_frm.data = frame->data[0];
- if (VIDEO_FORMAT_RGB24 == frame->format){
- tmp_frm.framesize = frame->width * frame->height * 3;
- tmp_frm.format = VIDEOQ_FORMAT_RGB24;
- }
- else {
- tmp_frm.framesize = frame->width * frame->height * 3/2;
- tmp_frm.format = VIDEOQ_FORMAT_I420;
- }
- tmp_frm.width = frame->width;
- tmp_frm.height = frame->height;
- tmp_frm.iframeid = iframeid;
- unsigned int nowtime = y2k_time_now();
- if (!shm_queue->InsertVideo(&tmp_frm, flags,nowtime)) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("caution: shm_queue video insert shm video failed!");
- return Error_Unexpect;
- } else {
- //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("shm_queue video insert shm video ok!");
- return Error_Succeed;
- }
- }
- //static int env_cap_on_frame_i420(void* user_data, video_frame* frame)
- //{
- // video_capture_t* video_cap = (video_capture_t*)user_data;
- // capture_t* cap = video_cap->parent;
- // return video_shm_enqueue(video_cap->render_shm_queue, frame, VIDEOQUEUE_FLAG_VERTICAL_FLIP);
- //}
- //
- //
- //static int opt_cap_on_frame_i420(void* user_data, video_frame* frame)
- //{
- // video_capture_t* video_cap = (video_capture_t*)user_data;
- // capture_t* cap = video_cap->parent;
- // return video_shm_enqueue(video_cap->opt_render_shm_queue, frame, VIDEOQUEUE_FLAG_VERTICAL_FLIP);
- //}
- static void env_cap_on_frame(void *user_data, video_frame *frame)
- {
- video_capture_t *video_cap = (video_capture_t *)user_data;
- capture_t *cap = video_cap->parent;
- int rc;
- video_cap->frame_id++;
- //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start env on frame, id=%d, tick=%d", video_cap->frame_id, GetTickCount());;
- //IplImage* img = NULL;
- //img = cvCreateImage(cvSize(frame->width, frame->height), IPL_DEPTH_8U, 3);
- //img->imageData = (char*)frame->data[0];
- //cvSaveImage("./env.jpg", img, 0);
- //cvReleaseImageHeader(&img);
- rc = video_shm_enqueue(video_cap->snapshot_shm_queue, frame, VIDEOQUEUE_FLAG_VERTICAL_FLIP, video_cap->frame_id);
- //static int isnapshot_shm_queue = 0;
- //if (isnapshot_shm_queue == 0) {
- // video_frame_save_bmpfile("snapshot_shm_queue_1.bmp", frame);
- // isnapshot_shm_queue++;
- //}
- if (rc != Error_Succeed)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("env snapshot queue enqueue failed! Error = %d, camera_type=%d.", rc, video_cap->camera_type);
- }
- else {
- //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("env snapshot queue enqueue[%d] success and camera_type=%d.", video_cap->frame_id, video_cap->camera_type);
- }
- // snapshot
- if (rc==Error_Succeed)
- {
- if (*cap->config.ref_env_capture_count)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("env camera ref_env_capture_count=%d", *cap->config.ref_env_capture_count);
- #ifdef RVC_OS_WIN
- InterlockedDecrement(cap->config.ref_env_capture_count);
- #else
- //__sync_fetch_and_sub(cap->config.ref_env_capture_count, 1);
- pthread_mutex_lock(cap->config.env_mutex);
- *(cap->config.ref_env_capture_count) -= 1;
- pthread_mutex_unlock(cap->config.env_mutex);
- #endif
- LogEvent(Severity_Middle, MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_ENV, "agent capture env ok, and capture env finished!");
- }
- else if (*cap->config.ref_envopt_capture_count & 2)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("env camera ref_envopt_capture_count=%d", *cap->config.ref_envopt_capture_count);
- #ifdef RVC_OS_WIN
- _InterlockedAnd(cap->config.ref_envopt_capture_count, 0xfffffffD);
- #else
- //__sync_fetch_and_add(cap->config.ref_env_capture_count, 0xfffffffD);
- pthread_mutex_lock(cap->config.envopt_mutex);
- LONG lcount = *(cap->config.ref_envopt_capture_count);
- *(cap->config.ref_envopt_capture_count) = (lcount & 0xfffffffD);
- pthread_mutex_unlock(cap->config.envopt_mutex);
- #endif // RVC_OS_WIN
- if (*cap->config.ref_envopt_capture_count == 0)
- {
- LogEvent(Severity_Middle, MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_ENVOPT, "agent capture env ok, and capture env opt finished!");
- }
- }
- }
- // preview
- {
- video_frame preview_frame;
-
- video_frame_alloc(REC_COMMON_VIDEO_PREVIEW_WIDTH, REC_COMMON_VIDEO_PREVIEW_HEIGHT, VIDEO_FORMAT_RGB24, &preview_frame);
- uint8_t *src_data[4] = {frame->data[0] + 80*3, 0, 0, 0};
- sws_scale(video_cap->preview_sws_ctx, src_data, frame->linesize, 0, frame->height, preview_frame.data, preview_frame.linesize);
- rc= video_shm_enqueue(video_cap->preview_shm_queue, &preview_frame, 0, video_cap->frame_id);
-
- if (rc != Error_Succeed){
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("env preview_shm_queue enqueue failed, error = %d", rc);
- }
- else {
- //if (0 == video_cap->frame_id%10){
- //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("env preview_shm_queue enqueue[%d] success, and current video queue len is %d.", video_cap->frame_id, video_cap->preview_shm_queue->GetVideoLens());
- //}
- }
- video_frame_free(&preview_frame);
- //static int ipreview_shm_queue = 0;
- //if (ipreview_shm_queue == 0) {
- // video_frame_save_bmpfile("preview_shm_queue_1.bmp", frame);
- // ipreview_shm_queue++;
- //}
- }
- // rtp
- {
- video_frame rtp_frame;
- video_frame_alloc(REC_COMMON_VIDEO_RTP_ENV_WIDTH, REC_COMMON_VIDEO_RTP_ENV_HEIGHT, VIDEO_FORMAT_RGB24, &rtp_frame);
- uint8_t *src_data[4] = {frame->data[0] + (frame->height-1) * frame->linesize[0], 0, 0, 0};
- int src_linesize[4] = {-frame->linesize[0], 0, 0, 0};
- sws_scale(video_cap->rtp_sws_ctx, src_data, src_linesize, 0, frame->height, rtp_frame.data, rtp_frame.linesize);
- rc = video_shm_enqueue(video_cap->rtp_shm_queue, &rtp_frame, 0, video_cap->frame_id);
- if (rc != Error_Succeed) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("env rtp_shm_queue enqueue failed! Error = %d", rc);
- }
- else {
- //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("env rtp_shm_queue enqueue[%d] success, and current video queue len is %d.", video_cap->frame_id, video_cap->rtp_shm_queue->GetVideoLens());
- }
- //static int irtp_frame = 0;
- //if (irtp_frame == 0){
- // video_frame_save_bmpfile("rtp_shm_queue.bmp", &rtp_frame);
- // irtp_frame++;
- //}
-
- #if 0
- static int i = 0;
- if (i == 0) {
- video_frame tmp_frame;
- video_frame_alloc(REC_COMMON_VIDEO_RTP_ENV_WIDTH, REC_COMMON_VIDEO_RTP_ENV_HEIGHT, VIDEO_FORMAT_RGB24, &tmp_frame);
- video_frame_fill_black(&tmp_frame);
- videoq_frame frm;
- frm.data = tmp_frame.data[0];
- video_cap->rtp_shm_queue->GetVideo(&frm, 0);
- video_frame_save_bmpfile("rtp_videoqueue_abc.bmp", &tmp_frame);
- video_frame_free(&tmp_frame);
- //video_frame_save_bmpfile("d:\\ab.bmp", &rtp_frame);
- i++;
- }
- #endif
- video_frame_free(&rtp_frame);
- }
- //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("end env on frame, id=%d, tick=%d", video_cap->frame_id, GetTickCount());;
- }
- static void opt_cap_on_frame(void *user_data, video_frame *frame)
- {
- video_capture_t *video_cap = (video_capture_t *)user_data;
- capture_t *cap = video_cap->parent;
- int rc;
-
- int rotate = 0;
- //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("opt on frame!");
- if (cap->config.video_opt_rotate == 90) {
- rotate = 1;
- } else if (cap->config.video_opt_rotate == 270) {
- rotate = -1;
- } else {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("video_opt_rotate is not 90 or 270 return.");
- return;
- }
- video_cap->frame_id++;
- #ifdef RVC_OS_WIN // use IPP, it's fast
- // prepare for rotation
- video_frame rframe;
- video_frame_alloc(frame->height, frame->width, frame->format, &rframe);
- {
- IppiSize srcSize;
- srcSize.width = frame->width;
- srcSize.height = frame->height;
- IppiRect srcROI;
- srcROI.width = frame->width;
- srcROI.height = frame->height;
- srcROI.x = 0;
- srcROI.y = 0;
- IppiRect dstROI;
- dstROI.width = frame->height;
- dstROI.height = frame->width;
- dstROI.x = 0;
- dstROI.y = 0;
- ippiTranspose_8u_C3R(frame->data[0], frame->linesize[0], rframe.data[0], rframe.linesize[0], srcSize);
- IppiAxis flip;
- if (rotate == 1)
- {
- flip = ippAxsVertical;
- ippiMirror_8u_C3IR(rframe.data[0], rframe.linesize[0], srcSize, flip);
- }
- else
- {
- IppiSize Size;
- Size.width = rframe.width;
- Size.height = rframe.height;
- //flip = ippAxsHorizontal;
- flip = ippAxsBoth;
- ippiMirror_8u_C3IR(rframe.data[0], rframe.linesize[0], Size, flip);
- }
- }
- rc = video_shm_enqueue(video_cap->snapshot_shm_queue, &rframe, 0, video_cap->frame_id);
- #else
- rc = video_shm_enqueue(video_cap->snapshot_shm_queue, frame, 0, video_cap->frame_id);
- #endif
- if (rc != Error_Succeed)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("opt snapshot queue enqueue shm failed! Error = %d, camera_type=%d", rc, video_cap->camera_type);
- }
- else {
- //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("opt snapshot queue enqueue[%d] success, and current video queue len is %d.", video_cap->frame_id, video_cap->snapshot_shm_queue->GetVideoLens());
- }
- //static int isnapshot_shm_queue = 0;
- //if (isnapshot_shm_queue == 0) {
- // video_frame_save_bmpfile("opt_snapshot_shm_queue.bmp", &rframe);
- // isnapshot_shm_queue++;
- //}
- // snapshot
- if (rc==Error_Succeed)
- {
- if (*cap->config.ref_opt_capture_count)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("opt camera ref_opt_capture_count=%d",*cap->config.ref_opt_capture_count);
- #ifdef RVC_OS_WIN
- InterlockedDecrement(cap->config.ref_opt_capture_count);
- #else
- //__sync_fetch_and_sub(cap->config.ref_opt_capture_count, 1);
- pthread_mutex_lock(cap->config.opt_mutex);
- *(cap->config.ref_opt_capture_count) -= 1;
- pthread_mutex_unlock(cap->config.opt_mutex);
- #endif
- LogEvent(Severity_Middle, MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_OPT, "agent capture opt ok, and capture opt finished!");
- }
- else if (*cap->config.ref_envopt_capture_count&1)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("opt camera ref_envopt_capture_count=%d", *cap->config.ref_envopt_capture_count);
- #ifdef RVC_OS_WIN
- if (InterlockedDecrement(cap->config.ref_envopt_capture_count) == 0)
- #else
- //__sync_fetch_and_sub(cap->config.ref_envopt_capture_count,1);
- pthread_mutex_lock(cap->config.envopt_mutex);
- *(cap->config.ref_envopt_capture_count) -= 1;
- pthread_mutex_unlock(cap->config.envopt_mutex);
- if (0 == *cap->config.ref_envopt_capture_count)
- #endif
- {
- LogEvent(Severity_Middle, MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_ENVOPT, "agent capture opt ok, and capture envopt finished!");
- }
- }
- }
- // rtp
- {
- video_frame rtp_frame = {0};
- video_frame_alloc(REC_COMMON_VIDEO_RTP_OPT_WIDTH, REC_COMMON_VIDEO_RTP_OPT_HEIGHT, VIDEO_FORMAT_RGB24, &rtp_frame);
- #ifdef RVC_OS_WIN
- sws_scale(video_cap->rtp_sws_ctx, rframe.data, rframe.linesize, 0, rframe.height, rtp_frame.data, rtp_frame.linesize);
- #else
- sws_scale(video_cap->rtp_sws_ctx, frame->data, frame->linesize, 0, frame->height, rtp_frame.data, rtp_frame.linesize);
- #endif
-
- rc = video_shm_enqueue(video_cap->rtp_shm_queue, &rtp_frame, 0, video_cap->frame_id);
- if (rc != Error_Succeed)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("opt rtp_shm_queue enqueue shm failed! Error = %d, camera_type=%d", rc, video_cap->camera_type);
- }
- else {
- //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("opt rtp_shm_queue enqueue[%d] success, and current video queue len is %d.", video_cap->frame_id, video_cap->rtp_shm_queue->GetVideoLens());
- }
- //static int irtp_frame = 0;
- //if (irtp_frame == 0){
- // video_frame_save_bmpfile("opt_rtp_shm_queue.bmp", &rtp_frame);
- // irtp_frame++;
- //}
- video_frame_free(&rtp_frame);
- // preview
- {
- video_frame preview_frame;
- video_frame_alloc(REC_COMMON_VIDEO_PREVIEW_HEIGHT, REC_COMMON_VIDEO_PREVIEW_WIDTH, VIDEO_FORMAT_RGB24, &preview_frame);
- uint8_t* src_data[4] = { frame->data[0] + frame->linesize[0] * 80, 0, 0, 0 };
- sws_scale(video_cap->preview_sws_ctx, src_data, frame->linesize, 0, REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_WIDTH, preview_frame.data, preview_frame.linesize);
- video_shm_enqueue(video_cap->preview_shm_queue, &preview_frame, 1, video_cap->frame_id);
-
- //static int ipreview_shm_queue = 0;
- //char strname[MAX_PATH] = { 0 };
- //snprintf(strname, MAX_PATH, "opt_preview_shm_queue_%d.bmp", ipreview_shm_queue++);
- //video_frame_save_bmpfile(strname, &preview_frame);
- video_frame_free(&preview_frame);
- }
- }
- #ifdef RVC_OS_WIN
- video_frame_free(&rframe);
- #endif
- }
- static video_capture_t *video_capture_create(capture_t *cap, int camera_type)
- {
- video_capture_t *video_cap = ZALLOC_T(video_capture_t);
- if (video_cap) {
- video_cap->parent = cap;
- video_cap->camera_type = camera_type;
- video_cap->frame_id = 0;
-
- if (camera_type == CAMERA_TYPE_ENV) {
- //video_cap->render_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_RENDER_QUEUE);
- //video_cap->opt_render_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_RENDER_QUEUE);
- video_cap->snapshot_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_SNAPSHOT_QUEUE);
- video_cap->rtp_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_RTP_QUEUE);
- video_cap->rtp_sws_ctx = sws_getContext(REC_COMMON_VIDEO_SNAPSHOT_WIDTH,
- REC_COMMON_VIDEO_SNAPSHOT_HEIGHT,
- AV_PIX_FMT_BGR24,
- REC_COMMON_VIDEO_RTP_ENV_WIDTH,
- REC_COMMON_VIDEO_RTP_ENV_HEIGHT,
- AV_PIX_FMT_BGR24,
- SWS_POINT, NULL, NULL, NULL);
- video_cap->preview_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_PREVIEW_QUEUE);
- video_cap->preview_sws_ctx = sws_getContext(REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_WIDTH,
- REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_HEIGHT,
- AV_PIX_FMT_BGR24,
- REC_COMMON_VIDEO_PREVIEW_WIDTH,
- REC_COMMON_VIDEO_PREVIEW_HEIGHT,
- AV_PIX_FMT_BGR24,
- SWS_FAST_BILINEAR, NULL, NULL, NULL);
- } else {
- //video_cap->render_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_RENDER_QUEUE);
- //video_cap->opt_render_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_RENDER_QUEUE);
- video_cap->snapshot_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_SNAPSHOT_QUEUE);
- video_cap->rtp_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_RTP_QUEUE);
- video_cap->rtp_sws_ctx = sws_getContext(REC_COMMON_VIDEO_SNAPSHOT_HEIGHT,
- REC_COMMON_VIDEO_SNAPSHOT_WIDTH,
- AV_PIX_FMT_BGR24,
- REC_COMMON_VIDEO_RTP_OPT_WIDTH,
- REC_COMMON_VIDEO_RTP_OPT_HEIGHT,
- AV_PIX_FMT_BGR24,
- SWS_POINT, NULL, NULL, NULL);
- video_cap->preview_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_PREVIEW_QUEUE);
- video_cap->preview_sws_ctx = sws_getContext(REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_HEIGHT,
- REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_WIDTH,
- AV_PIX_FMT_BGR24,
- REC_COMMON_VIDEO_PREVIEW_HEIGHT,
- REC_COMMON_VIDEO_PREVIEW_WIDTH,
- AV_PIX_FMT_BGR24,
- SWS_FAST_BILINEAR, NULL, NULL, NULL);
- //video_cap->preview_shm_queue = NULL;
- //video_cap->preview_sws_ctx = NULL;
- }
- }
- return video_cap;
- }
- static void video_capture_destroy(video_capture_t *video_cap)
- {
- if (video_cap) {
- if (video_cap->preview_sws_ctx) {
- sws_freeContext(video_cap->preview_sws_ctx);
- video_cap->preview_sws_ctx = NULL;
- }
- if (video_cap->rtp_sws_ctx) {
- sws_freeContext(video_cap->rtp_sws_ctx);
- video_cap->rtp_sws_ctx = NULL;
- }
- if (video_cap->snapshot_shm_queue) {
- delete video_cap->snapshot_shm_queue;
- video_cap->snapshot_shm_queue = NULL;
- }
- if (video_cap->rtp_shm_queue) {
- delete video_cap->rtp_shm_queue;
- video_cap->rtp_shm_queue = NULL;
- }
- if (video_cap->preview_shm_queue) {
- delete video_cap->preview_shm_queue;
- video_cap->preview_shm_queue = NULL;
- }
- //if (video_cap->render_shm_queue) {
- // delete video_cap->render_shm_queue;
- // video_cap->render_shm_queue = NULL;
- //}
- //if (video_cap->opt_render_shm_queue) {
- // delete video_cap->opt_render_shm_queue;
- // video_cap->opt_render_shm_queue = NULL;
- //}
- free(video_cap);
- }
- }
- static int get_video_capture_fps(int icapfps)
- {
- int ifps = REC_COMMON_VIDEO_PADRAW_FPS;
- if ((ePadtype == g_eDeviceType) || (eMobilePadType == g_eDeviceType) || (eDesk2SType == g_eDeviceType)
- || (eDesk1SType == g_eDeviceType) || (eDesk2SIntegratedType == g_eDeviceType)){
- ifps = REC_COMMON_VIDEO_PADRAW_FPS;
- }
- else{
- ifps = REC_COMMON_VIDEO_RAW_FPS;
- }
- if (icapfps <=30 && icapfps >=5){
- ifps = icapfps;
- }
- return ifps;
- }
- #ifdef RVC_OS_WIN
- static int video_capture_start_win(video_capture_t* video_cap)
- {
- capture_config_t* conf = &video_cap->parent->config;
- int dev_id;
- if (video_cap->camera_type == CAMERA_TYPE_ENV)
- {
- dev_id = capture_get_video_device_id(conf->strVideoEnv);
- if (dev_id == -1)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("No environment camera,please check config file or device!");
- return -1;
- }
- }
- else
- {
- dev_id = capture_get_video_device_id(conf->strVideoOpt);
- if (dev_id == -1)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("No operation camera,please check config file or device!");
- return -1;
- }
- }
- videocap_param param = { 0 };
- int cap_mode;
- int rc = -1;
- rc = calc_capture_mode(REC_COMMON_VIDEO_SNAPSHOT_WIDTH, REC_COMMON_VIDEO_SNAPSHOT_HEIGHT, &cap_mode);
- if (rc != 0)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("calc cap_mode failed!");
- return rc;
- }
- param.cap_mode = cap_mode;
- param.dev_id = dev_id;
- param.frame_fmt = VIDEO_FORMAT_RGB24;
- if ((ePadtype == g_eDeviceType) || (eMobilePadType == g_eDeviceType) || (eDesk2SType == g_eDeviceType) || (eDesk1SType == g_eDeviceType) || (eDesk2SIntegratedType == g_eDeviceType))
- {
- param.fps = REC_COMMON_VIDEO_PADRAW_FPS;
- }
- else
- {
- param.fps = REC_COMMON_VIDEO_RAW_FPS;
- }
- param.on_frame = (video_cap->camera_type == CAMERA_TYPE_ENV ? &env_cap_on_frame : &opt_cap_on_frame);
- param.user_data = video_cap;
- param.option = 0;
- rc = videocap_create(&video_cap->cap, ¶m);
- if (rc != 0)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("videocap create failed!");
- return rc;
- }
- rc = videocap_start(video_cap->cap);
- if (rc != 0)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("videocap start failed!");
- videocap_destroy(video_cap->cap);
- video_cap->cap = NULL;
- return rc;
- }
- else {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("videocap start success!");
- }
- return 0;
- }
- #else
- static void __videocaplog(void* user_data, const char* fmt, va_list arg)
- {
- vDbg(fmt, arg);
- }
- static void __logevent(int itype, const char* strmessage)
- {
- if (0 == itype) {
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_CAMERA_MATCHED_FORMAT, strmessage);
- }
- else if (1 == itype) {
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_CAMERA_OUTPUT_FORMAT, strmessage);
- }
- else {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("unknown type event.");
- }
- }
- static int video_capture_start_linux(video_capture_t* video_cap)
- {
- LOG_FUNCTION();
- int iret = -1;
- capture_config_t* conf = &video_cap->parent->config;
- int dev_id = -1;
- if (video_cap->camera_type == CAMERA_TYPE_ENV){
- dev_id = rvc_videocap_get_video_device_id(conf->strVideoEnv.GetData());
- if (-1 == dev_id){
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("No environment camera[%s], please check config file or device!", conf->strVideoEnv.GetData());
- return iret;
- }
- }
- else{
- dev_id = rvc_videocap_get_video_device_id(conf->strVideoOpt.GetData());
- if (-1 == dev_id){
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("No operation camera[%s], please check config file or device!", conf->strVideoOpt.GetData());
- return iret;
- }
- }
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("video dev_id is %d", dev_id);
- videocap_param_t t_param = { 0 };
- int cap_mode;
- int rc = calc_capture_mode(REC_COMMON_VIDEO_SNAPSHOT_WIDTH, REC_COMMON_VIDEO_SNAPSHOT_HEIGHT, &cap_mode);
- if (rc != 0){
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("calc cap_mode failed!");
- return iret;
- }
- else{
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("cap_mode == %d.",cap_mode);
- }
- t_param.cap_mode = cap_mode;
- t_param.dev_id = dev_id;
- t_param.frame_fmt = VIDEO_FORMAT_RGB24;
- t_param.fps = get_video_capture_fps(video_cap->camera_type == CAMERA_TYPE_ENV ? conf->video_env_fps : conf->video_opt_fps);
- t_param.irotate = (video_cap->camera_type == CAMERA_TYPE_ENV ? conf->video_env_rotate : conf->video_opt_rotate);
- t_param.on_frame = (video_cap->camera_type == CAMERA_TYPE_ENV ? &env_cap_on_frame : &opt_cap_on_frame);
- //t_param.on_frame_i420 = (video_cap->camera_type == CAMERA_TYPE_ENV ? &env_cap_on_frame_i420 : &opt_cap_on_frame_i420);
- t_param.user_data = video_cap;
- t_param.option = 0;
- videocap_callback_t t_callback = { 0 };
- t_callback.debug = &__videocaplog;
- t_callback.logevent = &__logevent;
- video_cap->pVideoCap = CreateVideoCaptureObj(&t_callback);
- if (NULL != video_cap->pVideoCap){
- if (0 == video_cap->pVideoCap->VideoCaptureSetParam(&t_param)) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("VideoCap SetVideoCaptureParam success!");
- if (0 == video_cap->pVideoCap->StartVideoCapture()) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("VideoCap StartVideoCapture success!");
- iret = 0;
- }
- else {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("VideoCap StartVideoCapture failed!");
- DestroyVideoCaptureObj(video_cap->pVideoCap);
- }
- }
- else {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("VideoCap SetVideoCaptureParam failed!");
- DestroyVideoCaptureObj(video_cap->pVideoCap);
- }
- }
- else{
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)(" CreateVideoCaptureObj failed!");
- }
- return iret;
- }
- #endif // RVC_OS_WIN
- static int video_capture_start(video_capture_t *video_cap)
- {
- #ifdef RVC_OS_WIN
- return video_capture_start_win(video_cap);
- #else
- return video_capture_start_linux(video_cap);
- #endif
- }
- static void video_capture_stop(video_capture_t *video_cap)
- {
- #ifdef RVC_OS_WIN
- if (video_cap->cap) {
- videocap_stop(video_cap->cap);
- videocap_destroy(video_cap->cap);
- video_cap->cap = NULL;
- }
- #else
- if (video_cap->pVideoCap) {
- video_cap->pVideoCap->StopVideoCapture();
- DestroyVideoCaptureObj(video_cap->pVideoCap);
- video_cap->pVideoCap = NULL;
- }
- #endif // RVC_OS_WIN
- }
- namespace MediaController {
- DeviceTypeEnum g_eDeviceType;
- int capture_create(const capture_config_t *config, capture_t **p_cap)
- {
- capture_t *cap = ZALLOC_T(capture_t);
- cap->audio = NULL;
- cap->env_video = NULL;
- cap->opt_video = NULL;
- memcpy(&cap->config, config, sizeof(capture_config_t));
- cap->audio = audio_capture_create(cap);
- if (!cap->audio) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create audio capture object failed!");
- return Error_Unexpect;
- }
- int dev_id_env = capture_get_video_device_id(config->strVideoEnv);
- if (dev_id_env != -1) {
- cap->env_video = video_capture_create(cap, CAMERA_TYPE_ENV);
- if (!cap->env_video) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create env video object failed!");
- return Error_Unexpect;
- }
- }
- int dev_id_opt = -1;
- if (eStand2sType == g_eDeviceType){
- dev_id_opt = capture_get_video_device_id(config->strVideoOpt);
- }
- if (dev_id_opt != -1){
- cap->opt_video = video_capture_create(cap, CAMERA_TYPE_OPT);
- if (!cap->opt_video) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create opt video object failed!");
- return Error_Unexpect;
- }
- }
- if((dev_id_env == -1)&&(dev_id_opt == -1)){
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("all camera device id error!");
- capture_destroy(cap);
- return Error_Unexpect;
- }
- else{
- *p_cap = cap;
- return 0;
- }
- }
- ErrorCodeEnum capture_create(const capture_config_t *config,capture_t *cap,int nCamera)
- {
- int dev_id1 = capture_get_video_device_id(config->strVideoEnv);
- int dev_id2 = capture_get_video_device_id(config->strVideoOpt);
- if((dev_id1 != -1)&&(nCamera==ENVCAMERA)&&(cap->env_video == NULL))
- {
- cap->env_video = video_capture_create(cap, CAMERA_TYPE_ENV);
- if (!cap->env_video)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create env video object failed!");
- return Error_Unexpect;
- }
- }
- else if((dev_id2 != -1)&&(nCamera == OPTCAMERA)&&(cap->opt_video == NULL))
- {
- cap->opt_video = video_capture_create(cap, CAMERA_TYPE_OPT);
- if (!cap->opt_video)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create opt video object failed!");
- return Error_Unexpect;
- }
- }
- else
- {
- return Error_Unexpect;
- }
- return Error_Succeed;
- }
- int salesaudio_capture_create(rvc_audio_capture_config_t *config, rvc_sales_audio_capture_t **p_cap)
- {
- rvc_sales_audio_capture_t *cap = ZALLOC_T(rvc_sales_audio_capture_t);
- cap->rvc_audio = NULL;
- memcpy(&cap->rvc_audio_config, config, sizeof(rvc_audio_capture_config_t));
- cap->rvc_audio = salesrecord_audio_capture_create(cap);
- if (!cap->rvc_audio) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create sales record audio capture object failed!");
- salesaudio_capture_destroy(cap);
- cap = NULL;
- return Error_Unexpect;
- }
- else
- {
- cap->rvc_audio->iaudio_capture_peroid = config->audio_capture_period;
- cap->rvc_audio->iaudio_capture_samplerate = config->audio_capture_samplerate;
- *p_cap = cap;
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create sales record audio capture object(0x%08x) success, capture sample rate is %d, and rvc audio(0x%08x) in device is %s.",
- cap, cap->rvc_audio->iaudio_capture_samplerate, cap->rvc_audio, cap->rvc_audio_config.strAudioIn.GetData());
- return 0;
- }
- }
- void salesaudio_capture_destroy(rvc_sales_audio_capture_t *cap)
- {
- if (NULL != cap){
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("sales audio capture destroy, and cap addr is 0x%08x.", cap);
- if (cap->rvc_audio) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("sales audio capture rvc_audio destroy, and rvc_audio addr is 0x%08x.", cap->rvc_audio);
- salesrecord_audio_capture_destroy(cap->rvc_audio);
- cap->rvc_audio = NULL;
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("set rvc_audio to null.");
- }
- free(cap);
- }
- }
- void capture_destroy(capture_t *cap)
- {
- if (cap) {
- if (cap->env_video) {
- video_capture_destroy(cap->env_video);
- cap->env_video = NULL;
- }
- if (cap->opt_video) {
- video_capture_destroy(cap->opt_video);
- cap->opt_video = NULL;
- }
- if (cap->audio) {
- audio_capture_destroy(cap->audio);
- cap->audio = NULL;
- }
- free(cap);
- }
- }
- void capture_destroy(capture_t *cap,int nCamera)
- {
- if (cap)
- {
- if((cap->env_video)&&(nCamera == ENVCAMERA))
- {
- video_capture_destroy(cap->env_video);
- cap->env_video = NULL;
- }
- else if((cap->opt_video)&&(nCamera==OPTCAMERA))
- {
- video_capture_destroy(cap->opt_video);
- cap->opt_video = NULL;
- }
- }
- }
- ErrorCodeEnum start_audio_capture(audio_capture_t *paudio)
- {
- ErrorCodeEnum rslt = Error_Succeed;
- int rc = 0;
- if (NULL != paudio) {
- rc = audio_capture_start(paudio);
- if (rc != Error_Succeed) {
- rslt = (ErrorCodeEnum)rc;
- if (rslt == Error_AudioIN){
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start audio In object failed! rc:%d", rc);
- LogWarn(Severity_Middle, Error_Debug,ERROR_MOD_MEDIACONTROLLER_HANDFREE_OPENFAIL,"open audioIn device fail,please check device");
- }
- else{
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start audio Out object failed! rc:%d", rc);
- LogWarn(Severity_Middle, Error_Debug,ERROR_MOD_MEDIACONTROLLER_HANDFREE_OPENFAIL,"open audioOut device fail,please check device");
- }
- }
- else{
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("audio capture start success.");
- }
- }
- else{
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("audio_capture_t is null.");
- rslt = Error_AudioIN;
- }
- return rslt;
- }
- ErrorCodeEnum capture_start(capture_t *cap)
- {
- int rc = 0;
- ErrorCodeEnum rslt = start_audio_capture(cap->audio);
- if (Error_Succeed != rslt) {
- return rslt;
- }
- if (cap->env_video)
- {
- rc = video_capture_start(cap->env_video);
- if (rc != Error_Succeed)
- {
- cap->env_video->ustarttime = 0;
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start env video capture object failed! rc:%d", rc);
- char strMessage[MAX_PATH*2] = {0};
- get_camera_exception_message(strMessage, MAX_PATH*2, cap->config.strVideoEnv, "open environ camera fail, please check device.");
- LogWarn(Severity_Middle,Error_NotInit,ERROR_MOD_MEDIACONTROLLER_ENVCAM_OPEN,strMessage);
- if (cap->opt_video)
- {
- if (cap->env_video)
- {
- ::Sleep(3000);
- }
- rc = video_capture_start(cap->opt_video);
- if (rc != Error_Succeed)
- {
- cap->opt_video->ustarttime = 0;
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start opt video capture object failed! rc:%d", rc);
- char strMessage[MAX_PATH*2] = {0};
- get_camera_exception_message(strMessage, MAX_PATH*2, cap->config.strVideoOpt, "open operate camera fail, please check device.");
- LogWarn(Severity_Middle,Error_NotInit,ERROR_MOD_MEDIACONTROLLER_OPTCAM_OPEN, strMessage);
- return Error_AllCamera;
- }
- else
- {
- cap->opt_video->ustarttime = y2k_time_now();
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_OPTCAM_OPEN, "start optcam success.");
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start env video capture object failed!");
- return Error_EnvCamera;
- }
- }
- else
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start all video capture object failed!");
- return Error_AllCamera;
- }
- }
- else
- {
- cap->env_video->ustarttime = y2k_time_now();
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_ENVCAM_OPEN, "start envcam success.");
- if (cap->opt_video)
- {
- if (cap->env_video)
- {
- ::Sleep(3000);
- }
- rc = video_capture_start(cap->opt_video);
- if (rc != Error_Succeed)
- {
- cap->opt_video->ustarttime = 0;
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start opt video capture object failed! rc:%d", rc);
- char strMessage[MAX_PATH*2] = {0};
- get_camera_exception_message(strMessage, MAX_PATH*2, cap->config.strVideoOpt, "open operate camera fail, please check device.");
- LogWarn(Severity_Middle,Error_NotInit,ERROR_MOD_MEDIACONTROLLER_OPTCAM_OPEN,strMessage);
- return Error_OptCamera;
- }
- else
- {
- cap->opt_video->ustarttime = y2k_time_now();
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_OPTCAM_OPEN, "start optcam success.");
- return Error_Succeed;
- }
- }
- else
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("opt_video = Null");
- return Error_OptCamera;
- }
- }
- }
- else
- {
- if (cap->opt_video)
- {
- if (cap->env_video)
- {
- ::Sleep(3000);
- }
- rc = video_capture_start(cap->opt_video);
- if (rc != Error_Succeed)
- {
- cap->opt_video->ustarttime = 0;
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start opt video capture object failed! rc:%d", rc);
- char strMessage[MAX_PATH*2] = {0};
- get_camera_exception_message(strMessage, MAX_PATH*2, cap->config.strVideoOpt, "open operate camera fail,please check device.");
- LogWarn(Severity_Middle,Error_NotInit,ERROR_MOD_MEDIACONTROLLER_OPTCAM_OPEN,strMessage);
- return Error_AllCamera;
- }
- else
- {
- cap->opt_video->ustarttime = y2k_time_now();
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start env video capture object failed!");
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_OPTCAM_OPEN, "start optcam success.");
- return Error_EnvCamera;
- }
- }
- else
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start env video Error_AllCamera");
- return Error_AllCamera;
- }
- }
- }
- ErrorCodeEnum capture_start(capture_t *cap,int nCamera)
- {
- int rc = 0;
- if (cap->env_video&&(nCamera==ENVCAMERA))
- {
- rc = video_capture_start(cap->env_video);
- if (rc != Error_Succeed)
- {
- cap->env_video->ustarttime = 0;
- return Error_Hardware;
- }
- else
- {
- cap->env_video->ustarttime = y2k_time_now();
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_ENVCAM_OPEN, "start envcam success.");
- return Error_Succeed;
- }
- }
- else if(cap->opt_video&&(nCamera==OPTCAMERA))
- {
- rc = video_capture_start(cap->opt_video);
- if (rc != Error_Succeed)
- {
- cap->opt_video->ustarttime = 0;
- return Error_Hardware;
- }
- else
- {
- cap->opt_video->ustarttime = y2k_time_now();
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_OPTCAM_OPEN, "start optcam success.");
- return Error_Succeed;
- }
- }
- else
- {
- return Error_Unexpect;
- }
- }
- ErrorCodeEnum salesrecord_audio_capture_start(rvc_sales_audio_capture_t *cap)
- {
- auto rc = Error_Param;
- if (NULL == cap){
- return rc;
- }
- if (cap->rvc_audio) {
- audiomgr_callback_t t_callback = { 0 };
- t_callback.debug = &__audiomgrlog;
- cap->rvc_audio->paudiocap = CreateAudioMgrObj(&t_callback);
- if (0 == cap->rvc_audio->paudiocap->audio_mgr_initialize()) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("audio manager initialize success!");
- }
- else {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("audio manager initialize failed!");
- return Error_Null;
- }
- int rslt = record_audio_capture_start(cap->rvc_audio);
- if (rslt != Error_Succeed) {
- if (Error_AudioIN == rslt){
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start audio In object failed! rc:%d", rc);
- LogWarn(Severity_Middle, Error_Debug,ERROR_MOD_MEDIACONTROLLER_HANDFREE_OPENFAIL,"open audioIn device fail,please check device");
- }
- rc = (ErrorCodeEnum)rslt;
- }
- else{
- rc = Error_Succeed;
- }
- }
- return rc;
- }
- void salesrecord_audio_capture_stop(rvc_sales_audio_capture_t *cap)
- {
- if (NULL != cap){
- if (cap->rvc_audio){
- if (cap->rvc_audio->pdata){
- if (eSingleWriteLocal == cap->rvc_audio->eType){
- fclose((FILE*)cap->rvc_audio->pdata);
- cap->rvc_audio->pdata = NULL;
- }
- }
- record_audio_capture_stop(cap->rvc_audio);
- #ifdef RVC_OS_LINUX
- if (NULL != cap->rvc_audio->paudiocap) {
- DestroyIAudioMgrObj(cap->rvc_audio->paudiocap);
- cap->rvc_audio->paudiocap = NULL;
- }
- #endif // RVC_OS_LINUX
- }
- }
- else{
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("sales record audio capture stop failed for param error.");
- }
- }
- void capture_stop(capture_t *cap)
- {
- if (cap->audio) {
- audio_capture_stop(cap->audio);
- }
- if (cap->env_video) {
- video_capture_stop(cap->env_video);
- if (0 != cap->env_video->ustarttime) {
- char strmsg[MAX_PATH] = { 0 };
- snprintf(strmsg, MAX_PATH, "stop envcam, and camera open time is %us.", y2k_time_now() - cap->env_video->ustarttime);
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_ENVCAM_CLOSE, strmsg);
- }
- }
- if (cap->opt_video) {
- if (0 != cap->opt_video->ustarttime) {
- char strinfo[MAX_PATH] = { 0 };
- snprintf(strinfo, MAX_PATH, "stop optcam, and camera open time is %us.", y2k_time_now() - cap->opt_video->ustarttime);
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_OPTCAM_CLOSE, strinfo);
- }
- video_capture_stop(cap->opt_video);
- }
- }
- int capture_detect_camera_bug(capture_t *cap, int *env_n, int *opt_n,BOOL IsPad)
- {
- *env_n = 0;
- *opt_n = 0;
- if (cap->env_video)
- {
- if (cap->env_video->rtp_shm_queue)
- {
- *env_n = cap->env_video->rtp_shm_queue->GetVideoLens();
- }
- }
- else
- {
- *env_n = -1;
- }
- if (cap->opt_video)
- {
- if (cap->opt_video->rtp_shm_queue)
- {
- *opt_n = cap->opt_video->rtp_shm_queue->GetVideoLens();
- }
- }
- else
- {
- if (!IsPad)
- {
- *opt_n = -1;
- }
- else
- {
- *opt_n = 1;
- }
- }
- return 0;
- }
- int capture_get_last_frametime(capture_t *cap, DWORD *env_n, DWORD *opt_n,BOOL IsPad)
- {
- *env_n = 0;
- *opt_n = 0;
- if (cap->env_video)
- {
- if (cap->env_video->rtp_shm_queue)
- {
- *env_n = cap->env_video->rtp_shm_queue->GetLastFrameTime();
- }
- }
- else
- {
- *env_n = 0;
- }
- if (cap->opt_video)
- {
- if (cap->opt_video->rtp_shm_queue)
- {
- *opt_n = cap->opt_video->rtp_shm_queue->GetLastFrameTime();
- }
- }
- else
- {
- *opt_n = 0;
- }
- return 0;
- }
- int capture_get_video_device_id(const char *dev_name)
- {
- #ifdef RVC_OS_WIN
- int n = videocap_get_device_count();
- for (int i = 0; i < n; ++i) {
- WCHAR tmp[256];
- char t[256];
- WCHAR tmp1[256];
- char t1[256];
- videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
- WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
- videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
- WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
- // save DevicePath (add by ly at 20160725)
- char t2[256];
- strcpy(t2,t1);
- for (int j = 0; j < strlen(t2); ++j)
- {
- t2[j] = toupper(t2[j]);
- if (t2[j] == '#') t2[j] = '\\';
- }
- {
- unsigned char x[MD5_DIGESTSIZE];
- md5_ctx_t ctx;
- md5_init(&ctx);
- md5(x, t1, strlen(t1));
- Bin2Str(x, sizeof(x), t1, sizeof(t1));
- }
-
- if (dev_name != NULL && strlen(dev_name) > 1 &&
- strstr(dev_name, ";") == NULL) // 外接摄像头 add by ly at 20160531
- {
- /*if (strstr(t, dev_name) != NULL)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[dbg] %s founded in %d cameras.", dev_name, n);
- return i;
- }*/
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[dbg] device_path: %s",t2);
- if (strstr(t2,dev_name) != NULL) // 判断外接摄像头DeviceLocationPaths是否是DevicePath的子串
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[dbg] %s founded in %d cameras.", dev_name, n);
- return i;
- }
- if (strcmp(dev_name, t) == 0) // 如果是直接用友好名称查询(适用于高拍仪) add by ly 2017/11/08
- return i;
- }
- else
- {
- strcat(t, ";");
- strcat(t, t1);
- if (strcmp(dev_name, t) == 0)
- return i;
- }
- }
- return -1; // not found
- #else
- return rvc_videocap_get_video_device_id(dev_name);
- #endif // RVC_OS_WIN
-
- }
- int capture_lib_init() // edit by ly at 20160401
- {
- #ifdef RVC_OS_WIN
- HRESULT hr = CoInitialize(NULL);
- int rc;
- {
- HMODULE hModule = GetModuleHandleA("MSVCR100.dll");
- if (hModule) {
- typedef char* (*f_setlocale)(int, const char*);
- f_setlocale f = (f_setlocale)GetProcAddress(hModule, "setlocale");
- (*f)(LC_ALL, "chs");
- }
- }
- if (SUCCEEDED(hr)) {
- PaError Error;
- Error = Pa_Initialize();
- if (Error == paNoError) {
- rc = videoframework_init();
- if (rc != 0) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("videoframework_init failed, rc=%d", rc);
- return Error_Resource;
- }
- }
- else {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("PaInitialize failed, rc=%d", Error);
- return Error_Resource;
- }
- }
- else {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("coinitialze failed! hr:%d", hr);
- return Error_Resource;
- }
- {
- int i, n;
- n = videocap_get_device_count();
- for (i = 0; i < n; ++i) {
- WCHAR tmp[256] = {0};
- char t[256] = {0};
- WCHAR tmp1[256] ={0};
- char t1[256] = {0};
- videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
- WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
- videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
- WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
- {
- unsigned char x[MD5_DIGESTSIZE];
- md5_ctx_t ctx;
- md5_init(&ctx);
- md5(x, t1, strlen(t1));
- Bin2Str(x, sizeof(x), t1, sizeof(t1));
- }
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%d = %s;%s", i, t, t1);
- }
- }
- {
- int icnt, ocnt;
- int rc = audio_get_dev_count(&icnt, &ocnt);
- if (rc == 0) {
- int i;
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("audio input devices(%d):", icnt);
- for (i = 0; i < icnt; ++i) {
- CSimpleStringA str = audio_get_dev_name(true, i);
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%d = %s", i, (LPCSTR)str);
- }
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("audio output devices(%d):", ocnt);
- for (i = 0; i < ocnt; ++i) {
- CSimpleStringA str = audio_get_dev_name(false, i);
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%d = %s", i, (LPCSTR)str);
- }
- }
- }
- #else
- {
- int inumber = 0;
- int icount = rvc_videocap_get_device_count();
- for (int i = 0; i < 64 && inumber < icount; ++i) {
- char strcamera[2 * MAX_PATH] = { 0 };
- char strpath[MAX_PATH] = { 0 };
- if (0 == rvc_videocap_get_device_fullpathname(i, strcamera, 2 * MAX_PATH)){
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%d = %s", inumber++, strcamera);
- }
- }
- }
- #endif // RVC_OS_WIN
- return Error_Succeed;
- }
- int videocap_innerdev_fetch(CSimpleStringA&frontcam,CSimpleStringA&rearcam) // edit by ly at 20160401
- {
- int i = 0, n = 0;
- #ifdef RVC_OS_WIN
- frontcam = "$", rearcam = "$";
- n = videocap_get_device_count();
- if (n == 2) // 未插入外接时
- {
- for (i = 0; i < n; ++i) {
- WCHAR tmp[256] = {0};
- char t[256] = {0};
- WCHAR tmp1[256] = {0};
- char t1[256] = {0};
- videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
- WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
- videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
- WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
- {
- unsigned char x[MD5_DIGESTSIZE];
- md5_ctx_t ctx;
- md5_init(&ctx);
- md5(x, t1, strlen(t1));
- Bin2Str(x, sizeof(x), t1, sizeof(t1));
- }
- strcat(t, ";");
- strcat(t, t1);
- if (i == 0)
- {
- frontcam = t;
- }
- else if (i == 1)
- {
- rearcam = t;
- }
- }
- }
- else
- {
- DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("[dbg] detect %d cameras.", n);
- }
- #else
- #endif // RVC_OS_WIN
- return n;
- }
- int videocap_outerdev_fetch( CSimpleStringA envcam,CSimpleStringA optcam,CSimpleStringA ewscam,CAutoArray<CSimpleStringA> &hspcams,CSimpleStringA &outercam )
- {
- int i = 0, n = 0;
- #ifdef RVC_OS_WIN
- outercam = "$";
- n = videocap_get_device_count();
- int m = hspcams.GetCount(); // 高拍仪的个数 add by ly 2017/11/07
- if (ewscam.GetLength() <= 1) // 如果外部广角摄像头未配置
- {
- if (n <= m || n > 3+m)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[dbg] [videocap_outerdev_fetch] detect %d cameras.", n);
- }
- else
- {
- int cnt = 0;
- for (i = 0; i < n; ++i) {
- WCHAR tmp[256];
- char t[256];
- WCHAR tmp1[256];
- char t1[256];
- videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
- WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
- videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
- WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
- // save DevicePath (add by ly at 20160725)
- char t2[256];
- strcpy(t2,t1);
- for (int j = 0; j < strlen(t2); ++j)
- {
- t2[j] = toupper(t2[j]);
- if (t2[j] == '#') t2[j] = '\\';
- }
- {
- unsigned char x[MD5_DIGESTSIZE];
- md5_ctx_t ctx;
- md5_init(&ctx);
- md5(x, t1, strlen(t1));
- Bin2Str(x, sizeof(x), t1, sizeof(t1));
- }
- // 检查是否为高拍仪摄像头,若是则直接枚举下一个摄像头
- bool isHspCam = false;
- for (int k = 0; k < m; ++k) {
- if (!strcmp((LPCTSTR)hspcams[k],t)) {
- isHspCam = true;
- break;
- }
- }
- if (isHspCam) {
- continue;
- }
- strcat(t, ";");
- strcat(t, t1);
- if(strcmp(t,(LPCTSTR)envcam) && strcmp(t,(LPCTSTR)optcam))
- {
- if (0 == cnt)
- {
- outercam = t2;
- }
- ++cnt;
- }
- }
- if(cnt != 1)
- {
- outercam = "$";
- }
- }
- }
- else // 如果外部广角摄像头已配置
- {
- if (n <= m || n > 4+m)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[dbg] [videocap_outerdev_fetch] detect %d cameras.", n);
- }
- else
- {
- int cnt = 0;
- for (i = 0; i < n; ++i) {
- WCHAR tmp[256];
- char t[256];
- WCHAR tmp1[256];
- char t1[256];
- videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
- WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
- videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
- WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
- // save DevicePath (add by ly at 20160725)
- char t2[256];
- strcpy(t2,t1);
- for (int j = 0; j < strlen(t2); ++j)
- {
- t2[j] = toupper(t2[j]);
- if (t2[j] == '#') t2[j] = '\\';
- }
- {
- unsigned char x[MD5_DIGESTSIZE];
- md5_ctx_t ctx;
- md5_init(&ctx);
- md5(x, t1, strlen(t1));
- Bin2Str(x, sizeof(x), t1, sizeof(t1));
- }
- // 检查是否为高拍仪摄像头,若是则直接枚举下一个摄像头
- bool isHspCam = false;
- for (int k = 0; k < m; ++k) {
- if (!strcmp((LPCTSTR)hspcams[k],t)) {
- isHspCam = true;
- break;
- }
- }
- if (isHspCam) {
- continue;
- }
- strcat(t, ";");
- strcat(t, t1);
- if(strcmp(t,(LPCTSTR)envcam) && strcmp(t,(LPCTSTR)optcam) && !strstr(t2,(LPCTSTR)ewscam))
- {
- if (0 == cnt)
- {
- outercam = t2;
- }
- ++cnt;
- }
- }
- if(cnt != 1)
- {
- outercam = "$";
- }
- }
- }
- #else
- #endif // RVC_OS_WIN
- return n;
- }
- void capture_enum_cameras(CAutoArray<CSimpleStringA>& cams)
- {
- #if defined(RVC_OS_LINUX)
- cams.Clear();
- CAutoArray<CSimpleStringA> tmp;
- int inumber = 0;
- int icount = rvc_videocap_get_device_count();
- if (icount >= 64) icount = 64;
- tmp.Init(icount);
- for (int i = 0; inumber < icount; ++i) {
- char strcamera[2 * MAX_PATH] = { 0 };
- char strpath[MAX_PATH] = { 0 };
- if (0 == rvc_videocap_get_device_fullpathname(i, strcamera, 2 * MAX_PATH)) {
- tmp[inumber] = strcamera;
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%d = %s", inumber++, strcamera);
- }
- }
- cams.Copy(tmp, 0, inumber);
- #endif //RVC_OS_LINUX
- }
- void capture_lib_term()
- {
- #ifdef RVC_OS_WIN
- Pa_Terminate();
- videoframework_term();
- CoUninitialize();
- #else
- #endif // RVC_OS_WIN
- }
- bool capture_adj_brightness(capture_t *cap,int nvalue,ErrorCodeEnum nCode)
- {
- #ifdef RVC_OS_WIN
- HRESULT rst = S_OK;
- if (cap->env_video&&(nCode!=Error_EnvCamera)&&(nCode!=Error_AllCamera))
- {
- rst = videocap_adj_brightness(cap->env_video->cap,nvalue);
- }
- HRESULT rst2 = S_OK;
- if (cap->opt_video->cap&&(nCode!=Error_OptCamera)&&(nCode!=Error_AllCamera))
- {
- rst2 = videocap_adj_brightness(cap->opt_video->cap,nvalue);
- }
- if (SUCCEEDED(rst)&&SUCCEEDED(rst2))
- return true;
- else
- return false;
- #else
- int ienv = -1;
- if (cap->env_video && cap->env_video->pVideoCap && (nCode != Error_EnvCamera) && (nCode != Error_AllCamera)){
- ienv = cap->env_video->pVideoCap->SetCamBrightness(nvalue);
- }
- int iopt = -1;
- if (cap->opt_video && cap->opt_video->pVideoCap && (nCode != Error_OptCamera) && (nCode != Error_AllCamera)){
- iopt = cap->opt_video->pVideoCap->SetCamBrightness(nvalue);
- }
- if (0 == ienv && 0 == iopt){
- return true;
- }
- else{
- return false;
- }
- #endif // RVC_OS_WIN
- }
- bool capture_set_autobrightness(capture_t *cap,ErrorCodeEnum nCode)
- {
- #ifdef RVC_OS_WIN
- HRESULT rst = S_OK;
- HRESULT rst2 = S_OK;
- if (cap->env_video && (nCode != Error_EnvCamera) && (nCode != Error_AllCamera))
- {
- rst = videocap_set_autobrightness(cap->env_video->cap);
- }
- if (cap->opt_video && (nCode != Error_OptCamera) && (nCode != Error_AllCamera))
- {
- rst2 = videocap_set_autobrightness(cap->opt_video->cap);
- }
- if (SUCCEEDED(rst) && SUCCEEDED(rst2))
- return true;
- else
- return false;
- #else
- int ienv = -1;
- int iopt = -1;
- if (cap->env_video && cap->env_video->pVideoCap && (nCode != Error_EnvCamera) && (nCode != Error_AllCamera))
- {
- ienv = cap->env_video->pVideoCap->SetCamAutoBrightness();
- }
- if (cap->opt_video && cap->opt_video->pVideoCap && (nCode != Error_OptCamera) && (nCode != Error_AllCamera))
- {
- iopt = cap->opt_video->pVideoCap->SetCamAutoBrightness();
- }
- if (0 == ienv && 0 == iopt) {
- return true;
- }
- else {
- return false;
- }
-
- #endif // RVC_OS_WIN
- }
- int capture_get_brightness(capture_t *cap,ErrorCodeEnum nCode)
- {
- #ifdef RVC_OS_WIN
- int nValue1 = 0;
- int nValue2 = 0;
- HRESULT rst = S_OK;
- if (cap->env_video && (nCode != Error_EnvCamera) && (nCode != Error_AllCamera))
- {
- HRESULT rst = videocap_get_brightness(cap->env_video->cap, &nValue1);
- }
- HRESULT rst2 = S_OK;
- if (cap->opt_video && (nCode != Error_OptCamera) && (nCode != Error_AllCamera))
- {
- rst2 = videocap_get_brightness(cap->opt_video->cap, &nValue2);
- }
- else
- {
- return -1;
- }
- if ((cap->opt_video == NULL) && cap->env_video)
- {
- return nValue1;
- }
- else if ((cap->env_video == NULL) && cap->opt_video)
- {
- return nValue2;
- }
- else
- {
- if (SUCCEEDED(rst) && SUCCEEDED(rst2))
- return (nValue1 <= nValue2) ? nValue1 : nValue2;
- else
- return -1;
- }
- #else
- int nValue1 = 0;
- int nValue2 = 0;
- int ienv = -1;
- if (cap->env_video && cap->env_video->pVideoCap && (nCode != Error_EnvCamera) && (nCode != Error_AllCamera)){
- ienv = cap->env_video->pVideoCap->GetCamBrightness(&nValue1);
- }
- int iopt = -1;
- if (cap->opt_video && cap->opt_video->pVideoCap && (nCode != Error_OptCamera) && (nCode != Error_AllCamera)){
- iopt = cap->opt_video->pVideoCap->GetCamBrightness(&nValue2);
- }
- else{
- return -1;
- }
- if ((cap->opt_video == NULL) && cap->env_video)
- {
- return nValue1;
- }
- else if ((cap->env_video == NULL) && cap->opt_video)
- {
- return nValue2;
- }
- else
- {
- if (0 == ienv && 0 == iopt) {
- return (nValue1 <= nValue2) ? nValue1 : nValue2;
- }
- else
- {
- return -1;
- }
- }
- #endif // RVC_OS_WIN
- }
- int StopCamera(capture_t *cap,int nCamera)
- {
- if((nCamera == ENVCAMERA)&&cap->env_video)
- {
- video_capture_stop(cap->env_video);
- if (0 != cap->env_video->ustarttime) {
- char strmsg[MAX_PATH] = { 0 };
- snprintf(strmsg, MAX_PATH, "stop envcam, and camera open time is %us.", y2k_time_now() - cap->env_video->ustarttime);
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_ENVCAM_CLOSE, strmsg);
- }
- video_capture_destroy(cap->env_video);
- cap->env_video = NULL;
- return 0;
- }
- else if((nCamera == OPTCAMERA)&&cap->opt_video)
- {
- video_capture_stop(cap->opt_video);
- if (0 != cap->opt_video->ustarttime) {
- char strinfo[MAX_PATH] = { 0 };
- snprintf(strinfo, MAX_PATH, "stop optcam, and camera open time is %us.", y2k_time_now() - cap->opt_video->ustarttime);
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIACONTROLLER_OPTCAM_CLOSE, strinfo);
- }
- video_capture_destroy(cap->opt_video);
- cap->opt_video = NULL;
- return 0;
- }
- else
- {
- return -1;
- }
- }
- //根据frontcam完整名(包含设备名+逗号+设备路径MD5)、ewscam设备路径、hspcams设备名,找到rearcam的完整名
- //排除以上设备后的唯一设备即为rearcam
- int videocap_optdev_fetch( CSimpleStringA frontcam,CSimpleStringA ewscam,CAutoArray<CSimpleStringA> &hspcams,CSimpleStringA &rearcam )
- {
- int i = 0, n = 0;
- #ifdef RVC_OS_WIN
- rearcam = "$";
- n = videocap_get_device_count();
- int m = hspcams.GetCount(); // 高拍仪的个数 add by ly 2017/11/07
- if (ewscam.GetLength() <= 1) // 如果外部广角摄像头未配置
- {
- if (n <= 2+m)
- {
- int cnt = 0;
- for (i = 0; i < n; ++i) {
- WCHAR tmp[256];
- char t[256];
- WCHAR tmp1[256];
- char t1[256];
- videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
- WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
- videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
- WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
- {
- unsigned char x[MD5_DIGESTSIZE];
- md5_ctx_t ctx;
- md5_init(&ctx);
- md5(x, t1, strlen(t1));
- Bin2Str(x, sizeof(x), t1, sizeof(t1));
- }
- // 检查是否为高拍仪摄像头,若是则直接枚举下一个摄像头
- bool isHspCam = false;
- for (int k = 0; k < m; ++k) {
- if (!strcmp((LPCTSTR)hspcams[k],t)) {
- isHspCam = true;
- break;
- }
- }
- if (isHspCam) {
- continue;
- }
- strcat(t, ";");
- strcat(t, t1);
- if (strcmp((LPCTSTR)frontcam, t))
- {
- if (0 == cnt)
- {
- rearcam = t;
- }
- cnt++;
- }
- }
- if (1 != cnt) // 未找到或无法识别内置后摄像头
- {
- rearcam = "$";
- }
- }
- else
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[dbg] [videocap_optdev_fetch] detect %d cameras.", n);
- }
- }
- else // 如果外部广角摄像头已配置
- {
- if (n <= 3+m)
- {
- int cnt = 0;
- for (i = 0; i < n; ++i) {
- WCHAR tmp[256];
- char t[256];
- WCHAR tmp1[256];
- char t1[256];
- videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
- WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
- videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
- WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
- // 检查是否为高拍仪摄像头,若是则直接枚举下一个摄像头
- bool isHspCam = false;
- for (int k = 0; k < m; ++k) {
- if (!strcmp((LPCTSTR)hspcams[k],t)) {
- isHspCam = true;
- break;
- }
- }
- if (isHspCam) {
- continue;
- }
- // save DevicePath (add by ly at 20160725)
- char t2[256];
- strcpy(t2,t1);
- for (int j = 0; j < strlen(t2); ++j)
- {
- t2[j] = toupper(t2[j]);
- if (t2[j] == '#') t2[j] = '\\';
- }
- {
- unsigned char x[MD5_DIGESTSIZE];
- md5_ctx_t ctx;
- md5_init(&ctx);
- md5(x, t1, strlen(t1));
- Bin2Str(x, sizeof(x), t1, sizeof(t1));
- }
- strcat(t, ";");
- strcat(t, t1);
- if (strcmp((LPCTSTR)frontcam, t) && !strstr(t2,(LPCTSTR)ewscam))
- {
- if (0 == cnt)
- {
- rearcam = t;
- }
- cnt++;
- }
- }
- if (1 != cnt) // 未找到或无法识别内置后摄像头
- {
- rearcam = "$";
- }
- }
- else
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[dbg] [videocap_optdev_fetch] detect %d cameras.", n);
- }
- }
- #else
- #endif // RVC_OS_WIN
- return n;
- }
- void capture_clearsnapshotvideo(capture_t *cap,int nCamera)
- {
- if((nCamera == ENVCAMERA)&&cap->env_video)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("clear ENV snapshot video!");
- cap->env_video->snapshot_shm_queue->ClearVideoQueue();
- return;
- }
- else if((nCamera == OPTCAMERA)&&cap->opt_video)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("clear OPT snapshot video!");
- cap->opt_video->snapshot_shm_queue->ClearVideoQueue();
- return;
- }
- else if(nCamera == AlLCAMERA)
- {
- if (cap->env_video)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("clear ENV snapshot video!");
- cap->env_video->snapshot_shm_queue->ClearVideoQueue();
- }
- if (cap->opt_video)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("clear OPT snapshot video!");
- cap->opt_video->snapshot_shm_queue->ClearVideoQueue();
- }
- return;
- }
- else
- {
- return ;
- }
- }
- int get_camera_exception_message(char* pBuffer, size_t uLen, CSimpleStringA strDeviceName, const char* strErrorMessage)
- {
- int iRet = 0;
- if (strDeviceName.GetLength() > 0){
- const char* strCameraName = strDeviceName.GetData();
- char strBuffer[MAX_PATH] = {0};
- if (snprintf(strBuffer, MAX_PATH, "%s", strCameraName) > 0){
- char *pIndex = NULL;
- if (pIndex = (char*)strstr(strBuffer, ";")){
- *pIndex = '\0';
- }
- }
- if (NULL != strErrorMessage){
- size_t uDataLen = strlen(strBuffer);
- size_t uErrorLen = strlen(strErrorMessage);
- if (uLen > uDataLen + uErrorLen + 10){
- iRet = snprintf(pBuffer, uLen, "[%s] %s", strBuffer, strErrorMessage);
- }
- }
- }
- if (0 == iRet){
- if (NULL != strErrorMessage){
- iRet = snprintf(pBuffer, uLen, "%s", strErrorMessage);
- }
- }
- return iRet;
- }
- }
|