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