1 /*
2  * Copyright 2016 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 "a2dp_vendor_aptx_encoder"
18 
19 #include "a2dp_vendor_aptx_encoder.h"
20 
21 #include <dlfcn.h>
22 #include <inttypes.h>
23 #include <stdio.h>
24 #include <string.h>
25 
26 #include "a2dp_vendor.h"
27 #include "a2dp_vendor_aptx.h"
28 #include "bt_common.h"
29 #include "common/time_util.h"
30 #include "osi/include/log.h"
31 #include "osi/include/osi.h"
32 
33 //
34 // Encoder for aptX Source Codec
35 //
36 
37 //
38 // The aptX encoder shared library, and the functions to use
39 //
40 static const char* APTX_ENCODER_LIB_NAME = "libaptX_encoder.so";
41 static void* aptx_encoder_lib_handle = NULL;
42 
43 static const char* APTX_ENCODER_INIT_NAME = "aptxbtenc_init";
44 typedef int (*tAPTX_ENCODER_INIT)(void* state, short endian);
45 
46 static const char* APTX_ENCODER_ENCODE_STEREO_NAME = "aptxbtenc_encodestereo";
47 typedef int (*tAPTX_ENCODER_ENCODE_STEREO)(void* state, void* pcmL, void* pcmR,
48                                            void* buffer);
49 
50 static const char* APTX_ENCODER_SIZEOF_PARAMS_NAME = "SizeofAptxbtenc";
51 typedef int (*tAPTX_ENCODER_SIZEOF_PARAMS)(void);
52 
53 static tAPTX_ENCODER_INIT aptx_encoder_init_func;
54 static tAPTX_ENCODER_ENCODE_STEREO aptx_encoder_encode_stereo_func;
55 static tAPTX_ENCODER_SIZEOF_PARAMS aptx_encoder_sizeof_params_func;
56 
57 // offset
58 #if (BTA_AV_CO_CP_SCMS_T == TRUE)
59 #define A2DP_APTX_OFFSET (AVDT_MEDIA_OFFSET + 1)
60 #else
61 // no RTP header for aptX classic
62 #define A2DP_APTX_OFFSET (AVDT_MEDIA_OFFSET - AVDT_MEDIA_HDR_SIZE)
63 #endif
64 
65 #define A2DP_APTX_MAX_PCM_BYTES_PER_READ 4096
66 
67 typedef struct {
68   uint64_t sleep_time_ns;
69   uint32_t pcm_reads;
70   uint32_t pcm_bytes_per_read;
71   uint32_t aptx_bytes;
72   uint32_t frame_size_counter;
73 } tAPTX_FRAMING_PARAMS;
74 
75 typedef struct {
76   uint64_t session_start_us;
77 
78   size_t media_read_total_expected_packets;
79   size_t media_read_total_expected_reads_count;
80   size_t media_read_total_expected_read_bytes;
81 
82   size_t media_read_total_dropped_packets;
83   size_t media_read_total_actual_reads_count;
84   size_t media_read_total_actual_read_bytes;
85 } a2dp_aptx_encoder_stats_t;
86 
87 typedef struct {
88   a2dp_source_read_callback_t read_callback;
89   a2dp_source_enqueue_callback_t enqueue_callback;
90 
91   bool use_SCMS_T;
92   bool is_peer_edr;          // True if the peer device supports EDR
93   bool peer_supports_3mbps;  // True if the peer device supports 3Mbps EDR
94   uint16_t peer_mtu;         // MTU of the A2DP peer
95   uint32_t timestamp;        // Timestamp for the A2DP frames
96 
97   tA2DP_FEEDING_PARAMS feeding_params;
98   tAPTX_FRAMING_PARAMS framing_params;
99   void* aptx_encoder_state;
100   a2dp_aptx_encoder_stats_t stats;
101 } tA2DP_APTX_ENCODER_CB;
102 
103 static tA2DP_APTX_ENCODER_CB a2dp_aptx_encoder_cb;
104 
105 static void a2dp_vendor_aptx_encoder_update(uint16_t peer_mtu,
106                                             A2dpCodecConfig* a2dp_codec_config,
107                                             bool* p_restart_input,
108                                             bool* p_restart_output,
109                                             bool* p_config_updated);
110 static void aptx_init_framing_params(tAPTX_FRAMING_PARAMS* framing_params);
111 static void aptx_update_framing_params(tAPTX_FRAMING_PARAMS* framing_params);
112 static size_t aptx_encode_16bit(tAPTX_FRAMING_PARAMS* framing_params,
113                                 size_t* data_out_index, uint16_t* data16_in,
114                                 uint8_t* data_out);
115 
A2DP_VendorLoadEncoderAptx(void)116 bool A2DP_VendorLoadEncoderAptx(void) {
117   if (aptx_encoder_lib_handle != NULL) return true;  // Already loaded
118 
119   // Open the encoder library
120   aptx_encoder_lib_handle = dlopen(APTX_ENCODER_LIB_NAME, RTLD_NOW);
121   if (aptx_encoder_lib_handle == NULL) {
122     LOG_ERROR("%s: cannot open aptX encoder library %s: %s", __func__,
123               APTX_ENCODER_LIB_NAME, dlerror());
124     return false;
125   }
126 
127   aptx_encoder_init_func = (tAPTX_ENCODER_INIT)dlsym(aptx_encoder_lib_handle,
128                                                      APTX_ENCODER_INIT_NAME);
129   if (aptx_encoder_init_func == NULL) {
130     LOG_ERROR("%s: cannot find function '%s' in the encoder library: %s",
131               __func__, APTX_ENCODER_INIT_NAME, dlerror());
132     A2DP_VendorUnloadEncoderAptx();
133     return false;
134   }
135 
136   aptx_encoder_encode_stereo_func = (tAPTX_ENCODER_ENCODE_STEREO)dlsym(
137       aptx_encoder_lib_handle, APTX_ENCODER_ENCODE_STEREO_NAME);
138   if (aptx_encoder_encode_stereo_func == NULL) {
139     LOG_ERROR("%s: cannot find function '%s' in the encoder library: %s",
140               __func__, APTX_ENCODER_ENCODE_STEREO_NAME, dlerror());
141     A2DP_VendorUnloadEncoderAptx();
142     return false;
143   }
144 
145   aptx_encoder_sizeof_params_func = (tAPTX_ENCODER_SIZEOF_PARAMS)dlsym(
146       aptx_encoder_lib_handle, APTX_ENCODER_SIZEOF_PARAMS_NAME);
147   if (aptx_encoder_sizeof_params_func == NULL) {
148     LOG_ERROR("%s: cannot find function '%s' in the encoder library: %s",
149               __func__, APTX_ENCODER_SIZEOF_PARAMS_NAME, dlerror());
150     A2DP_VendorUnloadEncoderAptx();
151     return false;
152   }
153 
154   return true;
155 }
156 
A2DP_VendorUnloadEncoderAptx(void)157 void A2DP_VendorUnloadEncoderAptx(void) {
158   aptx_encoder_init_func = NULL;
159   aptx_encoder_encode_stereo_func = NULL;
160   aptx_encoder_sizeof_params_func = NULL;
161 
162   if (aptx_encoder_lib_handle != NULL) {
163     dlclose(aptx_encoder_lib_handle);
164     aptx_encoder_lib_handle = NULL;
165   }
166 }
167 
a2dp_vendor_aptx_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)168 void a2dp_vendor_aptx_encoder_init(
169     const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params,
170     A2dpCodecConfig* a2dp_codec_config,
171     a2dp_source_read_callback_t read_callback,
172     a2dp_source_enqueue_callback_t enqueue_callback) {
173   memset(&a2dp_aptx_encoder_cb, 0, sizeof(a2dp_aptx_encoder_cb));
174 
175   a2dp_aptx_encoder_cb.stats.session_start_us =
176       bluetooth::common::time_get_os_boottime_us();
177 
178   a2dp_aptx_encoder_cb.read_callback = read_callback;
179   a2dp_aptx_encoder_cb.enqueue_callback = enqueue_callback;
180   a2dp_aptx_encoder_cb.is_peer_edr = p_peer_params->is_peer_edr;
181   a2dp_aptx_encoder_cb.peer_supports_3mbps = p_peer_params->peer_supports_3mbps;
182   a2dp_aptx_encoder_cb.peer_mtu = p_peer_params->peer_mtu;
183   a2dp_aptx_encoder_cb.timestamp = 0;
184 
185   /* aptX encoder config */
186   a2dp_aptx_encoder_cb.use_SCMS_T = false;  // TODO: should be a parameter
187 #if (BTA_AV_CO_CP_SCMS_T == TRUE)
188   a2dp_aptx_encoder_cb.use_SCMS_T = true;
189 #endif
190 
191   a2dp_aptx_encoder_cb.aptx_encoder_state =
192       osi_malloc(aptx_encoder_sizeof_params_func());
193   if (a2dp_aptx_encoder_cb.aptx_encoder_state != NULL) {
194     aptx_encoder_init_func(a2dp_aptx_encoder_cb.aptx_encoder_state, 0);
195   } else {
196     LOG_ERROR("%s: Cannot allocate aptX encoder state", __func__);
197     // TODO: Return an error?
198   }
199 
200   // NOTE: Ignore the restart_input / restart_output flags - this initization
201   // happens when the connection is (re)started.
202   bool restart_input = false;
203   bool restart_output = false;
204   bool config_updated = false;
205   a2dp_vendor_aptx_encoder_update(a2dp_aptx_encoder_cb.peer_mtu,
206                                   a2dp_codec_config, &restart_input,
207                                   &restart_output, &config_updated);
208 }
209 
updateEncoderUserConfig(const tA2DP_ENCODER_INIT_PEER_PARAMS * p_peer_params,bool * p_restart_input,bool * p_restart_output,bool * p_config_updated)210 bool A2dpCodecConfigAptx::updateEncoderUserConfig(
211     const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params, bool* p_restart_input,
212     bool* p_restart_output, bool* p_config_updated) {
213   a2dp_aptx_encoder_cb.is_peer_edr = p_peer_params->is_peer_edr;
214   a2dp_aptx_encoder_cb.peer_supports_3mbps = p_peer_params->peer_supports_3mbps;
215   a2dp_aptx_encoder_cb.peer_mtu = p_peer_params->peer_mtu;
216   a2dp_aptx_encoder_cb.timestamp = 0;
217 
218   if (a2dp_aptx_encoder_cb.peer_mtu == 0) {
219     LOG_ERROR(
220         "%s: Cannot update the codec encoder for %s: "
221         "invalid peer MTU",
222         __func__, name().c_str());
223     return false;
224   }
225 
226   a2dp_vendor_aptx_encoder_update(a2dp_aptx_encoder_cb.peer_mtu, this,
227                                   p_restart_input, p_restart_output,
228                                   p_config_updated);
229   return true;
230 }
231 
232 // Update the A2DP aptX encoder.
233 // |peer_mtu| is the peer MTU.
234 // |a2dp_codec_config| is the A2DP codec to use for the update.
a2dp_vendor_aptx_encoder_update(uint16_t peer_mtu,A2dpCodecConfig * a2dp_codec_config,bool * p_restart_input,bool * p_restart_output,bool * p_config_updated)235 static void a2dp_vendor_aptx_encoder_update(uint16_t peer_mtu,
236                                             A2dpCodecConfig* a2dp_codec_config,
237                                             bool* p_restart_input,
238                                             bool* p_restart_output,
239                                             bool* p_config_updated) {
240   uint8_t codec_info[AVDT_CODEC_SIZE];
241 
242   *p_restart_input = false;
243   *p_restart_output = false;
244   *p_config_updated = false;
245   if (!a2dp_codec_config->copyOutOtaCodecConfig(codec_info)) {
246     LOG_ERROR(
247         "%s: Cannot update the codec encoder for %s: "
248         "invalid codec config",
249         __func__, a2dp_codec_config->name().c_str());
250     return;
251   }
252   const uint8_t* p_codec_info = codec_info;
253 
254   // The feeding parameters
255   tA2DP_FEEDING_PARAMS* p_feeding_params = &a2dp_aptx_encoder_cb.feeding_params;
256   p_feeding_params->sample_rate =
257       A2DP_VendorGetTrackSampleRateAptx(p_codec_info);
258   p_feeding_params->bits_per_sample =
259       a2dp_codec_config->getAudioBitsPerSample();
260   p_feeding_params->channel_count =
261       A2DP_VendorGetTrackChannelCountAptx(p_codec_info);
262   LOG_DEBUG("%s: sample_rate=%u bits_per_sample=%u channel_count=%u", __func__,
263             p_feeding_params->sample_rate, p_feeding_params->bits_per_sample,
264             p_feeding_params->channel_count);
265   a2dp_vendor_aptx_feeding_reset();
266 }
267 
a2dp_vendor_aptx_encoder_cleanup(void)268 void a2dp_vendor_aptx_encoder_cleanup(void) {
269   osi_free(a2dp_aptx_encoder_cb.aptx_encoder_state);
270   memset(&a2dp_aptx_encoder_cb, 0, sizeof(a2dp_aptx_encoder_cb));
271 }
272 
273 //
274 // Initialize the framing parameters, and set those that don't change
275 // while streaming (e.g., 'sleep_time_ns').
276 //
aptx_init_framing_params(tAPTX_FRAMING_PARAMS * framing_params)277 static void aptx_init_framing_params(tAPTX_FRAMING_PARAMS* framing_params) {
278   framing_params->sleep_time_ns = 0;
279   framing_params->pcm_reads = 0;
280   framing_params->pcm_bytes_per_read = 0;
281   framing_params->aptx_bytes = 0;
282   framing_params->frame_size_counter = 0;
283 
284   if (a2dp_aptx_encoder_cb.feeding_params.sample_rate == 48000) {
285     if (a2dp_aptx_encoder_cb.use_SCMS_T) {
286       framing_params->sleep_time_ns = 13000000;
287     } else {
288       framing_params->sleep_time_ns = 14000000;
289     }
290   } else {
291     // Assume the sample rate is 44100
292     if (a2dp_aptx_encoder_cb.use_SCMS_T) {
293       framing_params->sleep_time_ns = 14000000;
294     } else {
295       framing_params->sleep_time_ns = 15000000;
296     }
297   }
298 
299   LOG_DEBUG("%s: sleep_time_ns = %" PRIu64, __func__,
300             framing_params->sleep_time_ns);
301 }
302 
303 //
304 // Set frame size and transmission interval needed to stream the required
305 // sample rate using 2-DH5 packets for aptX and 2-DH3 packets for aptX-LL.
306 // With SCMS-T enabled we need to reserve room for extra headers added later.
307 // Packets are always sent at equals time intervals but to achieve the
308 // required sample rate, the frame size needs to change on occasion.
309 //
310 // Also need to specify how many of the required PCM samples are read at a
311 // time:
312 //     aptx_bytes = pcm_reads * pcm_bytes_per_read / 4
313 // and
314 //     number of aptX samples produced = pcm_bytes_per_read / 16
315 //
aptx_update_framing_params(tAPTX_FRAMING_PARAMS * framing_params)316 static void aptx_update_framing_params(tAPTX_FRAMING_PARAMS* framing_params) {
317   if (a2dp_aptx_encoder_cb.feeding_params.sample_rate == 48000) {
318     if (a2dp_aptx_encoder_cb.use_SCMS_T) {
319       framing_params->aptx_bytes = 624;
320       framing_params->pcm_bytes_per_read = 208;
321       framing_params->pcm_reads = 12;
322     } else {
323       framing_params->aptx_bytes = 672;
324       framing_params->pcm_bytes_per_read = 224;
325       framing_params->pcm_reads = 12;
326     }
327   } else {
328     // Assume the sample rate is 44100
329     if (a2dp_aptx_encoder_cb.use_SCMS_T) {
330       if (++framing_params->frame_size_counter < 20) {
331         framing_params->aptx_bytes = 616;
332         framing_params->pcm_bytes_per_read = 224;
333         framing_params->pcm_reads = 11;
334       } else {
335         framing_params->aptx_bytes = 644;
336         framing_params->pcm_bytes_per_read = 368;
337         framing_params->pcm_reads = 7;
338         framing_params->frame_size_counter = 0;
339       }
340     } else {
341       if (++framing_params->frame_size_counter < 8) {
342         framing_params->aptx_bytes = 660;
343         framing_params->pcm_bytes_per_read = 240;
344         framing_params->pcm_reads = 11;
345       } else {
346         framing_params->aptx_bytes = 672;
347         framing_params->pcm_bytes_per_read = 224;
348         framing_params->pcm_reads = 12;
349         framing_params->frame_size_counter = 0;
350       }
351     }
352   }
353 
354   LOG_VERBOSE("%s: sleep_time_ns = %" PRIu64
355               " aptx_bytes = %u "
356               "pcm_bytes_per_read = %u pcm_reads = %u frame_size_counter = %u",
357               __func__, framing_params->sleep_time_ns,
358               framing_params->aptx_bytes, framing_params->pcm_bytes_per_read,
359               framing_params->pcm_reads, framing_params->frame_size_counter);
360 }
361 
a2dp_vendor_aptx_feeding_reset(void)362 void a2dp_vendor_aptx_feeding_reset(void) {
363   aptx_init_framing_params(&a2dp_aptx_encoder_cb.framing_params);
364 }
365 
a2dp_vendor_aptx_feeding_flush(void)366 void a2dp_vendor_aptx_feeding_flush(void) {
367   aptx_init_framing_params(&a2dp_aptx_encoder_cb.framing_params);
368 }
369 
a2dp_vendor_aptx_get_encoder_interval_ms(void)370 uint64_t a2dp_vendor_aptx_get_encoder_interval_ms(void) {
371   return a2dp_aptx_encoder_cb.framing_params.sleep_time_ns / (1000 * 1000);
372 }
373 
a2dp_vendor_aptx_send_frames(uint64_t timestamp_us)374 void a2dp_vendor_aptx_send_frames(uint64_t timestamp_us) {
375   tAPTX_FRAMING_PARAMS* framing_params = &a2dp_aptx_encoder_cb.framing_params;
376 
377   // Prepare the packet to send
378   BT_HDR* p_buf = (BT_HDR*)osi_malloc(BT_DEFAULT_BUFFER_SIZE);
379   p_buf->offset = A2DP_APTX_OFFSET;
380   p_buf->len = 0;
381   p_buf->layer_specific = 0;
382 
383   uint8_t* encoded_ptr = (uint8_t*)(p_buf + 1);
384   encoded_ptr += p_buf->offset;
385 
386   aptx_update_framing_params(framing_params);
387 
388   //
389   // Read the PCM data and encode it
390   //
391   uint16_t read_buffer16[A2DP_APTX_MAX_PCM_BYTES_PER_READ / sizeof(uint16_t)];
392   uint32_t expected_read_bytes =
393       framing_params->pcm_reads * framing_params->pcm_bytes_per_read;
394   size_t encoded_ptr_index = 0;
395   size_t pcm_bytes_encoded = 0;
396   uint32_t bytes_read = 0;
397 
398   a2dp_aptx_encoder_cb.stats.media_read_total_expected_packets++;
399   a2dp_aptx_encoder_cb.stats.media_read_total_expected_reads_count++;
400   a2dp_aptx_encoder_cb.stats.media_read_total_expected_read_bytes +=
401       expected_read_bytes;
402 
403   LOG_VERBOSE("%s: PCM read of size %u", __func__, expected_read_bytes);
404   bytes_read = a2dp_aptx_encoder_cb.read_callback((uint8_t*)read_buffer16,
405                                                   expected_read_bytes);
406   a2dp_aptx_encoder_cb.stats.media_read_total_actual_read_bytes += bytes_read;
407   if (bytes_read < expected_read_bytes) {
408     LOG_WARN("%s: underflow at PCM reading: read %u bytes instead of %u",
409              __func__, bytes_read, expected_read_bytes);
410     a2dp_aptx_encoder_cb.stats.media_read_total_dropped_packets++;
411     osi_free(p_buf);
412     return;
413   }
414   a2dp_aptx_encoder_cb.stats.media_read_total_actual_reads_count++;
415 
416   for (uint32_t reads = 0, offset = 0; reads < framing_params->pcm_reads;
417        reads++, offset +=
418                 (framing_params->pcm_bytes_per_read / sizeof(uint16_t))) {
419     pcm_bytes_encoded += aptx_encode_16bit(framing_params, &encoded_ptr_index,
420                                            read_buffer16 + offset, encoded_ptr);
421   }
422 
423   // Compute the number of encoded bytes
424   const int COMPRESSION_RATIO = 4;
425   size_t encoded_bytes = pcm_bytes_encoded / COMPRESSION_RATIO;
426   p_buf->len += encoded_bytes;
427   LOG_VERBOSE("%s: encoded %zu PCM bytes to %zu", __func__, pcm_bytes_encoded,
428               encoded_bytes);
429 
430   // Update the RTP timestamp
431   *((uint32_t*)(p_buf + 1)) = a2dp_aptx_encoder_cb.timestamp;
432   const uint8_t BYTES_PER_FRAME = 2;
433   uint32_t rtp_timestamp =
434       (pcm_bytes_encoded / a2dp_aptx_encoder_cb.feeding_params.channel_count) /
435       BYTES_PER_FRAME;
436   a2dp_aptx_encoder_cb.timestamp += rtp_timestamp;
437 
438   if (p_buf->len > 0) {
439     a2dp_aptx_encoder_cb.enqueue_callback(p_buf, 1, bytes_read);
440   } else {
441     a2dp_aptx_encoder_cb.stats.media_read_total_dropped_packets++;
442     osi_free(p_buf);
443   }
444 }
445 
aptx_encode_16bit(tAPTX_FRAMING_PARAMS * framing_params,size_t * data_out_index,uint16_t * data16_in,uint8_t * data_out)446 static size_t aptx_encode_16bit(tAPTX_FRAMING_PARAMS* framing_params,
447                                 size_t* data_out_index, uint16_t* data16_in,
448                                 uint8_t* data_out) {
449   size_t pcm_bytes_encoded = 0;
450   size_t frame = 0;
451 
452   for (size_t aptx_samples = 0;
453        aptx_samples < framing_params->pcm_bytes_per_read / 16; aptx_samples++) {
454     uint32_t pcmL[4];
455     uint32_t pcmR[4];
456     uint16_t encoded_sample[2];
457 
458     for (size_t i = 0, j = frame; i < 4; i++, j++) {
459       pcmL[i] = (uint16_t) * (data16_in + (2 * j));
460       pcmR[i] = (uint16_t) * (data16_in + ((2 * j) + 1));
461     }
462 
463     aptx_encoder_encode_stereo_func(a2dp_aptx_encoder_cb.aptx_encoder_state,
464                                     &pcmL, &pcmR, &encoded_sample);
465 
466     data_out[*data_out_index + 0] = (uint8_t)((encoded_sample[0] >> 8) & 0xff);
467     data_out[*data_out_index + 1] = (uint8_t)((encoded_sample[0] >> 0) & 0xff);
468     data_out[*data_out_index + 2] = (uint8_t)((encoded_sample[1] >> 8) & 0xff);
469     data_out[*data_out_index + 3] = (uint8_t)((encoded_sample[1] >> 0) & 0xff);
470 
471     frame += 4;
472     pcm_bytes_encoded += 16;
473     *data_out_index += 4;
474   }
475 
476   return pcm_bytes_encoded;
477 }
478 
encoderIntervalMs() const479 uint64_t A2dpCodecConfigAptx::encoderIntervalMs() const {
480   return a2dp_vendor_aptx_get_encoder_interval_ms();
481 }
482 
getEffectiveMtu() const483 int A2dpCodecConfigAptx::getEffectiveMtu() const {
484   return a2dp_aptx_encoder_cb.peer_mtu;
485 }
486 
debug_codec_dump(int fd)487 void A2dpCodecConfigAptx::debug_codec_dump(int fd) {
488   a2dp_aptx_encoder_stats_t* stats = &a2dp_aptx_encoder_cb.stats;
489 
490   A2dpCodecConfig::debug_codec_dump(fd);
491 
492   dprintf(fd,
493           "  Packet counts (expected/dropped)                        : %zu / "
494           "%zu\n",
495           stats->media_read_total_expected_packets,
496           stats->media_read_total_dropped_packets);
497 
498   dprintf(fd,
499           "  PCM read counts (expected/actual)                       : %zu / "
500           "%zu\n",
501           stats->media_read_total_expected_reads_count,
502           stats->media_read_total_actual_reads_count);
503 
504   dprintf(fd,
505           "  PCM read bytes (expected/actual)                        : %zu / "
506           "%zu\n",
507           stats->media_read_total_expected_read_bytes,
508           stats->media_read_total_actual_read_bytes);
509 }
510