Skip to content

Commit

Permalink
[wasapi] Fix COM initialization in console versions
Browse files Browse the repository at this point in the history
  • Loading branch information
starg2 committed Dec 27, 2023
1 parent 95fa1dc commit a14307c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
8 changes: 5 additions & 3 deletions timidity/timidity.c
Original file line number Diff line number Diff line change
Expand Up @@ -9081,7 +9081,7 @@ extern int volatile save_playlist_once_before_exit_flag;
#endif /* IA_W32GUI */


#if defined ( IA_W32GUI ) || defined ( IA_W32G_SYN )
#ifdef __W32__
static int CoInitializeOK = 0;
#endif

Expand Down Expand Up @@ -9238,9 +9238,11 @@ int main(int argc, char **argv)
return 0;
}
timidity_start_initialize();
#if defined (IA_W32GUI) || defined (IA_W32G_SYN)
#ifdef __W32__
if (SUCCEEDED(CoInitializeEx(NULL, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE)))
CoInitializeOK = 1;
#endif
#if defined (IA_W32GUI) || defined (IA_W32G_SYN)
w32g_initialize();
for (c = 1; c < argc; c++)
if (is_directory(argv[c])) {
Expand Down Expand Up @@ -9367,9 +9369,9 @@ int main(int argc, char **argv)
/* CUI, SYN */
main_ret = timidity_play_main(nfiles, files);
w32_atexit = 0;
#ifdef IA_W32G_SYN
if (CoInitializeOK)
CoUninitialize();
#ifdef IA_W32G_SYN
w32g_uninitialize();
#endif /* IA_W32G_SYN */
#else
Expand Down
18 changes: 9 additions & 9 deletions timidity/wasapi_a.c
Original file line number Diff line number Diff line change
Expand Up @@ -854,15 +854,15 @@ void close_output(void)
CloseHandle(hEventTcv);
hEventTcv = NULL;
}
if(IsCoInit && CoInitThreadId == GetCurrentThreadId()){
CoUninitialize();
IsCoInit = 0;
}
BufferFrames = 0;
free_avrt();
#ifdef USE_TEMP_ENCODE
reset_temporary_encoding();
#endif
if(IsCoInit && CoInitThreadId == GetCurrentThreadId()){
CoUninitialize();
IsCoInit = 0;
}
IsOpened = 0;
}

Expand All @@ -881,7 +881,11 @@ int open_output(void)
int device_id;

close_output();


if(check_hresult_failed(CoInitializeEx(NULL, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE), "CoInitializeEx()"))
goto error;
IsCoInit = 1;
CoInitThreadId = GetCurrentThreadId();
if(!get_winver()){
ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "WASAPI ERROR! WASAPI require Windows Vista and later.");
return -1;
Expand All @@ -900,10 +904,6 @@ int open_output(void)
hEventTcv = CreateEvent(NULL,FALSE,FALSE,NULL); // auto reset
if(!hEventTcv)
goto error;
if(check_hresult_failed(CoInitializeEx(NULL, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE), "CoInitializeEx()"))
goto error;
IsCoInit = 1;
CoInitThreadId = GetCurrentThreadId();
if(!get_device(&pMMDevice, device_id))
goto error;
if(check_hresult_failed(IMMDevice_Activate(pMMDevice, &tim_IID_IAudioClient, CLSCTX_INPROC_SERVER, NULL, (void**)&pAudioClient), "IMMDevice::Activate()"))
Expand Down

0 comments on commit a14307c

Please sign in to comment.