audio: Provide a way for Module to specify nominal latency
The latency figure depends on the module implementation.
Instead of using a hardcoded value, each module should be
able to specify its own value. This value is then used
for calculating the minimum buffer size.
Set the nominal latency of the primary (CF) module to a high
value since the virtual device implementation fails CTS tests
if it attempts to pretend that it provides low latency.
Bug: 302132812
Test: atest CtsMediaAudioTestCases --test-filter=".*AudioTrackTest.*"
Test: atest CtsMediaAudioTestCases --test-filter=".*AudioRecordTest.*"
Change-Id: I8ce9f230378eea787c9b3c7ce3660c1e4e7bc895
diff --git a/audio/aidl/default/primary/StreamPrimary.cpp b/audio/aidl/default/primary/StreamPrimary.cpp
index 17de2ba..1b1ea68 100644
--- a/audio/aidl/default/primary/StreamPrimary.cpp
+++ b/audio/aidl/default/primary/StreamPrimary.cpp
@@ -14,11 +14,12 @@
* limitations under the License.
*/
-#include <limits>
+#include <chrono>
#define LOG_TAG "AHAL_StreamPrimary"
#include <android-base/logging.h>
#include <android-base/properties.h>
+#include <audio_utils/clock.h>
#include <error/expected_utils.h>
#include "PrimaryMixer.h"
@@ -37,10 +38,34 @@
namespace aidl::android::hardware::audio::core {
StreamPrimary::StreamPrimary(StreamContext* context, const Metadata& metadata)
- : StreamAlsa(context, metadata, 3 /*readWriteRetries*/), mIsInput(isInput(metadata)) {
+ : StreamAlsa(context, metadata, 3 /*readWriteRetries*/),
+ mIsAsynchronous(!!getContext().getAsyncCallback()) {
context->startStreamDataProcessor();
}
+::android::status_t StreamPrimary::transfer(void* buffer, size_t frameCount,
+ size_t* actualFrameCount, int32_t* latencyMs) {
+ auto start = std::chrono::steady_clock::now();
+ if (auto status = StreamAlsa::transfer(buffer, frameCount, actualFrameCount, latencyMs);
+ status != ::android::OK) {
+ return status;
+ }
+ // This is a workaround for the emulator implementation which has a host-side buffer
+ // and this can result in reading faster than real time.
+ if (mIsInput && !mIsAsynchronous) {
+ auto recordDurationUs = std::chrono::duration_cast<std::chrono::microseconds>(
+ std::chrono::steady_clock::now() - start);
+ const long projectedVsObservedOffsetUs =
+ *actualFrameCount * MICROS_PER_SECOND / mContext.getSampleRate() -
+ recordDurationUs.count();
+ if (projectedVsObservedOffsetUs > 0) {
+ LOG(VERBOSE) << __func__ << ": sleeping for " << projectedVsObservedOffsetUs << " us";
+ usleep(projectedVsObservedOffsetUs);
+ }
+ }
+ return ::android::OK;
+}
+
std::vector<alsa::DeviceProfile> StreamPrimary::getDeviceProfiles() {
static const std::vector<alsa::DeviceProfile> kBuiltInSource{
alsa::DeviceProfile{.card = primary::PrimaryMixer::kAlsaCard,
@@ -66,7 +91,8 @@
GetBoolProperty("ro.boot.audio.tinyalsa.simulate_input", false);
return kSimulateInput || device.type.type == AudioDeviceType::IN_TELEPHONY_RX ||
device.type.type == AudioDeviceType::IN_FM_TUNER ||
- device.type.connection == AudioDeviceDescription::CONNECTION_BUS;
+ device.type.connection == AudioDeviceDescription::CONNECTION_BUS ||
+ (device.type.type == AudioDeviceType::IN_DEVICE && device.type.connection.empty());
}
StreamSwitcher::DeviceSwitchBehavior StreamInPrimary::switchCurrentStream(
@@ -132,7 +158,8 @@
static const bool kSimulateOutput =
GetBoolProperty("ro.boot.audio.tinyalsa.ignore_output", false);
return kSimulateOutput || device.type.type == AudioDeviceType::OUT_TELEPHONY_TX ||
- device.type.connection == AudioDeviceDescription::CONNECTION_BUS;
+ device.type.connection == AudioDeviceDescription::CONNECTION_BUS ||
+ (device.type.type == AudioDeviceType::OUT_DEVICE && device.type.connection.empty());
}
StreamSwitcher::DeviceSwitchBehavior StreamOutPrimary::switchCurrentStream(