1 /* 2 * Copyright (C) 2022 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 #pragma once 18 19 #include <iostream> 20 #include <map> 21 #include <memory> 22 #include <set> 23 24 #include <aidl/android/hardware/audio/core/BnModule.h> 25 26 #include "core-impl/ChildInterface.h" 27 #include "core-impl/Configuration.h" 28 #include "core-impl/Stream.h" 29 30 namespace aidl::android::hardware::audio::core { 31 32 class Module : public BnModule { 33 public: 34 // This value is used for all AudioPatches and reported by all streams. 35 static constexpr int32_t kLatencyMs = 10; 36 enum Type : int { DEFAULT, R_SUBMIX, STUB, USB, BLUETOOTH }; 37 enum BtInterface : int { BTCONF, BTA2DP, BTLE }; 38 39 static std::shared_ptr<Module> createInstance(Type type); 40 Module(Type type)41 explicit Module(Type type) : mType(type) {} 42 43 typedef std::tuple<std::weak_ptr<IBluetooth>, std::weak_ptr<IBluetoothA2dp>, 44 std::weak_ptr<IBluetoothLe>> 45 BtProfileHandles; 46 47 protected: 48 // The vendor extension done via inheritance can override interface methods and augment 49 // a call to the base implementation. 50 51 ndk::ScopedAStatus setModuleDebug( 52 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) override; 53 ndk::ScopedAStatus getTelephony(std::shared_ptr<ITelephony>* _aidl_return) override; 54 ndk::ScopedAStatus getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) override; 55 ndk::ScopedAStatus getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) override; 56 ndk::ScopedAStatus getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) override; 57 ndk::ScopedAStatus connectExternalDevice( 58 const ::aidl::android::media::audio::common::AudioPort& in_templateIdAndAdditionalData, 59 ::aidl::android::media::audio::common::AudioPort* _aidl_return) override; 60 ndk::ScopedAStatus disconnectExternalDevice(int32_t in_portId) override; 61 ndk::ScopedAStatus getAudioPatches(std::vector<AudioPatch>* _aidl_return) override; 62 ndk::ScopedAStatus getAudioPort( 63 int32_t in_portId, 64 ::aidl::android::media::audio::common::AudioPort* _aidl_return) override; 65 ndk::ScopedAStatus getAudioPortConfigs( 66 std::vector<::aidl::android::media::audio::common::AudioPortConfig>* _aidl_return) 67 override; 68 ndk::ScopedAStatus getAudioPorts( 69 std::vector<::aidl::android::media::audio::common::AudioPort>* _aidl_return) override; 70 ndk::ScopedAStatus getAudioRoutes(std::vector<AudioRoute>* _aidl_return) override; 71 ndk::ScopedAStatus getAudioRoutesForAudioPort( 72 int32_t in_portId, 73 std::vector<::aidl::android::hardware::audio::core::AudioRoute>* _aidl_return) override; 74 ndk::ScopedAStatus openInputStream( 75 const ::aidl::android::hardware::audio::core::IModule::OpenInputStreamArguments& 76 in_args, 77 ::aidl::android::hardware::audio::core::IModule::OpenInputStreamReturn* _aidl_return) 78 override; 79 ndk::ScopedAStatus openOutputStream( 80 const ::aidl::android::hardware::audio::core::IModule::OpenOutputStreamArguments& 81 in_args, 82 ::aidl::android::hardware::audio::core::IModule::OpenOutputStreamReturn* _aidl_return) 83 override; 84 ndk::ScopedAStatus getSupportedPlaybackRateFactors( 85 SupportedPlaybackRateFactors* _aidl_return) override; 86 ndk::ScopedAStatus setAudioPatch(const AudioPatch& in_requested, 87 AudioPatch* _aidl_return) override; 88 ndk::ScopedAStatus setAudioPortConfig( 89 const ::aidl::android::media::audio::common::AudioPortConfig& in_requested, 90 ::aidl::android::media::audio::common::AudioPortConfig* out_suggested, 91 bool* _aidl_return) override; 92 ndk::ScopedAStatus resetAudioPatch(int32_t in_patchId) override; 93 ndk::ScopedAStatus resetAudioPortConfig(int32_t in_portConfigId) override; 94 ndk::ScopedAStatus getMasterMute(bool* _aidl_return) override; 95 ndk::ScopedAStatus setMasterMute(bool in_mute) override; 96 ndk::ScopedAStatus getMasterVolume(float* _aidl_return) override; 97 ndk::ScopedAStatus setMasterVolume(float in_volume) override; 98 ndk::ScopedAStatus getMicMute(bool* _aidl_return) override; 99 ndk::ScopedAStatus setMicMute(bool in_mute) override; 100 ndk::ScopedAStatus getMicrophones( 101 std::vector<::aidl::android::media::audio::common::MicrophoneInfo>* _aidl_return) 102 override; 103 ndk::ScopedAStatus updateAudioMode( 104 ::aidl::android::media::audio::common::AudioMode in_mode) override; 105 ndk::ScopedAStatus updateScreenRotation( 106 ::aidl::android::hardware::audio::core::IModule::ScreenRotation in_rotation) override; 107 ndk::ScopedAStatus updateScreenState(bool in_isTurnedOn) override; 108 ndk::ScopedAStatus getSoundDose(std::shared_ptr<sounddose::ISoundDose>* _aidl_return) override; 109 ndk::ScopedAStatus generateHwAvSyncId(int32_t* _aidl_return) override; 110 ndk::ScopedAStatus getVendorParameters(const std::vector<std::string>& in_ids, 111 std::vector<VendorParameter>* _aidl_return) override; 112 ndk::ScopedAStatus setVendorParameters(const std::vector<VendorParameter>& in_parameters, 113 bool in_async) override; 114 ndk::ScopedAStatus addDeviceEffect( 115 int32_t in_portConfigId, 116 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) 117 override; 118 ndk::ScopedAStatus removeDeviceEffect( 119 int32_t in_portConfigId, 120 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) 121 override; 122 ndk::ScopedAStatus getMmapPolicyInfos( 123 ::aidl::android::media::audio::common::AudioMMapPolicyType mmapPolicyType, 124 std::vector<::aidl::android::media::audio::common::AudioMMapPolicyInfo>* _aidl_return) 125 override; 126 ndk::ScopedAStatus supportsVariableLatency(bool* _aidl_return) override; 127 ndk::ScopedAStatus getAAudioMixerBurstCount(int32_t* _aidl_return) override; 128 ndk::ScopedAStatus getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) override; 129 130 // This value is used for all AudioPatches. 131 static constexpr int32_t kMinimumStreamBufferSizeFrames = 256; 132 // The maximum stream buffer size is 1 GiB = 2 ** 30 bytes; 133 static constexpr int32_t kMaximumStreamBufferSizeBytes = 1 << 30; 134 135 private: 136 struct VendorDebug { 137 static const std::string kForceTransientBurstName; 138 static const std::string kForceSynchronousDrainName; 139 bool forceTransientBurst = false; 140 bool forceSynchronousDrain = false; 141 }; 142 // ids of device ports created at runtime via 'connectExternalDevice'. 143 // Also stores a list of ids of mix ports with dynamic profiles that were populated from 144 // the connected port. This list can be empty, thus an int->int multimap can't be used. 145 using ConnectedDevicePorts = std::map<int32_t, std::vector<int32_t>>; 146 // Maps port ids and port config ids to patch ids. 147 // Multimap because both ports and configs can be used by multiple patches. 148 using Patches = std::multimap<int32_t, int32_t>; 149 150 const Type mType; 151 std::unique_ptr<internal::Configuration> mConfig; 152 ModuleDebug mDebug; 153 VendorDebug mVendorDebug; 154 ConnectedDevicePorts mConnectedDevicePorts; 155 Streams mStreams; 156 Patches mPatches; 157 bool mMicMute = false; 158 bool mMasterMute = false; 159 float mMasterVolume = 1.0f; 160 ChildInterface<sounddose::ISoundDose> mSoundDose; 161 std::optional<bool> mIsMmapSupported; 162 163 protected: 164 // The following virtual functions are intended for vendor extension via inheritance. 165 166 virtual ndk::ScopedAStatus createInputStream( 167 StreamContext&& context, 168 const ::aidl::android::hardware::audio::common::SinkMetadata& sinkMetadata, 169 const std::vector<::aidl::android::media::audio::common::MicrophoneInfo>& microphones, 170 std::shared_ptr<StreamIn>* result) = 0; 171 virtual ndk::ScopedAStatus createOutputStream( 172 StreamContext&& context, 173 const ::aidl::android::hardware::audio::common::SourceMetadata& sourceMetadata, 174 const std::optional<::aidl::android::media::audio::common::AudioOffloadInfo>& 175 offloadInfo, 176 std::shared_ptr<StreamOut>* result) = 0; 177 // If the module is unable to populate the connected device port correctly, the returned error 178 // code must correspond to the errors of `IModule.connectedExternalDevice` method. 179 virtual ndk::ScopedAStatus populateConnectedDevicePort( 180 ::aidl::android::media::audio::common::AudioPort* audioPort); 181 // If the module finds that the patch endpoints configurations are not matched, the returned 182 // error code must correspond to the errors of `IModule.setAudioPatch` method. 183 virtual ndk::ScopedAStatus checkAudioPatchEndpointsMatch( 184 const std::vector<::aidl::android::media::audio::common::AudioPortConfig*>& sources, 185 const std::vector<::aidl::android::media::audio::common::AudioPortConfig*>& sinks); 186 virtual void onExternalDeviceConnectionChanged( 187 const ::aidl::android::media::audio::common::AudioPort& audioPort, bool connected); 188 virtual ndk::ScopedAStatus onMasterMuteChanged(bool mute); 189 virtual ndk::ScopedAStatus onMasterVolumeChanged(float volume); 190 virtual std::unique_ptr<internal::Configuration> initializeConfig(); 191 192 // Utility and helper functions accessible to subclasses. 193 ndk::ScopedAStatus bluetoothParametersUpdated(); 194 void cleanUpPatch(int32_t patchId); 195 ndk::ScopedAStatus createStreamContext( 196 int32_t in_portConfigId, int64_t in_bufferSizeFrames, 197 std::shared_ptr<IStreamCallback> asyncCallback, 198 std::shared_ptr<IStreamOutEventCallback> outEventCallback, 199 ::aidl::android::hardware::audio::core::StreamContext* out_context); 200 std::vector<::aidl::android::media::audio::common::AudioDevice> findConnectedDevices( 201 int32_t portConfigId); 202 std::set<int32_t> findConnectedPortConfigIds(int32_t portConfigId); 203 ndk::ScopedAStatus findPortIdForNewStream( 204 int32_t in_portConfigId, ::aidl::android::media::audio::common::AudioPort** port); 205 virtual BtProfileHandles getBtProfileManagerHandles(); 206 internal::Configuration& getConfig(); getConnectedDevicePorts()207 const ConnectedDevicePorts& getConnectedDevicePorts() const { return mConnectedDevicePorts; } getMasterMute()208 bool getMasterMute() const { return mMasterMute; } getMasterVolume()209 bool getMasterVolume() const { return mMasterVolume; } getMicMute()210 bool getMicMute() const { return mMicMute; } getPatches()211 const Patches& getPatches() const { return mPatches; } getStreams()212 const Streams& getStreams() const { return mStreams; } getType()213 Type getType() const { return mType; } 214 bool isMmapSupported(); 215 template <typename C> 216 std::set<int32_t> portIdsFromPortConfigIds(C portConfigIds); 217 void registerPatch(const AudioPatch& patch); 218 ndk::ScopedAStatus updateStreamsConnectedState(const AudioPatch& oldPatch, 219 const AudioPatch& newPatch); 220 }; 221 222 std::ostream& operator<<(std::ostream& os, Module::Type t); 223 224 } // namespace aidl::android::hardware::audio::core 225