C#, STA Mode and IDeckLinkVideoConversion.ConvertFrame

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

JakeSimpson

  • Posts: 2
  • Joined: Sun Oct 04, 2020 5:23 am
  • Real Name: Jake Simpson

C#, STA Mode and IDeckLinkVideoConversion.ConvertFrame

PostSun Oct 04, 2020 5:31 am

I'm using the Blackmagic C# SDK in a C# app.

Thankfully, there is one provided.

Unthankfully, it appears to want to run in MTA mode. I get why - being able to have callbacks happen on the drivers frequency, rather than from the straight windows message pump (ie on the windows app frequency) is essential for timecoded video. Totally understand that.

However, I cannot run my app in MTA mode by default - there's too much .net functionality I require that doesn't work in MTA mode (drag and drop, fileopen dialogs, etc) to be viable.

Now, I've managed to put almost all the necessary code that I've cut n pasted from the C# sample app in side of MTA thread calls
E.g.

Code: Select all
 m_selectedCaptureDevice = device;
                m_selectedCaptureDevice.VideoFrameArrivedHandler += FrameArrived;
                Thread t = new Thread((ThreadStart)(() =>
                {
                    List<IDeckLinkDisplayMode> modes = new List<IDeckLinkDisplayMode>();
                    foreach (IDeckLinkDisplayMode displayMode in m_selectedCaptureDevice)
                    {
                        modes.Add(displayMode);
                    }
                    m_selectedCaptureDevice.StartCapture(modes[0], null, true);
                }));

                // Run your code from a thread that joins the MTA Thread
                t.SetApartmentState(ApartmentState.MTA);
                t.Start();
                t.Join();


And all is good, except..
The call to convert video frame formats , when a frame is received by the app.

I'm doing this -
Code: Select all
public IDeckLinkVideoFrame ConvertFrame(IDeckLinkVideoFrame srcFrame)
        {
         IDeckLinkVideoFrame dstFrame = srcFrame;
            _BMDPixelFormat blah = srcFrame.GetPixelFormat();

            // Check whether srcFrame is already bmdFormat8BitBGRA
            if (blah != _BMDPixelFormat.bmdFormat8BitBGRA)
            {
                Thread t = new Thread((ThreadStart)(() =>
                {
                    dstFrame = new Bgra32VideoFrame(srcFrame.GetWidth(), srcFrame.GetHeight(), srcFrame.GetFlags()) as IDeckLinkVideoFrame;
                    m_deckLinkConversion.ConvertFrame(srcFrame, dstFrame);
                }));

                // Run your code from a thread that joins the MTA Thread
                t.SetApartmentState(ApartmentState.MTA);
                t.Start();
                t.Join();
            }

            return dstFrame;
        }

But I still crash inside of the ConvertFrame function, even though it's called from an MTA thread?

The only thing I can think of is that there is a callback inside that function, that then does a look up assuming MTA format, and that's crashing.

What can I do about this? This is somewhat crucial for the app I am writing.
Offline

JakeSimpson

  • Posts: 2
  • Joined: Sun Oct 04, 2020 5:23 am
  • Real Name: Jake Simpson

Re: C#, STA Mode and IDeckLinkVideoConversion.ConvertFrame

PostMon Oct 05, 2020 6:53 pm

Solved!

Turns out, I had to create the IDeckLinkVideoConversion instance inside of an MTA thread, and then the code I had written works!

E.g.

Code: Select all
        public Bgra32FrameConverter()
        {
            Thread t = new Thread((ThreadStart)(() =>
            {
                m_deckLinkConversion = new CDeckLinkVideoConversion();
            }));

            // Run your code from a thread that joins the MTA Thread
            t.SetApartmentState(ApartmentState.MTA);
            t.Start();
            t.Join();
        }

Return to Software Developers

Who is online

Users browsing this forum: No registered users and 52 guests