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 android {
22 namespace bluetooth {
23 namespace audio {
24 
25 class BluetoothAudioSessionControl {
26  public:
27   // The control API helps to check if session is ready or not
28   // @return: true if the Bluetooth stack has started th specified session
IsSessionReady(const SessionType & session_type)29   static bool IsSessionReady(const SessionType& session_type) {
30     std::shared_ptr<BluetoothAudioSession> session_ptr =
31         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
32     if (session_ptr != nullptr) {
33       return session_ptr->IsSessionReady();
34     }
35     return false;
36   }
37 
38   // The control API helps the bluetooth_audio module to register
39   // PortStatusCallbacks
40   // @return: cookie - the assigned number to this bluetooth_audio output
RegisterControlResultCback(const SessionType & session_type,const PortStatusCallbacks & cbacks)41   static uint16_t RegisterControlResultCback(
42       const SessionType& session_type, const PortStatusCallbacks& cbacks) {
43     std::shared_ptr<BluetoothAudioSession> session_ptr =
44         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
45     if (session_ptr != nullptr) {
46       return session_ptr->RegisterStatusCback(cbacks);
47     }
48     return kObserversCookieUndefined;
49   }
50 
51   // The control API helps the bluetooth_audio module to unregister
52   // PortStatusCallbacks
53   // @param: cookie - indicates which bluetooth_audio output is
UnregisterControlResultCback(const SessionType & session_type,uint16_t cookie)54   static void UnregisterControlResultCback(const SessionType& session_type,
55                                            uint16_t cookie) {
56     std::shared_ptr<BluetoothAudioSession> session_ptr =
57         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
58     if (session_ptr != nullptr) {
59       session_ptr->UnregisterStatusCback(cookie);
60     }
61   }
62 
63   // The control API for the bluetooth_audio module to get current
64   // AudioConfiguration
GetAudioConfig(const SessionType & session_type)65   static const AudioConfiguration& GetAudioConfig(
66       const SessionType& session_type) {
67     std::shared_ptr<BluetoothAudioSession> session_ptr =
68         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
69     if (session_ptr != nullptr) {
70       return session_ptr->GetAudioConfig();
71     } else if (session_type == SessionType::A2DP_HARDWARE_OFFLOAD_DATAPATH) {
72       return BluetoothAudioSession::kInvalidOffloadAudioConfiguration;
73     } else {
74       return BluetoothAudioSession::kInvalidSoftwareAudioConfiguration;
75     }
76   }
77 
78   // Those control APIs for the bluetooth_audio module to start / suspend / stop
79   // stream, to check position, and to update metadata.
StartStream(const SessionType & session_type)80   static bool StartStream(const SessionType& session_type) {
81     std::shared_ptr<BluetoothAudioSession> session_ptr =
82         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
83     if (session_ptr != nullptr) {
84       return session_ptr->StartStream();
85     }
86     return false;
87   }
88 
SuspendStream(const SessionType & session_type)89   static bool SuspendStream(const SessionType& session_type) {
90     std::shared_ptr<BluetoothAudioSession> session_ptr =
91         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
92     if (session_ptr != nullptr) {
93       return session_ptr->SuspendStream();
94     }
95     return false;
96   }
97 
StopStream(const SessionType & session_type)98   static void StopStream(const SessionType& session_type) {
99     std::shared_ptr<BluetoothAudioSession> session_ptr =
100         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
101     if (session_ptr != nullptr) {
102       session_ptr->StopStream();
103     }
104   }
105 
GetPresentationPosition(const SessionType & session_type,uint64_t * remote_delay_report_ns,uint64_t * total_bytes_readed,timespec * data_position)106   static bool GetPresentationPosition(const SessionType& session_type,
107                                       uint64_t* remote_delay_report_ns,
108                                       uint64_t* total_bytes_readed,
109                                       timespec* data_position) {
110     std::shared_ptr<BluetoothAudioSession> session_ptr =
111         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
112     if (session_ptr != nullptr) {
113       return session_ptr->GetPresentationPosition(
114           remote_delay_report_ns, total_bytes_readed, data_position);
115     }
116     return false;
117   }
118 
UpdateTracksMetadata(const SessionType & session_type,const struct source_metadata * source_metadata)119   static void UpdateTracksMetadata(
120       const SessionType& session_type,
121       const struct source_metadata* source_metadata) {
122     std::shared_ptr<BluetoothAudioSession> session_ptr =
123         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
124     if (session_ptr != nullptr) {
125       session_ptr->UpdateTracksMetadata(source_metadata);
126     }
127   }
128 
129   // The control API writes stream to FMQ
OutWritePcmData(const SessionType & session_type,const void * buffer,size_t bytes)130   static size_t OutWritePcmData(const SessionType& session_type,
131                                 const void* buffer, size_t bytes) {
132     std::shared_ptr<BluetoothAudioSession> session_ptr =
133         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
134     if (session_ptr != nullptr) {
135       return session_ptr->OutWritePcmData(buffer, bytes);
136     }
137     return 0;
138   }
139 };
140 
141 }  // namespace audio
142 }  // namespace bluetooth
143 }  // namespace android
144