1

我正在向 UWP 应用程序添加一个简单的网络摄像头预览。使预览启动并运行正常,因此我正在正确处理由引发的异常,InitializeAsync()并且StartPreviewAsync()无法正确捕获第二个异常。

我的代码基于显示相机预览,它说如果应用程序没有被授予对捕获设备的访问权限,它将UnauthorizedAccessException在调用InitializeAsync(). 对于 C++/WinRT,似乎可以捕获如下所示的hresult_error代码。E_ACCESSDENIED我已经通过在应用程序选项中关闭对网络摄像头的访问来对此进行了测试,并且 try/catch 块的工作方式与您期望的一样,弹出内容对话框并向用户解释问题。

该参考资料还说,如果另一个应用程序对捕获设备具有独占控制权,则StartPreviewAsync()应该抛出FileLoadException. 首先,我无法弄清楚该异常的 C++/WinRT 等价物是什么。其次,我似乎根本无法捕捉到任何异常。我尝试使用与我使用的相同类型的 catch 块,InitializeAsync()因为这是捕获异常中描述的内容,但是当它不起作用时,我尝试使用下面的块来捕获任何东西。文档说您可以在捕获异常时注册CaptureDeviceExclusiveControlStatusChanged事件,但是由于我无法捕获异常,因此我不确定在哪里进行该操作的合适位置,或者如果我的应用程序在另一个应用程序之后启动,该事件是否会触发已经控制了捕获设备。我从来没有看到任何文字OutputDebugString()在 catch 块中,但我在调试输出窗口中确实收到以下消息(两次):

在 DBRacing.exe 中的 0x00007FFC7114A839 (KernelBase.dll) 处引发异常:WinRT 发起错误 - 0xC00D3704:“由于缺乏硬件资源,硬件 MFT 无法开始流式传输。”。

似乎正在生成异常,由于某种原因我似乎无法捕捉到它。

在下面的代码中,与我的 ViewModel() 一起使用的方法只是提供对本地设置的访问,我存储上次使用的设备 ID 并且当我的应用程序拥有网络摄像头的独占控制权时一切正常。

所以,我的问题是:如何正确识别另一个应用程序何时对捕获设备具有独占控制权?

我有一个用于 MediaCapture 对象的私有页面类变量:

private:
    Windows::Media::Capture::MediaCapture m_mediaCapture;

导航到页面时,相机预览开始:

void VideoPage::OnNavigatedTo(NavigationEventArgs /*e*/) {

    StartPreviewAsync();
}

StartPreviewAsync() 定义如下:

Windows::Foundation::IAsyncAction VideoPage::StartPreviewAsync() {

    // if we have a previously used device, then we should check if it's valid and use it
    if (!ViewModel().hasDeviceID()) {

        // device picker to choose camera
        DevicePicker picker;

        // create a filter that only looks for video capture devices
        picker.Filter().SupportedDeviceClasses().Append(DeviceClass::VideoCapture);

        // show the picker below the button that opens it and get the chosen device
        DeviceInformation device = co_await picker.PickSingleDeviceAsync({ 0,0,100,100 });

        // the user can cancel the dialog before picking something
        if (!device)
            return;

        // store the device ID
        ViewModel().deviceID(device.Id());

        // store the device name
        ViewModel().deviceName(device.Name());
    }

    // settings for the media capture object (such as which camera to use)
    MediaCaptureInitializationSettings settings;

    // add the chosen device to the settings so we initialize on that camera
    settings.VideoDeviceId(ViewModel().deviceID());

    try {

        // initialize the mediacapture object using the chosen camera
        co_await m_mediaCapture.InitializeAsync(settings);

        // dont let the screen go to sleep while the preview is active
        m_displayRequest.RequestActive();
    }

    // an exception is thrown if the user does not allow access to the camera
    catch (winrt::hresult_error const& ex) {

        winrt::hresult hr = ex.to_abi();

        if (hr == E_ACCESSDENIED) {

            ContentDialog msg;

            // set all the options for the dialog
            msg.Title(box_value(L"Access Denied"));
            msg.Content(box_value(L"This App has not been given permission to use the Camera and/or Microphone.\nPlease go to the settings in Windows for this App to allow access."));
            msg.CloseButtonText(L"OK");

            // Show the message dialog.
            msg.ShowAsync();
        }

        return;
    }

    try {

        // assign the source to the Capture Element on the XAML page
        capturePreview().Source(m_mediaCapture);

        co_await m_mediaCapture.StartPreviewAsync();
    }

    // This method should throw a FileLoadException (0x80070020) if another app has exclusive control of the capture device
    catch(...) {

        OutputDebugString(L"Exception Message\n");

        return;
    }
}
4

1 回答 1

0

通过我的测试,当我在捕获异常之前第一次注册 CaptureDeviceExclusiveControlStatusChanged 事件时,其中一个应用程序已经使用了相机。之后,我运行另一个应用程序,它也将使用相同的相机,它可以捕获异常。您可以尝试先添加事件进行测试,如下所示,mediaCapture.Failed事件具有相同的效果。

try 
{
    DisplayRequest displayRequest = DisplayRequest();
    m_mediaCapture = MediaCapture();
    // initialize the mediacapture object using the chosen camera
    co_await m_mediaCapture.InitializeAsync();
    //Register
    m_mediaCapture.CaptureDeviceExclusiveControlStatusChanged({ this, &MainPage::MediaCapture_CaptureDeviceExclusiveControlStatusChanged });

    displayRequest.RequestActive();
}
catch (winrt::hresult_error const& ex) 
{
    winrt::hresult hr = ex.to_abi();
    if (hr == E_ACCESSDENIED) {
    }

    return;
}

try 
{
    PreviewControl().Source(m_mediaCapture);
    co_await m_mediaCapture.StartPreviewAsync();
}
catch (winrt::hresult_error const& ex)
{
    winrt::hresult hr = ex.to_abi(); // HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND).
    winrt::hstring message = ex.message(); // The system cannot find the file specified.
}

//event
void MainPage::MediaCapture_CaptureDeviceExclusiveControlStatusChanged(MediaCapture const&, MediaCaptureDeviceExclusiveControlStatusChangedEventArgs const&)
{
    throw hresult_not_implemented();
}
于 2019-10-25T09:04:05.230 回答