blob: 00a7a8426b2fe39714c20d9caae13e8552481c37 [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
17#include <limits>
18
19#define LOG_TAG "AHAL_StreamAlsa"
20#include <android-base/logging.h>
21
22#include <Utils.h>
Mikhail Naganov704aec42023-07-13 11:08:29 -070023#include <audio_utils/clock.h>
Mikhail Naganovc337a872023-07-07 12:01:17 -070024#include <error/expected_utils.h>
25
26#include "core-impl/StreamAlsa.h"
27
28namespace aidl::android::hardware::audio::core {
29
Mikhail Naganov1eedc132023-07-21 17:45:28 -070030StreamAlsa::StreamAlsa(StreamContext* context, const Metadata& metadata, int readWriteRetries)
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -070031 : StreamCommonImpl(context, metadata),
Mikhail Naganovc337a872023-07-07 12:01:17 -070032 mFrameSizeBytes(getContext().getFrameSize()),
33 mIsInput(isInput(metadata)),
Mikhail Naganov422f7e62023-07-13 16:32:08 -070034 mConfig(alsa::getPcmConfig(getContext(), mIsInput)),
35 mReadWriteRetries(readWriteRetries) {}
Mikhail Naganovc337a872023-07-07 12:01:17 -070036
37::android::status_t StreamAlsa::init() {
38 return mConfig.has_value() ? ::android::OK : ::android::NO_INIT;
39}
40
Mikhail Naganov422f7e62023-07-13 16:32:08 -070041::android::status_t StreamAlsa::drain(StreamDescriptor::DrainMode) {
42 usleep(1000);
43 return ::android::OK;
44}
45
46::android::status_t StreamAlsa::flush() {
47 usleep(1000);
48 return ::android::OK;
49}
50
51::android::status_t StreamAlsa::pause() {
52 usleep(1000);
53 return ::android::OK;
54}
55
Mikhail Naganovc337a872023-07-07 12:01:17 -070056::android::status_t StreamAlsa::standby() {
57 mAlsaDeviceProxies.clear();
58 return ::android::OK;
59}
60
61::android::status_t StreamAlsa::start() {
62 decltype(mAlsaDeviceProxies) alsaDeviceProxies;
63 for (const auto& device : getDeviceProfiles()) {
Mikhail Naganov422f7e62023-07-13 16:32:08 -070064 alsa::DeviceProxy proxy;
65 if (device.isExternal) {
66 // Always ask alsa configure as required since the configuration should be supported
67 // by the connected device. That is guaranteed by `setAudioPortConfig` and
68 // `setAudioPatch`.
69 proxy = alsa::openProxyForExternalDevice(
70 device, const_cast<struct pcm_config*>(&mConfig.value()),
71 true /*require_exact_match*/);
72 } else {
73 proxy = alsa::openProxyForAttachedDevice(
74 device, const_cast<struct pcm_config*>(&mConfig.value()),
75 getContext().getBufferSizeInFrames());
Mikhail Naganovc337a872023-07-07 12:01:17 -070076 }
Mikhail Naganov422f7e62023-07-13 16:32:08 -070077 if (!proxy) {
78 return ::android::NO_INIT;
Mikhail Naganovc337a872023-07-07 12:01:17 -070079 }
80 alsaDeviceProxies.push_back(std::move(proxy));
81 }
82 mAlsaDeviceProxies = std::move(alsaDeviceProxies);
83 return ::android::OK;
84}
85
86::android::status_t StreamAlsa::transfer(void* buffer, size_t frameCount, size_t* actualFrameCount,
87 int32_t* latencyMs) {
88 const size_t bytesToTransfer = frameCount * mFrameSizeBytes;
89 unsigned maxLatency = 0;
90 if (mIsInput) {
91 if (mAlsaDeviceProxies.empty()) {
92 LOG(FATAL) << __func__ << ": no input devices";
93 return ::android::NO_INIT;
94 }
95 // For input case, only support single device.
Mikhail Naganov422f7e62023-07-13 16:32:08 -070096 proxy_read_with_retries(mAlsaDeviceProxies[0].get(), buffer, bytesToTransfer,
97 mReadWriteRetries);
Mikhail Naganovc337a872023-07-07 12:01:17 -070098 maxLatency = proxy_get_latency(mAlsaDeviceProxies[0].get());
99 } else {
100 for (auto& proxy : mAlsaDeviceProxies) {
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700101 proxy_write_with_retries(proxy.get(), buffer, bytesToTransfer, mReadWriteRetries);
Mikhail Naganovc337a872023-07-07 12:01:17 -0700102 maxLatency = std::max(maxLatency, proxy_get_latency(proxy.get()));
103 }
104 }
105 *actualFrameCount = frameCount;
106 maxLatency = std::min(maxLatency, static_cast<unsigned>(std::numeric_limits<int32_t>::max()));
107 *latencyMs = maxLatency;
108 return ::android::OK;
109}
110
Mikhail Naganov704aec42023-07-13 11:08:29 -0700111::android::status_t StreamAlsa::getPosition(StreamDescriptor::Position* position) {
112 if (mAlsaDeviceProxies.empty()) {
113 LOG(FATAL) << __func__ << ": no input devices";
114 return ::android::NO_INIT;
115 }
116 if (mIsInput) {
117 if (int ret = proxy_get_capture_position(mAlsaDeviceProxies[0].get(), &position->frames,
118 &position->timeNs);
119 ret != 0) {
120 LOG(WARNING) << __func__ << ": failed to retrieve capture position: " << ret;
121 return ::android::INVALID_OPERATION;
122 }
123 } else {
124 uint64_t hwFrames;
125 struct timespec timestamp;
126 if (int ret = proxy_get_presentation_position(mAlsaDeviceProxies[0].get(), &hwFrames,
127 &timestamp);
128 ret == 0) {
129 if (hwFrames > std::numeric_limits<int64_t>::max()) {
130 hwFrames -= std::numeric_limits<int64_t>::max();
131 }
132 position->frames = static_cast<int64_t>(hwFrames);
133 position->timeNs = audio_utils_ns_from_timespec(&timestamp);
134 } else {
135 LOG(WARNING) << __func__ << ": failed to retrieve presentation position: " << ret;
136 return ::android::INVALID_OPERATION;
137 }
138 }
139 return ::android::OK;
140}
141
Mikhail Naganovc337a872023-07-07 12:01:17 -0700142void StreamAlsa::shutdown() {
143 mAlsaDeviceProxies.clear();
144}
145
146} // namespace aidl::android::hardware::audio::core