blob: 5d1d7febc80a03e8f049ed4482a6663741e6fbe7 [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
jiabin783c48b2023-02-28 18:28:06 +000020#include <Utils.h>
21
22#include "UsbAlsaMixerControl.h"
jiabin253bd322023-01-25 23:57:31 +000023#include "UsbAlsaUtils.h"
24#include "core-impl/Module.h"
25#include "core-impl/StreamUsb.h"
26
27extern "C" {
28#include "alsa_device_profile.h"
29}
30
Mikhail Naganov872d4a62023-03-09 18:19:01 -080031using aidl::android::hardware::audio::common::getChannelCount;
jiabin253bd322023-01-25 23:57:31 +000032using aidl::android::hardware::audio::common::SinkMetadata;
33using aidl::android::hardware::audio::common::SourceMetadata;
34using aidl::android::media::audio::common::AudioDevice;
35using aidl::android::media::audio::common::AudioDeviceAddress;
36using aidl::android::media::audio::common::AudioOffloadInfo;
jiabin783c48b2023-02-28 18:28:06 +000037using aidl::android::media::audio::common::AudioPortExt;
Mikhail Naganov6725ef52023-02-09 17:52:50 -080038using aidl::android::media::audio::common::MicrophoneDynamicInfo;
39using aidl::android::media::audio::common::MicrophoneInfo;
jiabin783c48b2023-02-28 18:28:06 +000040using android::OK;
41using android::status_t;
jiabin253bd322023-01-25 23:57:31 +000042
43namespace aidl::android::hardware::audio::core {
44
45DriverUsb::DriverUsb(const StreamContext& context, bool isInput)
46 : mFrameSizeBytes(context.getFrameSize()), mIsInput(isInput) {
47 struct pcm_config config;
48 config.channels = usb::getChannelCountFromChannelMask(context.getChannelLayout(), isInput);
49 if (config.channels == 0) {
50 LOG(ERROR) << __func__ << ": invalid channel=" << context.getChannelLayout().toString();
51 return;
52 }
53 config.format = usb::aidl2legacy_AudioFormatDescription_pcm_format(context.getFormat());
54 if (config.format == PCM_FORMAT_INVALID) {
55 LOG(ERROR) << __func__ << ": invalid format=" << context.getFormat().toString();
56 return;
57 }
58 config.rate = context.getSampleRate();
59 if (config.rate == 0) {
60 LOG(ERROR) << __func__ << ": invalid sample rate=" << config.rate;
61 return;
62 }
63 mConfig = config;
64}
65
66::android::status_t DriverUsb::init() {
67 return mConfig.has_value() ? ::android::OK : ::android::NO_INIT;
68}
69
70::android::status_t DriverUsb::setConnectedDevices(
71 const std::vector<AudioDevice>& connectedDevices) {
72 if (mIsInput && connectedDevices.size() > 1) {
73 LOG(ERROR) << __func__ << ": wrong device size(" << connectedDevices.size()
74 << ") for input stream";
75 return ::android::BAD_VALUE;
76 }
77 for (const auto& connectedDevice : connectedDevices) {
78 if (connectedDevice.address.getTag() != AudioDeviceAddress::alsa) {
79 LOG(ERROR) << __func__ << ": bad device address" << connectedDevice.address.toString();
80 return ::android::BAD_VALUE;
81 }
82 }
83 std::lock_guard guard(mLock);
84 mAlsaDeviceProxies.clear();
85 mConnectedDevices.clear();
86 for (const auto& connectedDevice : connectedDevices) {
87 mConnectedDevices.push_back(connectedDevice.address);
88 }
89 return ::android::OK;
90}
91
92::android::status_t DriverUsb::drain(StreamDescriptor::DrainMode) {
93 usleep(1000);
94 return ::android::OK;
95}
96
97::android::status_t DriverUsb::flush() {
98 usleep(1000);
99 return ::android::OK;
100}
101
102::android::status_t DriverUsb::pause() {
103 usleep(1000);
104 return ::android::OK;
105}
106
107::android::status_t DriverUsb::transfer(void* buffer, size_t frameCount, size_t* actualFrameCount,
108 int32_t* latencyMs) {
109 if (!mConfig.has_value() || mConnectedDevices.empty()) {
jiabinfdee3222023-03-22 22:16:13 +0000110 LOG(ERROR) << __func__ << ": failed, has config: " << mConfig.has_value()
111 << ", has connected devices: " << mConnectedDevices.empty();
jiabin253bd322023-01-25 23:57:31 +0000112 return ::android::NO_INIT;
113 }
114 if (mIsStandby) {
115 if (::android::status_t status = exitStandby(); status != ::android::OK) {
jiabinfdee3222023-03-22 22:16:13 +0000116 LOG(ERROR) << __func__ << ": failed to exit standby, status=" << status;
jiabin253bd322023-01-25 23:57:31 +0000117 return status;
118 }
119 }
120 std::vector<std::shared_ptr<alsa_device_proxy>> alsaDeviceProxies;
121 {
122 std::lock_guard guard(mLock);
123 alsaDeviceProxies = mAlsaDeviceProxies;
124 }
125 const size_t bytesToTransfer = frameCount * mFrameSizeBytes;
126 if (mIsInput) {
127 // For input case, only support single device.
128 proxy_read(alsaDeviceProxies[0].get(), buffer, bytesToTransfer);
129 } else {
130 for (auto& proxy : alsaDeviceProxies) {
131 proxy_write(proxy.get(), buffer, bytesToTransfer);
132 }
133 }
134 *actualFrameCount = frameCount;
135 *latencyMs = Module::kLatencyMs;
136 return ::android::OK;
137}
138
139::android::status_t DriverUsb::standby() {
140 if (!mIsStandby) {
141 std::lock_guard guard(mLock);
142 mAlsaDeviceProxies.clear();
143 mIsStandby = true;
144 }
145 return ::android::OK;
146}
147
148::android::status_t DriverUsb::exitStandby() {
149 std::vector<AudioDeviceAddress> connectedDevices;
150 {
151 std::lock_guard guard(mLock);
152 connectedDevices = mConnectedDevices;
153 }
154 std::vector<std::shared_ptr<alsa_device_proxy>> alsaDeviceProxies;
155 for (const auto& device : connectedDevices) {
156 alsa_device_profile profile;
jiabinfdee3222023-03-22 22:16:13 +0000157 profile_init(&profile, mIsInput ? PCM_IN : PCM_OUT);
jiabin253bd322023-01-25 23:57:31 +0000158 profile.card = device.get<AudioDeviceAddress::alsa>()[0];
159 profile.device = device.get<AudioDeviceAddress::alsa>()[1];
160 if (!profile_read_device_info(&profile)) {
161 LOG(ERROR) << __func__
162 << ": unable to read device info, device address=" << device.toString();
163 return ::android::UNKNOWN_ERROR;
164 }
165
166 auto proxy = std::shared_ptr<alsa_device_proxy>(new alsa_device_proxy(),
167 [](alsa_device_proxy* proxy) {
168 proxy_close(proxy);
169 free(proxy);
170 });
171 // Always ask for alsa configure as required since the configuration should be supported
172 // by the connected device. That is guaranteed by `setAudioPortConfig` and
173 // `setAudioPatch`.
174 if (int err =
175 proxy_prepare(proxy.get(), &profile, &mConfig.value(), true /*is_bit_perfect*/);
176 err != 0) {
177 LOG(ERROR) << __func__ << ": fail to prepare for device address=" << device.toString()
178 << " error=" << err;
179 return ::android::UNKNOWN_ERROR;
180 }
jiabinfdee3222023-03-22 22:16:13 +0000181 if (int err = proxy_open(proxy.get()); err != 0) {
182 LOG(ERROR) << __func__ << ": failed to open device, address=" << device.toString()
183 << " error=" << err;
184 return ::android::UNKNOWN_ERROR;
185 }
jiabin253bd322023-01-25 23:57:31 +0000186 alsaDeviceProxies.push_back(std::move(proxy));
187 }
188 {
189 std::lock_guard guard(mLock);
190 mAlsaDeviceProxies = alsaDeviceProxies;
191 }
192 mIsStandby = false;
193 return ::android::OK;
194}
195
196// static
197ndk::ScopedAStatus StreamInUsb::createInstance(const SinkMetadata& sinkMetadata,
198 StreamContext&& context,
199 const std::vector<MicrophoneInfo>& microphones,
200 std::shared_ptr<StreamIn>* result) {
201 std::shared_ptr<StreamIn> stream =
202 ndk::SharedRefBase::make<StreamInUsb>(sinkMetadata, std::move(context), microphones);
203 if (auto status = initInstance(stream); !status.isOk()) {
204 return status;
205 }
206 *result = std::move(stream);
207 return ndk::ScopedAStatus::ok();
208}
209
210StreamInUsb::StreamInUsb(const SinkMetadata& sinkMetadata, StreamContext&& context,
211 const std::vector<MicrophoneInfo>& microphones)
212 : StreamIn(
213 sinkMetadata, std::move(context),
214 [](const StreamContext& ctx) -> DriverInterface* {
215 return new DriverUsb(ctx, true /*isInput*/);
216 },
217 [](const StreamContext& ctx, DriverInterface* driver) -> StreamWorkerInterface* {
218 // The default worker implementation is used.
219 return new StreamInWorker(ctx, driver);
220 },
221 microphones) {}
222
223ndk::ScopedAStatus StreamInUsb::getActiveMicrophones(
224 std::vector<MicrophoneDynamicInfo>* _aidl_return __unused) {
225 LOG(DEBUG) << __func__ << ": not supported";
226 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
227}
228
229// static
230ndk::ScopedAStatus StreamOutUsb::createInstance(const SourceMetadata& sourceMetadata,
231 StreamContext&& context,
232 const std::optional<AudioOffloadInfo>& offloadInfo,
233 std::shared_ptr<StreamOut>* result) {
234 if (offloadInfo.has_value()) {
235 LOG(ERROR) << __func__ << ": offload is not supported";
236 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
237 }
238 std::shared_ptr<StreamOut> stream =
239 ndk::SharedRefBase::make<StreamOutUsb>(sourceMetadata, std::move(context), offloadInfo);
240 if (auto status = initInstance(stream); !status.isOk()) {
241 return status;
242 }
243 *result = std::move(stream);
244 return ndk::ScopedAStatus::ok();
245}
246
247StreamOutUsb::StreamOutUsb(const SourceMetadata& sourceMetadata, StreamContext&& context,
248 const std::optional<AudioOffloadInfo>& offloadInfo)
249 : StreamOut(
250 sourceMetadata, std::move(context),
251 [](const StreamContext& ctx) -> DriverInterface* {
252 return new DriverUsb(ctx, false /*isInput*/);
253 },
254 [](const StreamContext& ctx, DriverInterface* driver) -> StreamWorkerInterface* {
255 // The default worker implementation is used.
256 return new StreamOutWorker(ctx, driver);
257 },
jiabin783c48b2023-02-28 18:28:06 +0000258 offloadInfo) {
259 mChannelCount = getChannelCount(mContext.getChannelLayout());
260}
261
262ndk::ScopedAStatus StreamOutUsb::getHwVolume(std::vector<float>* _aidl_return) {
263 *_aidl_return = mHwVolumes;
264 return ndk::ScopedAStatus::ok();
265}
266
267ndk::ScopedAStatus StreamOutUsb::setHwVolume(const std::vector<float>& in_channelVolumes) {
268 for (const auto& device : mConnectedDevices) {
269 if (device.address.getTag() != AudioDeviceAddress::alsa) {
270 LOG(DEBUG) << __func__ << ": skip as the device address is not alsa";
271 continue;
272 }
273 const int card = device.address.get<AudioDeviceAddress::alsa>()[0];
274 if (auto result =
275 usb::UsbAlsaMixerControl::getInstance().setVolumes(card, in_channelVolumes);
276 !result.isOk()) {
277 LOG(ERROR) << __func__ << ": failed to set volume for device, card=" << card;
278 return result;
279 }
280 }
281 mHwVolumes = in_channelVolumes;
282 return ndk::ScopedAStatus::ok();
283}
jiabin253bd322023-01-25 23:57:31 +0000284
Mikhail Naganov6725ef52023-02-09 17:52:50 -0800285} // namespace aidl::android::hardware::audio::core