Correctly Using the IDeckLinkGLScreenPreviewHelper in C#

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

AndrewSinger

  • Posts: 4
  • Joined: Fri Aug 26, 2016 8:08 pm

Correctly Using the IDeckLinkGLScreenPreviewHelper in C#

PostTue Aug 30, 2016 9:51 pm

Hi again,

At this point I've got the framerate issue sorted out (thanks Nick!) but that didn't fix my black GL surface like I was hoping it would. I've followed the documentation's instructions and mimicked the functional CapturePreview C++ as much as possible but can't seem to get the frames to show. I've tested the SharpGL surface with other GL tasks and confirmed that it is functional, which means that I might not understand or have correctly implemented either the GLScreenPreviewHelper, or perhaps I've simply not setup the orthographic projection correctly (code adapted from documentation). I've confirmed the settings and device to be correct (by debugging the C++ project).

So that being said, does the code below look like I've correctly setup the surface and established the correct GL frame setting/painting sequence?

Thanks,
Andrew

Code: Select all
public partial class FormSharpGLTexturesSample : Form, IDeckLinkScreenPreviewCallback
    {
        private IDeckLinkInput m_deckLinkInput;
        private IDeckLinkGLScreenPreviewHelper m_deckLinkScreenPreviewHelper;
        private int frameCount = 0;
        private bool glIsInitialized = false;

        public FormSharpGLTexturesSample()
        {
            InitializeBlackMagicDevice();

            InitializeComponent();

            //SharpGL.OpenGL gl = this.openGLControl1.OpenGL;
            //gl.Enable(OpenGL.GL_TEXTURE_2D);

            openGLControl1.OpenGL.RenderContextProvider.MakeCurrent();
            m_deckLinkScreenPreviewHelper.InitializeGL();
            openGLControl1.OpenGL.MakeNothingCurrent();

            glIsInitialized = true;
        }

        private void InitializeBlackMagicDevice()
        {
            IDeckLink m_deckLink;
            IDeckLinkIterator deckLinkIterator = new CDeckLinkIterator();
            deckLinkIterator.Next(out m_deckLink);

            m_deckLinkScreenPreviewHelper = new CDeckLinkGLScreenPreviewHelper();

            m_deckLinkInput = (IDeckLinkInput)m_deckLink;
            m_deckLinkInput.SetScreenPreviewCallback(this);

            m_deckLinkInput.EnableVideoInput(_BMDDisplayMode.bmdModeHD720p5994, _BMDPixelFormat.bmdFormat8BitYUV, _BMDVideoInputFlags.bmdVideoInputFlagDefault);
            //m_deckLinkInput.EnableAudioInput(_BMDAudioSampleRate.bmdAudioSampleRate48kHz, _BMDAudioSampleType.bmdAudioSampleType16bitInteger, 2);
            m_deckLinkInput.StartStreams();

        }

        public void DrawFrame(IDeckLinkVideoFrame theFrame)
        {
            if (!glIsInitialized) return;

            m_deckLinkScreenPreviewHelper.SetFrame(theFrame);

            openGLControl1.OpenGL.RenderContextProvider.MakeCurrent();
            m_deckLinkScreenPreviewHelper.PaintGL();
            openGLControl1.OpenGL.MakeNothingCurrent();

            System.Runtime.InteropServices.Marshal.ReleaseComObject(theFrame);
            //frameCount++;
        }

        private void openGLControl1_OpenGLDraw(object sender, RenderEventArgs e)
        {
            SharpGL.OpenGL gl = this.openGLControl1.OpenGL;

            gl.Viewport(0, 0, (int)openGLControl1.Width, (int)openGLControl1.Height);
            gl.MatrixMode(MatrixMode.Projection);
            gl.LoadIdentity();
            gl.Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
            gl.MatrixMode(MatrixMode.Modelview);

            //this.label1.Text = frameCount.ToString();
        }

    }
Andrew Singer
Offline

Waqqas Sharif

Blackmagic Design

  • Posts: 21
  • Joined: Tue Oct 04, 2016 1:47 am

Re: Correctly Using the IDeckLinkGLScreenPreviewHelper in C#

PostTue Oct 04, 2016 4:23 am

Hi Andrew,

I have played around with your C# code and managed to get it running.

Capture2.PNG
Capture2.PNG (19.25 KiB) Viewed 924 times


See the code below.

Code: Select all
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;

using DeckLinkAPI;
using SharpGL;
using SharpGL.SceneGraph.Assets;

namespace SharpGLTexturesSample
{
    public partial class FormSharpGLTexturesSample : Form, IDeckLinkScreenPreviewCallback
    {
        private IDeckLinkInput m_deckLinkInput;
        private IDeckLinkGLScreenPreviewHelper m_deckLinkScreenPreviewHelper;
        private int frameCount = 0;
        private bool glIsInitialized = false;

        public FormSharpGLTexturesSample()
        {
            InitializeComponent();

            InitializeBlackMagicDevice();

            SharpGL.OpenGL gl = this.openGLControl1.OpenGL;

            gl.RenderContextProvider.MakeCurrent();
            m_deckLinkScreenPreviewHelper.InitializeGL();

            glIsInitialized = true;
        }

        private void InitializeBlackMagicDevice()
        {
            IDeckLink m_deckLink;
            IDeckLinkIterator deckLinkIterator = new CDeckLinkIterator();
            deckLinkIterator.Next(out m_deckLink);

            m_deckLinkScreenPreviewHelper = new CDeckLinkGLScreenPreviewHelper();

            m_deckLinkInput = (IDeckLinkInput)m_deckLink;
            m_deckLinkInput.SetScreenPreviewCallback(this);

            m_deckLinkInput.EnableVideoInput(_BMDDisplayMode.bmdModeHD720p5994, _BMDPixelFormat.bmdFormat8BitYUV, _BMDVideoInputFlags.bmdVideoInputFlagDefault);
            //m_deckLinkInput.EnableAudioInput(_BMDAudioSampleRate.bmdAudioSampleRate48kHz, _BMDAudioSampleType.bmdAudioSampleType16bitInteger, 2);
            m_deckLinkInput.StartStreams();
        }

        public void DrawFrame(IDeckLinkVideoFrame theFrame)
        {
            if (!glIsInitialized) return;

            m_deckLinkScreenPreviewHelper.SetFrame(theFrame);

            SharpGL.OpenGL gl = this.openGLControl1.OpenGL;

            gl.Translate(-1.5f, 0.0f, -6.0f);            // Move Left And Into The Screen
           
            gl.Flush();

            m_deckLinkScreenPreviewHelper.PaintGL();

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

        private void openGLControl1_OpenGLDraw(object sender, RenderEventArgs e)
        {
            SharpGL.OpenGL gl = this.openGLControl1.OpenGL;

            gl.Viewport(0, 0, (int)openGLControl1.Width, (int)openGLControl1.Height);

            gl.MatrixMode(SharpGL.Enumerations.MatrixMode.Projection);
            gl.LoadIdentity();
            gl.Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
            gl.MatrixMode(SharpGL.Enumerations.MatrixMode.Modelview);

            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);   // Clear The Screen And The Depth Buffer
            gl.LoadIdentity();               // Reset The View

            m_deckLinkScreenPreviewHelper.PaintGL();

            return;
        }

    }
}

You also need to specify FBO as the RenderContextType for OpenGLControl1 as opposed to DIBSection.

this.OpenGLControl1.RenderContextType = SharpGL.RenderContextType.FBO;

See this helps.

Return to Software Developers

Who is online

Users browsing this forum: No registered users and 1 guest