Question Blackmagic Videohub

noumaan

New member
Joined
Aug 31, 2018
Messages
3
Programming Experience
Beginner
Hi Folks,

i'm trying to write Blackmagic Videohub application in c# but unfortunately BMD does not provide any sample project for C#.
please someone help me to understand about VideoHub interface and addref in C#

My Code is:
private Videohub.HubMonitor m_hubMonitor;
public Form1()
{
    InitializeComponent();
}
IVideohubDiscovery videoHubDiscovery;
public static IVideohub m_hub;
static IVideohubState m_state;
private void button1_Click(object sender, EventArgs e)
{
    videoHubDiscovery = new CVideohubDiscovery();
    try
    {
        videoHubDiscovery.ConnectTo(TBserver.Text, 10000, out m_hub);
    }
    catch(COMException ex)
    {
        if (ex.HResult == -2147467259)
            MessageBox.Show("Connection Timeout");
        return;
    }
    m_hub.GetState(out m_state);
    uint index = 0;
    _BMDVideohubHardwareState hardware = new _BMDVideohubHardwareState();
    System.Diagnostics.Debug.WriteLine("--- Video input ports\n");
    while (index <= 40 - 1)
    {
        m_state.GetLabel(_BMDVideohubPortType.BMDVideohubVideoInputPort, index, out string label);
        m_state.GetHardware(_BMDVideohubPortType.BMDVideohubVideoInputPort, index, out hardware);
        string bstrLabel = label;
        System.Diagnostics.Debug.WriteLine("  \n " + index + " " + hardware.ToString() + " " + bstrLabel);
        index++;
    }
    //m_hubMonitor = new Videohub.HubMonitor(m_hub);
    //m_hub.AddVideohubCallback(m_hubMonitor);
}

i'm getting input ports like this but when i'm trying to get input ports by calling callback i get E_NOINTERFACE error or even i try to get input ports from another method i get same E_NOInterface error

callback error line is:
public HubMonitor(IVideohub hub)
{
    m_hub = hub;
}

void IVideohubCallback.StateChanged()
{
    if (m_state == null)
    {
        m_hub.GetState(out m_state); //Getting Error Here NOINTERFACE
        DumpState(m_state);
    }
    else
    {
        m_hub.GetState(out m_state);
        DumpState(m_state);
    }
}
waiting for help

thanks and regards,
Noumaan
 
Last edited by a moderator:
thanks for your response .. appreciated !!

blackmagic provide SDK in C++ with example Link
according to sdk
Interface ReferenceEvery object interface subclasses the IUnknown interface.IUnknown InterfaceEach API interface is a subclass of the standard COM base class – IUnknown. The IUnknownobject interface provides reference counting and the ability to look up related interfaces byinterface ID. The interface ID mechanism allows interfaces to be added to the API withoutimpacting existing applications.
QueryInterface Provides access to supported childinterfaces of the object.
AddRef Increments the reference count of the object.
Release Decrements the reference count ofthe object. When the final reference isremoved, the object is freed.

IUnknown::QueryInterface methodThe QueryInterface method looks up a related interface of an object interface.
Syntax HRESULT QueryInterface(REFIID id, void **outputInterface);
IUnknown::AddRef methodThe AddRef method increments the reference count for an object interface.
Syntax ULONG AddRef();
IUnknown::Release methodThe Release method decrements the reference count for an object interface. When the lastreference is removed from an object, the object will be destroyed.
Syntax ULONG Release();

Example Code in C++:
/* -LICENSE-START-** Copyright (c) 2009 Blackmagic Design
**
** Permission is hereby granted, free of charge, to any person or organization
** obtaining a copy of the software and accompanying documentation covered by
** this license (the "Software") to use, reproduce, display, distribute,
** execute, and transmit the Software, and to prepare derivative works of the
** Software, and to permit third-parties to whom the Software is furnished to
** do so, all subject to the following:
** 
** The copyright notices in the Software and this entire statement, including
** the above license grant, this restriction and the following disclaimer,
** must be included in all copies of the Software, in whole or in part, and
** all derivative works of the Software, unless such copies or derivative
** works are solely in the form of machine-executable object code generated by
** a source language processor.
** 
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
** SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
** FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
** ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
** DEALINGS IN THE SOFTWARE.
** -LICENSE-END-
*/


#include <comutil.h>                // for _bstr_t
#include <iostream>
#include <objbase.h>                // Necessary for COM
#include <stdio.h>
#include <tchar.h>


#include "FormattedOutput.h"
#include "Menu.h"
#include "VideohubAPI_h.h"




class MyVideohubCallback : public IVideohubCallback
{
    int                    m_refCount;
    IVideohubState *    m_state;
    IVideohub *            m_hub;
    
public:
    
    MyVideohubCallback(IVideohub *hub) : m_hub(hub), m_refCount(0), m_state(NULL) {}
    ~MyVideohubCallback() 
    {
        if (m_state!=NULL)
            m_state->Release();
    }
    
