1 /*
2  * Copyright 2018 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 <android/hardware/bluetooth/audio/2.1/types.h>
20 #include "BluetoothAudioSession.h"
21 
22 #include <mutex>
23 #include <unordered_map>
24 
25 namespace android {
26 namespace bluetooth {
27 namespace audio {
28 
29 class BluetoothAudioSession_2_1 {
30  private:
31   std::shared_ptr<BluetoothAudioSession> audio_session;
32 
33   ::android::hardware::bluetooth::audio::V2_1::SessionType session_type_2_1_;
34 
35   // audio data configuration for both software and offloading
36   ::android::hardware::bluetooth::audio::V2_1::AudioConfiguration
37       audio_config_2_1_;
38 
39   bool UpdateAudioConfig(
40       const ::android::hardware::bluetooth::audio::V2_1::AudioConfiguration&
41           audio_config);
42 
43   static ::android::hardware::bluetooth::audio::V2_1::AudioConfiguration
44       invalidSoftwareAudioConfiguration;
45   static ::android::hardware::bluetooth::audio::V2_1::AudioConfiguration
46       invalidOffloadAudioConfiguration;
47 
48  public:
49   BluetoothAudioSession_2_1(
50       const ::android::hardware::bluetooth::audio::V2_1::SessionType&
51           session_type);
52 
53   std::shared_ptr<BluetoothAudioSession> GetAudioSession();
54 
55   // The report function is used to report that the Bluetooth stack has started
56   // this session without any failure, and will invoke session_changed_cb_ to
57   // notify those registered bluetooth_audio outputs
58   void OnSessionStarted(
59       const sp<IBluetoothAudioPort> stack_iface,
60       const DataMQ::Descriptor* dataMQ,
61       const ::android::hardware::bluetooth::audio::V2_1::AudioConfiguration&
62           audio_config);
63 
64   // The control function is for the bluetooth_audio module to get the current
65   // AudioConfiguration
66   const ::android::hardware::bluetooth::audio::V2_1::AudioConfiguration
67   GetAudioConfig();
68 
69   static constexpr ::android::hardware::bluetooth::audio::V2_1::
70       AudioConfiguration& kInvalidSoftwareAudioConfiguration =
71           invalidSoftwareAudioConfiguration;
72   static constexpr ::android::hardware::bluetooth::audio::V2_1::
73       AudioConfiguration& kInvalidOffloadAudioConfiguration =
74           invalidOffloadAudioConfiguration;
75 };
76 
77 class BluetoothAudioSessionInstance_2_1 {
78  public:
79   // The API is to fetch the specified session of A2DP / Hearing Aid
80   static std::shared_ptr<BluetoothAudioSession_2_1> GetSessionInstance(
81       const ::android::hardware::bluetooth::audio::V2_1::SessionType&
82           session_type);
83 
84  private:
85   static std::unique_ptr<BluetoothAudioSessionInstance_2_1> instance_ptr;
86   std::mutex mutex_;
87   std::unordered_map<::android::hardware::bluetooth::audio::V2_1::SessionType,
88                      std::shared_ptr<BluetoothAudioSession_2_1>>
89       sessions_map_;
90 };
91 
92 }  // namespace audio
93 }  // namespace bluetooth
94 }  // namespace android
95