audio: don't uninit device on stop

in case initing a device is expensive, don't do it
unnecessarily, and treat 'stop' as more of a 'pause'.
This commit is contained in:
Nevyn Bengtsson 2021-01-19 13:11:56 +01:00 committed by Bjorn
parent c542be254e
commit 7f5e46ad0b
1 changed files with 23 additions and 10 deletions

View File

@ -162,6 +162,9 @@ void lovrAudioDestroy() {
if (!state.initialized) return;
lovrAudioStop(AUDIO_PLAYBACK);
lovrAudioStop(AUDIO_CAPTURE);
for(int i = 0; i < AUDIO_TYPE_COUNT; i++) {
ma_device_uninit(&state.devices[i]);
}
ma_mutex_uninit(&state.playbackLock);
ma_context_uninit(&state.context);
@ -242,11 +245,16 @@ bool lovrAudioInitDevice(AudioType type) {
}
bool lovrAudioStart(AudioType type) {
bool initResult = lovrAudioInitDevice(type);
if (initResult == false && type == AUDIO_CAPTURE) {
lovrPlatformRequestPermission(AUDIO_CAPTURE_PERMISSION);
// lovrAudioStart will be retried from boot.lua upon permission granted event
return false;
ma_uint32 deviceState = state.devices[type].state;
if (deviceState == MA_STATE_UNINITIALIZED) {
bool initResult = lovrAudioInitDevice(type);
if (initResult == false) {
if(type == AUDIO_CAPTURE) {
lovrPlatformRequestPermission(AUDIO_CAPTURE_PERMISSION);
// lovrAudioStart will be retried from boot.lua upon permission granted event
}
return false;
}
}
ma_result status = ma_device_start(&state.devices[type]);
return status == MA_SUCCESS;
@ -254,7 +262,6 @@ bool lovrAudioStart(AudioType type) {
bool lovrAudioStop(AudioType type) {
ma_result stoppingResult = ma_device_stop(&state.devices[type]);
ma_device_uninit(&state.devices[type]);
return stoppingResult == MA_SUCCESS;
}
@ -451,9 +458,12 @@ void lovrAudioSetCaptureFormat(SampleFormat format, int sampleRate)
// restart device if needed
ma_uint32 previousState = state.devices[AUDIO_CAPTURE].state;
if (previousState != MA_STATE_UNINITIALIZED && previousState != MA_STATE_STOPPED) {
if (previousState != MA_STATE_UNINITIALIZED) {
lovrAudioStop(AUDIO_CAPTURE);
lovrAudioStart(AUDIO_CAPTURE);
ma_device_uninit(&state.devices[AUDIO_CAPTURE]);
if (previousState == MA_STATE_STARTED) {
lovrAudioStart(AUDIO_CAPTURE);
}
}
}
@ -471,8 +481,11 @@ void lovrAudioUseDevice(AudioType type, const char *deviceName) {
// restart device if needed
ma_uint32 previousState = state.devices[type].state;
if (previousState != MA_STATE_UNINITIALIZED && previousState != MA_STATE_STOPPED) {
if (previousState != MA_STATE_UNINITIALIZED) {
lovrAudioStop(type);
lovrAudioStart(type);
ma_device_uninit(&state.devices[type]);
if (previousState == MA_STATE_STARTED) {
lovrAudioStart(type);
}
}
}