From 3102efaf4ffec37a630d1b71d9198cf858dbe3d4 Mon Sep 17 00:00:00 2001 From: Dmytro Bogovych Date: Fri, 11 Oct 2019 20:31:45 +0300 Subject: [PATCH] - fixes --- src/CMakeLists.txt | 14 +- src/engine/audio/Audio_DirectSound.cpp | 1456 ++++++++++++------------ src/engine/audio/Audio_DirectSound.h | 50 +- src/engine/helper/CMakeLists.txt | 6 +- src/engine/helper/HL_Usb.cpp | 21 +- 5 files changed, 774 insertions(+), 773 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4f511cb6..3d2c12fa 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -60,21 +60,21 @@ set (rtphone_engine engine) set (USE_AMR_CODEC OFF CACHE BOOL "Use AMR codec. Requires libraries.") set (USE_EVS_CODEC OFF CACHE BOOL "Use EVS codec.") -set(CMAKE_POSITION_INDEPENDENT_CODE ON) +set (CMAKE_POSITION_INDEPENDENT_CODE ON) set (OPENSSL_INCLUDE ${LIB_PLATFORM}/openssl/1.0/include) message ("Using OpenSSL include files from ${OPENSSL_INCLUDE}") if (CMAKE_SYSTEM MATCHES "Windows*") - add_definitions(-DTARGET_WIN) + add_definitions (-DTARGET_WIN -D_SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS -D_UNICODE -D_CRT_SECURE_NO_WARNINGS) endif() if (CMAKE_SYSTEM MATCHES "Linux*") - add_definitions(-DTARGET_LINUX) + add_definitions (-DTARGET_LINUX) endif() if (CMAKE_SYSTEM MATCHES "Darwin*") - add_definitions(-DTARGET_OSX) + add_definitions (-DTARGET_OSX) endif() set (RTPHONE_SOURCES @@ -133,7 +133,7 @@ set (RTPHONE_HEADERS ${rtphone_engine}/endpoint/EP_Session.h ) -add_library(rtphone STATIC ${RTPHONE_SOURCES} ${RTPHONE_HEADERS}) +add_library (rtphone STATIC ${RTPHONE_SOURCES} ${RTPHONE_HEADERS}) add_subdirectory(${rtphone_libs}/resiprocate) add_subdirectory(${rtphone_libs}/ice) @@ -150,7 +150,7 @@ add_subdirectory(${rtphone_engine}/helper) add_subdirectory(${rtphone_engine}/audio) add_subdirectory(${rtphone_engine}/media) -set(LIBS ice_stack jrtplib g729_codec gsm_codec +set (LIBS ice_stack jrtplib g729_codec gsm_codec gsmhr_codec g722_codec srtp resiprocate helper_lib audio_lib webrtc speexdsp uuid) @@ -161,7 +161,7 @@ else () endif () if (USE_AMR_CODEC) - set(LIBS ${LIBS} opencore-amrnb opencore-amrwb) + set (LIBS ${LIBS} opencore-amrnb opencore-amrwb) endif (USE_AMR_CODEC) target_link_libraries(rtphone diff --git a/src/engine/audio/Audio_DirectSound.cpp b/src/engine/audio/Audio_DirectSound.cpp index 7ac2455c..62291c9f 100644 --- a/src/engine/audio/Audio_DirectSound.cpp +++ b/src/engine/audio/Audio_DirectSound.cpp @@ -29,249 +29,249 @@ using namespace Audio; class DSoundInit { public: - DSoundInit(); - virtual ~DSoundInit(); + DSoundInit(); + virtual ~DSoundInit(); - void load(); - void unload(); + void load(); + void unload(); - struct EntryPoints - { - HINSTANCE mInstance; + struct EntryPoints + { + HINSTANCE mInstance; - HRESULT (WINAPI *DirectSoundCreate8)(LPGUID, LPDIRECTSOUND8 *, LPUNKNOWN); - HRESULT (WINAPI *DirectSoundEnumerateW)(LPDSENUMCALLBACKW, LPVOID); - HRESULT (WINAPI *DirectSoundEnumerateA)(LPDSENUMCALLBACKA, LPVOID); + HRESULT (WINAPI *DirectSoundCreate8)(LPGUID, LPDIRECTSOUND8 *, LPUNKNOWN); + HRESULT (WINAPI *DirectSoundEnumerateW)(LPDSENUMCALLBACKW, LPVOID); + HRESULT (WINAPI *DirectSoundEnumerateA)(LPDSENUMCALLBACKA, LPVOID); - HRESULT (WINAPI *DirectSoundCaptureCreate8)(LPGUID, LPDIRECTSOUNDCAPTURE8* , LPUNKNOWN); - HRESULT (WINAPI *DirectSoundCaptureEnumerateW)(LPDSENUMCALLBACKW, LPVOID); - HRESULT (WINAPI *DirectSoundCaptureEnumerateA)(LPDSENUMCALLBACKA, LPVOID); - HRESULT (WINAPI *GetDeviceID)(LPCGUID src, LPGUID dst); - } mRoutines; + HRESULT (WINAPI *DirectSoundCaptureCreate8)(LPGUID, LPDIRECTSOUNDCAPTURE8* , LPUNKNOWN); + HRESULT (WINAPI *DirectSoundCaptureEnumerateW)(LPDSENUMCALLBACKW, LPVOID); + HRESULT (WINAPI *DirectSoundCaptureEnumerateA)(LPDSENUMCALLBACKA, LPVOID); + HRESULT (WINAPI *GetDeviceID)(LPCGUID src, LPGUID dst); + } mRoutines; protected: - LPDIRECTSOUND mDirectSound; - Mutex mGuard; - unsigned int mRefCount; + LPDIRECTSOUND mDirectSound; + Mutex mGuard; + unsigned int mRefCount; }; DSoundInit gDSoundInit; DSoundInit::DSoundInit() - :mRefCount(0) + :mRefCount(0) { } DSoundInit::~DSoundInit() { - //Unload(); + //Unload(); } void DSoundInit::load() { - Lock l(mGuard); + Lock l(mGuard); - if (++mRefCount == 1) - { - HRESULT hr = E_FAIL; + if (++mRefCount == 1) + { + HRESULT hr = E_FAIL; - hr = ::CoInitialize(NULL); + hr = ::CoInitialize(NULL); - //load the DirectSound DLL - mRoutines.mInstance = ::LoadLibrary(L"dsound.dll"); - if (!mRoutines.mInstance) - throw std::logic_error("Cannot load dsound.dll"); + //load the DirectSound DLL + mRoutines.mInstance = ::LoadLibraryW(L"dsound.dll"); + if (!mRoutines.mInstance) + throw std::logic_error("Cannot load dsound.dll"); - mRoutines.DirectSoundCaptureCreate8 = (HRESULT (WINAPI *)(LPGUID, LPDIRECTSOUNDCAPTURE8 *, LPUNKNOWN))::GetProcAddress(mRoutines.mInstance, "DirectSoundCaptureCreate8"); - mRoutines.DirectSoundCaptureEnumerateW = (HRESULT (WINAPI *)(LPDSENUMCALLBACKW, LPVOID))::GetProcAddress(mRoutines.mInstance, "DirectSoundCaptureEnumerateW"); - mRoutines.DirectSoundCreate8 = (HRESULT (WINAPI *)(LPGUID, LPDIRECTSOUND8 *, LPUNKNOWN))::GetProcAddress(mRoutines.mInstance, "DirectSoundCreate8"); - mRoutines.DirectSoundEnumerateW = (HRESULT (WINAPI *)(LPDSENUMCALLBACKW, LPVOID))::GetProcAddress(mRoutines.mInstance, "DirectSoundEnumerateW"); - mRoutines.GetDeviceID = (HRESULT (WINAPI*) (LPCGUID, LPGUID)) GetProcAddress(mRoutines.mInstance, "GetDeviceID"); - } + mRoutines.DirectSoundCaptureCreate8 = (HRESULT (WINAPI *)(LPGUID, LPDIRECTSOUNDCAPTURE8 *, LPUNKNOWN))::GetProcAddress(mRoutines.mInstance, "DirectSoundCaptureCreate8"); + mRoutines.DirectSoundCaptureEnumerateW = (HRESULT (WINAPI *)(LPDSENUMCALLBACKW, LPVOID))::GetProcAddress(mRoutines.mInstance, "DirectSoundCaptureEnumerateW"); + mRoutines.DirectSoundCreate8 = (HRESULT (WINAPI *)(LPGUID, LPDIRECTSOUND8 *, LPUNKNOWN))::GetProcAddress(mRoutines.mInstance, "DirectSoundCreate8"); + mRoutines.DirectSoundEnumerateW = (HRESULT (WINAPI *)(LPDSENUMCALLBACKW, LPVOID))::GetProcAddress(mRoutines.mInstance, "DirectSoundEnumerateW"); + mRoutines.GetDeviceID = (HRESULT (WINAPI*) (LPCGUID, LPGUID)) GetProcAddress(mRoutines.mInstance, "GetDeviceID"); + } } void DSoundInit::unload() { - Lock l(mGuard); - if (--mRefCount == 0) - { - if (mRoutines.mInstance) + Lock l(mGuard); + if (--mRefCount == 0) { - ::FreeLibrary(mRoutines.mInstance); - mRoutines.mInstance = NULL; - } + if (mRoutines.mInstance) + { + ::FreeLibrary(mRoutines.mInstance); + mRoutines.mInstance = NULL; + } - CoUninitialize(); - } + CoUninitialize(); + } } // --------------- VistaEnumerator --------------------- VistaEnumerator::VistaEnumerator() -:mCollection(NULL), mDefaultDevice(NULL), mEnumerator(NULL), mDirection(eCapture) + :mCollection(NULL), mDefaultDevice(NULL), mEnumerator(NULL), mDirection(eCapture) { } VistaEnumerator::~VistaEnumerator() { - close(); + close(); } void VistaEnumerator::open(int direction) { - const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator); - const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator); + const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator); + const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator); - mDirection = (direction == myMicrophone) ? eCapture : eRender; + mDirection = (direction == myMicrophone) ? eCapture : eRender; - HRESULT hr = CoCreateInstance( - CLSID_MMDeviceEnumerator, NULL, - CLSCTX_ALL, IID_IMMDeviceEnumerator, - (void**)&mEnumerator); - if (!mEnumerator) - return; + HRESULT hr = CoCreateInstance( + CLSID_MMDeviceEnumerator, NULL, + CLSCTX_ALL, IID_IMMDeviceEnumerator, + (void**)&mEnumerator); + if (!mEnumerator) + return; - hr = mEnumerator->EnumAudioEndpoints(mDirection, DEVICE_STATE_ACTIVE, &mCollection); - if (!mCollection) - return; - hr = mEnumerator->GetDefaultAudioEndpoint(mDirection, eCommunications, &mDefaultDevice); - if (!mDefaultDevice) - return; + hr = mEnumerator->EnumAudioEndpoints(mDirection, DEVICE_STATE_ACTIVE, &mCollection); + if (!mCollection) + return; + hr = mEnumerator->GetDefaultAudioEndpoint(mDirection, eCommunications, &mDefaultDevice); + if (!mDefaultDevice) + return; - enumerate(); + enumerate(); } void VistaEnumerator::close() { - try - { - if (mCollection) + try { - mCollection->Release(); - mCollection = NULL; - } + if (mCollection) + { + mCollection->Release(); + mCollection = NULL; + } - if (mDefaultDevice) - { - //mDefaultDevice->Release(); - mDefaultDevice = NULL; - } + if (mDefaultDevice) + { + //mDefaultDevice->Release(); + mDefaultDevice = NULL; + } - if (mEnumerator) - { - mEnumerator->Release(); - mEnumerator = NULL; + if (mEnumerator) + { + mEnumerator->Release(); + mEnumerator = NULL; + } + } + catch(...) + { } - } - catch(...) - { - } } IMMDevice* VistaEnumerator::mapIndexToInterface(int index) { - if (!mCollection) - return NULL; + if (!mCollection) + return NULL; - if (index == -1) - return mDefaultDevice; + if (index == -1) + return mDefaultDevice; - size_t idSize = 0; - MMRESULT mmres = 0; - WCHAR* id = NULL; - if (mDirection == eCapture) - { - mmres = waveInMessage((HWAVEIN)index, DRV_QUERYFUNCTIONINSTANCEIDSIZE, (DWORD_PTR)&idSize, NULL); + size_t idSize = 0; + MMRESULT mmres = 0; + WCHAR* id = NULL; + if (mDirection == eCapture) + { + mmres = waveInMessage((HWAVEIN)index, DRV_QUERYFUNCTIONINSTANCEIDSIZE, (DWORD_PTR)&idSize, NULL); + + if (mmres != MMSYSERR_NOERROR) + return NULL; + + id = (WCHAR*)_alloca(idSize*sizeof(WCHAR)); + mmres = waveInMessage((HWAVEIN)index, DRV_QUERYFUNCTIONINSTANCEID, (DWORD_PTR)id, idSize); + } + else + { + mmres = waveOutMessage((HWAVEOUT)index, DRV_QUERYFUNCTIONINSTANCEIDSIZE, (DWORD_PTR)&idSize, NULL); + + if (mmres != MMSYSERR_NOERROR) + return NULL; + + id = (WCHAR*)_alloca(idSize*sizeof(WCHAR)); + mmres = waveOutMessage((HWAVEOUT)index, DRV_QUERYFUNCTIONINSTANCEID, (DWORD_PTR)id, idSize); + } if (mmres != MMSYSERR_NOERROR) - return NULL; + return NULL; - id = (WCHAR*)_alloca(idSize*sizeof(WCHAR)); - mmres = waveInMessage((HWAVEIN)index, DRV_QUERYFUNCTIONINSTANCEID, (DWORD_PTR)id, idSize); - } - else - { - mmres = waveOutMessage((HWAVEOUT)index, DRV_QUERYFUNCTIONINSTANCEIDSIZE, (DWORD_PTR)&idSize, NULL); + IMMDevice* pDevice = NULL; + mEnumerator->GetDevice(id, &pDevice); - if (mmres != MMSYSERR_NOERROR) - return NULL; - - id = (WCHAR*)_alloca(idSize*sizeof(WCHAR)); - mmres = waveOutMessage((HWAVEOUT)index, DRV_QUERYFUNCTIONINSTANCEID, (DWORD_PTR)id, idSize); - } - - if (mmres != MMSYSERR_NOERROR) - return NULL; - - IMMDevice* pDevice = NULL; - mEnumerator->GetDevice(id, &pDevice); - - return pDevice; + return pDevice; } void VistaEnumerator::enumerate() { - mNameList.clear(); - int res = (int)count(); + mNameList.clear(); + int res = (int)count(); - for (int i=0; iOpenPropertyStore(STGM_READ, &store); - if (store) - { - PROPVARIANT varName; - PropVariantInit(&varName); - if (store->GetValue(PKEY_Device_FriendlyName, &varName) == S_OK) - mNameList.push_back(varName.pwszVal); - PropVariantClear(&varName); - store->Release(); - } - dev->Release(); + IMMDevice* dev = mapIndexToInterface(i); + if (dev) + { + IPropertyStore* store = NULL; + dev->OpenPropertyStore(STGM_READ, &store); + if (store) + { + PROPVARIANT varName; + PropVariantInit(&varName); + if (store->GetValue(PKEY_Device_FriendlyName, &varName) == S_OK) + mNameList.push_back(varName.pwszVal); + PropVariantClear(&varName); + store->Release(); + } + dev->Release(); + } } - } } std::tstring VistaEnumerator::nameAt(int index) { - return mNameList[index]; + return mNameList[index]; } int VistaEnumerator::idAt(int index) { - return index; + return index; } int VistaEnumerator::count() { - if (mDirection == eCapture) - return waveInGetNumDevs(); - else - return waveOutGetNumDevs(); + if (mDirection == eCapture) + return waveInGetNumDevs(); + else + return waveOutGetNumDevs(); } int VistaEnumerator::indexOfDefaultDevice() { - DWORD devID = -1, status = 0; + DWORD devID = -1, status = 0; - if (mDirection == mySpeaker) - { - if (waveOutMessage((HWAVEOUT)WAVE_MAPPER, DRVM_MAPPER_CONSOLEVOICECOM_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status) != MMSYSERR_NOERROR) - waveOutMessage((HWAVEOUT)WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status); - } - else - { - if (waveInMessage((HWAVEIN)WAVE_MAPPER, DRVM_MAPPER_CONSOLEVOICECOM_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status) != MMSYSERR_NOERROR) - waveInMessage((HWAVEIN)WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status); - } - return devID; + if (mDirection == mySpeaker) + { + if (waveOutMessage((HWAVEOUT)WAVE_MAPPER, DRVM_MAPPER_CONSOLEVOICECOM_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status) != MMSYSERR_NOERROR) + waveOutMessage((HWAVEOUT)WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status); + } + else + { + if (waveInMessage((HWAVEIN)WAVE_MAPPER, DRVM_MAPPER_CONSOLEVOICECOM_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status) != MMSYSERR_NOERROR) + waveInMessage((HWAVEIN)WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status); + } + return devID; } // -------------- XpEnumerator --------------- XpEnumerator::XpEnumerator() -:mDirection(-1) + :mDirection(-1) { } @@ -281,31 +281,31 @@ XpEnumerator::~XpEnumerator() void XpEnumerator::open(int direction) { - mNameList.clear(); - if (direction == myMicrophone) - { - int count = waveInGetNumDevs(); - for (int i=0; iCreateCaptureBuffer(&dsbd, &dscb, NULL)); - DSoundHelper::checkComResult(dscb->QueryInterface(IID_IDirectSoundCaptureBuffer8, (void**)&mBuffer)); - DSoundHelper::checkComResult(dscb->QueryInterface(IID_IDirectSoundNotify, (void**)&mNotifications)); - DSoundHelper::checkComResult(mNotifications->SetNotificationPositions(AUDIO_MIC_BUFFER_COUNT, mEventArray)); - DSoundHelper::checkComResult(mBuffer->Start(DSCBSTART_LOOPING)); - dscb->Release(); - setSimulate( false ); + //wfx.cbSize = sizeof(wfx); + wfx.nChannels = AUDIO_CHANNELS; + wfx.nSamplesPerSec = AUDIO_SAMPLERATE; + wfx.wBitsPerSample = 16; + wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;; + wfx.nAvgBytesPerSec = AUDIO_SAMPLERATE * 2 * AUDIO_CHANNELS; + wfx.wFormatTag = WAVE_FORMAT_PCM; + + DSCBUFFERDESC dsbd; + ZeroMemory(&dsbd, sizeof(dsbd)); + dsbd.dwSize = sizeof(DSCBUFFERDESC); + dsbd.dwFlags = 0;//DSBCAPS_CTRLPOSITIONNOTIFY; + dsbd.dwBufferBytes = AUDIO_MIC_BUFFER_COUNT * AUDIO_MIC_BUFFER_SIZE; + dsbd.lpwfxFormat = &wfx; + dsbd.dwFXCount = 0; + dsbd.lpDSCFXDesc = NULL; + + IDirectSoundCaptureBuffer* dscb = NULL; + DSoundHelper::checkComResult(mDevice->CreateCaptureBuffer(&dsbd, &dscb, NULL)); + DSoundHelper::checkComResult(dscb->QueryInterface(IID_IDirectSoundCaptureBuffer8, (void**)&mBuffer)); + DSoundHelper::checkComResult(dscb->QueryInterface(IID_IDirectSoundNotify, (void**)&mNotifications)); + DSoundHelper::checkComResult(mNotifications->SetNotificationPositions(AUDIO_MIC_BUFFER_COUNT, mEventArray)); + DSoundHelper::checkComResult(mBuffer->Start(DSCBSTART_LOOPING)); + dscb->Release(); + setSimulate( false ); } bool DSoundInputDevice::open() { - ICELogInfo(<< "Request to DirectSound audio input"); - Lock lock(mGuard); - mRefCount++; - if (mRefCount == 1) - { - ICELogInfo(<< "Schedule DirectSound audio input thread"); - mThreadHandle = (HANDLE)_beginthread(&threadProc, 0, this); - } - return true; + ICELogInfo(<< "Request to DirectSound audio input"); + Lock lock(mGuard); + mRefCount++; + if (mRefCount == 1) + { + ICELogInfo(<< "Schedule DirectSound audio input thread"); + mThreadHandle = (HANDLE)_beginthread(&threadProc, 0, this); + } + return true; } void DSoundInputDevice::closeDevice() { - ICELogInfo(<<"Close DirectSound audio input"); - Lock l(mGuard); + ICELogInfo(<<"Close DirectSound audio input"); + Lock l(mGuard); #ifdef AUDIO_DUMPINPUT - mDump.close(); + mDump.close(); #endif - if (mBuffer) - { - mBuffer->Stop(); - mBuffer->Release(); - mBuffer = NULL; - } - if (mNotifications) - { - mNotifications->Release(); - mNotifications = NULL; - } - if (mDevice) - { - mDevice->Release(); - mDevice = NULL; - } - else - return; + if (mBuffer) + { + mBuffer->Stop(); + mBuffer->Release(); + mBuffer = NULL; + } + if (mNotifications) + { + mNotifications->Release(); + mNotifications = NULL; + } + if (mDevice) + { + mDevice->Release(); + mDevice = NULL; + } + else + return; - ::CoUninitialize(); + ::CoUninitialize(); } void DSoundInputDevice::close() { - { - Lock l(mGuard); - mRefCount--; - if (mRefCount != 0) - return; - - // Set shutdown signal - if (!mThreadHandle) - return; + { + Lock l(mGuard); + mRefCount--; + if (mRefCount != 0) + return; - ::SetEvent(mShutdownSignal); - } + // Set shutdown signal + if (!mThreadHandle) + return; - ::WaitForSingleObject(mThreadHandle, INFINITE); - mThreadHandle = 0; + ::SetEvent(mShutdownSignal); + } + + ::WaitForSingleObject(mThreadHandle, INFINITE); + mThreadHandle = 0; } bool DSoundInputDevice::tryReadBuffer(void* buffer) { - // Ensure device exists - if (!mDevice) - { - setSimulate( true ); - return false; - } - - if (mQueue.size() >= AUDIO_MIC_BUFFER_SIZE) - { - memcpy(buffer, mQueue.data(), AUDIO_MIC_BUFFER_SIZE); - if (mEnableDenoiser && AUDIO_CHANNELS == 1) - mDenoiser.fromMic(buffer, AUDIO_MIC_BUFFER_LENGTH); - -#ifdef AUDIO_DUMPINPUT - mDump.write(buffer, AUDIO_MIC_BUFFER_SIZE); -#endif - mQueue.erase(0, AUDIO_MIC_BUFFER_SIZE); - return true; - } - - try - { - if (::WaitForSingleObject(mEventArray[mNextBuffer].hEventNotify, AUDIO_MIC_BUFFER_COUNT * AUDIO_MIC_BUFFER_LENGTH * 4) != WAIT_OBJECT_0) + // Ensure device exists + if (!mDevice) { - setSimulate( true ); - return false; + setSimulate( true ); + return false; } - - // See if all other buffers are signaled - if (::WaitForMultipleObjects(AUDIO_MIC_BUFFER_COUNT, mEventSignals, TRUE, 0) != WAIT_TIMEOUT) - { - // Possible overflow. Consider current buffer resulting. Reset ALL events. - for (int i = 0; iLock(mReadOffset, AUDIO_MIC_BUFFER_SIZE, &ptr1, &len1, &ptr2, &len2, 0)); - - // Copy&Enqueue captured data to mQueue - if (ptr1 && len1) - mQueue.appendBuffer(ptr1, len1); - - if (ptr2 && len2) - mQueue.appendBuffer(ptr2, len2); - - DSoundHelper::checkComResult(mBuffer->Unlock(ptr1, len1, ptr2, len2)); if (mQueue.size() >= AUDIO_MIC_BUFFER_SIZE) { - memcpy(buffer, mQueue.data(), AUDIO_MIC_BUFFER_SIZE); - if (mEnableDenoiser && AUDIO_CHANNELS == 1) - mDenoiser.fromMic(buffer, AUDIO_MIC_BUFFER_LENGTH); + memcpy(buffer, mQueue.data(), AUDIO_MIC_BUFFER_SIZE); + if (mEnableDenoiser && AUDIO_CHANNELS == 1) + mDenoiser.fromMic(buffer, AUDIO_MIC_BUFFER_LENGTH); #ifdef AUDIO_DUMPINPUT - mDump.write(buffer, AUDIO_MIC_BUFFER_SIZE); + mDump.write(buffer, AUDIO_MIC_BUFFER_SIZE); #endif - mQueue.erase(0, AUDIO_MIC_BUFFER_SIZE); + mQueue.erase(0, AUDIO_MIC_BUFFER_SIZE); + return true; } - else - return false; - - return true; - } - catch(...) - { - setSimulate( true ); - } - return false; + + try + { + if (::WaitForSingleObject(mEventArray[mNextBuffer].hEventNotify, AUDIO_MIC_BUFFER_COUNT * AUDIO_MIC_BUFFER_LENGTH * 4) != WAIT_OBJECT_0) + { + setSimulate( true ); + return false; + } + + // See if all other buffers are signaled + if (::WaitForMultipleObjects(AUDIO_MIC_BUFFER_COUNT, mEventSignals, TRUE, 0) != WAIT_TIMEOUT) + { + // Possible overflow. Consider current buffer resulting. Reset ALL events. + for (int i = 0; iLock(mReadOffset, AUDIO_MIC_BUFFER_SIZE, &ptr1, &len1, &ptr2, &len2, 0)); + + // Copy&Enqueue captured data to mQueue + if (ptr1 && len1) + mQueue.appendBuffer(ptr1, len1); + + if (ptr2 && len2) + mQueue.appendBuffer(ptr2, len2); + + DSoundHelper::checkComResult(mBuffer->Unlock(ptr1, len1, ptr2, len2)); + if (mQueue.size() >= AUDIO_MIC_BUFFER_SIZE) + { + memcpy(buffer, mQueue.data(), AUDIO_MIC_BUFFER_SIZE); + if (mEnableDenoiser && AUDIO_CHANNELS == 1) + mDenoiser.fromMic(buffer, AUDIO_MIC_BUFFER_LENGTH); + +#ifdef AUDIO_DUMPINPUT + mDump.write(buffer, AUDIO_MIC_BUFFER_SIZE); +#endif + mQueue.erase(0, AUDIO_MIC_BUFFER_SIZE); + } + else + return false; + + return true; + } + catch(...) + { + setSimulate( true ); + } + return false; } void DSoundInputDevice::setSimulate(bool s) { - if (!mSimulate && s) - mNullAudio.start(); - else - if (mSimulate && !s) - mNullAudio.stop(); + if (!mSimulate && s) + mNullAudio.start(); + else + if (mSimulate && !s) + mNullAudio.stop(); - mSimulate = s; + mSimulate = s; } Format DSoundInputDevice::getFormat() { - return Format(); + return Format(); } int DSoundInputDevice::readBuffer(void* buffer) { - //Lock lock(mGuard); - if (mRefCount <= 0 || isSimulate()) - return 0; - - // Check for finished buffer - if (!tryReadBuffer(buffer)) - return 0; - - return AUDIO_MIC_BUFFER_SIZE; + //Lock lock(mGuard); + if (mRefCount <= 0 || isSimulate()) + return 0; + + // Check for finished buffer + if (!tryReadBuffer(buffer)) + return 0; + + return AUDIO_MIC_BUFFER_SIZE; } void DSoundInputDevice::threadProc(void* arg) { - DSoundInputDevice* impl = (DSoundInputDevice*)arg; - - impl->openDevice(); - - while (true) - { - // Poll for shutdown signal - if (::WaitForSingleObject(impl->mShutdownSignal, 0) == WAIT_OBJECT_0) - break; - - // Preset buffer with silence - memset(impl->mTempBuffer, 0, AUDIO_MIC_BUFFER_SIZE); - - // Try to read buffer - if (!impl->readBuffer(impl->mTempBuffer)) + DSoundInputDevice* impl = (DSoundInputDevice*)arg; + + impl->openDevice(); + + while (true) { - // Introduce delay here to simulate true audio - impl->mNullAudio.waitForBuffer(); + // Poll for shutdown signal + if (::WaitForSingleObject(impl->mShutdownSignal, 0) == WAIT_OBJECT_0) + break; + + // Preset buffer with silence + memset(impl->mTempBuffer, 0, AUDIO_MIC_BUFFER_SIZE); + + // Try to read buffer + if (!impl->readBuffer(impl->mTempBuffer)) + { + // Introduce delay here to simulate true audio + impl->mNullAudio.waitForBuffer(); + } + + // Distribute the captured buffer + if (impl->connection()) + impl->connection()->onMicData(impl->getFormat(), impl->mTempBuffer, AUDIO_MIC_BUFFER_SIZE); } - // Distribute the captured buffer - if (impl->connection()) - impl->connection()->onMicData(impl->getFormat(), impl->mTempBuffer, AUDIO_MIC_BUFFER_SIZE); - } - - impl->closeDevice(); + impl->closeDevice(); } DSoundOutputDevice::DSoundOutputDevice(GUID deviceId) -:mDevice(NULL), mPrimaryBuffer(NULL), mBuffer(NULL), -mWriteOffset(0), mPlayedSamples(0), mTotalPlayed(0), mTail(0), -mThreadHandle(0), mSimulate(false), mGUID(deviceId), -mNullAudio(AUDIO_SPK_BUFFER_LENGTH, AUDIO_SPK_BUFFER_COUNT) + :mDevice(NULL), mPrimaryBuffer(NULL), mBuffer(NULL), + mWriteOffset(0), mPlayedSamples(0), mTotalPlayed(0), mTail(0), + mThreadHandle(0), mSimulate(false), mGUID(deviceId), + mNullAudio(AUDIO_SPK_BUFFER_LENGTH, AUDIO_SPK_BUFFER_COUNT) { - gDSoundInit.load(); - mShutdownSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL); - mBufferSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL); - mRefCount = 0; + gDSoundInit.load(); + mShutdownSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL); + mBufferSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL); + mRefCount = 0; } DSoundOutputDevice::~DSoundOutputDevice() { - close(); + close(); - // Destroy used signals - ::CloseHandle(mShutdownSignal); - ::CloseHandle(mBufferSignal); - gDSoundInit.unload(); + // Destroy used signals + ::CloseHandle(mShutdownSignal); + ::CloseHandle(mBufferSignal); + gDSoundInit.unload(); } bool DSoundOutputDevice::open() { - ICELogInfo(<< "Request to DirectSound audio output"); - // Start thread - mRefCount++; - if (mRefCount == 1) - { - ICELogInfo(<< "Schedule DirectSound audio output thread"); - mThreadHandle = (HANDLE)_beginthread(&threadProc, 0, this); - ::SetThreadPriority(mThreadHandle, THREAD_PRIORITY_TIME_CRITICAL); - } - return true; + ICELogInfo(<< "Request to DirectSound audio output"); + // Start thread + mRefCount++; + if (mRefCount == 1) + { + ICELogInfo(<< "Schedule DirectSound audio output thread"); + mThreadHandle = (HANDLE)_beginthread(&threadProc, 0, this); + ::SetThreadPriority(mThreadHandle, THREAD_PRIORITY_TIME_CRITICAL); + } + return true; } void DSoundOutputDevice::close() { - if (mRefCount == 0) - return; - mRefCount--; - if (mRefCount > 0) - return; + if (mRefCount == 0) + return; + mRefCount--; + if (mRefCount > 0) + return; - // Tell the thread to exit - SetEvent(mShutdownSignal); + // Tell the thread to exit + SetEvent(mShutdownSignal); - // Wait for thread - if (mThreadHandle) - WaitForSingleObject(mThreadHandle, INFINITE); - mThreadHandle = 0; + // Wait for thread + if (mThreadHandle) + WaitForSingleObject(mThreadHandle, INFINITE); + mThreadHandle = 0; } void DSoundOutputDevice::openDevice() { - ICELogInfo(<< "Open DirectSound audio output"); - if (IsEqualGUID(mGUID, GUID_NULL)) - { - setSimulate( true ); - return; - } - - mWriteOffset = 0; - mPlayedSamples = 0; - mSentBytes = 0; - mPlayCursor = 0; - mBufferSize = AUDIO_SPK_BUFFER_COUNT * AUDIO_SPK_BUFFER_SIZE; + ICELogInfo(<< "Open DirectSound audio output"); + if (IsEqualGUID(mGUID, GUID_NULL)) + { + setSimulate( true ); + return; + } - DSoundHelper::checkComResult(gDSoundInit.mRoutines.DirectSoundCreate8(&mGUID, &mDevice, NULL)); - DSoundHelper::checkComResult(mDevice->SetCooperativeLevel(::GetDesktopWindow(), DSSCL_PRIORITY)); + mWriteOffset = 0; + mPlayedSamples = 0; + mSentBytes = 0; + mPlayCursor = 0; + mBufferSize = AUDIO_SPK_BUFFER_COUNT * AUDIO_SPK_BUFFER_SIZE; - WAVEFORMATEX wfx; - memset(&wfx, 0, sizeof(wfx)); + DSoundHelper::checkComResult(gDSoundInit.mRoutines.DirectSoundCreate8(&mGUID, &mDevice, NULL)); + DSoundHelper::checkComResult(mDevice->SetCooperativeLevel(::GetDesktopWindow(), DSSCL_PRIORITY)); - wfx.cbSize = sizeof(wfx); - wfx.nChannels = AUDIO_CHANNELS; - wfx.nSamplesPerSec = AUDIO_SAMPLERATE; - wfx.wBitsPerSample = 16; - wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8; - wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign; - wfx.wFormatTag = WAVE_FORMAT_PCM; + WAVEFORMATEX wfx; + memset(&wfx, 0, sizeof(wfx)); - DSBUFFERDESC dsbd; - ZeroMemory(&dsbd, sizeof(dsbd)); - dsbd.dwSize = sizeof(DSBUFFERDESC); - dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER; - dsbd.dwBufferBytes = 0; - dsbd.lpwfxFormat = NULL;//&wfx; - dsbd.guid3DAlgorithm = DS3DALG_DEFAULT; + wfx.cbSize = sizeof(wfx); + wfx.nChannels = AUDIO_CHANNELS; + wfx.nSamplesPerSec = AUDIO_SAMPLERATE; + wfx.wBitsPerSample = 16; + wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8; + wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign; + wfx.wFormatTag = WAVE_FORMAT_PCM; - DSoundHelper::checkComResult(mDevice->CreateSoundBuffer(&dsbd, &mPrimaryBuffer, NULL )); - DSBCAPS caps; - caps.dwSize = sizeof(caps); - caps.dwFlags = 0; - caps.dwBufferBytes = 0; - caps.dwPlayCpuOverhead = 0; - caps.dwUnlockTransferRate = 0; - - DSoundHelper::checkComResult(mPrimaryBuffer->GetCaps(&caps)); + DSBUFFERDESC dsbd; + ZeroMemory(&dsbd, sizeof(dsbd)); + dsbd.dwSize = sizeof(DSBUFFERDESC); + dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER; + dsbd.dwBufferBytes = 0; + dsbd.lpwfxFormat = NULL;//&wfx; + dsbd.guid3DAlgorithm = DS3DALG_DEFAULT; - dsbd.dwSize = sizeof(caps); - dsbd.dwFlags = DSBCAPS_GLOBALFOCUS; - dsbd.lpwfxFormat = &wfx; - dsbd.guid3DAlgorithm = DS3DALG_DEFAULT; - dsbd.dwBufferBytes = mBufferSize; - - DSoundHelper::checkComResult(mDevice->CreateSoundBuffer(&dsbd, &mBuffer, NULL)); - - // Fill the buffer with silence - LPVOID ptr1 = NULL, ptr2 = NULL; DWORD len1 = 0, len2 = 0; - DSoundHelper::checkComResult(mBuffer->Lock(0, AUDIO_SPK_BUFFER_SIZE * AUDIO_SPK_BUFFER_COUNT, &ptr1, &len1, &ptr2, &len2, 0)); - if (len1 && ptr1) - memset(ptr1, 0, len1); - if (len2 && ptr2) - memset(ptr2, 0, len2); - DSoundHelper::checkComResult(mBuffer->Unlock(ptr1, len1, ptr2, len2)); - DSoundHelper::checkComResult(mBuffer->Play(0,0,DSBPLAY_LOOPING)); - mBuffer->GetCurrentPosition(NULL, &mWriteCursor); + DSoundHelper::checkComResult(mDevice->CreateSoundBuffer(&dsbd, &mPrimaryBuffer, NULL )); + DSBCAPS caps; + caps.dwSize = sizeof(caps); + caps.dwFlags = 0; + caps.dwBufferBytes = 0; + caps.dwPlayCpuOverhead = 0; + caps.dwUnlockTransferRate = 0; + + DSoundHelper::checkComResult(mPrimaryBuffer->GetCaps(&caps)); + + dsbd.dwSize = sizeof(caps); + dsbd.dwFlags = DSBCAPS_GLOBALFOCUS; + dsbd.lpwfxFormat = &wfx; + dsbd.guid3DAlgorithm = DS3DALG_DEFAULT; + dsbd.dwBufferBytes = mBufferSize; + + DSoundHelper::checkComResult(mDevice->CreateSoundBuffer(&dsbd, &mBuffer, NULL)); + + // Fill the buffer with silence + LPVOID ptr1 = NULL, ptr2 = NULL; DWORD len1 = 0, len2 = 0; + DSoundHelper::checkComResult(mBuffer->Lock(0, AUDIO_SPK_BUFFER_SIZE * AUDIO_SPK_BUFFER_COUNT, &ptr1, &len1, &ptr2, &len2, 0)); + if (len1 && ptr1) + memset(ptr1, 0, len1); + if (len2 && ptr2) + memset(ptr2, 0, len2); + DSoundHelper::checkComResult(mBuffer->Unlock(ptr1, len1, ptr2, len2)); + DSoundHelper::checkComResult(mBuffer->Play(0,0,DSBPLAY_LOOPING)); + mBuffer->GetCurrentPosition(NULL, &mWriteCursor); } void DSoundOutputDevice::closeDevice() { - if (mBuffer) - { - mBuffer->Stop(); - mBuffer->Release(); - mBuffer = NULL; - } - - if (mPrimaryBuffer) - { - mPrimaryBuffer->Stop(); - mPrimaryBuffer->Release(); - mPrimaryBuffer = NULL; - } - - if (mDevice) - { - mDevice->Release(); - mDevice = NULL; - } + if (mBuffer) + { + mBuffer->Stop(); + mBuffer->Release(); + mBuffer = NULL; + } + + if (mPrimaryBuffer) + { + mPrimaryBuffer->Stop(); + mPrimaryBuffer->Release(); + mPrimaryBuffer = NULL; + } + + if (mDevice) + { + mDevice->Release(); + mDevice = NULL; + } } void DSoundOutputDevice::restoreBuffer() { - if (mSimulate) - return; + if (mSimulate) + return; - DWORD status = 0; - DSoundHelper::checkComResult(mBuffer->GetStatus(&status)); - if (DSBSTATUS_BUFFERLOST == status) - DSoundHelper::checkComResult(mBuffer->Restore()); + DWORD status = 0; + DSoundHelper::checkComResult(mBuffer->GetStatus(&status)); + if (DSBSTATUS_BUFFERLOST == status) + DSoundHelper::checkComResult(mBuffer->Restore()); } bool DSoundOutputDevice::getMediaFrame() { - try - { - memset(mMediaFrame, 0, sizeof mMediaFrame); - if (mConnection) - mConnection->onSpkData(getFormat(), mMediaFrame, sizeof mMediaFrame); - } - catch(...) - {} + try + { + memset(mMediaFrame, 0, sizeof mMediaFrame); + if (mConnection) + mConnection->onSpkData(getFormat(), mMediaFrame, sizeof mMediaFrame); + } + catch(...) + {} - return true; + return true; } bool DSoundOutputDevice::process() { - if (mSimulate) - return false; + if (mSimulate) + return false; - // Find amount of written data from last call - DWORD cursor = 0; - DSoundHelper::checkComResult(mBuffer->GetCurrentPosition(NULL, &cursor)); - unsigned written; - if (cursor < mWriteCursor) - written = mBufferSize - mWriteCursor + cursor; - else - written = cursor - mWriteCursor; - - mWriteCursor += (written / AUDIO_SPK_BUFFER_SIZE) * AUDIO_SPK_BUFFER_SIZE; - mWriteCursor %= mBufferSize; - bool finished = false; - for (unsigned frameIndex = 0; frameIndex < written / AUDIO_SPK_BUFFER_SIZE && !finished; frameIndex++) - { - unsigned offset = mWriteOffset + frameIndex * AUDIO_SPK_BUFFER_SIZE; - offset %= mBufferSize; + // Find amount of written data from last call + DWORD cursor = 0; + DSoundHelper::checkComResult(mBuffer->GetCurrentPosition(NULL, &cursor)); + unsigned written; + if (cursor < mWriteCursor) + written = mBufferSize - mWriteCursor + cursor; + else + written = cursor - mWriteCursor; - // See what we can write - LPVOID ptr1 = NULL, ptr2 = NULL; DWORD len1 = 0, len2 = 0; - DSoundHelper::checkComResult(mBuffer->Lock(offset, AUDIO_SPK_BUFFER_SIZE, &ptr1, &len1, &ptr2, &len2, 0)); - - assert(ptr2 == NULL); - assert(len1 >= AUDIO_SPK_BUFFER_SIZE); - - if (getMediaFrame()) - finished = true; - memmove(ptr1, mMediaFrame, AUDIO_SPK_BUFFER_SIZE); - DSoundHelper::checkComResult(mBuffer->Unlock(ptr1, AUDIO_SPK_BUFFER_SIZE, ptr2, 0)); - } + mWriteCursor += (written / AUDIO_SPK_BUFFER_SIZE) * AUDIO_SPK_BUFFER_SIZE; + mWriteCursor %= mBufferSize; + bool finished = false; + for (unsigned frameIndex = 0; frameIndex < written / AUDIO_SPK_BUFFER_SIZE && !finished; frameIndex++) + { + unsigned offset = mWriteOffset + frameIndex * AUDIO_SPK_BUFFER_SIZE; + offset %= mBufferSize; - // Increase write offset - mWriteOffset += (written / AUDIO_SPK_BUFFER_SIZE) * AUDIO_SPK_BUFFER_SIZE; - mWriteOffset %= mBufferSize; + // See what we can write + LPVOID ptr1 = NULL, ptr2 = NULL; DWORD len1 = 0, len2 = 0; + DSoundHelper::checkComResult(mBuffer->Lock(offset, AUDIO_SPK_BUFFER_SIZE, &ptr1, &len1, &ptr2, &len2, 0)); - return true; + assert(ptr2 == NULL); + assert(len1 >= AUDIO_SPK_BUFFER_SIZE); + + if (getMediaFrame()) + finished = true; + memmove(ptr1, mMediaFrame, AUDIO_SPK_BUFFER_SIZE); + DSoundHelper::checkComResult(mBuffer->Unlock(ptr1, AUDIO_SPK_BUFFER_SIZE, ptr2, 0)); + } + + // Increase write offset + mWriteOffset += (written / AUDIO_SPK_BUFFER_SIZE) * AUDIO_SPK_BUFFER_SIZE; + mWriteOffset %= mBufferSize; + + return true; } void DSoundOutputDevice::threadProc(void* arg) { - DSoundOutputDevice* impl = (DSoundOutputDevice*)arg; - impl->openDevice(); - - DWORD waitResult = 0; - HANDLE waitArray[2] = {impl->mBufferSignal, impl->mShutdownSignal}; - unsigned exitCount = 0; - bool exitSignal = false; - while (true) - { - // Poll for shutdown signal - if (WAIT_OBJECT_0 == ::WaitForSingleObject(impl->mShutdownSignal, 0)) - break; - - if (impl->isSimulate()) + DSoundOutputDevice* impl = (DSoundOutputDevice*)arg; + impl->openDevice(); + + DWORD waitResult = 0; + HANDLE waitArray[2] = {impl->mBufferSignal, impl->mShutdownSignal}; + unsigned exitCount = 0; + bool exitSignal = false; + while (true) { - impl->mNullAudio.waitForBuffer(); - impl->getMediaFrame(); + // Poll for shutdown signal + if (WAIT_OBJECT_0 == ::WaitForSingleObject(impl->mShutdownSignal, 0)) + break; + + if (impl->isSimulate()) + { + impl->mNullAudio.waitForBuffer(); + impl->getMediaFrame(); + } + else + { + // Poll events + waitResult = ::WaitForMultipleObjects(2, waitArray, FALSE, 5); + if (waitResult == WAIT_OBJECT_0 + 1) + break; + try + { + impl->restoreBuffer(); + impl->process(); + } + catch(const Exception& e) + { + ICELogError(<< "DirectSound output failed with code = " << e.code() << ", subcode = " << e.subcode()); + impl->setSimulate(true); + } + catch(...) + { + ICELogError(<< "DirectSound output failed due to unexpected exception."); + impl->setSimulate(true); + } + } } - else - { - // Poll events - waitResult = ::WaitForMultipleObjects(2, waitArray, FALSE, 5); - if (waitResult == WAIT_OBJECT_0 + 1) - break; - try - { - impl->restoreBuffer(); - impl->process(); - } - catch(const Exception& e) - { - ICELogError(<< "DirectSound output failed with code = " << e.code() << ", subcode = " << e.subcode()); - impl->setSimulate(true); - } - catch(...) - { - ICELogError(<< "DirectSound output failed due to unexpected exception."); - impl->setSimulate(true); - } - } - } - impl->closeDevice(); + impl->closeDevice(); } unsigned DSoundOutputDevice::playedTime() const { - return 0; + return 0; } void DSoundOutputDevice::setSimulate(bool s) { - mSimulate = s; + mSimulate = s; } bool DSoundOutputDevice::isSimulate() const { - return mSimulate; + return mSimulate; } Format DSoundOutputDevice::getFormat() { - return Format(); + return Format(); } bool DSoundOutputDevice::closing() { - return false; + return false; } typedef WINUSERAPI HRESULT (WINAPI *LPFNDLLGETCLASSOBJECT) (const CLSID &, const IID &, void **); HRESULT DirectSoundPrivateCreate (OUT LPKSPROPERTYSET * ppKsPropertySet) { - HMODULE hLibDsound = NULL; - LPFNDLLGETCLASSOBJECT pfnDllGetClassObject = NULL; - LPCLASSFACTORY pClassFactory = NULL; - LPKSPROPERTYSET pKsPropertySet = NULL; - HRESULT hr = DS_OK; + HMODULE hLibDsound = NULL; + LPFNDLLGETCLASSOBJECT pfnDllGetClassObject = NULL; + LPCLASSFACTORY pClassFactory = NULL; + LPKSPROPERTYSET pKsPropertySet = NULL; + HRESULT hr = DS_OK; - // Load dsound.dll - hLibDsound = LoadLibrary(TEXT("dsound.dll")); + // Load dsound.dll + hLibDsound = LoadLibrary(TEXT("dsound.dll")); - if(!hLibDsound) - { - hr = DSERR_GENERIC; - } + if(!hLibDsound) + { + hr = DSERR_GENERIC; + } - // Find DllGetClassObject - if(SUCCEEDED(hr)) - { - pfnDllGetClassObject = - (LPFNDLLGETCLASSOBJECT)GetProcAddress ( hLibDsound, "DllGetClassObject" ); + // Find DllGetClassObject + if(SUCCEEDED(hr)) + { + pfnDllGetClassObject = + (LPFNDLLGETCLASSOBJECT)GetProcAddress ( hLibDsound, "DllGetClassObject" ); - if(!pfnDllGetClassObject) - { - hr = DSERR_GENERIC; - } - } + if(!pfnDllGetClassObject) + { + hr = DSERR_GENERIC; + } + } - // Create a class factory object - if(SUCCEEDED(hr)) - { - hr = pfnDllGetClassObject (CLSID_DirectSoundPrivate, IID_IClassFactory, (LPVOID *)&pClassFactory ); - } + // Create a class factory object + if(SUCCEEDED(hr)) + { + hr = pfnDllGetClassObject (CLSID_DirectSoundPrivate, IID_IClassFactory, (LPVOID *)&pClassFactory ); + } - // Create the DirectSoundPrivate object and query for an IKsPropertySet - // interface - if(SUCCEEDED(hr)) - { - hr = pClassFactory->CreateInstance ( NULL, IID_IKsPropertySet, (LPVOID *)&pKsPropertySet ); - } + // Create the DirectSoundPrivate object and query for an IKsPropertySet + // interface + if(SUCCEEDED(hr)) + { + hr = pClassFactory->CreateInstance ( NULL, IID_IKsPropertySet, (LPVOID *)&pKsPropertySet ); + } - // Release the class factory - if(pClassFactory) - { - pClassFactory->Release(); - } + // Release the class factory + if(pClassFactory) + { + pClassFactory->Release(); + } - // Handle final success or failure - if(SUCCEEDED(hr)) - { - *ppKsPropertySet = pKsPropertySet; - } - else if(pKsPropertySet) - { - pKsPropertySet->Release(); - } + // Handle final success or failure + if(SUCCEEDED(hr)) + { + *ppKsPropertySet = pKsPropertySet; + } + else if(pKsPropertySet) + { + pKsPropertySet->Release(); + } - FreeLibrary(hLibDsound); + FreeLibrary(hLibDsound); - return hr; + return hr; } BOOL GetInfoFromDSoundGUID( GUID i_sGUID, int &dwWaveID) { - LPKSPROPERTYSET pKsPropertySet = NULL; - HRESULT hr; - BOOL retval = FALSE; + LPKSPROPERTYSET pKsPropertySet = NULL; + HRESULT hr; + BOOL retval = FALSE; - PDSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION_DATA psDirectSoundDeviceDescription = NULL; - DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION_DATA sDirectSoundDeviceDescription; + PDSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION_DATA psDirectSoundDeviceDescription = NULL; + DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION_DATA sDirectSoundDeviceDescription; - memset(&sDirectSoundDeviceDescription,0,sizeof(sDirectSoundDeviceDescription)); - hr = DirectSoundPrivateCreate( &pKsPropertySet ); - if(SUCCEEDED(hr)) - { - ULONG ulBytesReturned = 0; - sDirectSoundDeviceDescription.DeviceId = i_sGUID; - - // On the first call the final size is unknown so pass the size of the struct in order to receive - // "Type" and "DataFlow" values, ulBytesReturned will be populated with bytes required for struct+strings. - hr = pKsPropertySet->Get(DSPROPSETID_DirectSoundDevice, - DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION, - NULL, - 0, - &sDirectSoundDeviceDescription, - sizeof(sDirectSoundDeviceDescription), - &ulBytesReturned - ); - - if (ulBytesReturned) + memset(&sDirectSoundDeviceDescription,0,sizeof(sDirectSoundDeviceDescription)); + hr = DirectSoundPrivateCreate( &pKsPropertySet ); + if(SUCCEEDED(hr)) { - // On the first call it notifies us of the required amount of memory in order to receive the strings. - // Allocate the required memory, the strings will be pointed to the memory space directly after the struct. - psDirectSoundDeviceDescription = (PDSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION_DATA)new BYTE[ulBytesReturned]; - *psDirectSoundDeviceDescription = sDirectSoundDeviceDescription; + ULONG ulBytesReturned = 0; + sDirectSoundDeviceDescription.DeviceId = i_sGUID; - hr = pKsPropertySet->Get(DSPROPSETID_DirectSoundDevice, - DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION, - NULL, - 0, - psDirectSoundDeviceDescription, - ulBytesReturned, - &ulBytesReturned - ); + // On the first call the final size is unknown so pass the size of the struct in order to receive + // "Type" and "DataFlow" values, ulBytesReturned will be populated with bytes required for struct+strings. + hr = pKsPropertySet->Get(DSPROPSETID_DirectSoundDevice, + DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION, + NULL, + 0, + &sDirectSoundDeviceDescription, + sizeof(sDirectSoundDeviceDescription), + &ulBytesReturned + ); - dwWaveID = psDirectSoundDeviceDescription->WaveDeviceId; - /*Description = psDirectSoundDeviceDescription->Description; + if (ulBytesReturned) + { + // On the first call it notifies us of the required amount of memory in order to receive the strings. + // Allocate the required memory, the strings will be pointed to the memory space directly after the struct. + psDirectSoundDeviceDescription = (PDSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION_DATA)new BYTE[ulBytesReturned]; + *psDirectSoundDeviceDescription = sDirectSoundDeviceDescription; + + hr = pKsPropertySet->Get(DSPROPSETID_DirectSoundDevice, + DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION, + NULL, + 0, + psDirectSoundDeviceDescription, + ulBytesReturned, + &ulBytesReturned + ); + + dwWaveID = psDirectSoundDeviceDescription->WaveDeviceId; + /*Description = psDirectSoundDeviceDescription->Description; Module = psDirectSoundDeviceDescription->Module; Interface = psDirectSoundDeviceDescription->Interface;*/ - delete [] psDirectSoundDeviceDescription; - retval = TRUE; + delete [] psDirectSoundDeviceDescription; + retval = TRUE; + } + + pKsPropertySet->Release(); } - pKsPropertySet->Release(); - } - - return retval; + return retval; } struct EnumResult { - int mDeviceId; - GUID mGuid; + int mDeviceId; + GUID mGuid; }; BOOL CALLBACK DSEnumCallback( - LPGUID lpGuid, - LPCTSTR lpcstrDescription, - LPCTSTR lpcstrModule, - LPVOID lpContext - ) + LPGUID lpGuid, + LPCTSTR lpcstrDescription, + LPCTSTR lpcstrModule, + LPVOID lpContext + ) { - if (lpGuid) - { - - int devId = -1; - GetInfoFromDSoundGUID(*lpGuid, devId); - EnumResult* er = (EnumResult*)lpContext; - if (er->mDeviceId == devId) + if (lpGuid) { - er->mGuid = *lpGuid; - return FALSE; + + int devId = -1; + GetInfoFromDSoundGUID(*lpGuid, devId); + EnumResult* er = (EnumResult*)lpContext; + if (er->mDeviceId == devId) + { + er->mGuid = *lpGuid; + return FALSE; + } + else + return TRUE; } else - return TRUE; - } - else - return TRUE; + return TRUE; } GUID DSoundHelper::deviceId2Guid(int deviceId, bool captureDevice) { - EnumResult er; - er.mDeviceId = deviceId; - er.mGuid = GUID_NULL; - memset(&er.mGuid, 0, sizeof er.mGuid); - if (captureDevice) - DirectSoundCaptureEnumerate(DSEnumCallback, &er); - else - DirectSoundEnumerate(DSEnumCallback, &er); + EnumResult er; + er.mDeviceId = deviceId; + er.mGuid = GUID_NULL; + memset(&er.mGuid, 0, sizeof er.mGuid); + if (captureDevice) + DirectSoundCaptureEnumerate(DSEnumCallback, &er); + else + DirectSoundEnumerate(DSEnumCallback, &er); - return er.mGuid; + return er.mGuid; } void DSoundHelper::checkComResult(HRESULT code) { if (FAILED(code)) - throw Exception(ERR_DSOUND); + throw Exception(ERR_DSOUND); } #endif diff --git a/src/engine/audio/Audio_DirectSound.h b/src/engine/audio/Audio_DirectSound.h index 65e686ec..2f394fe4 100644 --- a/src/engine/audio/Audio_DirectSound.h +++ b/src/engine/audio/Audio_DirectSound.h @@ -31,9 +31,9 @@ namespace Audio { - class VistaEnumerator: public Enumerator - { - public: +class VistaEnumerator: public Enumerator +{ +public: VistaEnumerator(); ~VistaEnumerator(); @@ -45,7 +45,7 @@ namespace Audio int idAt(int index); int indexOfDefaultDevice(); - protected: +protected: IMMDeviceCollection* mCollection; IMMDevice* mDefaultDevice; IMMDeviceEnumerator* mEnumerator; @@ -54,11 +54,11 @@ namespace Audio void enumerate(); IMMDevice* mapIndexToInterface(int index); - }; +}; - class XpEnumerator: public Enumerator - { - public: +class XpEnumerator: public Enumerator +{ +public: XpEnumerator(); ~XpEnumerator(); @@ -70,25 +70,25 @@ namespace Audio int idAt(int index); int indexOfDefaultDevice(); - protected: +protected: std::vector mNameList; int mDirection; - }; +}; - class DSoundHelper - { - public: +class DSoundHelper +{ +public: static void checkComResult(HRESULT code); static GUID deviceId2Guid(int deviceId, bool captureDevice); - }; +}; #if !defined(_MSC_VER) - typedef struct IDirectSoundNotify8 *LPDIRECTSOUNDNOTIFY8; +typedef struct IDirectSoundNotify8 *LPDIRECTSOUNDNOTIFY8; #endif - class DSoundInputDevice: public InputDevice - { - public: +class DSoundInputDevice: public InputDevice +{ +public: DSoundInputDevice(GUID deviceId); ~DSoundInputDevice(); @@ -102,7 +102,7 @@ namespace Audio int readBuffer(void* buffer); Format getFormat(); - protected: +protected: Mutex mGuard; /// Mutex to protect this instance. LPDIRECTSOUNDCAPTURE8 mDevice; LPDIRECTSOUNDCAPTUREBUFFER8 mBuffer; @@ -134,11 +134,11 @@ namespace Audio void closeDevice(); static void threadProc(void* arg); - }; +}; - class DSoundOutputDevice: public OutputDevice - { - public: +class DSoundOutputDevice: public OutputDevice +{ +public: DSoundOutputDevice(GUID deviceId); ~DSoundOutputDevice(); @@ -151,7 +151,7 @@ namespace Audio bool closing(); Format getFormat(); - protected: +protected: Mutex mGuard; /// Mutex to protect this instance int mDeviceID; LPDIRECTSOUND8 mDevice; @@ -181,7 +181,7 @@ namespace Audio bool getMediaFrame(); static void threadProc(void* arg); - }; +}; } #endif diff --git a/src/engine/helper/CMakeLists.txt b/src/engine/helper/CMakeLists.txt index 466cebaa..1b0615bb 100644 --- a/src/engine/helper/CMakeLists.txt +++ b/src/engine/helper/CMakeLists.txt @@ -5,10 +5,10 @@ set (CMAKE_CXX_STANDARD 11) set (CMAKE_CXX_STANDARD_REQUIRED ON) set (USE_NULL_UUID OFF CACHE BOOL "When enabled linking to libuuid is avoided") -set(CMAKE_POSITION_INDEPENDENT_CODE ON) +set (CMAKE_POSITION_INDEPENDENT_CODE ON) -file(GLOB HELPER_LIB_SOURCES "*.cpp" "*.h") +file (GLOB HELPER_LIB_SOURCES "*.cpp" "*.h") add_library(helper_lib ${HELPER_LIB_SOURCES}) target_include_directories(helper_lib PRIVATE ../../libs/ ../../engine ../) -target_compile_definitions(helper_lib PRIVATE -D_CRT_SECURE_NO_WARNINGS) +target_compile_definitions(helper_lib PRIVATE -D_CRT_SECURE_NO_WARNINGS -D_UNICODE) diff --git a/src/engine/helper/HL_Usb.cpp b/src/engine/helper/HL_Usb.cpp index f474357d..52ba2faa 100644 --- a/src/engine/helper/HL_Usb.cpp +++ b/src/engine/helper/HL_Usb.cpp @@ -1,5 +1,6 @@ #include "HL_Usb.h" #include "HL_Exception.h" + #ifdef TARGET_WIN #include @@ -9,7 +10,7 @@ UsbChangeListener::UsbChangeListener() :mNotifyHandle(NULL), mHiddenWindow(NULL), mDelegate(NULL) { - wsprintf(mWindowClassName, ADR_WINDOW_CLASS_NAME, (unsigned int)rand()); + wsprintfW(mWindowClassName, ADR_WINDOW_CLASS_NAME, (unsigned int)rand()); } UsbChangeListener::~UsbChangeListener() @@ -30,16 +31,16 @@ UsbChangeListener::Delegate* UsbChangeListener::getDelegate() const void UsbChangeListener::start() { // Exposing Window to Mixer - WNDCLASSEX wcx; - memset( &wcx, 0, sizeof(WNDCLASSEX) ); - wcx.cbSize = sizeof(WNDCLASSEX); - wcx.lpszClassName = mWindowClassName; + WNDCLASSEXW wcx; + memset( &wcx, 0, sizeof(WNDCLASSEXW) ); + wcx.cbSize = sizeof(WNDCLASSEXW); + wcx.lpszClassName = mWindowClassName; wcx.lpfnWndProc = (WNDPROC)ADRWindowProc; - ::RegisterClassEx(&wcx); + ::RegisterClassExW(&wcx); wchar_t windowname[128]; - wsprintf(windowname, ADR_WINDOW_NAME, rand()); - mHiddenWindow = CreateWindow( mWindowClassName, + wsprintfW(windowname, ADR_WINDOW_NAME, rand()); + mHiddenWindow = CreateWindowW( mWindowClassName, windowname, WS_POPUP | WS_DISABLED, 0, 0, 0, 0, @@ -102,8 +103,8 @@ void UsbChangeListener::stop() ::DestroyWindow(mHiddenWindow); mHiddenWindow = NULL; - ::UnregisterClass(mWindowClassName, NULL); + ::UnregisterClassW(mWindowClassName, NULL); } } -#endif \ No newline at end of file +#endif