MFInitStatus MFHeap_InitModule() { MFCALLSTACK; MFDebug_Assert(MFThread_GetMutexSizePlatformSpecific() <= sizeof(gMutexBuffer), "Mutex buffer too small!"); MFThread_InitMutexPlatformSpecific((MFMutex)gMutexBuffer, "MFHeap alloc mutex"); gAllocMutex = (MFMutex)gMutexBuffer; // any heap tracking allocations should be made in the debug space MFHeap *pOld = MFHeap_SetActiveHeap(MFHeap_GetDebugHeap()); #if defined(_USE_TRACKING_HASH_TABLE) // set up the memory tracking hash table // note: this is slightly complicated due to the chicken and egg nature // of making the first allocation for the pool its self ;) static const int numAllocHeaders = 1024; // uint32 bytes = (sizeof(MFHeap_AllocItem) + sizeof(void**)) * numAllocHeaders; // void *pPoolMemory = MFHeap_Alloc(bytes); // gAllocHeaderPool.Init(sizeof(MFHeap_AllocItem), numAllocHeaders, 1024, pPoolMemory, bytes); gAllocHeaderPool.Init(sizeof(MFHeap_AllocItem), numAllocHeaders, 1024); MFZeroMemory(gpAllocTable, sizeof(gpAllocTable)); // artificially add an entry for the pool its self... MFHeap_AllocItem *pHeader = (MFHeap_AllocItem*)gAllocHeaderPool.Alloc(); pHeader->header.pHeap = &gExternalHeap; pHeader->header.size = bytes; pHeader->header.pFile = __FILE__; pHeader->header.line = (uint16)__LINE__; pHeader->header.alignment = 4; pHeader->pMemory = pPoolMemory; pHeader->pNext = NULL; gpAllocTable[MFUtil_HashPointer(pPoolMemory) % MFHeap_AllocTableLength] = pHeader; #endif #if defined(_USE_ALLOC_TRACKER) gAllocList.Init(sizeof(void*), 1024, 1024); gPoolInitialised = true; #endif // restore the active heap MFHeap_SetActiveHeap(pOld); // init the heap MFHeap_InitModulePlatformSpecific(); return MFAIC_Succeeded; }
MFInitStatus MFString_InitModule(int moduleId, bool bPerformInitialisation) { if(!bPerformInitialisation) return MFIS_Succeeded; if(gModuleInitCount == 0) { ++gModuleInitCount; stringHeap.Init(gStringGroups, sizeof(gStringGroups) / sizeof(gStringGroups[0])); stringPool.Init(sizeof(MFStringData), 128, 128); } return MFIS_Succeeded; }
void MFSound_InitModulePlatformSpecific(int *pSoundDataSize, int *pVoiceDataSize) { MFCALLSTACK; gDevices.Init(sizeof(AudioDevice), 8, 8); ALCint minor, major; alcGetIntegerv(NULL, ALC_MAJOR_VERSION, 1, &major); alcGetIntegerv(NULL, ALC_MINOR_VERSION, 1, &minor); gAPIVersion = major*100 + minor; bool bCanEnumerate, bHasCapture; if(gAPIVersion >= 101) { bCanEnumerate = true; bHasCapture = true; } else { bCanEnumerate = alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT") == AL_TRUE; bHasCapture = alcIsExtensionPresent(NULL, "ALC_EXT_CAPTURE") == AL_TRUE; } if(bCanEnumerate) { const char *pDevices = alcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER); const char *pDefault = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER); while(pDevices && *pDevices) { bool bIsDefault = !MFString_Compare(pDevices, pDefault); MFDebug_Log(2, MFStr("OpenAL: found output device '%s'%s", pDevices, bIsDefault ? " (default)" : "")); MFDevice *pDevice = MFDevice_AllocDevice(MFDT_AudioRender, &DestroyDevice); pDevice->pInternal = gDevices.AllocAndZero(); pDevice->state = MFDevState_Ready; AudioDevice &device = *(AudioDevice*)pDevice->pInternal; MFString_CopyN(pDevice->strings[MFDS_ID], pDevices, sizeof(pDevice->strings[MFDS_ID])-1); pDevice->strings[MFDS_ID][sizeof(pDevice->strings[MFDS_ID])-1] = 0; device.pDevice = NULL; if(bIsDefault) MFDevice_SetDefaultDevice(MFDT_AudioRender, MFDDT_All, pDevice); pDevices += MFString_Length(pDevices) + 1; } if(!MFDevice_GetDefaultDevice(MFDT_AudioRender, MFDDT_Default)) { MFDebug_Warn(2, "OpenAL: No default output device?"); // HACK: set it to the first one... MFDevice *pDevice = MFDevice_GetDeviceByIndex(MFDT_AudioRender, 0); MFDevice_SetDefaultDevice(MFDT_AudioRender, MFDDT_All, pDevice); } if(bHasCapture) { pDevices = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER); pDefault = alcGetString(NULL, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER); while(pDevices && *pDevices) { bool bIsDefault = !MFString_Compare(pDevices, pDefault); MFDebug_Log(2, MFStr("OpenAL: found capture device '%s'%s", pDevices, bIsDefault ? " (default)" : "")); MFDevice *pDevice = MFDevice_AllocDevice(MFDT_AudioCapture, &DestroyDevice); pDevice->pInternal = gDevices.AllocAndZero(); pDevice->state = MFDevState_Ready; AudioDevice &device = *(AudioDevice*)pDevice->pInternal; MFString_CopyN(pDevice->strings[MFDS_ID], pDevices, sizeof(pDevice->strings[MFDS_ID])-1); pDevice->strings[MFDS_ID][sizeof(pDevice->strings[MFDS_ID])-1] = 0; device.pDevice = NULL; if(bIsDefault) MFDevice_SetDefaultDevice(MFDT_AudioCapture, MFDDT_All, pDevice); pDevices += MFString_Length(pDevices) + 1; } if(!MFDevice_GetDefaultDevice(MFDT_AudioCapture, MFDDT_Default)) MFDebug_Warn(2, "OpenAL: No default capture device?"); } } // create a context Context *pContext = CreateContext(MFDevice_GetDefaultDevice(MFDT_AudioRender, MFDDT_Default)); MakeCurrent(pContext); // we need to return the size of the internal structures so the platform independant // code can make the correct allocations.. *pSoundDataSize = sizeof(MFSoundDataInternal); *pVoiceDataSize = sizeof(MFVoiceDataInternal); }
void MFSound_InitWASAPI() { gDevices.Init(sizeof(MFAudioDevice), 8, 8); gCaptureDevices.Init(sizeof(MFAudioCaptureDevice), 8, 8); HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&gpEnumerator); if(FAILED(hr)) { MFDebug_Assert(false, "Couldn't create multimedia device enumerator!"); return; } // enumerate audio devices... gpNotification = new MFAudioDeviceNotification; gpEnumerator->RegisterEndpointNotificationCallback(gpNotification); // enumerate render devices IMMDeviceCollection *pDevices; gpEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE | DEVICE_STATE_UNPLUGGED, &pDevices); if(pDevices) { UINT count; pDevices->GetCount(&count); for(UINT i=0; i<count; ++i) { IMMDevice *pDevice; pDevices->Item(i, &pDevice); MFDevice *pDev = MFDevice_AllocDevice(MFDT_AudioRender, NULL); pDev->pInternal = gDevices.AllocAndZero(); MFAudioDevice &device = *(MFAudioDevice*)pDev->pInternal; GetDeviceInfo(pDevice, pDev); pDevice->Release(); MFDebug_Log(0, MFStr("Found audio device: %s, %s - state: %d (%s)", pDev->strings[MFDS_DeviceName], pDev->strings[MFDS_Manufacturer], device.state, pDev->strings[MFDS_ID])); } pDevices->Release(); } // enumerate capture devices gpEnumerator->EnumAudioEndpoints(eCapture, DEVICE_STATE_ACTIVE | DEVICE_STATE_UNPLUGGED, &pDevices); if(pDevices) { UINT count; pDevices->GetCount(&count); for(UINT i=0; i<count; ++i) { IMMDevice *pDevice; pDevices->Item(i, &pDevice); MFDevice *pDev = MFDevice_AllocDevice(MFDT_AudioCapture, NULL); pDev->pInternal = gCaptureDevices.AllocAndZero(); MFAudioCaptureDevice &device = *(MFAudioCaptureDevice*)pDev->pInternal; GetDeviceInfo(pDevice, pDev); pDevice->Release(); MFDebug_Log(0, MFStr("Found audio capture device: %s, %s - state: %d (%s)", pDev->strings[MFDS_DeviceName], pDev->strings[MFDS_Manufacturer], device.state, pDev->strings[MFDS_ID])); } pDevices->Release(); } // set defaults (this is some awkward windows code!) for(int i=0; i<2; ++i) { for(int j=0; j<3; ++j) { IMMDevice *pDevice; gpEnumerator->GetDefaultAudioEndpoint(gDirection[i], gRole[j], &pDevice); if(pDevice) { wchar_t *pDefaultId; pDevice->GetId(&pDefaultId); char temp[128]; MFString_CopyUTF16ToUTF8(temp, pDefaultId); MFDevice *pDev = MFDevice_GetDeviceById(temp); MFDevice_SetDefaultDevice(gDt[i], gDef[j], pDev); CoTaskMemFree(pDefaultId); pDevice->Release(); } } } }