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 <map>
20 
21 #include "BluetoothAudioProvider.h"
22 #include "aidl/android/hardware/bluetooth/audio/LeAudioAseConfiguration.h"
23 #include "aidl/android/hardware/bluetooth/audio/MetadataLtv.h"
24 #include "aidl/android/hardware/bluetooth/audio/SessionType.h"
25 
26 namespace aidl {
27 namespace android {
28 namespace hardware {
29 namespace bluetooth {
30 namespace audio {
31 
32 using LeAudioAseConfigurationSetting =
33     IBluetoothAudioProvider::LeAudioAseConfigurationSetting;
34 using AseDirectionRequirement = IBluetoothAudioProvider::
35     LeAudioConfigurationRequirement::AseDirectionRequirement;
36 using AseDirectionConfiguration = IBluetoothAudioProvider::
37     LeAudioAseConfigurationSetting::AseDirectionConfiguration;
38 using AseQosDirectionRequirement = IBluetoothAudioProvider::
39     LeAudioAseQosConfigurationRequirement::AseQosDirectionRequirement;
40 using LeAudioAseQosConfiguration =
41     IBluetoothAudioProvider::LeAudioAseQosConfiguration;
42 using LeAudioBroadcastConfigurationSetting =
43     IBluetoothAudioProvider::LeAudioBroadcastConfigurationSetting;
44 
45 class LeAudioOffloadAudioProvider : public BluetoothAudioProvider {
46  public:
47   LeAudioOffloadAudioProvider();
48 
49   bool isValid(const SessionType& sessionType) override;
50 
51   ndk::ScopedAStatus startSession(
52       const std::shared_ptr<IBluetoothAudioPort>& host_if,
53       const AudioConfiguration& audio_config,
54       const std::vector<LatencyMode>& latency_modes, DataMQDesc* _aidl_return);
55   ndk::ScopedAStatus setCodecPriority(const CodecId& in_codecId,
56                                       int32_t in_priority) override;
57   ndk::ScopedAStatus getLeAudioAseConfiguration(
58       const std::optional<std::vector<
59           std::optional<IBluetoothAudioProvider::LeAudioDeviceCapabilities>>>&
60           in_remoteSinkAudioCapabilities,
61       const std::optional<std::vector<
62           std::optional<IBluetoothAudioProvider::LeAudioDeviceCapabilities>>>&
63           in_remoteSourceAudioCapabilities,
64       const std::vector<
65           IBluetoothAudioProvider::LeAudioConfigurationRequirement>&
66           in_requirements,
67       std::vector<IBluetoothAudioProvider::LeAudioAseConfigurationSetting>*
68           _aidl_return) override;
69   ndk::ScopedAStatus getLeAudioAseQosConfiguration(
70       const IBluetoothAudioProvider::LeAudioAseQosConfigurationRequirement&
71           in_qosRequirement,
72       IBluetoothAudioProvider::LeAudioAseQosConfigurationPair* _aidl_return)
73       override;
74   ndk::ScopedAStatus onSourceAseMetadataChanged(
75       IBluetoothAudioProvider::AseState in_state, int32_t in_cigId,
76       int32_t in_cisId,
77       const std::optional<std::vector<std::optional<MetadataLtv>>>& in_metadata)
78       override;
79   ndk::ScopedAStatus onSinkAseMetadataChanged(
80       IBluetoothAudioProvider::AseState in_state, int32_t in_cigId,
81       int32_t in_cisId,
82       const std::optional<std::vector<std::optional<MetadataLtv>>>& in_metadata)
83       override;
84   ndk::ScopedAStatus getLeAudioBroadcastConfiguration(
85       const std::optional<std::vector<
86           std::optional<IBluetoothAudioProvider::LeAudioDeviceCapabilities>>>&
87           in_remoteSinkAudioCapabilities,
88       const IBluetoothAudioProvider::LeAudioBroadcastConfigurationRequirement&
89           in_requirement,
90       LeAudioBroadcastConfigurationSetting* _aidl_return) override;
91 
92  private:
93   ndk::ScopedAStatus onSessionReady(DataMQDesc* _aidl_return) override;
94   std::map<CodecId, uint32_t> codec_priority_map_;
95   std::vector<LeAudioBroadcastConfigurationSetting> broadcast_settings;
96 
97   // Private matching function definitions
98   bool isMatchedValidCodec(CodecId cfg_codec, CodecId req_codec);
99   bool filterCapabilitiesMatchedContext(
100       AudioContext& setting_context,
101       const IBluetoothAudioProvider::LeAudioDeviceCapabilities& capabilities);
102   bool isMatchedSamplingFreq(
103       CodecSpecificConfigurationLtv::SamplingFrequency& cfg_freq,
104       CodecSpecificCapabilitiesLtv::SupportedSamplingFrequencies&
105           capability_freq);
106   bool isMatchedFrameDuration(
107       CodecSpecificConfigurationLtv::FrameDuration& cfg_fduration,
108       CodecSpecificCapabilitiesLtv::SupportedFrameDurations&
109           capability_fduration);
110   bool isMatchedAudioChannel(
111       CodecSpecificConfigurationLtv::AudioChannelAllocation& cfg_channel,
112       CodecSpecificCapabilitiesLtv::SupportedAudioChannelCounts&
113           capability_channel);
114   bool isMatchedCodecFramesPerSDU(
115       CodecSpecificConfigurationLtv::CodecFrameBlocksPerSDU& cfg_frame_sdu,
116       CodecSpecificCapabilitiesLtv::SupportedMaxCodecFramesPerSDU&
117           capability_frame_sdu);
118   bool isMatchedOctetsPerCodecFrame(
119       CodecSpecificConfigurationLtv::OctetsPerCodecFrame& cfg_octets,
120       CodecSpecificCapabilitiesLtv::SupportedOctetsPerCodecFrame&
121           capability_octets);
122   bool isCapabilitiesMatchedCodecConfiguration(
123       std::vector<CodecSpecificConfigurationLtv>& codec_cfg,
124       std::vector<CodecSpecificCapabilitiesLtv> codec_capabilities);
125   bool filterMatchedAseConfiguration(
126       LeAudioAseConfiguration& setting_cfg,
127       const LeAudioAseConfiguration& requirement_cfg);
128   bool isMatchedBISConfiguration(
129       LeAudioBisConfiguration bis_cfg,
130       const IBluetoothAudioProvider::LeAudioDeviceCapabilities& capabilities);
131   void filterCapabilitiesAseDirectionConfiguration(
132       std::vector<std::optional<AseDirectionConfiguration>>&
133           direction_configurations,
134       const IBluetoothAudioProvider::LeAudioDeviceCapabilities& capabilities,
135       std::vector<std::optional<AseDirectionConfiguration>>&
136           valid_direction_configurations);
137   void filterRequirementAseDirectionConfiguration(
138       std::optional<std::vector<std::optional<AseDirectionConfiguration>>>&
139           direction_configurations,
140       const std::vector<std::optional<AseDirectionRequirement>>& requirements,
141       std::optional<std::vector<std::optional<AseDirectionConfiguration>>>&
142           valid_direction_configurations,
143       bool is_exact);
144   std::optional<LeAudioAseConfigurationSetting>
145   getCapabilitiesMatchedAseConfigurationSettings(
146       IBluetoothAudioProvider::LeAudioAseConfigurationSetting& setting,
147       const IBluetoothAudioProvider::LeAudioDeviceCapabilities& capabilities,
148       uint8_t direction);
149   std::optional<LeAudioAseConfigurationSetting>
150   getRequirementMatchedAseConfigurationSettings(
151       IBluetoothAudioProvider::LeAudioAseConfigurationSetting& setting,
152       const IBluetoothAudioProvider::LeAudioConfigurationRequirement&
153           requirement,
154       bool is_exact);
155   bool isMatchedQosRequirement(LeAudioAseQosConfiguration setting_qos,
156                                AseQosDirectionRequirement requirement_qos);
157   std::optional<LeAudioBroadcastConfigurationSetting>
158   getCapabilitiesMatchedBroadcastConfigurationSettings(
159       LeAudioBroadcastConfigurationSetting& setting,
160       const IBluetoothAudioProvider::LeAudioDeviceCapabilities& capabilities);
161   void getBroadcastSettings();
162   std::optional<LeAudioAseQosConfiguration> getDirectionQosConfiguration(
163       uint8_t direction,
164       const IBluetoothAudioProvider::LeAudioAseQosConfigurationRequirement&
165           qosRequirement,
166       std::vector<LeAudioAseConfigurationSetting>& ase_configuration_settings,
167       bool is_exact);
168   bool isSubgroupConfigurationMatchedContext(
169       AudioContext requirement_context,
170       IBluetoothAudioProvider::BroadcastQuality quality,
171       LeAudioBroadcastSubgroupConfiguration configuration);
172   std::vector<IBluetoothAudioProvider::LeAudioAseConfigurationSetting>
173   matchWithRequirement(
174       std::vector<IBluetoothAudioProvider::LeAudioAseConfigurationSetting>&
175           matched_ase_configuration_settings,
176       const std::vector<
177           IBluetoothAudioProvider::LeAudioConfigurationRequirement>&
178           in_requirements,
179       bool is_exact);
180 };
181 
182 class LeAudioOffloadOutputAudioProvider : public LeAudioOffloadAudioProvider {
183  public:
184   LeAudioOffloadOutputAudioProvider();
185 };
186 
187 class LeAudioOffloadInputAudioProvider : public LeAudioOffloadAudioProvider {
188  public:
189   LeAudioOffloadInputAudioProvider();
190 };
191 
192 class LeAudioOffloadBroadcastAudioProvider
193     : public LeAudioOffloadAudioProvider {
194  public:
195   LeAudioOffloadBroadcastAudioProvider();
196 };
197 
198 }  // namespace audio
199 }  // namespace bluetooth
200 }  // namespace hardware
201 }  // namespace android
202 }  // namespace aidl
203