2

I am having a problem on windows 10 (not an issue on server 2016) where IMFSourceReader->ReadSample (reader->ReadSample in the code below) will never return if the device was unplugged during capture. This causes errors to not be raised when a device is unplugged during capture. Everything works fine if no device is unplugged. I would expect something like MF_E_INVALIDREQUEST to be returned but instead it just hangs forever. Any thoughts on what might be the issue or guidance would be highly appreciated.

capture::Sample *capture::MfDeviceSampleProvider::GetNextSample(bool video) {
    Sample *sample = NULL;
    IMFSample *mfSample = NULL;
    DWORD streamFlags;
    if (reader) {       
        HRESULT hr = reader->ReadSample(streamIndex, 0, NULL, &streamFlags, NULL, &mfSample);           
        LOG_IF_FAILED(hr, "Unable to read sample from device.");
        if (SUCCEEDED(hr)) {          
            if (streamFlags == 0) {
                if (type == DeviceBase::Type::Video) {
                    sample = new MFVideoSample(prop.type, mfSample, prop.size.width, prop.size.height);
                } else if (type == DeviceBase::Type::Audio) {
                    sample = new MFAudioSample(mfSample);
                }
            } else if (streamFlags & MF_SOURCE_READERF_STREAMTICK) {                          
                return GetNextSample(video);
        }
    } 
    SafeRelease(&mfSample);

    return sample;
}
`
Nick3306
  • 25
  • 4
  • https://stackoverflow.com/questions/34332619/detect-usb-camera-disconnection-using-imfsourcereadercallback – VuVirt Mar 02 '19 at 17:01
  • It might be a good reason for you to switch to asynchronous mode with the source reader: [Using the Source Reader in Asynchronous Mode](https://learn.microsoft.com/en-us/windows/desktop/medfound/using-the-source-reader-in-asynchronous-mode). Doing it synchronously you might want to try `IMFSourceReader::Flush` from a worker thread once you detect loss of the device (that is, expecting flush request to unlock the wait inside `ReadSample`). – Roman R. Mar 03 '19 at 09:25
  • @RomanR. Thanks, this will be the direction I take. Do you have any thoughts on why this is only a problem with video devices being removed on win 10? If an audio device is removed it correctly throws an error like `MF_E_AUDIO_RECORDING_DEVICE_INVALIDATED` which is what happens when a video device is disconnected when on server 2016 but not win 10. – Nick3306 Mar 06 '19 at 20:58
  • It's hard to tell because it's hidden in API implementation. I think it's not something intended, maybe they are hitting the problem described in this question: https://stackoverflow.com/questions/54927290/detecting-uvc-video-device-disconnection-wm-devicechange-works-on-windows-y-bu – Roman R. Mar 06 '19 at 21:23

0 Answers0