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 <vector>
18
19 #define LOG_TAG "AHAL_ModuleUsb"
20 #include <Utils.h>
21 #include <android-base/logging.h>
22
23 #include "UsbAlsaMixerControl.h"
24 #include "alsa/Utils.h"
25 #include "core-impl/ModuleUsb.h"
26 #include "core-impl/StreamUsb.h"
27
28 using aidl::android::hardware::audio::common::SinkMetadata;
29 using aidl::android::hardware::audio::common::SourceMetadata;
30 using aidl::android::media::audio::common::AudioDeviceDescription;
31 using aidl::android::media::audio::common::AudioOffloadInfo;
32 using aidl::android::media::audio::common::AudioPort;
33 using aidl::android::media::audio::common::AudioPortConfig;
34 using aidl::android::media::audio::common::AudioPortExt;
35 using aidl::android::media::audio::common::MicrophoneInfo;
36
37 namespace aidl::android::hardware::audio::core {
38
39 namespace {
40
isUsbDevicePort(const AudioPort & audioPort)41 bool isUsbDevicePort(const AudioPort& audioPort) {
42 return audioPort.ext.getTag() == AudioPortExt::Tag::device &&
43 audioPort.ext.get<AudioPortExt::Tag::device>().device.type.connection ==
44 AudioDeviceDescription::CONNECTION_USB;
45 }
46
47 } // namespace
48
getTelephony(std::shared_ptr<ITelephony> * _aidl_return)49 ndk::ScopedAStatus ModuleUsb::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
50 *_aidl_return = nullptr;
51 LOG(DEBUG) << __func__ << ": returning null";
52 return ndk::ScopedAStatus::ok();
53 }
54
getBluetooth(std::shared_ptr<IBluetooth> * _aidl_return)55 ndk::ScopedAStatus ModuleUsb::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
56 *_aidl_return = nullptr;
57 LOG(DEBUG) << __func__ << ": returning null";
58 return ndk::ScopedAStatus::ok();
59 }
60
getMicMute(bool * _aidl_return __unused)61 ndk::ScopedAStatus ModuleUsb::getMicMute(bool* _aidl_return __unused) {
62 LOG(DEBUG) << __func__ << ": is not supported";
63 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
64 }
65
setMicMute(bool in_mute __unused)66 ndk::ScopedAStatus ModuleUsb::setMicMute(bool in_mute __unused) {
67 LOG(DEBUG) << __func__ << ": is not supported";
68 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
69 }
70
createInputStream(StreamContext && context,const SinkMetadata & sinkMetadata,const std::vector<MicrophoneInfo> & microphones,std::shared_ptr<StreamIn> * result)71 ndk::ScopedAStatus ModuleUsb::createInputStream(StreamContext&& context,
72 const SinkMetadata& sinkMetadata,
73 const std::vector<MicrophoneInfo>& microphones,
74 std::shared_ptr<StreamIn>* result) {
75 return createStreamInstance<StreamInUsb>(result, std::move(context), sinkMetadata, microphones);
76 }
77
createOutputStream(StreamContext && context,const SourceMetadata & sourceMetadata,const std::optional<AudioOffloadInfo> & offloadInfo,std::shared_ptr<StreamOut> * result)78 ndk::ScopedAStatus ModuleUsb::createOutputStream(StreamContext&& context,
79 const SourceMetadata& sourceMetadata,
80 const std::optional<AudioOffloadInfo>& offloadInfo,
81 std::shared_ptr<StreamOut>* result) {
82 if (offloadInfo.has_value()) {
83 LOG(ERROR) << __func__ << ": offload is not supported";
84 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
85 }
86 return createStreamInstance<StreamOutUsb>(result, std::move(context), sourceMetadata,
87 offloadInfo);
88 }
89
populateConnectedDevicePort(AudioPort * audioPort,int32_t nextPortId)90 ndk::ScopedAStatus ModuleUsb::populateConnectedDevicePort(AudioPort* audioPort,
91 int32_t nextPortId) {
92 if (!isUsbDevicePort(*audioPort)) {
93 LOG(ERROR) << __func__ << ": port id " << audioPort->id << " is not a usb device port";
94 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
95 }
96 return ModuleAlsa::populateConnectedDevicePort(audioPort, nextPortId);
97 }
98
checkAudioPatchEndpointsMatch(const std::vector<AudioPortConfig * > & sources,const std::vector<AudioPortConfig * > & sinks)99 ndk::ScopedAStatus ModuleUsb::checkAudioPatchEndpointsMatch(
100 const std::vector<AudioPortConfig*>& sources, const std::vector<AudioPortConfig*>& sinks) {
101 for (const auto& source : sources) {
102 for (const auto& sink : sinks) {
103 if (source->sampleRate != sink->sampleRate ||
104 source->channelMask != sink->channelMask || source->format != sink->format) {
105 LOG(ERROR) << __func__
106 << ": mismatch port configuration, source=" << source->toString()
107 << ", sink=" << sink->toString();
108 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
109 }
110 }
111 }
112 return ndk::ScopedAStatus::ok();
113 }
114
onExternalDeviceConnectionChanged(const::aidl::android::media::audio::common::AudioPort & audioPort,bool connected)115 void ModuleUsb::onExternalDeviceConnectionChanged(
116 const ::aidl::android::media::audio::common::AudioPort& audioPort, bool connected) {
117 if (!isUsbDevicePort(audioPort)) {
118 return;
119 }
120 auto profile = alsa::getDeviceProfile(audioPort);
121 if (!profile.has_value()) {
122 return;
123 }
124 usb::UsbAlsaMixerControl::getInstance().setDeviceConnectionState(profile->card, getMasterMute(),
125 getMasterVolume(), connected);
126 }
127
onMasterMuteChanged(bool mute)128 ndk::ScopedAStatus ModuleUsb::onMasterMuteChanged(bool mute) {
129 return usb::UsbAlsaMixerControl::getInstance().setMasterMute(mute);
130 }
131
onMasterVolumeChanged(float volume)132 ndk::ScopedAStatus ModuleUsb::onMasterVolumeChanged(float volume) {
133 return usb::UsbAlsaMixerControl::getInstance().setMasterVolume(volume);
134 }
135
136 } // namespace aidl::android::hardware::audio::core
137