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 #pragma once 18 19 #include <map> 20 21 #include "core-impl/Bluetooth.h" 22 #include "core-impl/DevicePortProxy.h" 23 #include "core-impl/Module.h" 24 25 namespace aidl::android::hardware::audio::core { 26 27 class ModuleBluetooth final : public Module { 28 public: 29 enum BtInterface : int { BTSCO, BTA2DP, BTLE }; 30 typedef std::tuple<std::weak_ptr<IBluetooth>, std::weak_ptr<IBluetoothA2dp>, 31 std::weak_ptr<IBluetoothLe>> 32 BtProfileHandles; 33 34 ModuleBluetooth(std::unique_ptr<Configuration>&& config); 35 36 private: 37 struct CachedProxy { 38 std::shared_ptr<::android::bluetooth::audio::aidl::BluetoothAudioPortAidl> ptr; 39 ::aidl::android::hardware::bluetooth::audio::PcmConfiguration pcmConfig; 40 }; 41 42 ChildInterface<BluetoothA2dp>& getBtA2dp(); 43 ChildInterface<BluetoothLe>& getBtLe(); 44 BtProfileHandles getBtProfileManagerHandles(); 45 46 ndk::ScopedAStatus getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) override; 47 ndk::ScopedAStatus getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) override; 48 ndk::ScopedAStatus getMicMute(bool* _aidl_return) override; 49 ndk::ScopedAStatus setMicMute(bool in_mute) override; 50 51 ndk::ScopedAStatus setAudioPortConfig( 52 const ::aidl::android::media::audio::common::AudioPortConfig& in_requested, 53 ::aidl::android::media::audio::common::AudioPortConfig* out_suggested, 54 bool* _aidl_return) override; 55 56 ndk::ScopedAStatus checkAudioPatchEndpointsMatch( 57 const std::vector<::aidl::android::media::audio::common::AudioPortConfig*>& sources, 58 const std::vector<::aidl::android::media::audio::common::AudioPortConfig*>& sinks) 59 override; 60 void onExternalDeviceConnectionChanged( 61 const ::aidl::android::media::audio::common::AudioPort& audioPort, bool connected); 62 ndk::ScopedAStatus createInputStream( 63 StreamContext&& context, 64 const ::aidl::android::hardware::audio::common::SinkMetadata& sinkMetadata, 65 const std::vector<::aidl::android::media::audio::common::MicrophoneInfo>& microphones, 66 std::shared_ptr<StreamIn>* result) override; 67 ndk::ScopedAStatus createOutputStream( 68 StreamContext&& context, 69 const ::aidl::android::hardware::audio::common::SourceMetadata& sourceMetadata, 70 const std::optional<::aidl::android::media::audio::common::AudioOffloadInfo>& 71 offloadInfo, 72 std::shared_ptr<StreamOut>* result) override; 73 ndk::ScopedAStatus populateConnectedDevicePort( 74 ::aidl::android::media::audio::common::AudioPort* audioPort, 75 int32_t nextPortId) override; 76 ndk::ScopedAStatus onMasterMuteChanged(bool mute) override; 77 ndk::ScopedAStatus onMasterVolumeChanged(float volume) override; 78 int32_t getNominalLatencyMs( 79 const ::aidl::android::media::audio::common::AudioPortConfig& portConfig) override; 80 81 ndk::ScopedAStatus createProxy( 82 const ::aidl::android::media::audio::common::AudioPort& audioPort, 83 int32_t instancePortId, CachedProxy& proxy); 84 ndk::ScopedAStatus fetchAndCheckProxy(const StreamContext& context, CachedProxy& proxy); 85 ndk::ScopedAStatus findOrCreateProxy( 86 const ::aidl::android::media::audio::common::AudioPort& audioPort, CachedProxy& proxy); 87 88 static constexpr int kCreateProxyRetries = 5; 89 static constexpr int kCreateProxyRetrySleepMs = 75; 90 ChildInterface<BluetoothA2dp> mBluetoothA2dp; 91 ChildInterface<BluetoothLe> mBluetoothLe; 92 std::map<int32_t /*instantiated device port ID*/, CachedProxy> mProxies; 93 std::map<int32_t /*mix port handle*/, int32_t /*instantiated device port ID*/> mConnections; 94 }; 95 96 } // namespace aidl::android::hardware::audio::core 97