Commit cbabd2d1 authored by znakeeye's avatar znakeeye Committed by David Reid

AAudio re-routing slightly improved. Retrying re-routing at most three times....

AAudio re-routing slightly improved. Retrying re-routing at most three times. If some BlueTooth device goes nuts and toggles connection state back and forth, we bail out.
parent 546e23c0
...@@ -37735,44 +37735,35 @@ static ma_aaudio_allowed_capture_policy_t ma_to_allowed_capture_policy__aaudio(m ...@@ -37735,44 +37735,35 @@ static ma_aaudio_allowed_capture_policy_t ma_to_allowed_capture_policy__aaudio(m
return MA_AAUDIO_ALLOW_CAPTURE_BY_ALL; return MA_AAUDIO_ALLOW_CAPTURE_BY_ALL;
} }
static void ma_post_reroute_job__aaudio(ma_device* pDevice, ma_device_type deviceType) static void ma_stream_error_callback__aaudio(ma_AAudioStream* pStream, void* pUserData, ma_aaudio_result_t error)
{ {
ma_result result; ma_result result;
ma_job job; ma_job job;
job = ma_job_init(MA_JOB_TYPE_DEVICE_AAUDIO_REROUTE);
job.data.device.aaudio.reroute.pDevice = pDevice;
job.data.device.aaudio.reroute.deviceType = deviceType;
result = ma_device_job_thread_post(&pDevice->pContext->aaudio.jobThread, &job);
if (result != MA_SUCCESS) {
ma_log_postf(ma_device_get_log(pDevice), MA_LOG_LEVEL_INFO, "[AAudio] Device Disconnected. Failed to post job for rerouting.\n");
}
}
static void ma_stream_error_callback__aaudio(ma_AAudioStream* pStream, void* pUserData, ma_aaudio_result_t error)
{
ma_device* pDevice = (ma_device*)pUserData; ma_device* pDevice = (ma_device*)pUserData;
ma_device_type deviceType;
MA_ASSERT(pDevice != NULL); MA_ASSERT(pDevice != NULL);
(void)error; (void)error;
ma_log_postf(ma_device_get_log(pDevice), MA_LOG_LEVEL_INFO, "[AAudio] ERROR CALLBACK: error=%d, AAudioStream_getState()=%d\n", error, ((MA_PFN_AAudioStream_getState)pDevice->pContext->aaudio.AAudioStream_getState)(pStream)); ma_log_postf(ma_device_get_log(pDevice), MA_LOG_LEVEL_INFO, "[AAudio] ERROR CALLBACK: error=%d, AAudioStream_getState()=%d\n", error, ((MA_PFN_AAudioStream_getState)pDevice->pContext->aaudio.AAudioStream_getState)(pStream));
/* /*
When we get an error, we'll assume that the stream is in an erroneous state and needs to be restarted. From the documentation, When we get an error, we'll assume that the stream is in an erroneous state and needs to be restarted. From the documentation,
we cannot do this from the error callback. Therefore we are going to use an event thread for the AAudio backend to do this we cannot do this from the error callback. Therefore we are going to use an event thread for the AAudio backend to do this
cleanly and safely. cleanly and safely.
*/ */
job = ma_job_init(MA_JOB_TYPE_DEVICE_AAUDIO_REROUTE);
job.data.device.aaudio.reroute.pDevice = pDevice;
if (pStream == pDevice->aaudio.pStreamCapture) { if (pStream == pDevice->aaudio.pStreamCapture) {
deviceType = ma_device_type_capture; job.data.device.aaudio.reroute.deviceType = ma_device_type_capture;
} else { }
deviceType = ma_device_type_playback; else {
job.data.device.aaudio.reroute.deviceType = ma_device_type_playback;
} }
ma_post_reroute_job__aaudio(pDevice, deviceType); result = ma_device_job_thread_post(&pDevice->pContext->aaudio.jobThread, &job);
if (result != MA_SUCCESS) {
ma_log_postf(ma_device_get_log(pDevice), MA_LOG_LEVEL_INFO, "[AAudio] Device Disconnected. Failed to post job for rerouting.\n");
return;
}
} }
static ma_aaudio_data_callback_result_t ma_stream_data_callback_capture__aaudio(ma_AAudioStream* pStream, void* pUserData, void* pAudioData, int32_t frameCount) static ma_aaudio_data_callback_result_t ma_stream_data_callback_capture__aaudio(ma_AAudioStream* pStream, void* pUserData, void* pAudioData, int32_t frameCount)
...@@ -38310,9 +38301,11 @@ static ma_result ma_device_stop__aaudio(ma_device* pDevice) ...@@ -38310,9 +38301,11 @@ static ma_result ma_device_stop__aaudio(ma_device* pDevice)
static ma_result ma_device_reinit__aaudio(ma_device* pDevice, ma_device_type deviceType) static ma_result ma_device_reinit__aaudio(ma_device* pDevice, ma_device_type deviceType)
{ {
ma_result result; ma_result result;
int32_t retries = 0;
MA_ASSERT(pDevice != NULL); MA_ASSERT(pDevice != NULL);
error_disconnected:
/* The first thing to do is close the streams. */ /* The first thing to do is close the streams. */
if (deviceType == ma_device_type_capture || deviceType == ma_device_type_duplex) { if (deviceType == ma_device_type_capture || deviceType == ma_device_type_duplex) {
ma_close_stream__aaudio(pDevice->pContext, (ma_AAudioStream*)pDevice->aaudio.pStreamCapture); ma_close_stream__aaudio(pDevice->pContext, (ma_AAudioStream*)pDevice->aaudio.pStreamCapture);
...@@ -38397,8 +38390,12 @@ static ma_result ma_device_reinit__aaudio(ma_device* pDevice, ma_device_type dev ...@@ -38397,8 +38390,12 @@ static ma_result ma_device_reinit__aaudio(ma_device* pDevice, ma_device_type dev
if (pDevice->aaudio.noAutoStartAfterReroute == MA_FALSE) { if (pDevice->aaudio.noAutoStartAfterReroute == MA_FALSE) {
result = ma_device_start__aaudio(pDevice); result = ma_device_start__aaudio(pDevice);
if (result != MA_SUCCESS) { if (result != MA_SUCCESS) {
/* We got disconnected! Retry a few times, until we find a connected device! */
if (++retries <= 3) {
ma_log_postf(ma_device_get_log(pDevice), MA_LOG_LEVEL_INFO, "[AAudio] Failed to start stream after route change, retrying(%d)", retries);
goto error_disconnected;
}
ma_log_post(ma_device_get_log(pDevice), MA_LOG_LEVEL_INFO, "[AAudio] Failed to start stream after route change."); ma_log_post(ma_device_get_log(pDevice), MA_LOG_LEVEL_INFO, "[AAudio] Failed to start stream after route change.");
ma_post_reroute_job__aaudio(pDevice, deviceType); /* Bad luck! We got disconnected again. Retry re-routing! */
return result; return result;
} }
} else { } else {
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment