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