1 /*
2  * Copyright 2021 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 "bluetooth-a2dp"
18 
19 #include "a2dp_vendor_opus_encoder.h"
20 
21 #include <bluetooth/log.h>
22 #include <opus.h>
23 #include <stdio.h>
24 #include <string.h>
25 
26 #include <cstdint>
27 
28 #include "a2dp_codec_api.h"
29 #include "a2dp_vendor_opus.h"
30 #include "a2dp_vendor_opus_constants.h"
31 #include "avdt_api.h"
32 #include "common/time_util.h"
33 #include "hardware/bt_av.h"
34 #include "internal_include/bt_target.h"
35 #include "opus_defines.h"
36 #include "opus_types.h"
37 #include "osi/include/allocator.h"
38 #include "stack/include/bt_hdr.h"
39 
40 using namespace bluetooth;
41 
42 typedef struct {
43   uint32_t sample_rate;
44   uint16_t bitrate;
45   uint16_t framesize;
46   uint8_t channel_mode;
47   uint8_t bits_per_sample;
48   uint8_t quality_mode_index;
49   int pcm_wlength;
50   uint8_t pcm_fmt;
51 } tA2DP_OPUS_ENCODER_PARAMS;
52 
53 typedef struct {
54   float counter;
55   uint32_t bytes_per_tick;
56   uint64_t last_frame_us;
57 } tA2DP_OPUS_FEEDING_STATE;
58 
59 typedef struct {
60   uint64_t session_start_us;
61 
62   size_t media_read_total_expected_packets;
63   size_t media_read_total_expected_reads_count;
64   size_t media_read_total_expected_read_bytes;
65 
66   size_t media_read_total_dropped_packets;
67   size_t media_read_total_actual_reads_count;
68   size_t media_read_total_actual_read_bytes;
69 } a2dp_opus_encoder_stats_t;
70 
71 typedef struct {
72   a2dp_source_read_callback_t read_callback;
73   a2dp_source_enqueue_callback_t enqueue_callback;
74   uint16_t TxAaMtuSize;
75   size_t TxQueueLength;
76 
77   bool use_SCMS_T;
78   bool is_peer_edr;          // True if the peer device supports EDR
79   bool peer_supports_3mbps;  // True if the peer device supports 3Mbps EDR
80   uint16_t peer_mtu;         // MTU of the A2DP peer
81   uint32_t timestamp;        // Timestamp for the A2DP frames
82 
83   OpusEncoder* opus_handle;
84   bool has_opus_handle;  // True if opus_handle is valid
85 
86   tA2DP_FEEDING_PARAMS feeding_params;
87   tA2DP_OPUS_ENCODER_PARAMS opus_encoder_params;
88   tA2DP_OPUS_FEEDING_STATE opus_feeding_state;
89 
90   a2dp_opus_encoder_stats_t stats;
91 } tA2DP_OPUS_ENCODER_CB;
92 
93 static tA2DP_OPUS_ENCODER_CB a2dp_opus_encoder_cb;
94 
95 static bool a2dp_vendor_opus_encoder_update(uint16_t peer_mtu, A2dpCodecConfig* a2dp_codec_config,
96                                             bool* p_restart_input, bool* p_restart_output,
97                                             bool* p_config_updated);
98 static void a2dp_opus_get_num_frame_iteration(uint8_t* num_of_iterations, uint8_t* num_of_frames,
99                                               uint64_t timestamp_us);
100 static void a2dp_opus_encode_frames(uint8_t nb_frame);
101 static bool a2dp_opus_read_feeding(uint8_t* read_buffer, uint32_t* bytes_read);
102 
a2dp_vendor_opus_encoder_cleanup(void)103 void a2dp_vendor_opus_encoder_cleanup(void) {
104   if (a2dp_opus_encoder_cb.has_opus_handle) {
105     osi_free(a2dp_opus_encoder_cb.opus_handle);
106     a2dp_opus_encoder_cb.has_opus_handle = false;
107     a2dp_opus_encoder_cb.opus_handle = nullptr;
108   }
109   memset(&a2dp_opus_encoder_cb, 0, sizeof(a2dp_opus_encoder_cb));
110 
111   a2dp_opus_encoder_cb.stats.session_start_us = bluetooth::common::time_get_os_boottime_us();
112 
113   a2dp_opus_encoder_cb.timestamp = 0;
114   a2dp_opus_encoder_cb.use_SCMS_T = false;
115 
116   return;
117 }
118 
a2dp_vendor_opus_encoder_init(const tA2DP_ENCODER_INIT_PEER_PARAMS * p_peer_params,A2dpCodecConfig * a2dp_codec_config,a2dp_source_read_callback_t read_callback,a2dp_source_enqueue_callback_t enqueue_callback)119 void a2dp_vendor_opus_encoder_init(const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params,
120                                    A2dpCodecConfig* a2dp_codec_config,
121                                    a2dp_source_read_callback_t read_callback,
122                                    a2dp_source_enqueue_callback_t enqueue_callback) {
123   uint32_t error_val;
124 
125   a2dp_vendor_opus_encoder_cleanup();
126 
127   a2dp_opus_encoder_cb.read_callback = read_callback;
128   a2dp_opus_encoder_cb.enqueue_callback = enqueue_callback;
129   a2dp_opus_encoder_cb.is_peer_edr = p_peer_params->is_peer_edr;
130   a2dp_opus_encoder_cb.peer_supports_3mbps = p_peer_params->peer_supports_3mbps;
131   a2dp_opus_encoder_cb.peer_mtu = p_peer_params->peer_mtu;
132 
133   // NOTE: Ignore the restart_input / restart_output flags - this initization
134   // happens when the connection is (re)started.
135   bool restart_input = false;
136   bool restart_output = false;
137   bool config_updated = false;
138 
139   uint32_t size = opus_encoder_get_size(A2DP_OPUS_CODEC_OUTPUT_CHS);
140   a2dp_opus_encoder_cb.opus_handle = static_cast<OpusEncoder*>(osi_malloc(size));
141   if (a2dp_opus_encoder_cb.opus_handle == nullptr) {
142     log::error("failed to allocate opus encoder handle");
143     return;
144   }
145 
146   error_val =
147           opus_encoder_init(a2dp_opus_encoder_cb.opus_handle, A2DP_OPUS_CODEC_DEFAULT_SAMPLERATE,
148                             A2DP_OPUS_CODEC_OUTPUT_CHS, OPUS_APPLICATION_AUDIO);
149 
150   if (error_val != OPUS_OK) {
151     log::error(
152             "failed to init opus encoder (handle size {}, sampling rate {}, output "
153             "chs {}, error {})",
154             size, A2DP_OPUS_CODEC_DEFAULT_SAMPLERATE, A2DP_OPUS_CODEC_OUTPUT_CHS, error_val);
155     osi_free(a2dp_opus_encoder_cb.opus_handle);
156     return;
157   } else {
158     a2dp_opus_encoder_cb.has_opus_handle = true;
159   }
160 
161   a2dp_vendor_opus_encoder_update(a2dp_opus_encoder_cb.peer_mtu, a2dp_codec_config, &restart_input,
162                                   &restart_output, &config_updated);
163 
164   return;
165 }
166 
updateEncoderUserConfig(const tA2DP_ENCODER_INIT_PEER_PARAMS *,bool * p_restart_input,bool * p_restart_output,bool * p_config_updated)167 bool A2dpCodecConfigOpusSource::updateEncoderUserConfig(
168         const tA2DP_ENCODER_INIT_PEER_PARAMS* /* p_peer_params */, bool* p_restart_input,
169         bool* p_restart_output, bool* p_config_updated) {
170   if (a2dp_opus_encoder_cb.peer_mtu == 0) {
171     log::error("Cannot update the codec encoder for {}: invalid peer MTU", name());
172     return false;
173   }
174 
175   return a2dp_vendor_opus_encoder_update(a2dp_opus_encoder_cb.peer_mtu, this, p_restart_input,
176                                          p_restart_output, p_config_updated);
177 }
178 
a2dp_vendor_opus_encoder_update(uint16_t peer_mtu,A2dpCodecConfig * a2dp_codec_config,bool * p_restart_input,bool * p_restart_output,bool * p_config_updated)179 static bool a2dp_vendor_opus_encoder_update(uint16_t peer_mtu, A2dpCodecConfig* a2dp_codec_config,
180                                             bool* p_restart_input, bool* p_restart_output,
181                                             bool* p_config_updated) {
182   tA2DP_OPUS_ENCODER_PARAMS* p_encoder_params = &a2dp_opus_encoder_cb.opus_encoder_params;
183   uint8_t codec_info[AVDT_CODEC_SIZE];
184   uint32_t error = 0;
185 
186   *p_restart_input = false;
187   *p_restart_output = false;
188   *p_config_updated = false;
189 
190   if (!a2dp_opus_encoder_cb.has_opus_handle || a2dp_opus_encoder_cb.opus_handle == NULL) {
191     log::error("Cannot get Opus encoder handle");
192     return false;
193   }
194   log::assert_that(a2dp_opus_encoder_cb.opus_handle != nullptr,
195                    "assert failed: a2dp_opus_encoder_cb.opus_handle != nullptr");
196 
197   if (!a2dp_codec_config->copyOutOtaCodecConfig(codec_info)) {
198     log::error("Cannot update the codec encoder for {}: invalid codec config",
199                a2dp_codec_config->name());
200     return false;
201   }
202   const uint8_t* p_codec_info = codec_info;
203   btav_a2dp_codec_config_t codec_config = a2dp_codec_config->getCodecConfig();
204 
205   // The feeding parameters
206   tA2DP_FEEDING_PARAMS* p_feeding_params = &a2dp_opus_encoder_cb.feeding_params;
207   p_feeding_params->sample_rate = A2DP_VendorGetTrackSampleRateOpus(p_codec_info);
208   p_feeding_params->bits_per_sample = a2dp_codec_config->getAudioBitsPerSample();
209   p_feeding_params->channel_count = A2DP_VendorGetTrackChannelCountOpus(p_codec_info);
210   log::info("sample_rate={} bits_per_sample={} channel_count={}", p_feeding_params->sample_rate,
211             p_feeding_params->bits_per_sample, p_feeding_params->channel_count);
212 
213   // The codec parameters
214   p_encoder_params->sample_rate = a2dp_opus_encoder_cb.feeding_params.sample_rate;
215   p_encoder_params->channel_mode = A2DP_VendorGetChannelModeCodeOpus(p_codec_info);
216   p_encoder_params->framesize = A2DP_VendorGetFrameSizeOpus(p_codec_info);
217   p_encoder_params->bitrate = A2DP_VendorGetBitRateOpus(p_codec_info);
218 
219   a2dp_vendor_opus_feeding_reset();
220 
221   uint16_t mtu_size = BT_DEFAULT_BUFFER_SIZE - A2DP_OPUS_OFFSET - sizeof(BT_HDR);
222   if (mtu_size < peer_mtu) {
223     a2dp_opus_encoder_cb.TxAaMtuSize = mtu_size;
224   } else {
225     a2dp_opus_encoder_cb.TxAaMtuSize = peer_mtu;
226   }
227 
228   // Set the bitrate quality mode index
229   if (codec_config.codec_specific_3 != 0) {
230     p_encoder_params->quality_mode_index = codec_config.codec_specific_3 % 10;
231     log::info("setting bitrate quality mode to {}", p_encoder_params->quality_mode_index);
232   } else {
233     p_encoder_params->quality_mode_index = 5;
234     log::info("setting bitrate quality mode to default {}", p_encoder_params->quality_mode_index);
235   }
236 
237   error = opus_encoder_ctl(a2dp_opus_encoder_cb.opus_handle,
238                            OPUS_SET_COMPLEXITY(p_encoder_params->quality_mode_index));
239 
240   if (error != OPUS_OK) {
241     log::error("failed to set encoder bitrate quality setting");
242     return false;
243   }
244 
245   p_encoder_params->pcm_wlength = a2dp_opus_encoder_cb.feeding_params.bits_per_sample >> 3;
246 
247   log::info("setting bitrate to {}", p_encoder_params->bitrate);
248   error = opus_encoder_ctl(a2dp_opus_encoder_cb.opus_handle,
249                            OPUS_SET_BITRATE(p_encoder_params->bitrate));
250 
251   if (error != OPUS_OK) {
252     log::error("failed to set encoder bitrate");
253     return false;
254   }
255 
256   // Set the Audio format from pcm_wlength
257   if (p_encoder_params->pcm_wlength == 2) {
258     p_encoder_params->pcm_fmt = 16;
259   } else if (p_encoder_params->pcm_wlength == 3) {
260     p_encoder_params->pcm_fmt = 24;
261   } else if (p_encoder_params->pcm_wlength == 4) {
262     p_encoder_params->pcm_fmt = 32;
263   }
264 
265   return true;
266 }
267 
a2dp_vendor_opus_feeding_reset(void)268 void a2dp_vendor_opus_feeding_reset(void) {
269   memset(&a2dp_opus_encoder_cb.opus_feeding_state, 0,
270          sizeof(a2dp_opus_encoder_cb.opus_feeding_state));
271 
272   a2dp_opus_encoder_cb.opus_feeding_state.bytes_per_tick =
273           (a2dp_opus_encoder_cb.feeding_params.sample_rate *
274            a2dp_opus_encoder_cb.feeding_params.bits_per_sample / 8 *
275            a2dp_opus_encoder_cb.feeding_params.channel_count *
276            a2dp_vendor_opus_get_encoder_interval_ms()) /
277           1000;
278 
279   return;
280 }
281 
a2dp_vendor_opus_feeding_flush(void)282 void a2dp_vendor_opus_feeding_flush(void) {
283   a2dp_opus_encoder_cb.opus_feeding_state.counter = 0.0f;
284 
285   return;
286 }
287 
a2dp_vendor_opus_get_encoder_interval_ms(void)288 uint64_t a2dp_vendor_opus_get_encoder_interval_ms(void) {
289   return (a2dp_opus_encoder_cb.opus_encoder_params.framesize * 1000) /
290          a2dp_opus_encoder_cb.opus_encoder_params.sample_rate;
291 }
292 
a2dp_vendor_opus_send_frames(uint64_t timestamp_us)293 void a2dp_vendor_opus_send_frames(uint64_t timestamp_us) {
294   uint8_t nb_frame = 0;
295   uint8_t nb_iterations = 0;
296 
297   a2dp_opus_get_num_frame_iteration(&nb_iterations, &nb_frame, timestamp_us);
298   if (nb_frame == 0) {
299     return;
300   }
301 
302   for (uint8_t counter = 0; counter < nb_iterations; counter++) {
303     // Transcode frame and enqueue
304     a2dp_opus_encode_frames(nb_frame);
305   }
306 
307   return;
308 }
309 
310 // Obtains the number of frames to send and number of iterations
311 // to be used. |num_of_iterations| and |num_of_frames| parameters
312 // are used as output param for returning the respective values.
a2dp_opus_get_num_frame_iteration(uint8_t * num_of_iterations,uint8_t * num_of_frames,uint64_t timestamp_us)313 static void a2dp_opus_get_num_frame_iteration(uint8_t* num_of_iterations, uint8_t* num_of_frames,
314                                               uint64_t timestamp_us) {
315   uint32_t result = 0;
316   uint8_t nof = 0;
317   uint8_t noi = 1;
318 
319   uint32_t pcm_bytes_per_frame = a2dp_opus_encoder_cb.opus_encoder_params.framesize *
320                                  a2dp_opus_encoder_cb.feeding_params.channel_count *
321                                  a2dp_opus_encoder_cb.feeding_params.bits_per_sample / 8;
322 
323   uint32_t us_this_tick = a2dp_vendor_opus_get_encoder_interval_ms() * 1000;
324   uint64_t now_us = timestamp_us;
325   if (a2dp_opus_encoder_cb.opus_feeding_state.last_frame_us != 0) {
326     us_this_tick = (now_us - a2dp_opus_encoder_cb.opus_feeding_state.last_frame_us);
327   }
328   a2dp_opus_encoder_cb.opus_feeding_state.last_frame_us = now_us;
329 
330   a2dp_opus_encoder_cb.opus_feeding_state.counter +=
331           (float)a2dp_opus_encoder_cb.opus_feeding_state.bytes_per_tick * us_this_tick /
332           (a2dp_vendor_opus_get_encoder_interval_ms() * 1000);
333 
334   result = a2dp_opus_encoder_cb.opus_feeding_state.counter / pcm_bytes_per_frame;
335   a2dp_opus_encoder_cb.opus_feeding_state.counter -= result * pcm_bytes_per_frame;
336   nof = result;
337 
338   *num_of_frames = nof;
339   *num_of_iterations = noi;
340 }
341 
a2dp_opus_encode_frames(uint8_t nb_frame)342 static void a2dp_opus_encode_frames(uint8_t nb_frame) {
343   tA2DP_OPUS_ENCODER_PARAMS* p_encoder_params = &a2dp_opus_encoder_cb.opus_encoder_params;
344   unsigned char* packet;
345   uint8_t remain_nb_frame = nb_frame;
346   uint16_t opus_frame_size = p_encoder_params->framesize;
347   uint8_t read_buffer[p_encoder_params->framesize * p_encoder_params->pcm_wlength *
348                       p_encoder_params->channel_mode];
349 
350   int32_t out_frames = 0;
351   int32_t written = 0;
352 
353   uint32_t bytes_read = 0;
354   while (nb_frame) {
355     BT_HDR* p_buf = (BT_HDR*)osi_malloc(BT_DEFAULT_BUFFER_SIZE);
356     p_buf->offset = A2DP_OPUS_OFFSET;
357     p_buf->len = 0;
358     p_buf->layer_specific = 0;
359     a2dp_opus_encoder_cb.stats.media_read_total_expected_packets++;
360 
361     do {
362       //
363       // Read the PCM data and encode it
364       //
365       uint32_t temp_bytes_read = 0;
366       if (a2dp_opus_read_feeding(read_buffer, &temp_bytes_read)) {
367         bytes_read += temp_bytes_read;
368         packet = (unsigned char*)(p_buf + 1) + p_buf->offset + p_buf->len;
369 
370         if (a2dp_opus_encoder_cb.opus_handle == NULL) {
371           log::error("invalid OPUS handle");
372           a2dp_opus_encoder_cb.stats.media_read_total_dropped_packets++;
373           osi_free(p_buf);
374           return;
375         }
376 
377         written = opus_encode(a2dp_opus_encoder_cb.opus_handle, (const opus_int16*)&read_buffer[0],
378                               opus_frame_size, packet, (BT_DEFAULT_BUFFER_SIZE - p_buf->offset));
379 
380         if (written <= 0) {
381           log::error("OPUS encoding error");
382           a2dp_opus_encoder_cb.stats.media_read_total_dropped_packets++;
383           osi_free(p_buf);
384           return;
385         } else {
386           out_frames++;
387         }
388         p_buf->len += written;
389         nb_frame--;
390         p_buf->layer_specific += out_frames;  // added a frame to the buffer
391       } else {
392         log::warn("Opus src buffer underflow {}", nb_frame);
393         a2dp_opus_encoder_cb.opus_feeding_state.counter +=
394                 nb_frame * opus_frame_size * a2dp_opus_encoder_cb.feeding_params.channel_count *
395                 a2dp_opus_encoder_cb.feeding_params.bits_per_sample / 8;
396 
397         // no more pcm to read
398         nb_frame = 0;
399       }
400     } while ((written == 0) && nb_frame);
401 
402     if (p_buf->len) {
403       /*
404        * Timestamp of the media packet header represent the TS of the
405        * first frame, i.e. the timestamp before including this frame.
406        */
407       *((uint32_t*)(p_buf + 1)) = a2dp_opus_encoder_cb.timestamp;
408 
409       // Timestamp will wrap over to 0 if stream continues on long enough
410       // (>25H @ 48KHz). The parameters are promoted to 64bit to ensure that
411       // no unsigned overflow is triggered as ubsan is always enabled.
412       a2dp_opus_encoder_cb.timestamp = ((uint64_t)a2dp_opus_encoder_cb.timestamp +
413                                         (p_buf->layer_specific * opus_frame_size)) &
414                                        UINT32_MAX;
415 
416       uint8_t done_nb_frame = remain_nb_frame - nb_frame;
417       remain_nb_frame = nb_frame;
418 
419       if (!a2dp_opus_encoder_cb.enqueue_callback(p_buf, done_nb_frame, bytes_read)) {
420         return;
421       }
422     } else {
423       a2dp_opus_encoder_cb.stats.media_read_total_dropped_packets++;
424       osi_free(p_buf);
425     }
426   }
427 }
428 
a2dp_opus_read_feeding(uint8_t * read_buffer,uint32_t * bytes_read)429 static bool a2dp_opus_read_feeding(uint8_t* read_buffer, uint32_t* bytes_read) {
430   uint32_t read_size = a2dp_opus_encoder_cb.opus_encoder_params.framesize *
431                        a2dp_opus_encoder_cb.feeding_params.channel_count *
432                        a2dp_opus_encoder_cb.feeding_params.bits_per_sample / 8;
433 
434   a2dp_opus_encoder_cb.stats.media_read_total_expected_reads_count++;
435   a2dp_opus_encoder_cb.stats.media_read_total_expected_read_bytes += read_size;
436 
437   /* Read Data from UIPC channel */
438   uint32_t nb_byte_read = a2dp_opus_encoder_cb.read_callback(read_buffer, read_size);
439   a2dp_opus_encoder_cb.stats.media_read_total_actual_read_bytes += nb_byte_read;
440 
441   if (nb_byte_read < read_size) {
442     if (nb_byte_read == 0) {
443       return false;
444     }
445 
446     /* Fill the unfilled part of the read buffer with silence (0) */
447     memset(((uint8_t*)read_buffer) + nb_byte_read, 0, read_size - nb_byte_read);
448     nb_byte_read = read_size;
449   }
450   a2dp_opus_encoder_cb.stats.media_read_total_actual_reads_count++;
451 
452   *bytes_read = nb_byte_read;
453   return true;
454 }
455 
a2dp_vendor_opus_set_transmit_queue_length(size_t transmit_queue_length)456 void a2dp_vendor_opus_set_transmit_queue_length(size_t transmit_queue_length) {
457   a2dp_opus_encoder_cb.TxQueueLength = transmit_queue_length;
458 
459   return;
460 }
461 
encoderIntervalMs() const462 uint64_t A2dpCodecConfigOpusSource::encoderIntervalMs() const {
463   return a2dp_vendor_opus_get_encoder_interval_ms();
464 }
465 
a2dp_vendor_opus_get_effective_frame_size()466 int a2dp_vendor_opus_get_effective_frame_size() { return a2dp_opus_encoder_cb.TxAaMtuSize; }
467 
debug_codec_dump(int fd)468 void A2dpCodecConfigOpusSource::debug_codec_dump(int fd) {
469   a2dp_opus_encoder_stats_t* stats = &a2dp_opus_encoder_cb.stats;
470   tA2DP_OPUS_ENCODER_PARAMS* p_encoder_params = &a2dp_opus_encoder_cb.opus_encoder_params;
471 
472   A2dpCodecConfig::debug_codec_dump(fd);
473 
474   dprintf(fd,
475           "  Packet counts (expected/dropped)                        : %zu / "
476           "%zu\n",
477           stats->media_read_total_expected_packets, stats->media_read_total_dropped_packets);
478 
479   dprintf(fd,
480           "  PCM read counts (expected/actual)                       : %zu / "
481           "%zu\n",
482           stats->media_read_total_expected_reads_count, stats->media_read_total_actual_reads_count);
483 
484   dprintf(fd,
485           "  PCM read bytes (expected/actual)                        : %zu / "
486           "%zu\n",
487           stats->media_read_total_expected_read_bytes, stats->media_read_total_actual_read_bytes);
488 
489   dprintf(fd, "  OPUS transmission bitrate (Kbps)                        : %d\n",
490           p_encoder_params->bitrate);
491 
492   dprintf(fd, "  OPUS saved transmit queue length                        : %zu\n",
493           a2dp_opus_encoder_cb.TxQueueLength);
494 
495   return;
496 }
497