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> |
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 "core-impl/StreamUsb.h" |
| 25 | |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 26 | using aidl::android::hardware::audio::common::SinkMetadata; |
| 27 | using aidl::android::hardware::audio::common::SourceMetadata; |
| 28 | using aidl::android::media::audio::common::AudioDevice; |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 29 | using aidl::android::media::audio::common::AudioOffloadInfo; |
Mikhail Naganov | 6725ef5 | 2023-02-09 17:52:50 -0800 | [diff] [blame] | 30 | using aidl::android::media::audio::common::MicrophoneDynamicInfo; |
| 31 | using aidl::android::media::audio::common::MicrophoneInfo; |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 32 | |
| 33 | namespace aidl::android::hardware::audio::core { |
| 34 | |
Mikhail Naganov | 1eedc13 | 2023-07-21 17:45:28 -0700 | [diff] [blame] | 35 | StreamUsb::StreamUsb(StreamContext* context, const Metadata& metadata) |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 36 | : StreamAlsa(context, metadata, 1 /*readWriteRetries*/) {} |
Mikhail Naganov | d5554cf | 2023-06-21 15:20:31 -0700 | [diff] [blame] | 37 | |
| 38 | ndk::ScopedAStatus StreamUsb::setConnectedDevices( |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 39 | const std::vector<AudioDevice>& connectedDevices) { |
| 40 | if (mIsInput && connectedDevices.size() > 1) { |
| 41 | LOG(ERROR) << __func__ << ": wrong device size(" << connectedDevices.size() |
| 42 | << ") for input stream"; |
Mikhail Naganov | d5554cf | 2023-06-21 15:20:31 -0700 | [diff] [blame] | 43 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 44 | } |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 45 | std::vector<alsa::DeviceProfile> connectedDeviceProfiles; |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 46 | for (const auto& connectedDevice : connectedDevices) { |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 47 | auto profile = alsa::getDeviceProfile(connectedDevice, mIsInput); |
| 48 | if (!profile.has_value()) { |
| 49 | LOG(ERROR) << __func__ |
| 50 | << ": unsupported device address=" << connectedDevice.address.toString(); |
Mikhail Naganov | d5554cf | 2023-06-21 15:20:31 -0700 | [diff] [blame] | 51 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 52 | } |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 53 | connectedDeviceProfiles.push_back(*profile); |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 54 | } |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 55 | RETURN_STATUS_IF_ERROR(setConnectedDevices(connectedDevices)); |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 56 | std::lock_guard guard(mLock); |
| 57 | mConnectedDeviceProfiles = std::move(connectedDeviceProfiles); |
Mikhail Naganov | 49712b5 | 2023-06-27 16:39:33 -0700 | [diff] [blame] | 58 | mConnectedDevicesUpdated.store(true, std::memory_order_release); |
Mikhail Naganov | d5554cf | 2023-06-21 15:20:31 -0700 | [diff] [blame] | 59 | return ndk::ScopedAStatus::ok(); |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Mikhail Naganov | d5554cf | 2023-06-21 15:20:31 -0700 | [diff] [blame] | 62 | ::android::status_t StreamUsb::transfer(void* buffer, size_t frameCount, size_t* actualFrameCount, |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 63 | int32_t* latencyMs) { |
Mikhail Naganov | 49712b5 | 2023-06-27 16:39:33 -0700 | [diff] [blame] | 64 | if (mConnectedDevicesUpdated.load(std::memory_order_acquire)) { |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 65 | // 'setConnectedDevices' was called. I/O will be restarted. |
Mikhail Naganov | 49712b5 | 2023-06-27 16:39:33 -0700 | [diff] [blame] | 66 | *actualFrameCount = 0; |
| 67 | *latencyMs = StreamDescriptor::LATENCY_UNKNOWN; |
| 68 | return ::android::OK; |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 69 | } |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 70 | return StreamAlsa::transfer(buffer, frameCount, actualFrameCount, latencyMs); |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 73 | std::vector<alsa::DeviceProfile> StreamUsb::getDeviceProfiles() { |
| 74 | std::vector<alsa::DeviceProfile> connectedDevices; |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 75 | { |
| 76 | std::lock_guard guard(mLock); |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 77 | connectedDevices = mConnectedDeviceProfiles; |
Mikhail Naganov | 49712b5 | 2023-06-27 16:39:33 -0700 | [diff] [blame] | 78 | mConnectedDevicesUpdated.store(false, std::memory_order_release); |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 79 | } |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 80 | return connectedDevices; |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 83 | StreamInUsb::StreamInUsb(StreamContext&& context, const SinkMetadata& sinkMetadata, |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 84 | const std::vector<MicrophoneInfo>& microphones) |
Mikhail Naganov | 459b733 | 2023-08-03 10:26:21 -0700 | [diff] [blame] | 85 | : StreamIn(std::move(context), microphones), StreamUsb(&mContextInstance, sinkMetadata) {} |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 86 | |
| 87 | ndk::ScopedAStatus StreamInUsb::getActiveMicrophones( |
| 88 | std::vector<MicrophoneDynamicInfo>* _aidl_return __unused) { |
| 89 | LOG(DEBUG) << __func__ << ": not supported"; |
| 90 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 91 | } |
| 92 | |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 93 | StreamOutUsb::StreamOutUsb(StreamContext&& context, const SourceMetadata& sourceMetadata, |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 94 | const std::optional<AudioOffloadInfo>& offloadInfo) |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 95 | : StreamOut(std::move(context), offloadInfo), |
Mikhail Naganov | 459b733 | 2023-08-03 10:26:21 -0700 | [diff] [blame] | 96 | StreamUsb(&mContextInstance, sourceMetadata), |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame] | 97 | StreamOutHwVolumeHelper(&mContextInstance) {} |
jiabin | 783c48b | 2023-02-28 18:28:06 +0000 | [diff] [blame] | 98 | |
| 99 | ndk::ScopedAStatus StreamOutUsb::getHwVolume(std::vector<float>* _aidl_return) { |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame] | 100 | return getHwVolumeImpl(_aidl_return); |
jiabin | 783c48b | 2023-02-28 18:28:06 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | ndk::ScopedAStatus StreamOutUsb::setHwVolume(const std::vector<float>& in_channelVolumes) { |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame] | 104 | auto currentVolumes = mHwVolumes; |
| 105 | RETURN_STATUS_IF_ERROR(setHwVolumeImpl(in_channelVolumes)); |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 106 | // Avoid using mConnectedDeviceProfiles because it requires a lock. |
Mikhail Naganov | d5554cf | 2023-06-21 15:20:31 -0700 | [diff] [blame] | 107 | for (const auto& device : getConnectedDevices()) { |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 108 | if (auto deviceProfile = alsa::getDeviceProfile(device, mIsInput); |
| 109 | deviceProfile.has_value()) { |
| 110 | if (auto result = usb::UsbAlsaMixerControl::getInstance().setVolumes( |
| 111 | deviceProfile->card, in_channelVolumes); |
| 112 | !result.isOk()) { |
| 113 | LOG(ERROR) << __func__ |
| 114 | << ": failed to set volume for device address=" << *deviceProfile; |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame] | 115 | mHwVolumes = currentVolumes; |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 116 | return result; |
| 117 | } |
jiabin | 783c48b | 2023-02-28 18:28:06 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
jiabin | 783c48b | 2023-02-28 18:28:06 +0000 | [diff] [blame] | 120 | return ndk::ScopedAStatus::ok(); |
| 121 | } |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 122 | |
Mikhail Naganov | 6725ef5 | 2023-02-09 17:52:50 -0800 | [diff] [blame] | 123 | } // namespace aidl::android::hardware::audio::core |