1 /*
2  * Copyright 2022 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 <vector>
20 
21 #include "a2dp_constants.h"
22 #include "a2dp_encoding.h"
23 #include "a2dp_sbc_constants.h"
24 #include "common/message_loop_thread.h"
25 #include "hardware/bt_av.h"
26 #include "osi/include/properties.h"
27 #include "types/raw_address.h"
28 
29 namespace bluetooth {
30 namespace audio {
31 namespace aidl {
32 namespace a2dp {
33 
34 bool update_codec_offloading_capabilities(
35         const std::vector<btav_a2dp_codec_config_t>& framework_preference,
36         bool supports_a2dp_hw_offload_v2);
37 
38 /***
39  * Check if new bluetooth_audio is enabled
40  ***/
41 bool is_hal_enabled();
42 
43 /***
44  * Check if new bluetooth_audio is running with offloading encoders
45  ***/
46 bool is_hal_offloading();
47 
48 /***
49  * Initialize BluetoothAudio HAL: openProvider
50  ***/
51 bool init(bluetooth::common::MessageLoopThread* message_loop,
52           bluetooth::audio::a2dp::StreamCallbacks const* stream_callbacks, bool offload_enabled);
53 
54 /***
55  * Clean up BluetoothAudio HAL
56  ***/
57 void cleanup();
58 
59 /***
60  * Set up the codec into BluetoothAudio HAL
61  ***/
62 bool setup_codec(A2dpCodecConfig* a2dp_config, uint16_t peer_mtu,
63                  int preferred_encoding_interval_us);
64 
65 /***
66  * Send command to the BluetoothAudio HAL: StartSession, EndSession,
67  * StreamStarted, StreamSuspended
68  ***/
69 void start_session();
70 void end_session();
71 void ack_stream_started(::bluetooth::audio::a2dp::Status status);
72 void ack_stream_suspended(::bluetooth::audio::a2dp::Status status);
73 
74 /***
75  * Read from the FMQ of BluetoothAudio HAL
76  ***/
77 size_t read(uint8_t* p_buf, uint32_t len);
78 
79 /***
80  * Update A2DP delay report to BluetoothAudio HAL
81  ***/
82 void set_remote_delay(uint16_t delay_report);
83 
84 /***
85  * Set low latency buffer mode allowed or disallowed
86  ***/
87 void set_low_latency_mode_allowed(bool allowed);
88 
89 namespace provider {
90 
91 /***
92  * Lookup the codec info in the list of supported offloaded sink codecs.
93  * Should not be called before update_codec_offloading_capabilities.
94  ***/
95 std::optional<btav_a2dp_codec_index_t> sink_codec_index(const uint8_t* p_codec_info);
96 
97 /***
98  * Lookup the codec info in the list of supported offloaded source codecs.
99  * Should not be called before update_codec_offloading_capabilities.
100  ***/
101 std::optional<btav_a2dp_codec_index_t> source_codec_index(const uint8_t* p_codec_info);
102 
103 /***
104  * Return the name of the codec which is assigned to the input index.
105  * The codec index must be in the ranges
106  * BTAV_A2DP_CODEC_INDEX_SINK_EXT_MIN..BTAV_A2DP_CODEC_INDEX_SINK_EXT_MAX or
107  * BTAV_A2DP_CODEC_INDEX_SOURCE_EXT_MIN..BTAV_A2DP_CODEC_INDEX_SOURCE_EXT_MAX.
108  * Returns nullopt if the codec_index is not assigned or codec extensibility
109  * is not supported or enabled.
110  * Should not be called before update_codec_offloading_capabilities.
111  ***/
112 std::optional<const char*> codec_index_str(btav_a2dp_codec_index_t codec_index);
113 
114 /***
115  * Return true if the codec is supported for the session type
116  * A2DP_HARDWARE_ENCODING_DATAPATH or A2DP_HARDWARE_DECODING_DATAPATH.
117  ***/
118 bool supports_codec(btav_a2dp_codec_index_t codec_index);
119 
120 /***
121  * Return the A2DP capabilities for the selected codec.
122  ***/
123 bool codec_info(btav_a2dp_codec_index_t codec_index, bluetooth::a2dp::CodecId* codec_id,
124                 uint8_t* codec_info, btav_a2dp_codec_config_t* codec_config);
125 
126 /***
127  * Query the codec selection fromt the audio HAL.
128  * The HAL is expected to pick the best audio configuration based on the
129  * discovered remote SEPs.
130  ***/
131 std::optional<::bluetooth::audio::a2dp::provider::a2dp_configuration> get_a2dp_configuration(
132         RawAddress peer_address,
133         std::vector<::bluetooth::audio::a2dp::provider::a2dp_remote_capabilities> const&
134                 remote_seps,
135         btav_a2dp_codec_config_t const& user_preferences);
136 
137 /***
138  * Query the codec parameters from the audio HAL.
139  * The HAL is expected to parse the codec configuration
140  * received from the peer and decide whether accept
141  * the it or not.
142  ***/
143 tA2DP_STATUS parse_a2dp_configuration(btav_a2dp_codec_index_t codec_index,
144                                       const uint8_t* codec_info,
145                                       btav_a2dp_codec_config_t* codec_parameters,
146                                       std::vector<uint8_t>* vendor_specific_parameters);
147 
148 }  // namespace provider
149 }  // namespace a2dp
150 }  // namespace aidl
151 }  // namespace audio
152 }  // namespace bluetooth
153