blob: 6707765638c90664ec8667e13c90ed45865261f2 [file] [log] [blame]
Cheney Ni47a83092018-11-17 01:42:19 +08001/*
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
21namespace android {
22namespace bluetooth {
23namespace audio {
24
25class 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
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
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
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
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.
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
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
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
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
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
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