WPF problems: InvalidCastException/Invalid Frames

Ask software engineering and SDK questions for developers working on Mac OS X, Windows or Linux.
  • Author
  • Message
Offline

andrei.nicolaescu15

  • Posts: 2
  • Joined: Mon Aug 09, 2021 2:14 pm
  • Real Name: Andrei Nicolaescu

WPF problems: InvalidCastException/Invalid Frames

PostMon Aug 09, 2021 3:56 pm

Hello,

I've been trying to create a WPF project (.NET Framework 4.7.2) to grab the stream (and render it frame by frame in an Image WPF Control) from a SDI camera connected to a Decklink Duo capture card. I've been unable to do so as of yet, encountering some difficulties.

For a start, I have so far tried two codebases (API implementations, straight from official Blackmagic examples/samples). Firstly, from the "Blackmagic Video Capture and Playback", I used the "DeckLinkCapture" example. More exactly, the "DeckLinkInput.cs" implementation of an input device. Next, I took a look at the "CapturePreviewCSharp" example, and I used the "DeckLinkDevice.cs" implementation of a DeckLink device. The code is similar when comparing the two, but there are small differences. Unfortunately, both these options resulted in the same problems.

For the video parameters, I've been tinkering with the Blackmagic Media Express desktop application and have used some settings that I know for sure work.
I set the display mode for 720p59.94, and the pixel format to 10bitRGB.

Now, the problems are as follows:

1. When setting the "flags" of the EnableVideoInput method to 1 (_BMDVideoInputFlags.bmdVideoInputEnableFormatDetection), telling it to watch for format changes, at some point (after receiving several frames - all of them invalid for some reason) the
"VideoInputFormatChanged" callback is triggered and the API tries to reinitialize the stream with the new detected settings. However, the first line (inside the callback) involving my m_deckLinkInput object crashed with the dreaded m_deckLinkInput error.
It tell me that it was Unable to cast COM object of type 'System.__ComObject' to interface type 'DeckLinkAPI.IDeckLinkInput'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{C21CDB6E-F414-46E4-A636-80A566E0ED37}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

Code: Select all
void IDeckLinkInputCallback.VideoInputFrameArrived(IDeckLinkVideoInputFrame videoFrame, IDeckLinkAudioInputPacket audioPacket)
        {
            if (videoFrame != null)
            {
                bool inputSignalAbsent = videoFrame.GetFlags().HasFlag(_BMDFrameFlags.bmdFrameHasNoInputSource);

                // Detect change in input signal, restart stream when valid stream detected
                bool restartStream = !inputSignalAbsent && m_prevInputSignalAbsent;
                if (restartStream)
                {
                    m_deckLinkInput.StopStreams();
                    m_deckLinkInput.FlushStreams();
                    m_deckLinkInput.StartStreams();
                }
                m_prevInputSignalAbsent = inputSignalAbsent;

                var frameWidth = videoFrame.GetWidth();
                var frameHeight = videoFrame.GetHeight();

                long frameTime, frameDuration;
                videoFrame.GetStreamTime(out frameTime, out frameDuration, timeScale);
                Debug.WriteLine(value: "Frame received #" + frameCount++ + "; Frame time = " + frameTime +
                    "; Frame size = " + frameWidth + "x" + frameHeight + "; " + (inputSignalAbsent ? "Invalid" : "Valid")
                    + (restartStream ? " - restarting" : ""));

                // Register video frame received event
                var handler = VideoFrameArrivedHandler;
                if (handler != null)
                {
                    handler.Invoke(this, new DeckLinkVideoFrameArrivedEventArgs(videoFrame, inputSignalAbsent));
                }
            }

            System.Runtime.InteropServices.Marshal.ReleaseComObject(videoFrame);
        }


This is pretty weird, as all the calls to my "DeckLinkInput" object (before this callback is triggered) work perfectly fine.

