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 <mutex>
20 #include <unordered_map>
21
22 #include <android/hardware/bluetooth/audio/2.0/IBluetoothAudioPort.h>
23 #include <fmq/MessageQueue.h>
24 #include <hardware/audio.h>
25 #include <hidl/MQDescriptor.h>
26
27 namespace android {
28 namespace bluetooth {
29 namespace audio {
30
31 using ::android::sp;
32 using ::android::hardware::kSynchronizedReadWrite;
33 using ::android::hardware::MessageQueue;
34 using ::android::hardware::bluetooth::audio::V2_0::AudioConfiguration;
35 using ::android::hardware::bluetooth::audio::V2_0::BitsPerSample;
36 using ::android::hardware::bluetooth::audio::V2_0::ChannelMode;
37 using ::android::hardware::bluetooth::audio::V2_0::CodecConfiguration;
38 using ::android::hardware::bluetooth::audio::V2_0::IBluetoothAudioPort;
39 using ::android::hardware::bluetooth::audio::V2_0::PcmParameters;
40 using ::android::hardware::bluetooth::audio::V2_0::SampleRate;
41 using ::android::hardware::bluetooth::audio::V2_0::SessionType;
42
43 using BluetoothAudioStatus =
44 ::android::hardware::bluetooth::audio::V2_0::Status;
45
46 using DataMQ = MessageQueue<uint8_t, kSynchronizedReadWrite>;
47
48 static constexpr uint16_t kObserversCookieSize = 0x0010; // 0x0000 ~ 0x000f
49 constexpr uint16_t kObserversCookieUndefined =
50 (static_cast<uint16_t>(SessionType::UNKNOWN) << 8 & 0xff00);
ObserversCookieGetSessionType(uint16_t cookie)51 inline SessionType ObserversCookieGetSessionType(uint16_t cookie) {
52 return static_cast<SessionType>(cookie >> 8 & 0x00ff);
53 }
ObserversCookieGetInitValue(SessionType session_type)54 inline uint16_t ObserversCookieGetInitValue(SessionType session_type) {
55 return (static_cast<uint16_t>(session_type) << 8 & 0xff00);
56 }
ObserversCookieGetUpperBound(SessionType session_type)57 inline uint16_t ObserversCookieGetUpperBound(SessionType session_type) {
58 return (static_cast<uint16_t>(session_type) << 8 & 0xff00) +
59 kObserversCookieSize;
60 }
61
62 // This presents the callbacks of started / suspended and session changed,
63 // and the bluetooth_audio module uses to receive the status notification
64 struct PortStatusCallbacks {
65 // control_result_cb_ - when the Bluetooth stack reports results of
66 // streamStarted or streamSuspended, the BluetoothAudioProvider will invoke
67 // this callback to report to the bluetooth_audio module.
68 // @param: cookie - indicates which bluetooth_audio output should handle
69 // @param: start_resp - this report is for startStream or not
70 // @param: status - the result of startStream
71 std::function<void(uint16_t cookie, bool start_resp,
72 const BluetoothAudioStatus& status)>
73 control_result_cb_;
74 // session_changed_cb_ - when the Bluetooth stack start / end session, the
75 // BluetoothAudioProvider will invoke this callback to notify to the
76 // bluetooth_audio module.
77 // @param: cookie - indicates which bluetooth_audio output should handle
78 std::function<void(uint16_t cookie)> session_changed_cb_;
79 };
80
81 class BluetoothAudioSession {
82 friend class BluetoothAudioSession_2_1;
83 friend class BluetoothAudioSession_2_2;
84
85 private:
86 // using recursive_mutex to allow hwbinder to re-enter agian.
87 std::recursive_mutex mutex_;
88 SessionType session_type_;
89
90 // audio control path to use for both software and offloading
91 sp<IBluetoothAudioPort> stack_iface_;
92 // audio data path (FMQ) for software encoding
93 std::unique_ptr<DataMQ> mDataMQ;
94 // audio data configuration for both software and offloading
95 AudioConfiguration audio_config_;
96
97 static AudioConfiguration invalidSoftwareAudioConfiguration;
98 static AudioConfiguration invalidOffloadAudioConfiguration;
99
100 // saving those registered bluetooth_audio's callbacks
101 std::unordered_map<uint16_t, std::shared_ptr<struct PortStatusCallbacks>>
102 observers_;
103
104 bool UpdateDataPath(const DataMQ::Descriptor* dataMQ);
105 bool UpdateAudioConfig(const AudioConfiguration& audio_config);
106 // invoking the registered session_changed_cb_
107 void ReportSessionStatus();
108
109 public:
110 BluetoothAudioSession(const SessionType& session_type);
111
112 // The function helps to check if this session is ready or not
113 // @return: true if the Bluetooth stack has started the specified session
114 bool IsSessionReady();
115
116 // The report function is used to report that the Bluetooth stack has started
117 // this session without any failure, and will invoke session_changed_cb_ to
118 // notify those registered bluetooth_audio outputs
119 void OnSessionStarted(const sp<IBluetoothAudioPort> stack_iface,
120 const DataMQ::Descriptor* dataMQ,
121 const AudioConfiguration& audio_config);
122
123 // The report function is used to report that the Bluetooth stack has ended
124 // the session, and will invoke session_changed_cb_ to notify registered
125 // bluetooth_audio outputs
126 void OnSessionEnded();
127
128 // The report function is used to report that the Bluetooth stack has notified
129 // the result of startStream or suspendStream, and will invoke
130 // control_result_cb_ to notify registered bluetooth_audio outputs
131 void ReportControlStatus(bool start_resp, const BluetoothAudioStatus& status);
132
133 // The control function helps the bluetooth_audio module to register
134 // PortStatusCallbacks
135 // @return: cookie - the assigned number to this bluetooth_audio output
136 uint16_t RegisterStatusCback(const PortStatusCallbacks& cbacks);
137
138 // The control function helps the bluetooth_audio module to unregister
139 // PortStatusCallbacks
140 // @param: cookie - indicates which bluetooth_audio output is
141 void UnregisterStatusCback(uint16_t cookie);
142
143 // The control function is for the bluetooth_audio module to get the current
144 // AudioConfiguration
145 const AudioConfiguration& GetAudioConfig();
146
147 // Those control functions are for the bluetooth_audio module to start,
148 // suspend, stop stream, to check position, and to update metadata.
149 bool StartStream();
150 bool SuspendStream();
151 void StopStream();
152 bool GetPresentationPosition(uint64_t* remote_delay_report_ns,
153 uint64_t* total_bytes_readed,
154 timespec* data_position);
155 void UpdateTracksMetadata(const struct source_metadata* source_metadata);
156
157 // The control function writes stream to FMQ
158 size_t OutWritePcmData(const void* buffer, size_t bytes);
159 // The control function read stream from FMQ
160 size_t InReadPcmData(void* buffer, size_t bytes);
161
162 static constexpr PcmParameters kInvalidPcmParameters = {
163 .sampleRate = SampleRate::RATE_UNKNOWN,
164 .channelMode = ChannelMode::UNKNOWN,
165 .bitsPerSample = BitsPerSample::BITS_UNKNOWN,
166 };
167 // can't be constexpr because of non-literal type
168 static const CodecConfiguration kInvalidCodecConfiguration;
169
170 static constexpr AudioConfiguration& kInvalidSoftwareAudioConfiguration =
171 invalidSoftwareAudioConfiguration;
172 static constexpr AudioConfiguration& kInvalidOffloadAudioConfiguration =
173 invalidOffloadAudioConfiguration;
174 };
175
176 class BluetoothAudioSessionInstance {
177 public:
178 // The API is to fetch the specified session of A2DP / Hearing Aid
179 static std::shared_ptr<BluetoothAudioSession> GetSessionInstance(
180 const SessionType& session_type);
181
182 private:
183 static std::unique_ptr<BluetoothAudioSessionInstance> instance_ptr;
184 std::mutex mutex_;
185 std::unordered_map<SessionType, std::shared_ptr<BluetoothAudioSession>>
186 sessions_map_;
187 };
188
189 } // namespace audio
190 } // namespace bluetooth
191 } // namespace android
192