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 #include <limits>
18
19 #define LOG_TAG "AHAL_StreamUsb"
20 #include <android-base/logging.h>
21 #include <error/expected_utils.h>
22
23 #include "UsbAlsaMixerControl.h"
24 #include "core-impl/StreamUsb.h"
25
26 using aidl::android::hardware::audio::common::SinkMetadata;
27 using aidl::android::hardware::audio::common::SourceMetadata;
28 using aidl::android::media::audio::common::AudioDevice;
29 using aidl::android::media::audio::common::AudioOffloadInfo;
30 using aidl::android::media::audio::common::MicrophoneDynamicInfo;
31 using aidl::android::media::audio::common::MicrophoneInfo;
32
33 namespace aidl::android::hardware::audio::core {
34
StreamUsb(StreamContext * context,const Metadata & metadata)35 StreamUsb::StreamUsb(StreamContext* context, const Metadata& metadata)
36 : StreamAlsa(context, metadata, 1 /*readWriteRetries*/) {}
37
setConnectedDevices(const std::vector<AudioDevice> & connectedDevices)38 ndk::ScopedAStatus StreamUsb::setConnectedDevices(
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";
43 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
44 }
45 std::vector<alsa::DeviceProfile> connectedDeviceProfiles;
46 for (const auto& connectedDevice : connectedDevices) {
47 auto profile = alsa::getDeviceProfile(connectedDevice, mIsInput);
48 if (!profile.has_value()) {
49 LOG(ERROR) << __func__
50 << ": unsupported device address=" << connectedDevice.address.toString();
51 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
52 }
53 connectedDeviceProfiles.push_back(*profile);
54 }
55 RETURN_STATUS_IF_ERROR(StreamCommonImpl::setConnectedDevices(connectedDevices));
56 std::lock_guard guard(mLock);
57 mConnectedDeviceProfiles = std::move(connectedDeviceProfiles);
58 mConnectedDevicesUpdated.store(true, std::memory_order_release);
59 return ndk::ScopedAStatus::ok();
60 }
61
transfer(void * buffer,size_t frameCount,size_t * actualFrameCount,int32_t * latencyMs)62 ::android::status_t StreamUsb::transfer(void* buffer, size_t frameCount, size_t* actualFrameCount,
63 int32_t* latencyMs) {
64 if (mConnectedDevicesUpdated.load(std::memory_order_acquire)) {
65 // 'setConnectedDevices' was called. I/O will be restarted.
66 *actualFrameCount = 0;
67 *latencyMs = StreamDescriptor::LATENCY_UNKNOWN;
68 return ::android::OK;
69 }
70 return StreamAlsa::transfer(buffer, frameCount, actualFrameCount, latencyMs);
71 }
72
getDeviceProfiles()73 std::vector<alsa::DeviceProfile> StreamUsb::getDeviceProfiles() {
74 std::vector<alsa::DeviceProfile> connectedDevices;
75 {
76 std::lock_guard guard(mLock);
77 connectedDevices = mConnectedDeviceProfiles;
78 mConnectedDevicesUpdated.store(false, std::memory_order_release);
79 }
80 return connectedDevices;
81 }
82
StreamInUsb(StreamContext && context,const SinkMetadata & sinkMetadata,const std::vector<MicrophoneInfo> & microphones)83 StreamInUsb::StreamInUsb(StreamContext&& context, const SinkMetadata& sinkMetadata,
84 const std::vector<MicrophoneInfo>& microphones)
85 : StreamIn(std::move(context), microphones), StreamUsb(&mContextInstance, sinkMetadata) {}
86
getActiveMicrophones(std::vector<MicrophoneDynamicInfo> * _aidl_return __unused)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
StreamOutUsb(StreamContext && context,const SourceMetadata & sourceMetadata,const std::optional<AudioOffloadInfo> & offloadInfo)93 StreamOutUsb::StreamOutUsb(StreamContext&& context, const SourceMetadata& sourceMetadata,
94 const std::optional<AudioOffloadInfo>& offloadInfo)
95 : StreamOut(std::move(context), offloadInfo),
96 StreamUsb(&mContextInstance, sourceMetadata),
97 StreamOutHwVolumeHelper(&mContextInstance) {}
98
getHwVolume(std::vector<float> * _aidl_return)99 ndk::ScopedAStatus StreamOutUsb::getHwVolume(std::vector<float>* _aidl_return) {
100 return getHwVolumeImpl(_aidl_return);
101 }
102
setHwVolume(const std::vector<float> & in_channelVolumes)103 ndk::ScopedAStatus StreamOutUsb::setHwVolume(const std::vector<float>& in_channelVolumes) {
104 auto currentVolumes = mHwVolumes;
105 RETURN_STATUS_IF_ERROR(setHwVolumeImpl(in_channelVolumes));
106 // Avoid using mConnectedDeviceProfiles because it requires a lock.
107 for (const auto& device : getConnectedDevices()) {
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;
115 mHwVolumes = currentVolumes;
116 return result;
117 }
118 }
119 }
120 return ndk::ScopedAStatus::ok();
121 }
122
123 } // namespace aidl::android::hardware::audio::core
124