audio: fix race when stopping a stream
Stopping a Legacy stream could result in the stream
becoming killed. Then it could not be restarted.
This change results in the stream callback just returning safely.
Bug: 192095564
Test: see bug report
Test: atest CtsNativeMediaAAudioTestCases
Change-Id: I2d58e94bf9e7288b56e750e03188102b383b6479
diff --git a/media/libaaudio/src/legacy/AudioStreamLegacy.cpp b/media/libaaudio/src/legacy/AudioStreamLegacy.cpp
index 60eb73a..e96e134 100644
--- a/media/libaaudio/src/legacy/AudioStreamLegacy.cpp
+++ b/media/libaaudio/src/legacy/AudioStreamLegacy.cpp
@@ -94,10 +94,15 @@
AudioTrack::Buffer *audioBuffer = static_cast<AudioTrack::Buffer *>(info);
if (getState() == AAUDIO_STREAM_STATE_DISCONNECTED) {
ALOGW("processCallbackCommon() data, stream disconnected");
+ // This will kill the stream and prevent it from being restarted.
+ // That is OK because the stream is disconnected.
audioBuffer->size = SIZE_STOP_CALLBACKS;
} else if (!mCallbackEnabled.load()) {
- ALOGW("processCallbackCommon() no data because callback disabled");
- audioBuffer->size = SIZE_STOP_CALLBACKS;
+ ALOGW("processCallbackCommon() no data because callback disabled, set size=0");
+ // Do NOT use SIZE_STOP_CALLBACKS here because that will kill the stream and
+ // prevent it from being restarted. This can occur because of a race condition
+ // caused by Legacy callbacks running after the track is "stopped".
+ audioBuffer->size = 0;
} else {
if (audioBuffer->frameCount == 0) {
ALOGW("processCallbackCommon() data, frameCount is zero");