mirror of
https://github.com/outbackdingo/UltraGrid.git
synced 2026-03-21 08:40:19 +00:00
use common com_[un]initialize
This commit is contained in:
@@ -71,6 +71,7 @@ using std::string;
|
|||||||
using std::wstring;
|
using std::wstring;
|
||||||
|
|
||||||
struct state_acap_wasapi {
|
struct state_acap_wasapi {
|
||||||
|
bool com_initialized;
|
||||||
struct audio_frame frame;
|
struct audio_frame frame;
|
||||||
IMMDevice *pDevice;
|
IMMDevice *pDevice;
|
||||||
IAudioClient *pAudioClient;
|
IAudioClient *pAudioClient;
|
||||||
@@ -91,13 +92,13 @@ static void audio_cap_wasapi_probe(struct device_info **available_devices, int *
|
|||||||
*deleter = free;
|
*deleter = free;
|
||||||
*available_devices = (struct device_info *) malloc(0);
|
*available_devices = (struct device_info *) malloc(0);
|
||||||
*dev_count = 0;
|
*dev_count = 0;
|
||||||
|
bool com_initialized = false;
|
||||||
|
|
||||||
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
|
if (!com_initialize(&com_initialized, MOD_NAME)) {
|
||||||
IMMDeviceEnumerator *enumerator = nullptr;
|
|
||||||
IMMDeviceCollection *pEndpoints = nullptr;
|
|
||||||
if (hr != S_OK && hr != S_FALSE) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
IMMDeviceEnumerator *enumerator = nullptr;
|
||||||
|
IMMDeviceCollection *pEndpoints = nullptr;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
THROW_IF_FAILED(CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator,
|
THROW_IF_FAILED(CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator,
|
||||||
@@ -127,7 +128,7 @@ static void audio_cap_wasapi_probe(struct device_info **available_devices, int *
|
|||||||
}
|
}
|
||||||
SAFE_RELEASE(enumerator);
|
SAFE_RELEASE(enumerator);
|
||||||
SAFE_RELEASE(pEndpoints);
|
SAFE_RELEASE(pEndpoints);
|
||||||
CoUninitialize();
|
com_uninitialize(&com_initialized);
|
||||||
}
|
}
|
||||||
|
|
||||||
static string wstring_to_string(wstring const & wstr) {
|
static string wstring_to_string(wstring const & wstr) {
|
||||||
@@ -180,10 +181,10 @@ static void show_help() {
|
|||||||
style::bold << fg::red << "\t-s wasapi" << fg::reset << "[:<index>|:<ID>]\n" << style::reset <<
|
style::bold << fg::red << "\t-s wasapi" << fg::reset << "[:<index>|:<ID>]\n" << style::reset <<
|
||||||
"\nAvailable devices:\n";
|
"\nAvailable devices:\n";
|
||||||
|
|
||||||
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
|
|
||||||
IMMDeviceEnumerator *enumerator = nullptr;
|
IMMDeviceEnumerator *enumerator = nullptr;
|
||||||
IMMDeviceCollection *pEndpoints = nullptr;
|
IMMDeviceCollection *pEndpoints = nullptr;
|
||||||
if (hr != S_OK && hr != S_FALSE) {
|
bool com_initialized = false;
|
||||||
|
if (!com_initialize(&com_initialized, MOD_NAME)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,7 +213,7 @@ static void show_help() {
|
|||||||
}
|
}
|
||||||
SAFE_RELEASE(enumerator);
|
SAFE_RELEASE(enumerator);
|
||||||
SAFE_RELEASE(pEndpoints);
|
SAFE_RELEASE(pEndpoints);
|
||||||
CoUninitialize();
|
com_uninitialize(&com_initialized);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void * audio_cap_wasapi_init(struct module *parent, const char *cfg)
|
static void * audio_cap_wasapi_init(struct module *parent, const char *cfg)
|
||||||
@@ -232,11 +233,11 @@ static void * audio_cap_wasapi_init(struct module *parent, const char *cfg)
|
|||||||
mbtowc(deviceID, cfg, (sizeof deviceID / 2) - 1);
|
mbtowc(deviceID, cfg, (sizeof deviceID / 2) - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
|
auto s = new state_acap_wasapi();
|
||||||
if (hr != S_OK && hr != S_FALSE) {
|
if (!com_initialize(&s->com_initialized, MOD_NAME)) {
|
||||||
|
delete s;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
auto s = new state_acap_wasapi();
|
|
||||||
IMMDeviceEnumerator *enumerator = nullptr;
|
IMMDeviceEnumerator *enumerator = nullptr;
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
|||||||
@@ -73,6 +73,7 @@ using std::wcout;
|
|||||||
using std::wstring;
|
using std::wstring;
|
||||||
|
|
||||||
struct state_aplay_wasapi {
|
struct state_aplay_wasapi {
|
||||||
|
bool com_initialized;
|
||||||
struct audio_desc desc;
|
struct audio_desc desc;
|
||||||
IMMDevice *pDevice;
|
IMMDevice *pDevice;
|
||||||
IAudioClient *pAudioClient;
|
IAudioClient *pAudioClient;
|
||||||
@@ -122,10 +123,10 @@ static void audio_play_wasapi_probe(struct device_info **available_devices, int
|
|||||||
*available_devices = (struct device_info *) malloc(0);
|
*available_devices = (struct device_info *) malloc(0);
|
||||||
*dev_count = 0;
|
*dev_count = 0;
|
||||||
|
|
||||||
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
|
|
||||||
IMMDeviceEnumerator *enumerator = nullptr;
|
IMMDeviceEnumerator *enumerator = nullptr;
|
||||||
IMMDeviceCollection *pEndpoints = nullptr;
|
IMMDeviceCollection *pEndpoints = nullptr;
|
||||||
if (hr != S_OK && hr != S_FALSE) {
|
bool com_initialized = false;
|
||||||
|
if (!com_initialize(&com_initialized, MOD_NAME)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,7 +158,7 @@ static void audio_play_wasapi_probe(struct device_info **available_devices, int
|
|||||||
}
|
}
|
||||||
SAFE_RELEASE(enumerator);
|
SAFE_RELEASE(enumerator);
|
||||||
SAFE_RELEASE(pEndpoints);
|
SAFE_RELEASE(pEndpoints);
|
||||||
CoUninitialize();
|
com_uninitialize(&com_initialized);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void audio_play_wasapi_help(const char *driver_name)
|
static void audio_play_wasapi_help(const char *driver_name)
|
||||||
@@ -171,8 +172,8 @@ static void show_help() {
|
|||||||
style::bold << fg::red << "\t-r wasapi" << fg::reset << "[:<index>|:<ID>] --param audio-buffer-len=<ms>\n" << style::reset <<
|
style::bold << fg::red << "\t-r wasapi" << fg::reset << "[:<index>|:<ID>] --param audio-buffer-len=<ms>\n" << style::reset <<
|
||||||
"\nAvailable devices:\n";
|
"\nAvailable devices:\n";
|
||||||
|
|
||||||
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
|
bool com_initialized = false;
|
||||||
if (hr != S_OK && hr != S_FALSE) {
|
if (!com_initialize(&com_initialized, MOD_NAME)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
IMMDeviceEnumerator *enumerator = nullptr;
|
IMMDeviceEnumerator *enumerator = nullptr;
|
||||||
@@ -204,7 +205,7 @@ static void show_help() {
|
|||||||
}
|
}
|
||||||
SAFE_RELEASE(enumerator);
|
SAFE_RELEASE(enumerator);
|
||||||
SAFE_RELEASE(pEndpoints);
|
SAFE_RELEASE(pEndpoints);
|
||||||
CoUninitialize();
|
com_uninitialize(&com_initialized);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void * audio_play_wasapi_init(const char *cfg)
|
static void * audio_play_wasapi_init(const char *cfg)
|
||||||
@@ -222,11 +223,11 @@ static void * audio_play_wasapi_init(const char *cfg)
|
|||||||
mbtowc(deviceID, cfg, (sizeof deviceID / 2) - 1);
|
mbtowc(deviceID, cfg, (sizeof deviceID / 2) - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
|
auto s = new state_aplay_wasapi();
|
||||||
if (hr != S_OK && hr != S_FALSE) {
|
if (!com_initialize(&s->com_initialized, MOD_NAME)) {
|
||||||
|
delete s;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
auto s = new state_aplay_wasapi();
|
|
||||||
IMMDeviceEnumerator *enumerator = nullptr;
|
IMMDeviceEnumerator *enumerator = nullptr;
|
||||||
try {
|
try {
|
||||||
|
|
||||||
@@ -266,7 +267,7 @@ static void * audio_play_wasapi_init(const char *cfg)
|
|||||||
}
|
}
|
||||||
} catch (ug_runtime_error &e) {
|
} catch (ug_runtime_error &e) {
|
||||||
LOG(LOG_LEVEL_ERROR) << MOD_NAME << e.what() << "\n";
|
LOG(LOG_LEVEL_ERROR) << MOD_NAME << e.what() << "\n";
|
||||||
CoUninitialize();
|
com_uninitialize(&s->com_initialized);
|
||||||
delete s;
|
delete s;
|
||||||
s = nullptr;
|
s = nullptr;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
* @author Martin Pulec <pulec@cesnet.cz>
|
* @author Martin Pulec <pulec@cesnet.cz>
|
||||||
*/
|
*/
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2014-2021 CESNET, z. s. p. o.
|
* Copyright (c) 2014-2023 CESNET, z. s. p. o.
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
@@ -147,7 +147,7 @@ IDeckLinkIterator *create_decklink_iterator(bool *com_initialized, bool verbose,
|
|||||||
IDeckLinkIterator *deckLinkIterator = nullptr;
|
IDeckLinkIterator *deckLinkIterator = nullptr;
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
if (coinit) {
|
if (coinit) {
|
||||||
com_initialize(com_initialized);
|
com_initialize(com_initialized, "[BMD] ");
|
||||||
}
|
}
|
||||||
HRESULT result = CoCreateInstance(CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL,
|
HRESULT result = CoCreateInstance(CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL,
|
||||||
IID_IDeckLinkIterator, (void **) &deckLinkIterator);
|
IID_IDeckLinkIterator, (void **) &deckLinkIterator);
|
||||||
@@ -183,7 +183,7 @@ bool blackmagic_api_version_check()
|
|||||||
HRESULT result;
|
HRESULT result;
|
||||||
bool com_initialized = false;
|
bool com_initialized = false;
|
||||||
|
|
||||||
if (!com_initialize(&com_initialized)) {
|
if (!com_initialize(&com_initialized, "[BMD] ")) {
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
@@ -230,11 +230,8 @@ void print_decklink_version()
|
|||||||
HRESULT result;
|
HRESULT result;
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
// Initialize COM on this thread
|
bool com_initialized = false;
|
||||||
result = CoInitializeEx(NULL, COINIT_MULTITHREADED);
|
if (!com_initialize(&com_initialized, "[BMD] ")) {
|
||||||
if(FAILED(result)) {
|
|
||||||
fprintf(stderr, "Initialize of COM failed - result = "
|
|
||||||
"%08lx.\n", result);
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,7 +263,7 @@ cleanup:
|
|||||||
APIInformation->Release();
|
APIInformation->Release();
|
||||||
}
|
}
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
CoUninitialize();
|
com_uninitialize(&com_initialized);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
15
src/host.cpp
15
src/host.cpp
@@ -142,11 +142,7 @@ void common_cleanup(struct init_data *init)
|
|||||||
dlclose(a);
|
dlclose(a);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef _WIN32
|
com_uninitialize(&init->com_initialized);
|
||||||
if (init->com_initialized) {
|
|
||||||
CoUninitialize();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
delete init;
|
delete init;
|
||||||
|
|
||||||
@@ -412,14 +408,7 @@ struct init_data *common_preinit(int argc, char *argv[])
|
|||||||
|
|
||||||
// Initialize COM on main thread - otherwise Portaudio would initialize it as COINIT_APARTMENTTHREADED but MULTITHREADED
|
// Initialize COM on main thread - otherwise Portaudio would initialize it as COINIT_APARTMENTTHREADED but MULTITHREADED
|
||||||
// is perhaps better variant (Portaudio would accept that).
|
// is perhaps better variant (Portaudio would accept that).
|
||||||
HRESULT result = CoInitializeEx(NULL, COINIT_MULTITHREADED);
|
com_initialize(&init.com_initialized, nullptr);
|
||||||
if (SUCCEEDED(result)) {
|
|
||||||
init.com_initialized = true;
|
|
||||||
} else if (result == RPC_E_CHANGED_MODE) {
|
|
||||||
log_msg(LOG_LEVEL_WARNING, "COM already intiialized in a different mode!\n");
|
|
||||||
} else {
|
|
||||||
log_msg(LOG_LEVEL_ERROR, "Initialize of COM failed - %s\n", hresult_to_str(result));
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (strstr(argv[0], "run_tests") == nullptr) {
|
if (strstr(argv[0], "run_tests") == nullptr) {
|
||||||
|
|||||||
@@ -48,8 +48,11 @@
|
|||||||
#include <objbase.h>
|
#include <objbase.h>
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
|
||||||
bool com_initialize(bool *com_initialized)
|
bool com_initialize(bool *com_initialized, const char *err_prefix)
|
||||||
{
|
{
|
||||||
|
if (err_prefix == nullptr) {
|
||||||
|
err_prefix = "";
|
||||||
|
}
|
||||||
*com_initialized = false;
|
*com_initialized = false;
|
||||||
// Initialize COM on this thread
|
// Initialize COM on this thread
|
||||||
HRESULT result = CoInitializeEx(NULL, COINIT_MULTITHREADED);
|
HRESULT result = CoInitializeEx(NULL, COINIT_MULTITHREADED);
|
||||||
@@ -58,10 +61,10 @@ bool com_initialize(bool *com_initialized)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (result == RPC_E_CHANGED_MODE) {
|
if (result == RPC_E_CHANGED_MODE) {
|
||||||
LOG(LOG_LEVEL_WARNING) << "COM already intiialized with a different mode!\n";
|
LOG(LOG_LEVEL_WARNING) << err_prefix << "COM already intiialized with a different mode!\n";
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
LOG(LOG_LEVEL_ERROR) << "Initialize of COM failed - " << hresult_to_str(result) << "\n";
|
LOG(LOG_LEVEL_ERROR) << err_prefix << "Initialize of COM failed - " << hresult_to_str(result) << "\n";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,7 +108,7 @@ const char *hresult_to_str(HRESULT res) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
bool com_initialize(bool *com_initialized [[maybe_unused]])
|
bool com_initialize(bool *com_initialized [[maybe_unused]], const char *err_prefix [[maybe_unused]])
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,8 +65,11 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
///< param pass a pointer to a bool that will be passed to com_uninintialize()
|
/**
|
||||||
bool com_initialize(bool *com_initialized);
|
* @param[out] com_initialize a pointer to a bool that will be passed to com_uninintialize()
|
||||||
|
* @param[in] err_prefix optional error prefix to be used for eventual error messges (may be NULL)
|
||||||
|
*/
|
||||||
|
bool com_initialize(bool *com_initialized, const char *err_prefix);
|
||||||
///< @param com_initialized - pointer passed to com_initialize (or create_com_iterator)
|
///< @param com_initialized - pointer passed to com_initialize (or create_com_iterator)
|
||||||
void com_uninitialize(bool *com_initialized);
|
void com_uninitialize(bool *com_initialized);
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
#include "tv.h"
|
#include "tv.h"
|
||||||
#include "utils/color_out.h"
|
#include "utils/color_out.h"
|
||||||
#include "utils/macros.h"
|
#include "utils/macros.h"
|
||||||
|
#include "utils/windows.h"
|
||||||
#include "video.h"
|
#include "video.h"
|
||||||
#include "video_capture.h"
|
#include "video_capture.h"
|
||||||
|
|
||||||
@@ -75,6 +76,7 @@ static int *yuv_clamp;
|
|||||||
class SampleGrabberCallback;
|
class SampleGrabberCallback;
|
||||||
|
|
||||||
struct vidcap_dshow_state {
|
struct vidcap_dshow_state {
|
||||||
|
bool com_initialized;
|
||||||
int deviceNumber;
|
int deviceNumber;
|
||||||
char *deviceName;
|
char *deviceName;
|
||||||
int modeNumber;
|
int modeNumber;
|
||||||
@@ -216,7 +218,7 @@ static bool cleanup(struct vidcap_dshow_state *s) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// COM library uninitialization
|
// COM library uninitialization
|
||||||
CoUninitialize();
|
com_uninitialize(&s->com_initialized);
|
||||||
|
|
||||||
if (s->frame != NULL) vf_free(s->frame);
|
if (s->frame != NULL) vf_free(s->frame);
|
||||||
if (s->grabBuffer != NULL) free(s->grabBuffer);
|
if (s->grabBuffer != NULL) free(s->grabBuffer);
|
||||||
@@ -266,10 +268,7 @@ static bool common_init(struct vidcap_dshow_state *s) {
|
|||||||
|
|
||||||
// Initialize COM library
|
// Initialize COM library
|
||||||
// COINIT_APARTMENTTHREADED is used because we do not expect any other thread to work with the object
|
// COINIT_APARTMENTTHREADED is used because we do not expect any other thread to work with the object
|
||||||
res = CoInitializeEx(NULL, COINIT_MULTITHREADED);
|
if (com_initialize(&s->com_initialized, "widcap_dshow_init: ")) {
|
||||||
if (res != S_OK && res != S_FALSE && res != RPC_E_CHANGED_MODE) {
|
|
||||||
log_msg(LOG_LEVEL_ERROR, MOD_NAME "vidcap_dshow_init: COM library initialization failed.\n");
|
|
||||||
ErrorDescription(res);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -183,7 +183,7 @@ struct vidcap_decklink_state {
|
|||||||
void set_codec(codec_t c);
|
void set_codec(codec_t c);
|
||||||
|
|
||||||
vidcap_decklink_state() {
|
vidcap_decklink_state() {
|
||||||
if (!com_initialize(&com_initialized)) {
|
if (!com_initialize(&com_initialized, MOD_NAME)) {
|
||||||
throw 1;
|
throw 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user