fix crash if attemp record and thread stopped + try to find good camera to record instead of index 0 , finally use blacklist
This commit is contained in:
@ -43,7 +43,6 @@ target_link_libraries(webcam PRIVATE
|
|||||||
engine
|
engine
|
||||||
framework
|
framework
|
||||||
opencv_world300
|
opencv_world300
|
||||||
opencv_ts300
|
|
||||||
extern
|
extern
|
||||||
|
|
||||||
Ws2_32
|
Ws2_32
|
||||||
|
|||||||
@ -57,7 +57,7 @@ void ProcessWebcamThread::Execute() {
|
|||||||
if (temp_pause || !temp_record_frame)
|
if (temp_pause || !temp_record_frame)
|
||||||
Platform::Get().Sleep(10);
|
Platform::Get().Sleep(10);
|
||||||
else{
|
else{
|
||||||
InputVideo->read(*im);
|
InputVideo->read(*im);
|
||||||
outputVideo->write(*im);
|
outputVideo->write(*im);
|
||||||
|
|
||||||
lutFile.write(reinterpret_cast<const char*>(¤t_frame_clock),sizeof(float));
|
lutFile.write(reinterpret_cast<const char*>(¤t_frame_clock),sizeof(float));
|
||||||
@ -97,12 +97,27 @@ HRESULT EnumerateDevices(REFGUID category, IEnumMoniker **ppEnum)
|
|||||||
return hr;
|
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<int> DisplayDeviceInformation(IEnumMoniker *pEnum)
|
||||||
{
|
{
|
||||||
IMoniker *pMoniker = NULL;
|
IMoniker *pMoniker = NULL;
|
||||||
int return_id_device = -1;
|
int return_id_device = -1;
|
||||||
int counter_device = 0;
|
int counter_device = 0;
|
||||||
|
|
||||||
|
std::vector<int> camera_ids;
|
||||||
|
|
||||||
while (pEnum->Next(1, &pMoniker, NULL) == S_OK)
|
while (pEnum->Next(1, &pMoniker, NULL) == S_OK)
|
||||||
{
|
{
|
||||||
IPropertyBag *pPropBag;
|
IPropertyBag *pPropBag;
|
||||||
@ -124,10 +139,24 @@ int DisplayDeviceInformation(IEnumMoniker *pEnum)
|
|||||||
}
|
}
|
||||||
if (SUCCEEDED(hr))
|
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 "<<counter_device<<" NAME "<<name.c_str()<<"\n";
|
||||||
|
if (
|
||||||
|
wcsstr(var.bstrVal, L"MMP SDK") == nullptr &&
|
||||||
|
wcsstr(var.bstrVal, L"Oculus Rift") == nullptr &&
|
||||||
|
wcsstr(var.bstrVal, L"HTC Vive") == nullptr &&
|
||||||
|
wcsstr(var.bstrVal, L"RealSense") == nullptr &&
|
||||||
|
wcsstr(var.bstrVal, L"OBS") == nullptr
|
||||||
|
){
|
||||||
|
camera_ids.push_back(counter_device);
|
||||||
return_id_device = counter_device;
|
return_id_device = counter_device;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
__LOG_CAM__<<"RETURN DEVICE ID "<<return_id_device<<"\n";
|
||||||
|
|
||||||
VariantClear(&var);
|
VariantClear(&var);
|
||||||
}
|
}
|
||||||
@ -154,7 +183,8 @@ int DisplayDeviceInformation(IEnumMoniker *pEnum)
|
|||||||
pMoniker->Release();
|
pMoniker->Release();
|
||||||
++counter_device;
|
++counter_device;
|
||||||
}
|
}
|
||||||
return return_id_device;
|
|
||||||
|
return camera_ids;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -172,7 +202,7 @@ bool Webcam::Open(int width_, int height_) {
|
|||||||
height = height_;
|
height = height_;
|
||||||
|
|
||||||
|
|
||||||
int id_device = -1;
|
std::vector<int> id_devices;
|
||||||
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
|
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
|
||||||
if (SUCCEEDED(hr))
|
if (SUCCEEDED(hr))
|
||||||
{
|
{
|
||||||
@ -181,23 +211,38 @@ bool Webcam::Open(int width_, int height_) {
|
|||||||
hr = EnumerateDevices(CLSID_VideoInputDeviceCategory, &pEnum);
|
hr = EnumerateDevices(CLSID_VideoInputDeviceCategory, &pEnum);
|
||||||
if (SUCCEEDED(hr))
|
if (SUCCEEDED(hr))
|
||||||
{
|
{
|
||||||
id_device = DisplayDeviceInformation(pEnum);
|
id_devices = DisplayDeviceInformation(pEnum);
|
||||||
pEnum->Release();
|
pEnum->Release();
|
||||||
}
|
}
|
||||||
|
|
||||||
CoUninitialize();
|
CoUninitialize();
|
||||||
}
|
}
|
||||||
if (id_device == -1)
|
|
||||||
|
if (id_devices.size() == 0)
|
||||||
return false;
|
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 "<<device_id <<"\n";
|
||||||
|
|
||||||
InputVideo->set(CV_CAP_PROP_FRAME_WIDTH, width);
|
InputVideo->set(CV_CAP_PROP_FRAME_WIDTH, width);
|
||||||
InputVideo->set(CV_CAP_PROP_FRAME_HEIGHT, height);
|
InputVideo->set(CV_CAP_PROP_FRAME_HEIGHT, height);
|
||||||
InputVideo->set(CAP_PROP_FPS, webcam_fps);
|
InputVideo->set(CAP_PROP_FPS, webcam_fps);
|
||||||
|
|
||||||
if (!InputVideo->isOpened())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
outputVideo = new VideoWriter;
|
outputVideo = new VideoWriter;
|
||||||
ReplayInputVideo = new VideoCapture();
|
ReplayInputVideo = new VideoCapture();
|
||||||
|
|
||||||
@ -220,6 +265,8 @@ void Webcam::Reset(bool _reset_video) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Webcam::RecordFrame(float _clock) {
|
void Webcam::RecordFrame(float _clock) {
|
||||||
|
if (!thread_process)
|
||||||
|
return;
|
||||||
thread_process->mutex_record_frame.Lock();
|
thread_process->mutex_record_frame.Lock();
|
||||||
thread_process->record_frame = true;
|
thread_process->record_frame = true;
|
||||||
thread_process->current_frame_clock = _clock;
|
thread_process->current_frame_clock = _clock;
|
||||||
|
|||||||
Reference in New Issue
Block a user