• 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 
36 static const uint8_t kBisIndexInvalid = 0;
37 
38 bool ToRawPacket(bluetooth::le_audio::BasicAudioAnnouncementData const&,
39                  std::vector<uint8_t>&);
40 
41 void PrepareAdvertisingData(bluetooth::le_audio::BroadcastId& broadcast_id,
42                             std::vector<uint8_t>& periodic_data);
43 void PreparePeriodicData(
44     const bluetooth::le_audio::BasicAudioAnnouncementData& announcement,
45     std::vector<uint8_t>& periodic_data);
46 
47 struct BroadcastCodecWrapper {
48   BroadcastCodecWrapper(types::LeAudioCodecId codec_id,
49                         LeAudioCodecConfiguration source_codec_config,
50                         uint32_t codec_bitrate, uint32_t codec_frame_len,
51                         uint8_t blocks_per_sdu = 1)
codec_idBroadcastCodecWrapper52       : codec_id(codec_id),
53         source_codec_config(source_codec_config),
54         codec_bitrate(codec_bitrate),
55         codec_frame_len(codec_frame_len),
56         blocks_per_sdu(blocks_per_sdu) {
57     if (codec_id.coding_format != types::kLeAudioCodingFormatLC3)
58       LOG(ERROR) << "Unsupported coding format!";
59   }
60 
61   /* We need this copy-assignment operator as we currently use global copy of a
62    * wrapper for the currently active Broadcast. Maybe we should consider using
63    * shared pointer instead.
64    */
65   BroadcastCodecWrapper& operator=(const BroadcastCodecWrapper& other) {
66     codec_id = other.codec_id;
67     source_codec_config = other.source_codec_config;
68     codec_bitrate = other.codec_bitrate;
69     codec_frame_len = other.codec_frame_len;
70     blocks_per_sdu = other.blocks_per_sdu;
71     return *this;
72   };
73 
74   types::LeAudioLtvMap GetSubgroupCodecSpecData() const;
75   types::LeAudioLtvMap GetBisCodecSpecData(uint8_t bis_idx) const;
76 
GetMaxSduSizePerChannelBroadcastCodecWrapper77   uint16_t GetMaxSduSizePerChannel() const {
78     if (codec_id.coding_format == types::kLeAudioCodingFormatLC3) {
79       return GetFrameLen() * blocks_per_sdu;
80     }
81 
82     LOG(ERROR) << "Invalid codec ID: "
83                << "[" << +codec_id.coding_format << ":"
84                << +codec_id.vendor_company_id << ":"
85                << +codec_id.vendor_codec_id << "]";
86     return 0;
87   }
88 
GetMaxSduSizeBroadcastCodecWrapper89   uint16_t GetMaxSduSize() const {
90     return GetNumChannelsPerBis() * GetMaxSduSizePerChannel();
91   }
92 
GetLeAudioCodecConfigurationBroadcastCodecWrapper93   const LeAudioCodecConfiguration& GetLeAudioCodecConfiguration() const {
94     return source_codec_config;
95   }
96 
GetLeAudioCodecIdBroadcastCodecWrapper97   const types::LeAudioCodecId& GetLeAudioCodecId() const { return codec_id; }
98 
GetNumChannelsBroadcastCodecWrapper99   uint8_t GetNumChannels() const { return source_codec_config.num_channels; }
100 
GetBitrateBroadcastCodecWrapper101   uint32_t GetBitrate() const { return codec_bitrate; }
102 
GetFrameLenBroadcastCodecWrapper103   uint32_t GetFrameLen() const { return codec_frame_len; }
104 
GetBitsPerSampleBroadcastCodecWrapper105   uint8_t GetBitsPerSample() const {
106     return source_codec_config.bits_per_sample;
107   }
108 
GetSampleRateBroadcastCodecWrapper109   uint32_t GetSampleRate() const { return source_codec_config.sample_rate; }
110 
GetDataIntervalUsBroadcastCodecWrapper111   uint32_t GetDataIntervalUs() const {
112     return source_codec_config.data_interval_us;
113   }
114 
GetNumChannelsPerBisBroadcastCodecWrapper115   uint8_t GetNumChannelsPerBis() const {
116     // TODO: Need to handle each BIS has more than one channel case
117     return 1;
118   }
119 
120  private:
121   types::LeAudioCodecId codec_id;
122   LeAudioCodecConfiguration source_codec_config;
123   uint32_t codec_bitrate;
124   uint32_t codec_frame_len;
125   uint8_t blocks_per_sdu;
126 };
127 
128 std::ostream& operator<<(
129     std::ostream& os,
130     const le_audio::broadcaster::BroadcastCodecWrapper& config);
131 
132 struct BroadcastQosConfig {
BroadcastQosConfigBroadcastQosConfig133   BroadcastQosConfig(uint8_t retransmission_number,
134                      uint16_t max_transport_latency)
135       : retransmission_number(retransmission_number),
136         max_transport_latency(max_transport_latency) {}
137 
138   BroadcastQosConfig& operator=(const BroadcastQosConfig& other) {
139     retransmission_number = other.retransmission_number;
140     max_transport_latency = other.max_transport_latency;
141     return *this;
142   };
143 
getRetransmissionNumberBroadcastQosConfig144   uint8_t getRetransmissionNumber() const { return retransmission_number; }
getMaxTransportLatencyBroadcastQosConfig145   uint16_t getMaxTransportLatency() const { return max_transport_latency; }
146 
147  private:
148   uint8_t retransmission_number;
149   uint16_t max_transport_latency;
150 };
151 
152 std::ostream& operator<<(
153     std::ostream& os, const le_audio::broadcaster::BroadcastQosConfig& config);
154 
155 std::pair<const BroadcastCodecWrapper&, const BroadcastQosConfig&>
156 getStreamConfigForContext(types::AudioContexts context);
157 
158 }  // namespace broadcaster
159 }  // namespace le_audio
160 
161 /* BroadcastAnnouncements compare helper */
162 namespace bluetooth {
163 namespace le_audio {
164 bool operator==(const BasicAudioAnnouncementData& lhs,
165                 const BasicAudioAnnouncementData& rhs);
166 }  // namespace le_audio
167 }  // namespace bluetooth
168