    HRESULT STDMETHODCALLTYPE StateChanged()
    {
        if (m_state == NULL)
        {
            // it is the first we are called, get the IvideohubAttributes interface
            // and print some details about the Videohub device
            printDeviceInfo();


            // we havent got an IVideohubState yet, get one
            if(m_hub->GetState(&m_state)==S_OK)
            {
                dumpState(m_state);
            }
        }
        else
        {
            // we already have an IVideohubState, update it
            // and get an iterator over the changes
            IVideohubStateChangeIterator *iter;
            
            if (m_state->UpdateState(&iter)==S_OK)
            {
                dumpChanges(m_state, iter);
                iter->Release();
            }
        }


        // re-print the user menu
        printMenu();


        return S_OK;
    }
    
    HRESULT STDMETHODCALLTYPE DeviceDisconnected()
    {
        printf_s("******\tVideohub DISCONNECTED\t******\n");
        return S_OK;
    }
    
    HRESULT STDMETHODCALLTYPE DeviceReconnected()
    {
        printf_s("******\tVideohub RECONNECTED\t******\n");
        return S_OK;
    }


    HRESULT STDMETHODCALLTYPE NewDeviceConnected()
    {
        return S_OK;
    }
    
    // IUnknown interface methods
    HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv)
    {
        HRESULT            result = E_NOINTERFACE;


        *ppv = NULL;


        if (iid == IID_IUnknown)
        {
            *ppv = this;
            AddRef();
            result = S_OK;
        }
        else if (iid == IID_IVideohubCallback)
        {
            *ppv = (IVideohubCallback*)this;
            AddRef();
            result = S_OK;
        }
        
        return result;
    }
    
    ULONG STDMETHODCALLTYPE AddRef(void)
    {
        m_hub->AddRef();
        return InterlockedIncrement((LONG*)&m_refCount);
    }
    
    ULONG STDMETHODCALLTYPE Release(void)
    {
        long        newRefValue;


        m_hub->Release();
        newRefValue = InterlockedDecrement((LONG*)&m_refCount);
        if (newRefValue == 0)
        {
            delete this;
        }
        
        return newRefValue;    
    }


    void printDeviceInfo()
    {
        IVideohubAttributes *attributes = NULL;
        
        if (m_hub->QueryInterface(IID_IVideohubAttributes, (void **) &attributes)==S_OK)
        {
            // print some info about the device
            dumpVideohubInfo(attributes);
            
            // release the interface
            attributes->Release();
        }
        else
        {
            printf("Error obtaining the Attributes interface\n");
        }
    }
};


int _tmain(int argc, _TCHAR* argv[])
{
    HRESULT                    result;
    IVideohubDiscovery        *videoHubDiscovery = NULL;
    IVideohub                *videohub;
    MyVideohubCallback        *cb;




    // check the arguments
    if (argc!=2)
    {
        printf_s("usage: DeviceState <videohub device address>");
        return 1;
    }


    // initialise the device address
    _bstr_t deviceAddress(argv[1]);
    
    // Initialize COM on this thread
    result = CoInitialize(NULL);
    if (FAILED(result))
    {
        printf_s("Initialization of COM failed - result = %08x.\n", result);
        return 1;
    }


    // Create an IVideohubDiscovery object
    result = CoCreateInstance(CLSID_CVideohubDiscovery, NULL, CLSCTX_ALL, IID_IVideohubDiscovery, (void**)&videoHubDiscovery);
    if (FAILED(result))
    {
        printf_s("A Videohub Discovery object could not be created.\n");
        goto bail;
    }


    // get an IVideohub object for our device address
    if(videoHubDiscovery->ConnectTo(deviceAddress, 10000, &videohub) == S_OK)
    {
        // instantiate an IVideohubCallback object to receive state change notifications
        cb = new MyVideohubCallback(videohub);
        cb->AddRef();


        // register the callback object
        videohub->AddVideohubCallback(cb);


        // wait for user input
        // this function returns when the user selects the Quit option
        waitForUserInput(videohub);


        // remove the callback object
        videohub->RemoveVideohubCallback(cb);


        // release the callback object
        cb->Release();


        // release the IVideohub object
        videohub->Release();
    }
    else
    {
        printf_s("An IVideohub object could not be created for the given address\n");
    }


    // release the VideohubDiscovery object
    videoHubDiscovery->Release();


bail:
    // Uninitalize COM on this thread
    CoUninitialize();


    return 0;
}


and when i'm trying to use SDK in c# i'm getting E_NOINTERFACE error as i mentioned it in 1st post. i think i have to query interface everytime but i don't know how
 
Last edited:
Hi Noumaan,
How do you write Blackmagic Videohub applications in C#?

I can't add API to reference.
 
I hope he responds. He hasn't logged on since 2018.

Anyway, did you even look at the original post he made? It was in C#.
 
Back
Top Bottom