From febb4103f120afbe228c6d51ab1e75971335b087 Mon Sep 17 00:00:00 2001
From: Vasil Kolev <vasil@ludost.net>
Date: Tue, 2 Dec 2014 21:25:58 +0200
Subject: [PATCH 1/2] fix device_list and COM initialization failed in
 decklink_common

---
 libavdevice/decklink_common.cpp | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/libavdevice/decklink_common.cpp b/libavdevice/decklink_common.cpp
index 9a9e44b..39b96f8 100644
--- a/libavdevice/decklink_common.cpp
+++ b/libavdevice/decklink_common.cpp
@@ -38,18 +38,21 @@ extern "C" {
 #include "decklink_common.h"
 
 #ifdef _WIN32
-IDeckLinkIterator *CreateDeckLinkIteratorInstance(void)
+IDeckLinkIterator *ff_CreateDeckLinkIteratorInstance(AVFormatContext *avctx)
 {
     IDeckLinkIterator *iter;
 
-    if (CoInitialize(NULL) != S_OK) {
-        av_log(NULL, AV_LOG_ERROR, "COM initialization failed.\n");
+    HRESULT result;
+    // Initialize COM on this thread
+    result = CoInitialize(NULL);
+    if (FAILED(result)) {
+        av_log(avctx, AV_LOG_ERROR, "Initialization of COM failed - result = %08x.\n", result);
         return NULL;
     }
-
-    if (CoCreateInstance(CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL,
-                         IID_IDeckLinkIterator, (void**) &iter) != S_OK) {
-        av_log(NULL, AV_LOG_ERROR, "DeckLink drivers not installed.\n");
+    // Create an IDeckLinkIterator object to enumerate all DeckLink cards in the system
+    result = CoCreateInstance(CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL, IID_IDeckLinkIterator, (void**)&iter);
+    if (FAILED(result)) {
+        avlog(avctx, AV_LOG_ERROR, "A DeckLink iterator could not be created.  The DeckLink drivers may not be installed.\n");
         return NULL;
     }
 
@@ -168,7 +171,7 @@ int ff_decklink_set_format(AVFormatContext *avctx, decklink_direction_t directio
 int ff_decklink_list_devices(AVFormatContext *avctx)
 {
     IDeckLink *dl = NULL;
-    IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
+    IDeckLinkIterator *iter = ff_CreateDeckLinkIteratorInstance(avctx);
     if (!iter) {
         av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
         return AVERROR(EIO);
-- 
2.1.3

