Skip to content

VideoCapture MSMF plugin won't open using camera/device index. #23937

@pdy

Description

@pdy

System Information

OpenCV version: at least from 4.5.5 to master
Operating System / Platform: Windows 10/11
Compiler & compiler version: Visual Studio 2022

Detailed description

It looks like unnecessary nullptr check of optional argument https://github.com/opencv/opencv/blob/4.8.0/modules/videoio/src/cap_msmf.cpp#L2722
Below linked fragment with my comments. By commenting out pointed filename check I was able to open camera on my local machine.

static
CvResult CV_API_CALL cv_capture_open_with_params(
    const char* filename, int camera_index,
    int* params, unsigned n_params,
    CV_OUT CvPluginCapture* handle
)
{
    if (!handle)
        return CV_ERROR_FAIL;
    *handle = NULL;
    if (!filename)                           // <--- filename is optional, see below
        return CV_ERROR_FAIL;      
    CaptureT* cap = 0;
    try
    {
        cv::VideoCaptureParameters parameters(params, n_params);
        cap = new CaptureT();
        bool res;
        if (filename)                                                    // if filename is true then we use
            res = cap->open(std::string(filename), &parameters);         //
        else                                                             // however if it's nullptr
            res = cap->open(camera_index, &parameters);                  // then we use index to search device
        if (res)
        {
            *handle = (CvPluginCapture)cap;
            return CV_ERROR_OK;
        }
    }
    catch (const std::exception& e)
    {
        CV_LOG_WARNING(NULL, "MSMF: Exception is raised: " << e.what());
    }
    catch (...)
    {
        CV_LOG_WARNING(NULL, "MSMF: Unknown C++ exception is raised");
    }
    if (cap)
        delete cap;
    return CV_ERROR_FAIL;
}

Steps to reproduce

Open video capture from camera (for example, laptop's built-in camera) using MSMF backend cv::VideoCapture vc(0, cv::CAP_MSMF); built as a plugin.

It's also very likely, that it's the same issue as #23056

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions