blob: f548903de2939cb14f6ec4e19547d4b6b3ec0d19 [file] [log] [blame]
Mikhail Naganovc337a872023-07-07 12:01:17 -07001/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mikhail Naganovcf824f62023-07-24 14:51:36 -070017#include <cmath>
Mikhail Naganovc337a872023-07-07 12:01:17 -070018#include <limits>
19
20#define LOG_TAG "AHAL_StreamAlsa"
21#include <android-base/logging.h>
22
23#include <Utils.h>
Mikhail Naganov704aec42023-07-13 11:08:29 -070024#include <audio_utils/clock.h>
Mikhail Naganovc337a872023-07-07 12:01:17 -070025#include <error/expected_utils.h>
26
27#include "core-impl/StreamAlsa.h"
28
29namespace aidl::android::hardware::audio::core {
30
Mikhail Naganov1eedc132023-07-21 17:45:28 -070031StreamAlsa::StreamAlsa(StreamContext* context, const Metadata& metadata, int readWriteRetries)
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -070032 : StreamCommonImpl(context, metadata),
Mikhail Naganovcf824f62023-07-24 14:51:36 -070033 mBufferSizeFrames(getContext().getBufferSizeInFrames()),
Mikhail Naganovc337a872023-07-07 12:01:17 -070034 mFrameSizeBytes(getContext().getFrameSize()),
Mikhail Naganovcf824f62023-07-24 14:51:36 -070035 mSampleRate(getContext().getSampleRate()),
Mikhail Naganovc337a872023-07-07 12:01:17 -070036 mIsInput(isInput(metadata)),
Mikhail Naganov422f7e62023-07-13 16:32:08 -070037 mConfig(alsa::getPcmConfig(getContext(), mIsInput)),
38 mReadWriteRetries(readWriteRetries) {}
Mikhail Naganovc337a872023-07-07 12:01:17 -070039
Mikhail Naganov0413d072024-08-15 14:12:39 -070040StreamAlsa::~StreamAlsa() {
41 cleanupWorker();
42}
43
Mikhail Naganovc337a872023-07-07 12:01:17 -070044::android::status_t StreamAlsa::init() {
45 return mConfig.has_value() ? ::android::OK : ::android::NO_INIT;
46}
47
Mikhail Naganov422f7e62023-07-13 16:32:08 -070048::android::status_t StreamAlsa::drain(StreamDescriptor::DrainMode) {
Mikhail Naganovcf824f62023-07-24 14:51:36 -070049 if (!mIsInput) {
50 static constexpr float kMicrosPerSecond = MICROS_PER_SECOND;
51 const size_t delayUs = static_cast<size_t>(
52 std::roundf(mBufferSizeFrames * kMicrosPerSecond / mSampleRate));
53 usleep(delayUs);
54 }
Mikhail Naganov422f7e62023-07-13 16:32:08 -070055 return ::android::OK;
56}
57
58::android::status_t StreamAlsa::flush() {
Mikhail Naganov422f7e62023-07-13 16:32:08 -070059 return ::android::OK;
60}
61
62::android::status_t StreamAlsa::pause() {
Mikhail Naganov422f7e62023-07-13 16:32:08 -070063 return ::android::OK;
64}
65
Mikhail Naganovc337a872023-07-07 12:01:17 -070066::android::status_t StreamAlsa::standby() {
67 mAlsaDeviceProxies.clear();
68 return ::android::OK;
69}
70
71::android::status_t StreamAlsa::start() {
Mikhail Naganovcf824f62023-07-24 14:51:36 -070072 if (!mAlsaDeviceProxies.empty()) {
73 // This is a resume after a pause.
74 return ::android::OK;
75 }
Mikhail Naganovc337a872023-07-07 12:01:17 -070076 decltype(mAlsaDeviceProxies) alsaDeviceProxies;
77 for (const auto& device : getDeviceProfiles()) {
Mikhail Naganov422f7e62023-07-13 16:32:08 -070078 alsa::DeviceProxy proxy;
79 if (device.isExternal) {
80 // Always ask alsa configure as required since the configuration should be supported
81 // by the connected device. That is guaranteed by `setAudioPortConfig` and
82 // `setAudioPatch`.
83 proxy = alsa::openProxyForExternalDevice(
84 device, const_cast<struct pcm_config*>(&mConfig.value()),
85 true /*require_exact_match*/);
86 } else {
87 proxy = alsa::openProxyForAttachedDevice(
Mikhail Naganovcf824f62023-07-24 14:51:36 -070088 device, const_cast<struct pcm_config*>(&mConfig.value()), mBufferSizeFrames);
Mikhail Naganovc337a872023-07-07 12:01:17 -070089 }
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -070090 if (proxy.get() == nullptr) {
Mikhail Naganov422f7e62023-07-13 16:32:08 -070091 return ::android::NO_INIT;
Mikhail Naganovc337a872023-07-07 12:01:17 -070092 }
93 alsaDeviceProxies.push_back(std::move(proxy));
94 }
95 mAlsaDeviceProxies = std::move(alsaDeviceProxies);
96 return ::android::OK;
97}
98
99::android::status_t StreamAlsa::transfer(void* buffer, size_t frameCount, size_t* actualFrameCount,
100 int32_t* latencyMs) {
Mikhail Naganovcf824f62023-07-24 14:51:36 -0700101 if (mAlsaDeviceProxies.empty()) {
102 LOG(FATAL) << __func__ << ": no opened devices";
103 return ::android::NO_INIT;
104 }
Mikhail Naganovc337a872023-07-07 12:01:17 -0700105 const size_t bytesToTransfer = frameCount * mFrameSizeBytes;
106 unsigned maxLatency = 0;
107 if (mIsInput) {
Mikhail Naganovc337a872023-07-07 12:01:17 -0700108 // For input case, only support single device.
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700109 proxy_read_with_retries(mAlsaDeviceProxies[0].get(), buffer, bytesToTransfer,
110 mReadWriteRetries);
Mikhail Naganovc337a872023-07-07 12:01:17 -0700111 maxLatency = proxy_get_latency(mAlsaDeviceProxies[0].get());
112 } else {
113 for (auto& proxy : mAlsaDeviceProxies) {
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700114 proxy_write_with_retries(proxy.get(), buffer, bytesToTransfer, mReadWriteRetries);
Mikhail Naganovc337a872023-07-07 12:01:17 -0700115 maxLatency = std::max(maxLatency, proxy_get_latency(proxy.get()));
116 }
117 }
118 *actualFrameCount = frameCount;
119 maxLatency = std::min(maxLatency, static_cast<unsigned>(std::numeric_limits<int32_t>::max()));
120 *latencyMs = maxLatency;
121 return ::android::OK;
122}
123
Mikhail Naganov459b7332023-08-03 10:26:21 -0700124::android::status_t StreamAlsa::refinePosition(StreamDescriptor::Position* position) {
Mikhail Naganov704aec42023-07-13 11:08:29 -0700125 if (mAlsaDeviceProxies.empty()) {
Mikhail Naganov13501872023-10-18 16:15:46 -0700126 LOG(WARNING) << __func__ << ": no opened devices";
Mikhail Naganov704aec42023-07-13 11:08:29 -0700127 return ::android::NO_INIT;
128 }
Mikhail Naganovcf824f62023-07-24 14:51:36 -0700129 // Since the proxy can only count transferred frames since its creation,
130 // we override its counter value with ours and let it to correct for buffered frames.
131 alsa::resetTransferredFrames(mAlsaDeviceProxies[0], position->frames);
Mikhail Naganov704aec42023-07-13 11:08:29 -0700132 if (mIsInput) {
133 if (int ret = proxy_get_capture_position(mAlsaDeviceProxies[0].get(), &position->frames,
134 &position->timeNs);
135 ret != 0) {
136 LOG(WARNING) << __func__ << ": failed to retrieve capture position: " << ret;
137 return ::android::INVALID_OPERATION;
138 }
139 } else {
140 uint64_t hwFrames;
141 struct timespec timestamp;
142 if (int ret = proxy_get_presentation_position(mAlsaDeviceProxies[0].get(), &hwFrames,
143 &timestamp);
144 ret == 0) {
145 if (hwFrames > std::numeric_limits<int64_t>::max()) {
146 hwFrames -= std::numeric_limits<int64_t>::max();
147 }
148 position->frames = static_cast<int64_t>(hwFrames);
149 position->timeNs = audio_utils_ns_from_timespec(&timestamp);
150 } else {
151 LOG(WARNING) << __func__ << ": failed to retrieve presentation position: " << ret;
152 return ::android::INVALID_OPERATION;
153 }
154 }
155 return ::android::OK;
156}
157
Mikhail Naganovc337a872023-07-07 12:01:17 -0700158void StreamAlsa::shutdown() {
159 mAlsaDeviceProxies.clear();
160}
161
162} // namespace aidl::android::hardware::audio::core