blob: d6f757c7a9bf169900eecca4e05dc9267791dbcc [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
31using aidl::android::hardware::audio::common::SinkMetadata;
32using aidl::android::hardware::audio::common::SourceMetadata;
33using aidl::android::media::audio::common::AudioDevice;
34using aidl::android::media::audio::common::AudioDeviceAddress;
35using aidl::android::media::audio::common::AudioOffloadInfo;
jiabin783c48b2023-02-28 18:28:06 +000036using aidl::android::media::audio::common::AudioPortExt;
Mikhail Naganov6725ef52023-02-09 17:52:50 -080037using aidl::android::media::audio::common::MicrophoneDynamicInfo;
38using aidl::android::media::audio::common::MicrophoneInfo;
jiabin783c48b2023-02-28 18:28:06 +000039using android::OK;
40using android::status_t;
41using android::hardware::audio::common::getChannelCount;
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()) {
110 return ::android::NO_INIT;
111 }
112 if (mIsStandby) {
113 if (::android::status_t status = exitStandby(); status != ::android::OK) {
114 return status;
115 }
116 }
117 std::vector<std::shared_ptr<alsa_device_proxy>> alsaDeviceProxies;
118 {
119 std::lock_guard guard(mLock);
120 alsaDeviceProxies = mAlsaDeviceProxies;
121 }
122 const size_t bytesToTransfer = frameCount * mFrameSizeBytes;
123 if (mIsInput) {
124 // For input case, only support single device.
125 proxy_read(alsaDeviceProxies[0].get(), buffer, bytesToTransfer);
126 } else {
127 for (auto& proxy : alsaDeviceProxies) {
128 proxy_write(proxy.get(), buffer, bytesToTransfer);
129 }
130 }
131 *actualFrameCount = frameCount;
132 *latencyMs = Module::kLatencyMs;
133 return ::android::OK;
134}
135
136::android::status_t DriverUsb::standby() {
137 if (!mIsStandby) {
138 std::lock_guard guard(mLock);
139 mAlsaDeviceProxies.clear();
140 mIsStandby = true;
141 }
142 return ::android::OK;
143}
144
145::android::status_t DriverUsb::exitStandby() {
146 std::vector<AudioDeviceAddress> connectedDevices;
147 {
148 std::lock_guard guard(mLock);
149 connectedDevices = mConnectedDevices;
150 }
151 std::vector<std::shared_ptr<alsa_device_proxy>> alsaDeviceProxies;
152 for (const auto& device : connectedDevices) {
153 alsa_device_profile profile;
154 profile.card = device.get<AudioDeviceAddress::alsa>()[0];
155 profile.device = device.get<AudioDeviceAddress::alsa>()[1];
156 if (!profile_read_device_info(&profile)) {
157 LOG(ERROR) << __func__
158 << ": unable to read device info, device address=" << device.toString();
159 return ::android::UNKNOWN_ERROR;
160 }
161
162 auto proxy = std::shared_ptr<alsa_device_proxy>(new alsa_device_proxy(),
163 [](alsa_device_proxy* proxy) {
164 proxy_close(proxy);
165 free(proxy);
166 });
167 // Always ask for alsa configure as required since the configuration should be supported
168 // by the connected device. That is guaranteed by `setAudioPortConfig` and
169 // `setAudioPatch`.
170 if (int err =
171 proxy_prepare(proxy.get(), &profile, &mConfig.value(), true /*is_bit_perfect*/);
172 err != 0) {
173 LOG(ERROR) << __func__ << ": fail to prepare for device address=" << device.toString()
174 << " error=" << err;
175 return ::android::UNKNOWN_ERROR;
176 }
177 alsaDeviceProxies.push_back(std::move(proxy));
178 }
179 {
180 std::lock_guard guard(mLock);
181 mAlsaDeviceProxies = alsaDeviceProxies;
182 }
183 mIsStandby = false;
184 return ::android::OK;
185}
186
187// static
188ndk::ScopedAStatus StreamInUsb::createInstance(const SinkMetadata& sinkMetadata,
189 StreamContext&& context,
190 const std::vector<MicrophoneInfo>& microphones,
191 std::shared_ptr<StreamIn>* result) {
192 std::shared_ptr<StreamIn> stream =
193 ndk::SharedRefBase::make<StreamInUsb>(sinkMetadata, std::move(context), microphones);
194 if (auto status = initInstance(stream); !status.isOk()) {
195 return status;
196 }
197 *result = std::move(stream);
198 return ndk::ScopedAStatus::ok();
199}
200
201StreamInUsb::StreamInUsb(const SinkMetadata& sinkMetadata, StreamContext&& context,
202 const std::vector<MicrophoneInfo>& microphones)
203 : StreamIn(
204 sinkMetadata, std::move(context),
205 [](const StreamContext& ctx) -> DriverInterface* {
206 return new DriverUsb(ctx, true /*isInput*/);
207 },
208 [](const StreamContext& ctx, DriverInterface* driver) -> StreamWorkerInterface* {
209 // The default worker implementation is used.
210 return new StreamInWorker(ctx, driver);
211 },
212 microphones) {}
213
214ndk::ScopedAStatus StreamInUsb::getActiveMicrophones(
215 std::vector<MicrophoneDynamicInfo>* _aidl_return __unused) {
216 LOG(DEBUG) << __func__ << ": not supported";
217 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
218}
219
220// static
221ndk::ScopedAStatus StreamOutUsb::createInstance(const SourceMetadata& sourceMetadata,
222 StreamContext&& context,
223 const std::optional<AudioOffloadInfo>& offloadInfo,
224 std::shared_ptr<StreamOut>* result) {
225 if (offloadInfo.has_value()) {
226 LOG(ERROR) << __func__ << ": offload is not supported";
227 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
228 }
229 std::shared_ptr<StreamOut> stream =
230 ndk::SharedRefBase::make<StreamOutUsb>(sourceMetadata, std::move(context), offloadInfo);
231 if (auto status = initInstance(stream); !status.isOk()) {
232 return status;
233 }
234 *result = std::move(stream);
235 return ndk::ScopedAStatus::ok();
236}
237
238StreamOutUsb::StreamOutUsb(const SourceMetadata& sourceMetadata, StreamContext&& context,
239 const std::optional<AudioOffloadInfo>& offloadInfo)
240 : StreamOut(
241 sourceMetadata, std::move(context),
242 [](const StreamContext& ctx) -> DriverInterface* {
243 return new DriverUsb(ctx, false /*isInput*/);
244 },
245 [](const StreamContext& ctx, DriverInterface* driver) -> StreamWorkerInterface* {
246 // The default worker implementation is used.
247 return new StreamOutWorker(ctx, driver);
248 },
jiabin783c48b2023-02-28 18:28:06 +0000249 offloadInfo) {
250 mChannelCount = getChannelCount(mContext.getChannelLayout());
251}
252
253ndk::ScopedAStatus StreamOutUsb::getHwVolume(std::vector<float>* _aidl_return) {
254 *_aidl_return = mHwVolumes;
255 return ndk::ScopedAStatus::ok();
256}
257
258ndk::ScopedAStatus StreamOutUsb::setHwVolume(const std::vector<float>& in_channelVolumes) {
259 for (const auto& device : mConnectedDevices) {
260 if (device.address.getTag() != AudioDeviceAddress::alsa) {
261 LOG(DEBUG) << __func__ << ": skip as the device address is not alsa";
262 continue;
263 }
264 const int card = device.address.get<AudioDeviceAddress::alsa>()[0];
265 if (auto result =
266 usb::UsbAlsaMixerControl::getInstance().setVolumes(card, in_channelVolumes);
267 !result.isOk()) {
268 LOG(ERROR) << __func__ << ": failed to set volume for device, card=" << card;
269 return result;
270 }
271 }
272 mHwVolumes = in_channelVolumes;
273 return ndk::ScopedAStatus::ok();
274}
jiabin253bd322023-01-25 23:57:31 +0000275
Mikhail Naganov6725ef52023-02-09 17:52:50 -0800276} // namespace aidl::android::hardware::audio::core