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