blob: bd53a0ef2afa9991b37e5440c92519fb57783de5 [file] [log] [blame]
jiabin253bd322023-01-25 23:57:31 +00001/*
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#define LOG_TAG "AHAL_StreamUsb"
18#include <android-base/logging.h>
19
20#include "UsbAlsaUtils.h"
21#include "core-impl/Module.h"
22#include "core-impl/StreamUsb.h"
23
24extern "C" {
25#include "alsa_device_profile.h"
26}
27
28using aidl::android::hardware::audio::common::SinkMetadata;
29using aidl::android::hardware::audio::common::SourceMetadata;
30using aidl::android::media::audio::common::AudioDevice;
31using aidl::android::media::audio::common::AudioDeviceAddress;
32using aidl::android::media::audio::common::AudioOffloadInfo;
Mikhail Naganov6725ef52023-02-09 17:52:50 -080033using aidl::android::media::audio::common::MicrophoneDynamicInfo;
34using aidl::android::media::audio::common::MicrophoneInfo;
jiabin253bd322023-01-25 23:57:31 +000035
36namespace aidl::android::hardware::audio::core {
37
38DriverUsb::DriverUsb(const StreamContext& context, bool isInput)
39 : mFrameSizeBytes(context.getFrameSize()), mIsInput(isInput) {
40 struct pcm_config config;
41 config.channels = usb::getChannelCountFromChannelMask(context.getChannelLayout(), isInput);
42 if (config.channels == 0) {
43 LOG(ERROR) << __func__ << ": invalid channel=" << context.getChannelLayout().toString();
44 return;
45 }
46 config.format = usb::aidl2legacy_AudioFormatDescription_pcm_format(context.getFormat());
47 if (config.format == PCM_FORMAT_INVALID) {
48 LOG(ERROR) << __func__ << ": invalid format=" << context.getFormat().toString();
49 return;
50 }
51 config.rate = context.getSampleRate();
52 if (config.rate == 0) {
53 LOG(ERROR) << __func__ << ": invalid sample rate=" << config.rate;
54 return;
55 }
56 mConfig = config;
57}
58
59::android::status_t DriverUsb::init() {
60 return mConfig.has_value() ? ::android::OK : ::android::NO_INIT;
61}
62
63::android::status_t DriverUsb::setConnectedDevices(
64 const std::vector<AudioDevice>& connectedDevices) {
65 if (mIsInput && connectedDevices.size() > 1) {
66 LOG(ERROR) << __func__ << ": wrong device size(" << connectedDevices.size()
67 << ") for input stream";
68 return ::android::BAD_VALUE;
69 }
70 for (const auto& connectedDevice : connectedDevices) {
71 if (connectedDevice.address.getTag() != AudioDeviceAddress::alsa) {
72 LOG(ERROR) << __func__ << ": bad device address" << connectedDevice.address.toString();
73 return ::android::BAD_VALUE;
74 }
75 }
76 std::lock_guard guard(mLock);
77 mAlsaDeviceProxies.clear();
78 mConnectedDevices.clear();
79 for (const auto& connectedDevice : connectedDevices) {
80 mConnectedDevices.push_back(connectedDevice.address);
81 }
82 return ::android::OK;
83}
84
85::android::status_t DriverUsb::drain(StreamDescriptor::DrainMode) {
86 usleep(1000);
87 return ::android::OK;
88}
89
90::android::status_t DriverUsb::flush() {
91 usleep(1000);
92 return ::android::OK;
93}
94
95::android::status_t DriverUsb::pause() {
96 usleep(1000);
97 return ::android::OK;
98}
99
100::android::status_t DriverUsb::transfer(void* buffer, size_t frameCount, size_t* actualFrameCount,
101 int32_t* latencyMs) {
102 if (!mConfig.has_value() || mConnectedDevices.empty()) {
103 return ::android::NO_INIT;
104 }
105 if (mIsStandby) {
106 if (::android::status_t status = exitStandby(); status != ::android::OK) {
107 return status;
108 }
109 }
110 std::vector<std::shared_ptr<alsa_device_proxy>> alsaDeviceProxies;
111 {
112 std::lock_guard guard(mLock);
113 alsaDeviceProxies = mAlsaDeviceProxies;
114 }
115 const size_t bytesToTransfer = frameCount * mFrameSizeBytes;
116 if (mIsInput) {
117 // For input case, only support single device.
118 proxy_read(alsaDeviceProxies[0].get(), buffer, bytesToTransfer);
119 } else {
120 for (auto& proxy : alsaDeviceProxies) {
121 proxy_write(proxy.get(), buffer, bytesToTransfer);
122 }
123 }
124 *actualFrameCount = frameCount;
125 *latencyMs = Module::kLatencyMs;
126 return ::android::OK;
127}
128
129::android::status_t DriverUsb::standby() {
130 if (!mIsStandby) {
131 std::lock_guard guard(mLock);
132 mAlsaDeviceProxies.clear();
133 mIsStandby = true;
134 }
135 return ::android::OK;
136}
137
138::android::status_t DriverUsb::exitStandby() {
139 std::vector<AudioDeviceAddress> connectedDevices;
140 {
141 std::lock_guard guard(mLock);
142 connectedDevices = mConnectedDevices;
143 }
144 std::vector<std::shared_ptr<alsa_device_proxy>> alsaDeviceProxies;
145 for (const auto& device : connectedDevices) {
146 alsa_device_profile profile;
147 profile.card = device.get<AudioDeviceAddress::alsa>()[0];
148 profile.device = device.get<AudioDeviceAddress::alsa>()[1];
149 if (!profile_read_device_info(&profile)) {
150 LOG(ERROR) << __func__
151 << ": unable to read device info, device address=" << device.toString();
152 return ::android::UNKNOWN_ERROR;
153 }
154
155 auto proxy = std::shared_ptr<alsa_device_proxy>(new alsa_device_proxy(),
156 [](alsa_device_proxy* proxy) {
157 proxy_close(proxy);
158 free(proxy);
159 });
160 // Always ask for alsa configure as required since the configuration should be supported
161 // by the connected device. That is guaranteed by `setAudioPortConfig` and
162 // `setAudioPatch`.
163 if (int err =
164 proxy_prepare(proxy.get(), &profile, &mConfig.value(), true /*is_bit_perfect*/);
165 err != 0) {
166 LOG(ERROR) << __func__ << ": fail to prepare for device address=" << device.toString()
167 << " error=" << err;
168 return ::android::UNKNOWN_ERROR;
169 }
170 alsaDeviceProxies.push_back(std::move(proxy));
171 }
172 {
173 std::lock_guard guard(mLock);
174 mAlsaDeviceProxies = alsaDeviceProxies;
175 }
176 mIsStandby = false;
177 return ::android::OK;
178}
179
180// static
181ndk::ScopedAStatus StreamInUsb::createInstance(const SinkMetadata& sinkMetadata,
182 StreamContext&& context,
183 const std::vector<MicrophoneInfo>& microphones,
184 std::shared_ptr<StreamIn>* result) {
185 std::shared_ptr<StreamIn> stream =
186 ndk::SharedRefBase::make<StreamInUsb>(sinkMetadata, std::move(context), microphones);
187 if (auto status = initInstance(stream); !status.isOk()) {
188 return status;
189 }
190 *result = std::move(stream);
191 return ndk::ScopedAStatus::ok();
192}
193
194StreamInUsb::StreamInUsb(const SinkMetadata& sinkMetadata, StreamContext&& context,
195 const std::vector<MicrophoneInfo>& microphones)
196 : StreamIn(
197 sinkMetadata, std::move(context),
198 [](const StreamContext& ctx) -> DriverInterface* {
199 return new DriverUsb(ctx, true /*isInput*/);
200 },
201 [](const StreamContext& ctx, DriverInterface* driver) -> StreamWorkerInterface* {
202 // The default worker implementation is used.
203 return new StreamInWorker(ctx, driver);
204 },
205 microphones) {}
206
207ndk::ScopedAStatus StreamInUsb::getActiveMicrophones(
208 std::vector<MicrophoneDynamicInfo>* _aidl_return __unused) {
209 LOG(DEBUG) << __func__ << ": not supported";
210 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
211}
212
213// static
214ndk::ScopedAStatus StreamOutUsb::createInstance(const SourceMetadata& sourceMetadata,
215 StreamContext&& context,
216 const std::optional<AudioOffloadInfo>& offloadInfo,
217 std::shared_ptr<StreamOut>* result) {
218 if (offloadInfo.has_value()) {
219 LOG(ERROR) << __func__ << ": offload is not supported";
220 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
221 }
222 std::shared_ptr<StreamOut> stream =
223 ndk::SharedRefBase::make<StreamOutUsb>(sourceMetadata, std::move(context), offloadInfo);
224 if (auto status = initInstance(stream); !status.isOk()) {
225 return status;
226 }
227 *result = std::move(stream);
228 return ndk::ScopedAStatus::ok();
229}
230
231StreamOutUsb::StreamOutUsb(const SourceMetadata& sourceMetadata, StreamContext&& context,
232 const std::optional<AudioOffloadInfo>& offloadInfo)
233 : StreamOut(
234 sourceMetadata, std::move(context),
235 [](const StreamContext& ctx) -> DriverInterface* {
236 return new DriverUsb(ctx, false /*isInput*/);
237 },
238 [](const StreamContext& ctx, DriverInterface* driver) -> StreamWorkerInterface* {
239 // The default worker implementation is used.
240 return new StreamOutWorker(ctx, driver);
241 },
242 offloadInfo) {}
243
Mikhail Naganov6725ef52023-02-09 17:52:50 -0800244} // namespace aidl::android::hardware::audio::core