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