• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 HIMSA II K/S - www.himsa.com.
3  * Represented by EHIMA - www.ehima.com
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include <variant>
21 
22 #include "bta/le_audio/audio_hal_client/audio_hal_client.h"
23 #include "bta/le_audio/le_audio_types.h"
24 #include "bta_le_audio_api.h"
25 #include "bta_le_audio_broadcaster_api.h"
26 
27 /* Types used internally by various modules of the broadcaster but not exposed
28  * in the API.
29  */
30 
31 namespace le_audio {
32 namespace broadcaster {
33 static const uint16_t kBroadcastAudioAnnouncementServiceUuid = 0x1852;
34 static const uint16_t kBasicAudioAnnouncementServiceUuid = 0x1851;
35 static const uint16_t kPublicBroadcastAnnouncementServiceUuid = 0x1856;
36 
37 static const uint8_t kBisIndexInvalid = 0;
38 
39 bool ToRawPacket(bluetooth::le_audio::BasicAudioAnnouncementData const&,
40                  std::vector<uint8_t>&);
41 
42 void PrepareAdvertisingData(
43     bool is_public, const std::string& broadcast_name,
44     bluetooth::le_audio::BroadcastId& broadcast_id,
45     const bluetooth::le_audio::PublicBroadcastAnnouncementData&
46         public_announcement,
47     std::vector<uint8_t>& adv_data);
48 void PreparePeriodicData(
49     const bluetooth::le_audio::BasicAudioAnnouncementData& announcement,
50     std::vector<uint8_t>& periodic_data);
51 
52 struct BroadcastCodecWrapper {
53   BroadcastCodecWrapper(types::LeAudioCodecId codec_id,
54                         LeAudioCodecConfiguration source_codec_config,
55                         uint32_t codec_bitrate, uint32_t codec_frame_len,
56                         uint8_t blocks_per_sdu = 1)
codec_idBroadcastCodecWrapper57       : codec_id(codec_id),
58         source_codec_config(source_codec_config),
59         codec_bitrate(codec_bitrate),
60         codec_frame_len(codec_frame_len),
61         blocks_per_sdu(blocks_per_sdu) {
62     if (codec_id.coding_format != types::kLeAudioCodingFormatLC3)
63       LOG(ERROR) << "Unsupported coding format!";
64   }
65 
66   /* We need this copy-assignment operator as we currently use global copy of a
67    * wrapper for the currently active Broadcast. Maybe we should consider using
68    * shared pointer instead.
69    */
70   BroadcastCodecWrapper& operator=(const BroadcastCodecWrapper& other) {
71     codec_id = other.codec_id;
72     source_codec_config = other.source_codec_config;
73     codec_bitrate = other.codec_bitrate;
74     codec_frame_len = other.codec_frame_len;
75     blocks_per_sdu = other.blocks_per_sdu;
76     return *this;
77   };
78 
79   types::LeAudioLtvMap GetSubgroupCodecSpecData() const;
80   types::LeAudioLtvMap GetBisCodecSpecData(uint8_t bis_idx) const;
81 
GetMaxSduSizePerChannelBroadcastCodecWrapper82   uint16_t GetMaxSduSizePerChannel() const {
83     if (codec_id.coding_format == types::kLeAudioCodingFormatLC3) {
84       return GetFrameLen() * blocks_per_sdu;
85     }
86 
87     LOG(ERROR) << "Invalid codec ID: "
88                << "[" << +codec_id.coding_format << ":"
89                << +codec_id.vendor_company_id << ":"
90                << +codec_id.vendor_codec_id << "]";
91     return 0;
92   }
93 
GetMaxSduSizeBroadcastCodecWrapper94   uint16_t GetMaxSduSize() const {
95     return GetNumChannelsPerBis() * GetMaxSduSizePerChannel();
96   }
97 
GetLeAudioCodecConfigurationBroadcastCodecWrapper98   const LeAudioCodecConfiguration& GetLeAudioCodecConfiguration() const {
99     return source_codec_config;
100   }
101 
GetLeAudioCodecIdBroadcastCodecWrapper102   const types::LeAudioCodecId& GetLeAudioCodecId() const { return codec_id; }
103 
GetNumChannelsBroadcastCodecWrapper104   uint8_t GetNumChannels() const { return source_codec_config.num_channels; }
105 
GetBitrateBroadcastCodecWrapper106   uint32_t GetBitrate() const { return codec_bitrate; }
107 
GetFrameLenBroadcastCodecWrapper108   uint32_t GetFrameLen() const { return codec_frame_len; }
109 
GetBitsPerSampleBroadcastCodecWrapper110   uint8_t GetBitsPerSample() const {
111     return source_codec_config.bits_per_sample;
112   }
113 
GetSampleRateBroadcastCodecWrapper114   uint32_t GetSampleRate() const { return source_codec_config.sample_rate; }
115 
GetDataIntervalUsBroadcastCodecWrapper116   uint32_t GetDataIntervalUs() const {
117     return source_codec_config.data_interval_us;
118   }
119 
GetNumChannelsPerBisBroadcastCodecWrapper120   uint8_t GetNumChannelsPerBis() const {
121     // TODO: Need to handle each BIS has more than one channel case
122     return 1;
123   }
124 
125  private:
126   types::LeAudioCodecId codec_id;
127   LeAudioCodecConfiguration source_codec_config;
128   uint32_t codec_bitrate;
129   uint32_t codec_frame_len;
130   uint8_t blocks_per_sdu;
131 };
132 
133 std::ostream& operator<<(
134     std::ostream& os,
135     const le_audio::broadcaster::BroadcastCodecWrapper& config);
136 
137 struct BroadcastQosConfig {
BroadcastQosConfigBroadcastQosConfig138   BroadcastQosConfig(uint8_t retransmission_number,
139                      uint16_t max_transport_latency)
140       : retransmission_number(retransmission_number),
141         max_transport_latency(max_transport_latency) {}
142 
143   BroadcastQosConfig& operator=(const BroadcastQosConfig& other) {
144     retransmission_number = other.retransmission_number;
145     max_transport_latency = other.max_transport_latency;
146     return *this;
147   };
148 
getRetransmissionNumberBroadcastQosConfig149   uint8_t getRetransmissionNumber() const { return retransmission_number; }
getMaxTransportLatencyBroadcastQosConfig150   uint16_t getMaxTransportLatency() const { return max_transport_latency; }
151 
152  private:
153   uint8_t retransmission_number;
154   uint16_t max_transport_latency;
155 };
156 
157 static const BroadcastQosConfig qos_config_2_10 = BroadcastQosConfig(2, 10);
158 static const BroadcastQosConfig qos_config_4_45 = BroadcastQosConfig(4, 45);
159 static const BroadcastQosConfig qos_config_4_50 = BroadcastQosConfig(4, 50);
160 static const BroadcastQosConfig qos_config_4_60 = BroadcastQosConfig(4, 60);
161 static const BroadcastQosConfig qos_config_4_65 = BroadcastQosConfig(4, 65);
162 
163 std::ostream& operator<<(
164     std::ostream& os, const le_audio::broadcaster::BroadcastQosConfig& config);
165 
166 std::pair<const BroadcastCodecWrapper&, const BroadcastQosConfig&>
167 getStreamConfigForContext(types::AudioContexts context);
168 
169 }  // namespace broadcaster
170 }  // namespace le_audio
171 
172 /* BroadcastAnnouncements compare helper */
173 namespace bluetooth {
174 namespace le_audio {
175 bool operator==(const BasicAudioAnnouncementData& lhs,
176                 const BasicAudioAnnouncementData& rhs);
177 bool operator==(const PublicBroadcastAnnouncementData& lhs,
178                 const PublicBroadcastAnnouncementData& rhs);
179 }  // namespace le_audio
180 }  // namespace bluetooth
181