• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "BluetoothAudioSession.h"
20 
21 namespace aidl {
22 namespace android {
23 namespace hardware {
24 namespace bluetooth {
25 namespace audio {
26 
27 class BluetoothAudioSessionControl {
28  public:
29   /***
30    * The control API helps to check if session is ready or not
31    * @return: true if the Bluetooth stack has started th specified session
32    ***/
IsSessionReady(const SessionType & session_type)33   static bool IsSessionReady(const SessionType& session_type) {
34     std::shared_ptr<BluetoothAudioSession> session_ptr =
35         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
36     if (session_ptr != nullptr) {
37       return session_ptr->IsSessionReady();
38     }
39 
40     return false;
41   }
42 
43   /***
44    * The control API helps the bluetooth_audio module to register
45    * PortStatusCallbacks
46    * @return: cookie - the assigned number to this bluetooth_audio output
47    ***/
RegisterControlResultCback(const SessionType & session_type,const PortStatusCallbacks & cbacks)48   static uint16_t RegisterControlResultCback(
49       const SessionType& session_type, const PortStatusCallbacks& cbacks) {
50     std::shared_ptr<BluetoothAudioSession> session_ptr =
51         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
52     if (session_ptr != nullptr) {
53       return session_ptr->RegisterStatusCback(cbacks);
54     }
55     return kObserversCookieUndefined;
56   }
57 
58   /***
59    * The control API helps the bluetooth_audio module to unregister
60    * PortStatusCallbacks
61    * @param: cookie - indicates which bluetooth_audio output is
62    ***/
UnregisterControlResultCback(const SessionType & session_type,uint16_t cookie)63   static void UnregisterControlResultCback(const SessionType& session_type,
64                                            uint16_t cookie) {
65     std::shared_ptr<BluetoothAudioSession> session_ptr =
66         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
67     if (session_ptr != nullptr) {
68       session_ptr->UnregisterStatusCback(cookie);
69     }
70   }
71 
72   /***
73    * The control API for the bluetooth_audio module to get current
74    * AudioConfiguration
75    ***/
GetAudioConfig(const SessionType & session_type)76   static const AudioConfiguration GetAudioConfig(
77       const SessionType& session_type) {
78     std::shared_ptr<BluetoothAudioSession> session_ptr =
79         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
80     if (session_ptr != nullptr) {
81       return session_ptr->GetAudioConfig();
82     }
83     switch (session_type) {
84       case SessionType::A2DP_HARDWARE_OFFLOAD_ENCODING_DATAPATH:
85       case SessionType::A2DP_HARDWARE_OFFLOAD_DECODING_DATAPATH:
86         return AudioConfiguration(CodecConfiguration{});
87       case SessionType::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH:
88       case SessionType::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH:
89         return AudioConfiguration(LeAudioConfiguration{});
90       case SessionType::LE_AUDIO_BROADCAST_HARDWARE_OFFLOAD_ENCODING_DATAPATH:
91         return AudioConfiguration(LeAudioBroadcastConfiguration{});
92       default:
93         return AudioConfiguration(PcmConfiguration{});
94     }
95   }
96 
97   /***
98    * Those control APIs for the bluetooth_audio module to start / suspend /
99   stop
100    * stream, to check position, and to update metadata.
101   ***/
102   static bool StartStream(const SessionType& session_type,
103                           bool low_latency = false) {
104     std::shared_ptr<BluetoothAudioSession> session_ptr =
105         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
106     if (session_ptr != nullptr) {
107       return session_ptr->StartStream(low_latency);
108     }
109     return false;
110   }
111 
SuspendStream(const SessionType & session_type)112   static bool SuspendStream(const SessionType& session_type) {
113     std::shared_ptr<BluetoothAudioSession> session_ptr =
114         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
115     if (session_ptr != nullptr) {
116       return session_ptr->SuspendStream();
117     }
118     return false;
119   }
120 
StopStream(const SessionType & session_type)121   static void StopStream(const SessionType& session_type) {
122     std::shared_ptr<BluetoothAudioSession> session_ptr =
123         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
124     if (session_ptr != nullptr) {
125       session_ptr->StopStream();
126     }
127   }
128 
GetPresentationPosition(const SessionType & session_type,PresentationPosition & presentation_position)129   static bool GetPresentationPosition(
130       const SessionType& session_type,
131       PresentationPosition& presentation_position) {
132     std::shared_ptr<BluetoothAudioSession> session_ptr =
133         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
134     if (session_ptr != nullptr) {
135       return session_ptr->GetPresentationPosition(presentation_position);
136     }
137     return false;
138   }
139 
UpdateSourceMetadata(const SessionType & session_type,const struct source_metadata & source_metadata)140   static void UpdateSourceMetadata(
141       const SessionType& session_type,
142       const struct source_metadata& source_metadata) {
143     std::shared_ptr<BluetoothAudioSession> session_ptr =
144         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
145     if (session_ptr != nullptr) {
146       session_ptr->UpdateSourceMetadata(source_metadata);
147     }
148   }
149 
UpdateSinkMetadata(const SessionType & session_type,const struct sink_metadata & sink_metadata)150   static void UpdateSinkMetadata(const SessionType& session_type,
151                                  const struct sink_metadata& sink_metadata) {
152     std::shared_ptr<BluetoothAudioSession> session_ptr =
153         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
154     if (session_ptr != nullptr) {
155       session_ptr->UpdateSinkMetadata(sink_metadata);
156     }
157   }
158 
UpdateSourceMetadata(const SessionType & session_type,const SourceMetadata & source_metadata)159   static bool UpdateSourceMetadata(const SessionType& session_type,
160                                    const SourceMetadata& source_metadata) {
161     std::shared_ptr<BluetoothAudioSession> session_ptr =
162         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
163     if (session_ptr != nullptr) {
164       return session_ptr->UpdateSourceMetadata(source_metadata);
165     }
166     return false;
167   }
168 
UpdateSinkMetadata(const SessionType & session_type,const SinkMetadata & sink_metadata)169   static bool UpdateSinkMetadata(const SessionType& session_type,
170                                  const SinkMetadata& sink_metadata) {
171     std::shared_ptr<BluetoothAudioSession> session_ptr =
172         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
173     if (session_ptr != nullptr) {
174       return session_ptr->UpdateSinkMetadata(sink_metadata);
175     }
176     return false;
177   }
178 
GetSupportedLatencyModes(const SessionType & session_type)179   static std::vector<LatencyMode> GetSupportedLatencyModes(
180       const SessionType& session_type) {
181     std::shared_ptr<BluetoothAudioSession> session_ptr =
182         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
183     if (session_ptr != nullptr) {
184       return session_ptr->GetSupportedLatencyModes();
185     }
186     return std::vector<LatencyMode>();
187   }
188 
SetLatencyMode(const SessionType & session_type,const LatencyMode & latency_mode)189   static void SetLatencyMode(const SessionType& session_type,
190                              const LatencyMode& latency_mode) {
191     std::shared_ptr<BluetoothAudioSession> session_ptr =
192         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
193     if (session_ptr != nullptr) {
194       session_ptr->SetLatencyMode(latency_mode);
195     }
196   }
197 
198   /***
199    * The control API writes stream to FMQ
200    ***/
OutWritePcmData(const SessionType & session_type,const void * buffer,size_t bytes)201   static size_t OutWritePcmData(const SessionType& session_type,
202                                 const void* buffer, size_t bytes) {
203     std::shared_ptr<BluetoothAudioSession> session_ptr =
204         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
205     if (session_ptr != nullptr) {
206       return session_ptr->OutWritePcmData(buffer, bytes);
207     }
208     return 0;
209   }
210 
211   /***
212    * The control API reads stream from FMQ
213    ***/
InReadPcmData(const SessionType & session_type,void * buffer,size_t bytes)214   static size_t InReadPcmData(const SessionType& session_type, void* buffer,
215                               size_t bytes) {
216     std::shared_ptr<BluetoothAudioSession> session_ptr =
217         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
218     if (session_ptr != nullptr) {
219       return session_ptr->InReadPcmData(buffer, bytes);
220     }
221     return 0;
222   }
223 };
224 
225 }  // namespace audio
226 }  // namespace bluetooth
227 }  // namespace hardware
228 }  // namespace android
229 }  // namespace aidl
230