blob: 0f349a4650158de54c05fbfe987cb98b257afafb [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#define LOG_TAG "BTAudioProviderStub"
18
19#include "BluetoothAudioProvider.h"
20
21#include <android-base/logging.h>
22
23#include "BluetoothAudioSessionReport.h"
24#include "BluetoothAudioSupportedCodecsDB.h"
25
26namespace android {
27namespace hardware {
28namespace bluetooth {
29namespace audio {
30namespace V2_1 {
31namespace implementation {
32
33using ::android::bluetooth::audio::BluetoothAudioSessionReport;
34using ::android::hardware::kSynchronizedReadWrite;
35using ::android::hardware::MessageQueue;
36using ::android::hardware::Void;
37
38using DataMQ = MessageQueue<uint8_t, kSynchronizedReadWrite>;
39
40void BluetoothAudioDeathRecipient::serviceDied(
41 uint64_t cookie __unused,
42 const wp<::android::hidl::base::V1_0::IBase>& who __unused) {
43 LOG(ERROR) << "BluetoothAudioDeathRecipient::" << __func__
44 << " - BluetoothAudio Service died";
45 provider_->endSession();
46}
47
48BluetoothAudioProvider::BluetoothAudioProvider()
49 : death_recipient_(new BluetoothAudioDeathRecipient(this)),
50 session_type_(SessionType::UNKNOWN),
51 audio_config_({}) {}
52
53Return<void> BluetoothAudioProvider::startSession(
54 const sp<IBluetoothAudioPort>& hostIf,
55 const V2_0::AudioConfiguration& audioConfig, startSession_cb _hidl_cb) {
56 AudioConfiguration audioConfig_2_1;
57
58 audioConfig_2_1.codecConfig() = audioConfig.codecConfig();
59 audioConfig_2_1.pcmConfig() = {
60 .sampleRate = static_cast<SampleRate>(audioConfig.pcmConfig().sampleRate),
61 .channelMode = audioConfig.pcmConfig().channelMode,
62 .bitsPerSample = audioConfig.pcmConfig().bitsPerSample,
63 .dataIntervalUs = 0};
64
65 return startSession_2_1(hostIf, audioConfig_2_1, _hidl_cb);
66}
67
68Return<void> BluetoothAudioProvider::startSession_2_1(
69 const sp<IBluetoothAudioPort>& hostIf,
70 const AudioConfiguration& audioConfig, startSession_cb _hidl_cb) {
71 if (hostIf == nullptr) {
72 _hidl_cb(BluetoothAudioStatus::FAILURE, DataMQ::Descriptor());
73 return Void();
74 }
75
76 /**
77 * Initialize the audio platform if audioConfiguration is supported.
78 * Save the IBluetoothAudioPort interface, so that it can be used
79 * later to send stream control commands to the HAL client, based on
80 * interaction with Audio framework.
81 */
82 audio_config_ = audioConfig;
83 stack_iface_ = hostIf;
84 stack_iface_->linkToDeath(death_recipient_, 0);
85
86 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
87 << ", AudioConfiguration=[" << toString(audio_config_) << "]";
88
89 onSessionReady(_hidl_cb);
90 return Void();
91}
92
93Return<void> BluetoothAudioProvider::streamStarted(
94 BluetoothAudioStatus status) {
95 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
96 << ", status=" << toString(status);
97
98 /**
99 * Streaming on control path has started,
100 * HAL server should start the streaming on data path.
101 */
102 if (stack_iface_) {
103 BluetoothAudioSessionReport::ReportControlStatus(session_type_, true,
104 status);
105 } else {
106 LOG(WARNING) << __func__ << " - SessionType=" << toString(session_type_)
107 << ", status=" << toString(status) << " has NO session";
108 }
109
110 return Void();
111}
112
113Return<void> BluetoothAudioProvider::streamSuspended(
114 BluetoothAudioStatus status) {
115 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
116 << ", status=" << toString(status);
117
118 /**
119 * Streaming on control path has suspend,
120 * HAL server should suspend the streaming on data path.
121 */
122 if (stack_iface_) {
123 BluetoothAudioSessionReport::ReportControlStatus(session_type_, false,
124 status);
125 } else {
126 LOG(WARNING) << __func__ << " - SessionType=" << toString(session_type_)
127 << ", status=" << toString(status) << " has NO session";
128 }
129
130 return Void();
131}
132
133Return<void> BluetoothAudioProvider::endSession() {
134 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_);
135
136 if (stack_iface_) {
137 BluetoothAudioSessionReport::OnSessionEnded(session_type_);
138 stack_iface_->unlinkToDeath(death_recipient_);
139 } else {
140 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
141 << " has NO session";
142 }
143
144 /**
145 * Clean up the audio platform as remote audio device is no
146 * longer active
147 */
148 stack_iface_ = nullptr;
149 audio_config_ = {};
150
151 return Void();
152}
153
154} // namespace implementation
155} // namespace V2_1
156} // namespace audio
157} // namespace bluetooth
158} // namespace hardware
159} // namespace android