diff --git a/CMakeLists.txt b/CMakeLists.txt index 6e152ea..30195f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,7 +43,6 @@ target_link_libraries(webcam PRIVATE engine framework opencv_world300 - opencv_ts300 extern Ws2_32 diff --git a/source/webcam.cpp b/source/webcam.cpp index 6b9f8df..f1445f1 100644 --- a/source/webcam.cpp +++ b/source/webcam.cpp @@ -57,7 +57,7 @@ void ProcessWebcamThread::Execute() { if (temp_pause || !temp_record_frame) Platform::Get().Sleep(10); else{ - InputVideo->read(*im); + InputVideo->read(*im); outputVideo->write(*im); lutFile.write(reinterpret_cast(¤t_frame_clock),sizeof(float)); @@ -97,12 +97,27 @@ HRESULT EnumerateDevices(REFGUID category, IEnumMoniker **ppEnum) return hr; } -int DisplayDeviceInformation(IEnumMoniker *pEnum) +std::string BstrToString(BSTR bstr) +{ + if (!bstr) return std::string(); + + int len = WideCharToMultiByte(CP_UTF8, 0, bstr, -1, NULL, 0, NULL, NULL); + if (len <= 0) return std::string(); + + std::string result(len - 1, '\0'); // -1 pour exclure le \0 final + WideCharToMultiByte(CP_UTF8, 0, bstr, -1, &result[0], len, NULL, NULL); + return result; +} + + +std::vector DisplayDeviceInformation(IEnumMoniker *pEnum) { IMoniker *pMoniker = NULL; int return_id_device = -1; int counter_device = 0; + std::vector camera_ids; + while (pEnum->Next(1, &pMoniker, NULL) == S_OK) { IPropertyBag *pPropBag; @@ -124,10 +139,24 @@ int DisplayDeviceInformation(IEnumMoniker *pEnum) } if (SUCCEEDED(hr)) { - printf("%S\n", var.bstrVal); - if (wcscmp(var.bstrVal, L"MMP SDK") != 0 && wcscmp(var.bstrVal, L"Oculus Rift") != 0 && wcscmp(var.bstrVal, L"HTC Vive") != 0) + std::string name = BstrToString(var.bstrVal); + + printf("%S\n", var.bstrVal); + __LOG_CAM__<<"FOUND CAM ID "<Release(); ++counter_device; } - return return_id_device; + + return camera_ids; } @@ -172,7 +202,7 @@ bool Webcam::Open(int width_, int height_) { height = height_; - int id_device = -1; + std::vector id_devices; HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); if (SUCCEEDED(hr)) { @@ -181,23 +211,38 @@ bool Webcam::Open(int width_, int height_) { hr = EnumerateDevices(CLSID_VideoInputDeviceCategory, &pEnum); if (SUCCEEDED(hr)) { - id_device = DisplayDeviceInformation(pEnum); + id_devices = DisplayDeviceInformation(pEnum); pEnum->Release(); } CoUninitialize(); } - if (id_device == -1) + + if (id_devices.size() == 0) return false; - InputVideo = new VideoCapture(0); + + InputVideo = new VideoCapture(); + int device_id = -1; + for (int i = 0; i < id_devices.size(); i++) + { + InputVideo->open(id_devices[i]); + if(InputVideo->isOpened()){ + device_id = id_devices[i]; + break; + } + } + + + if(device_id == -1) + return false; + + __LOG_CAM__ << "OPEN CAM ID "<set(CV_CAP_PROP_FRAME_WIDTH, width); InputVideo->set(CV_CAP_PROP_FRAME_HEIGHT, height); InputVideo->set(CAP_PROP_FPS, webcam_fps); - if (!InputVideo->isOpened()) - return false; - outputVideo = new VideoWriter; ReplayInputVideo = new VideoCapture(); @@ -220,6 +265,8 @@ void Webcam::Reset(bool _reset_video) { } void Webcam::RecordFrame(float _clock) { + if (!thread_process) + return; thread_process->mutex_record_frame.Lock(); thread_process->record_frame = true; thread_process->current_frame_clock = _clock;