1 /***
2 This file is part of PulseAudio.
3
4 PulseAudio is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
8
9 PulseAudio is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
16 ***/
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20
21 #include <pulsecore/core.h>
22 #include "bt-codec-api.h"
23
24 typedef struct codec_info {
25 pa_sample_spec sample_spec;
26 } codec_info_t;
27
init(bool for_encoding,bool for_backchannel,const uint8_t * config_buffer,uint8_t config_size,pa_sample_spec * sample_spec,pa_core * core)28 static void *init(bool for_encoding, bool for_backchannel, const uint8_t *config_buffer, uint8_t config_size, pa_sample_spec *sample_spec, pa_core *core) {
29 codec_info_t *info;
30
31 info = pa_xnew0(codec_info_t, 1);
32
33 info->sample_spec.format = PA_SAMPLE_S16LE;
34 info->sample_spec.channels = 1;
35 info->sample_spec.rate = 8000;
36
37 *sample_spec = info->sample_spec;
38
39 return info;
40 }
41
deinit(void * codec_info)42 static void deinit(void *codec_info) {
43 pa_xfree(codec_info);
44 }
45
reset(void * codec_info)46 static int reset(void *codec_info) {
47 return 0;
48 }
49
get_block_size(void * codec_info,size_t link_mtu)50 static size_t get_block_size(void *codec_info, size_t link_mtu) {
51 codec_info_t *info = (codec_info_t *) codec_info;
52 size_t block_size = link_mtu;
53
54 if (!pa_frame_aligned(block_size, &info->sample_spec)) {
55 pa_log_debug("Got invalid block size: %lu, rounding down", block_size);
56 block_size = pa_frame_align(block_size, &info->sample_spec);
57 }
58
59 return block_size;
60 }
61
get_encoded_block_size(void * codec_info,size_t input_size)62 static size_t get_encoded_block_size(void *codec_info, size_t input_size) {
63 codec_info_t *info = (codec_info_t *) codec_info;
64
65 /* input size should be aligned to sample spec */
66 pa_assert_fp(pa_frame_aligned(input_size, &info->sample_spec));
67
68 return input_size;
69 }
70
reduce_encoder_bitrate(void * codec_info,size_t write_link_mtu)71 static size_t reduce_encoder_bitrate(void *codec_info, size_t write_link_mtu) {
72 return 0;
73 }
74
increase_encoder_bitrate(void * codec_info,size_t write_link_mtu)75 static size_t increase_encoder_bitrate(void *codec_info, size_t write_link_mtu) {
76 return 0;
77 }
78
encode_buffer(void * codec_info,uint32_t timestamp,const uint8_t * input_buffer,size_t input_size,uint8_t * output_buffer,size_t output_size,size_t * processed)79 static size_t encode_buffer(void *codec_info, uint32_t timestamp, const uint8_t *input_buffer, size_t input_size, uint8_t *output_buffer, size_t output_size, size_t *processed) {
80 pa_assert(input_size <= output_size);
81
82 memcpy(output_buffer, input_buffer, input_size);
83 *processed = input_size;
84
85 return input_size;
86 }
87
decode_buffer(void * codec_info,const uint8_t * input_buffer,size_t input_size,uint8_t * output_buffer,size_t output_size,size_t * processed)88 static size_t decode_buffer(void *codec_info, const uint8_t *input_buffer, size_t input_size, uint8_t *output_buffer, size_t output_size, size_t *processed) {
89 codec_info_t *info = (codec_info_t *) codec_info;
90
91 *processed = input_size;
92
93 /* In some rare occasions, we might receive packets of a very strange
94 * size. This could potentially be possible if the SCO packet was
95 * received partially over-the-air, or more probably due to hardware
96 * issues in our Bluetooth adapter. In these cases, in order to avoid
97 * an assertion failure due to unaligned data, just discard the whole
98 * packet */
99 if (!pa_frame_aligned(input_size, &info->sample_spec)) {
100 pa_log_warn("SCO packet received of unaligned size: %zu", input_size);
101 return 0;
102 }
103
104 memcpy(output_buffer, input_buffer, input_size);
105
106 return input_size;
107 }
108
109 /* dummy passthrough codec used with HSP/HFP CVSD */
110 const pa_bt_codec pa_bt_codec_cvsd = {
111 .name = "CVSD",
112 .description = "CVSD",
113 .init = init,
114 .deinit = deinit,
115 .reset = reset,
116 .get_read_block_size = get_block_size,
117 .get_write_block_size = get_block_size,
118 .get_encoded_block_size = get_encoded_block_size,
119 .reduce_encoder_bitrate = reduce_encoder_bitrate,
120 .increase_encoder_bitrate = increase_encoder_bitrate,
121 .encode_buffer = encode_buffer,
122 .decode_buffer = decode_buffer,
123 };
124