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 #ifdef TARGET_FLOSS
20 #include <audio_hal_interface/audio_linux.h>
21 #else
22 #include <hardware/audio.h>
23 #endif
24 
25 #include <hardware/bt_le_audio.h>
26 
27 #include <bitset>
28 #include <vector>
29 
30 #include "audio_hal_client/audio_hal_client.h"
31 #include "le_audio_types.h"
32 
33 namespace bluetooth::le_audio {
34 namespace utils {
35 types::LeAudioContextType AudioContentToLeAudioContext(audio_content_type_t content_type,
36                                                        audio_usage_t usage);
37 types::AudioContexts GetAudioContextsFromSourceMetadata(
38         const std::vector<struct playback_track_metadata_v7>& source_metadata);
39 types::AudioContexts GetAudioContextsFromSinkMetadata(
40         const std::vector<struct record_track_metadata_v7>& sink_metadata);
GetTargetLatencyForAudioContext(types::LeAudioContextType ctx)41 inline uint8_t GetTargetLatencyForAudioContext(types::LeAudioContextType ctx) {
42   switch (ctx) {
43     case types::LeAudioContextType::GAME:
44       FALLTHROUGH_INTENDED;
45     case types::LeAudioContextType::VOICEASSISTANTS:
46       FALLTHROUGH_INTENDED;
47     case types::LeAudioContextType::LIVE:
48       FALLTHROUGH_INTENDED;
49     case types::LeAudioContextType::CONVERSATIONAL:
50       FALLTHROUGH_INTENDED;
51     case types::LeAudioContextType::RINGTONE:
52       return types::kTargetLatencyLower;
53 
54     default:
55       return types::kTargetLatencyUndefined;
56   }
57 
58   return types::kTargetLatencyUndefined;
59 }
60 
61 /* Helpers to get btle_audio_codec_config_t for Java */
62 bluetooth::le_audio::btle_audio_codec_index_t translateBluetoothCodecFormatToCodecType(
63         uint8_t codec_format);
64 
65 bluetooth::le_audio::btle_audio_sample_rate_index_t translateToBtLeAudioCodecConfigSampleRate(
66         uint32_t sample_rate_capa);
67 bluetooth::le_audio::btle_audio_bits_per_sample_index_t translateToBtLeAudioCodecConfigBitPerSample(
68         uint8_t bits_per_sample);
69 bluetooth::le_audio::btle_audio_channel_count_index_t translateToBtLeAudioCodecConfigChannelCount(
70         uint8_t channel_count);
71 bluetooth::le_audio::btle_audio_frame_duration_index_t translateToBtLeAudioCodecConfigFrameDuration(
72         int frame_duration);
73 void fillStreamParamsToBtLeAudioCodecConfig(
74         const std::vector<struct set_configurations::AseConfiguration>& confs,
75         bluetooth::le_audio::btle_audio_codec_config_t& out_config);
76 
77 std::vector<bluetooth::le_audio::btle_audio_codec_config_t> GetRemoteBtLeAudioCodecConfigFromPac(
78         const types::PublishedAudioCapabilities& group_pacs);
79 bool IsCodecUsingLtvFormat(const types::LeAudioCodecId& codec_id);
80 types::LeAudioConfigurationStrategy GetStrategyForAseConfig(
81         const std::vector<le_audio::set_configurations::AseConfiguration>& cfgs,
82         uint8_t device_cnt);
83 ::bluetooth::le_audio::LeAudioCodecConfiguration
84 GetAudioSessionCodecConfigFromAudioSetConfiguration(
85         const ::bluetooth::le_audio::set_configurations::AudioSetConfiguration& audio_set_conf,
86         uint8_t remote_direction);
87 const struct types::acs_ac_record* GetConfigurationSupportedPac(
88         const ::bluetooth::le_audio::types::PublishedAudioCapabilities& pacs,
89         const ::bluetooth::le_audio::set_configurations::CodecConfigSetting& codec_config_setting);
90 bool IsAseConfigMatchedWithPreferredRequirements(
91         const std::vector<struct set_configurations::AseConfiguration>& ase_confs,
92         const std::vector<
93                 CodecManager::UnicastConfigurationRequirements::DeviceDirectionRequirements>& reqs,
94         uint8_t channel_cnt_per_ase);
95 }  // namespace utils
96 }  // namespace bluetooth::le_audio
97