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'.
@@ -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,19 +245,23 @@ bool lovrAudioInitDevice(AudioType type) {
}
bool lovrAudioStart(AudioType type) {
+ ma_uint32 deviceState = state.devices[type].state;
+ if (deviceState == MA_STATE_UNINITIALIZED) {
bool initResult = lovrAudioInitDevice(type);
- if (initResult == false && type == AUDIO_CAPTURE) {
+ 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;
}
bool lovrAudioStop(AudioType type) {
ma_result stoppingResult = ma_device_stop(&state.devices[type]);
- ma_device_uninit(&state.devices[type]);
return stoppingResult == MA_SUCCESS;
}
@@ -451,11 +458,14 @@ 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);
+ ma_device_uninit(&state.devices[AUDIO_CAPTURE]);
+ if (previousState == MA_STATE_STARTED) {
lovrAudioStart(AUDIO_CAPTURE);
}
}
+}
void lovrAudioUseDevice(AudioType type, const char *deviceName) {
free(state.config[type].deviceName);
@@ -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);
+ ma_device_uninit(&state.devices[type]);
+ if (previousState == MA_STATE_STARTED) {
lovrAudioStart(type);
}
}
+}
