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
84 private:
85 // using recursive_mutex to allow hwbinder to re-enter agian.
86 std::recursive_mutex mutex_;
87 SessionType session_type_;
88
89 // audio control path to use for both software and offloading
90 sp<IBluetoothAudioPort> stack_iface_;
91 // audio data path (FMQ) for software encoding
92 std::unique_ptr<DataMQ> mDataMQ;
93 // audio data configuration for both software and offloading
94 AudioConfiguration audio_config_;
95
96 static AudioConfiguration invalidSoftwareAudioConfiguration;
97 static AudioConfiguration invalidOffloadAudioConfiguration;
98
99 // saving those registered bluetooth_audio's callbacks
100 std::unordered_map<uint16_t, std::shared_ptr<struct PortStatusCallbacks>>
101 observers_;
102
103 bool UpdateDataPath(const DataMQ::Descriptor* dataMQ);
104 bool UpdateAudioConfig(const AudioConfiguration& audio_config);
105 // invoking the registered session_changed_cb_
106 void ReportSessionStatus();
107
108 public:
109 BluetoothAudioSession(const SessionType& session_type);
110
111 // The function helps to check if this session is ready or not
112 // @return: true if the Bluetooth stack has started the specified session
113 bool IsSessionReady();
114
115 // The report function is used to report that the Bluetooth stack has started
116 // this session without any failure, and will invoke session_changed_cb_ to
117 // notify those registered bluetooth_audio outputs
118 void OnSessionStarted(const sp<IBluetoothAudioPort> stack_iface,
119 const DataMQ::Descriptor* dataMQ,
120 const AudioConfiguration& audio_config);
121
122 // The report function is used to report that the Bluetooth stack has ended
123 // the session, and will invoke session_changed_cb_ to notify registered
124 // bluetooth_audio outputs
125 void OnSessionEnded();
126
127 // The report function is used to report that the Bluetooth stack has notified
128 // the result of startStream or suspendStream, and will invoke
129 // control_result_cb_ to notify registered bluetooth_audio outputs
130 void ReportControlStatus(bool start_resp, const BluetoothAudioStatus& status);
131
132 // The control function helps the bluetooth_audio module to register
133 // PortStatusCallbacks
134 // @return: cookie - the assigned number to this bluetooth_audio output
135 uint16_t RegisterStatusCback(const PortStatusCallbacks& cbacks);
136
137 // The control function helps the bluetooth_audio module to unregister
138 // PortStatusCallbacks
139 // @param: cookie - indicates which bluetooth_audio output is
140 void UnregisterStatusCback(uint16_t cookie);
141
142 // The control function is for the bluetooth_audio module to get the current
143 // AudioConfiguration
144 const AudioConfiguration& GetAudioConfig();
145
146 // Those control functions are for the bluetooth_audio module to start,
147 // suspend, stop stream, to check position, and to update metadata.
148 bool StartStream();
149 bool SuspendStream();
150 void StopStream();
151 bool GetPresentationPosition(uint64_t* remote_delay_report_ns,
152 uint64_t* total_bytes_readed,
153 timespec* data_position);
154 void UpdateTracksMetadata(const struct source_metadata* source_metadata);
155
156 // The control function writes stream to FMQ
157 size_t OutWritePcmData(const void* buffer, size_t bytes);
158 // The control function read stream from FMQ
159 size_t InReadPcmData(void* buffer, size_t bytes);
160
161 static constexpr PcmParameters kInvalidPcmParameters = {
162 .sampleRate = SampleRate::RATE_UNKNOWN,
163 .channelMode = ChannelMode::UNKNOWN,
164 .bitsPerSample = BitsPerSample::BITS_UNKNOWN,
165 };
166 // can't be constexpr because of non-literal type
167 static const CodecConfiguration kInvalidCodecConfiguration;
168
169 static constexpr AudioConfiguration& kInvalidSoftwareAudioConfiguration =
170 invalidSoftwareAudioConfiguration;
171 static constexpr AudioConfiguration& kInvalidOffloadAudioConfiguration =
172 invalidOffloadAudioConfiguration;
173 };
174
175 class BluetoothAudioSessionInstance {
176 public:
177 // The API is to fetch the specified session of A2DP / Hearing Aid
178 static std::shared_ptr<BluetoothAudioSession> GetSessionInstance(
179 const SessionType& session_type);
180
181 private:
182 static std::unique_ptr<BluetoothAudioSessionInstance> instance_ptr;
183 std::mutex mutex_;
184 std::unordered_map<SessionType, std::shared_ptr<BluetoothAudioSession>>
185 sessions_map_;
186 };
187
188 } // namespace audio
189 } // namespace bluetooth
190 } // namespace android
191