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