I have read another forum post and have seen that some people managed to make this work by launching MTA threads for the instructions that trigger this error. However, after several attempts and different methods of implementing this workaround, I have not been able to make it work. Still, the error persisted.

2. If I use the default flags (which means an eventual format change is not detected), I receive frames without error, but here's the catch: even though I am grabbing frames from the correct port (the one on which the SDI camera is connected), all of them come back with the "invalid - has no input source" flag. Basically, it tells me that there is no input signal coming from the port. This instruction returns true:

Code: Select all
bool inputSignalAbsent = videoFrame.GetFlags().HasFlag(_BMDFrameFlags.bmdFrameHasNoInputSource);


What can possibly be wrong here?

---------------------------------

I don't think I need to detect the format changes of the stream while it is running, but I still do not understand how all these frames come up as invalid. I am using an iterator to detect the 4 ports available and then I select one of the 4 to grab frames from. I am pretty sure I'm trying to grab frames
from the SDI camera (and not from an empty port). In any way, all 4 ports, camera connected or not, return invalid frames. In the Blackmagic Media Express application, the correct port works just fine.

Any suggestion is highly appreciated.

Thank you in advance,
Andrei Nicolaescu
Offline

Cameron Nichols

Blackmagic Design

  • Posts: 443
  • Joined: Mon Sep 04, 2017 4:05 am

Re: WPF problems: InvalidCastException/Invalid Frames

PostTue Aug 10, 2021 7:39 am

Hi Andrei,

With your capture application running, please also run the DeviceStatusCSharp sample from the SDK package. This may help determine whether you are matching the colorspace that is detected with that enabled by EnableVideoInput. I suspect there is a mismatch in formats that is leading to bmdFrameHasNoInputSource flag being set.

If you are capturing RGB 4:4:4, however the detected signal is YUV 4:2:2, then you will need to use IDeckLinkVideoConversion interface to convert from YUV to RGB, please refer to the StillsCSharp SDK sample for an example of how to do this (see Bgra32VideoFrame.cs implementation).

Please also refer to the DeviceStatusCSharp sample, particularly the ApartmentHelper.cs. Here the sample provides apartment thread helpers for calling DeckLinkAPI from a WPF application.

With the E_NOINTERFACE issue, can I check what version of Desktop Video you have installed? Perhaps you have mismatch of versions and you are running a version prior to 11.5.1 and the REFIID is not recognised. To avoid mismatch, ensure that you are building using the installed DeckLink API type library in your Visual Studio solution.

Regards
Cameron
Offline

andrei.nicolaescu15

  • Posts: 2
  • Joined: Mon Aug 09, 2021 2:14 pm
  • Real Name: Andrei Nicolaescu

Re: WPF problems: InvalidCastException/Invalid Frames

PostThu Aug 12, 2021 1:07 pm

Greetings Cameron,

Sorry for my late reply and thank you sincerely for your prompt one.

Everything you said has been correct in my case. I have used the DeviceStatusCSharp sample to figure out what kind of input I am receiving and tweaked my VideoInput parameters accordingly. Indeed, it proved to be 720p59.94 but the PixelFormat was wrong. I needed to use 10bitYUV.

Afterwards, the frames which were coming from the board were finally valid. However, I still had to convert them and as you said, I used the StillsCSharp sample to figure out how to do that.

In the end, after scratching my head for a bit, I figured out that all those E_NOINTERFACE issues were caused by the fact that my API calls were not launched into a MTA Thread. Fair enough, I again followed your advice and imported the ApartmentHelper.cs implementation into my project (taken, of course, from the DeviceStatusCSharp example).

I am using Destkop Video version 12.1, and it is correctly installed and configured into my Visual Studio solution, so it wasn't about that, apparently.

Thanks a lot for your insight. Feel free to close this topic if you so desire, as my issues have been resolved.

Best regards,
Andrei Nicolaescu

Return to Software Developers

Who is online

Users browsing this forum: No registered users and 11 guests