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