aaudio: fix intermittent hang and position error
Fix hang caused by recursive mutex.
Fix disconnect caused by getPosition() failing, which was
just because the stream wasn't ready yet.
Bug: 63775537
Bug: 63709749
Test: run "aaudio_loopback -pl -Pl -c2 -n6 -te -m" many times
Change-Id: Ic1d54360b55cfc8ecc1809584c262bc0976c58bb
diff --git a/services/oboeservice/AAudioServiceEndpoint.cpp b/services/oboeservice/AAudioServiceEndpoint.cpp
index 5895974..1421468 100644
--- a/services/oboeservice/AAudioServiceEndpoint.cpp
+++ b/services/oboeservice/AAudioServiceEndpoint.cpp
@@ -112,10 +112,10 @@
}
aaudio_result_t AAudioServiceEndpoint::startStream(sp<AAudioServiceStreamShared> sharedStream) {
- // TODO use real-time technique to avoid mutex, eg. atomic command FIFO
aaudio_result_t result = AAUDIO_OK;
- std::lock_guard<std::mutex> lock(mLockStreams);
if (++mRunningStreams == 1) {
+ // TODO use real-time technique to avoid mutex, eg. atomic command FIFO
+ std::lock_guard<std::mutex> lock(mLockStreams);
result = getStreamInternal()->requestStart();
startSharingThread_l();
}
@@ -123,13 +123,8 @@
}
aaudio_result_t AAudioServiceEndpoint::stopStream(sp<AAudioServiceStreamShared> sharedStream) {
- int numRunningStreams = 0;
- {
- std::lock_guard<std::mutex> lock(mLockStreams);
- numRunningStreams = --mRunningStreams;
- }
- if (numRunningStreams == 0) {
- // Don't call this under a lock because the callbackLoop also uses the lock.
+ // Don't lock here because the disconnectRegisteredStreams also uses the lock.
+ if (--mRunningStreams == 0) { // atomic
stopSharingThread();
getStreamInternal()->requestStop();
}
diff --git a/services/oboeservice/AAudioServiceEndpoint.h b/services/oboeservice/AAudioServiceEndpoint.h
index ed995e5..e40a670 100644
--- a/services/oboeservice/AAudioServiceEndpoint.h
+++ b/services/oboeservice/AAudioServiceEndpoint.h
@@ -73,13 +73,13 @@
virtual AudioStreamInternal *getStreamInternal() = 0;
- std::atomic<bool> mCallbackEnabled;
+ std::atomic<bool> mCallbackEnabled{false};
mutable std::mutex mLockStreams;
std::vector<android::sp<AAudioServiceStreamShared>> mRegisteredStreams;
- size_t mRunningStreams = 0;
+ std::atomic<int> mRunningStreams{0};
private:
aaudio_result_t startSharingThread_l();
diff --git a/services/oboeservice/AAudioServiceStreamBase.cpp b/services/oboeservice/AAudioServiceStreamBase.cpp
index e0f6ad4..5f7d179 100644
--- a/services/oboeservice/AAudioServiceStreamBase.cpp
+++ b/services/oboeservice/AAudioServiceStreamBase.cpp
@@ -219,6 +219,8 @@
// (long long) command.timestamp.timestamp);
command.what = AAudioServiceMessage::code::TIMESTAMP;
result = writeUpMessageQueue(&command);
+ } else if (result == AAUDIO_ERROR_UNAVAILABLE) {
+ result = AAUDIO_OK; // just not available yet, try again later
}
return result;
}
diff --git a/services/oboeservice/AAudioServiceStreamBase.h b/services/oboeservice/AAudioServiceStreamBase.h
index 93a522e..cebefec 100644
--- a/services/oboeservice/AAudioServiceStreamBase.h
+++ b/services/oboeservice/AAudioServiceStreamBase.h
@@ -163,6 +163,11 @@
aaudio_result_t sendCurrentTimestamp();
+ /**
+ * @param positionFrames
+ * @param timeNanos
+ * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error
+ */
virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0;
virtual aaudio_result_t getDownDataDescription(AudioEndpointParcelable &parcelable) = 0;
diff --git a/services/oboeservice/AAudioServiceStreamMMAP.cpp b/services/oboeservice/AAudioServiceStreamMMAP.cpp
index 68be3c3..69dbea8 100644
--- a/services/oboeservice/AAudioServiceStreamMMAP.cpp
+++ b/services/oboeservice/AAudioServiceStreamMMAP.cpp
@@ -297,16 +297,18 @@
return AAUDIO_ERROR_NULL;
}
status_t status = mMmapStream->getMmapPosition(&position);
- if (status != OK) {
- ALOGE("sendCurrentTimestamp(): getMmapPosition() returned %d", status);
+ aaudio_result_t result = AAudioConvert_androidToAAudioResult(status);
+ if (result == AAUDIO_ERROR_UNAVAILABLE) {
+ ALOGW("sendCurrentTimestamp(): getMmapPosition() has no position data yet");
+ } else if (result != AAUDIO_OK) {
+ ALOGE("sendCurrentTimestamp(): getMmapPosition() returned status %d", status);
disconnect();
- return AAudioConvert_androidToAAudioResult(status);
} else {
mFramesRead.update32(position.position_frames);
*positionFrames = mFramesRead.get();
*timeNanos = position.time_nanoseconds;
}
- return AAUDIO_OK;
+ return result;
}
void AAudioServiceStreamMMAP::onTearDown() {