1 /* Copyright 2019 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5 #include <stdlib.h>
6
7 extern "C" {
8 #include "cras_audio_codec.h"
9 #include "sbc_codec_stub.h"
10 }
11
12 namespace {
13
14 static int create_fail;
15 static int create_called;
16 static int msbc_create_called;
17 static int destroy_called;
18 static uint8_t freq_val;
19 static uint8_t mode_val;
20 static uint8_t subbands_val;
21 static uint8_t alloc_val;
22 static uint8_t blocks_val;
23 static uint8_t bitpool_val;
24 static struct cras_audio_codec* sbc_codec;
25 static size_t decode_out_decoded_return_val;
26 static int decode_fail;
27 static size_t encode_out_encoded_return_val;
28 static int encode_fail;
29 static int cras_sbc_get_frame_length_val;
30 static int cras_sbc_get_codesize_val;
31
32 }; // namespace
33
sbc_codec_stub_reset()34 void sbc_codec_stub_reset() {
35 create_fail = 0;
36 create_called = 0;
37 msbc_create_called = 0;
38 destroy_called = 0;
39 freq_val = 0;
40 mode_val = 0;
41 subbands_val = 0;
42 alloc_val = 0;
43 blocks_val = 0;
44 bitpool_val = 0;
45
46 sbc_codec = NULL;
47 decode_out_decoded_return_val = 0;
48 encode_out_encoded_return_val = 0;
49 decode_fail = 0;
50 encode_fail = 0;
51
52 cras_sbc_get_frame_length_val = 5;
53 cras_sbc_get_codesize_val = 5;
54 }
55
set_sbc_codec_create_fail(int fail)56 void set_sbc_codec_create_fail(int fail) {
57 create_fail = fail;
58 }
59
get_sbc_codec_create_called()60 int get_sbc_codec_create_called() {
61 return create_called;
62 }
63
get_msbc_codec_create_called()64 int get_msbc_codec_create_called() {
65 return msbc_create_called;
66 }
67
get_sbc_codec_create_freq_val()68 uint8_t get_sbc_codec_create_freq_val() {
69 return freq_val;
70 }
71
get_sbc_codec_create_mode_val()72 uint8_t get_sbc_codec_create_mode_val() {
73 return mode_val;
74 }
75
get_sbc_codec_create_subbands_val()76 uint8_t get_sbc_codec_create_subbands_val() {
77 return subbands_val;
78 }
79
get_sbc_codec_create_alloc_val()80 uint8_t get_sbc_codec_create_alloc_val() {
81 return alloc_val;
82 }
83
get_sbc_codec_create_blocks_val()84 uint8_t get_sbc_codec_create_blocks_val() {
85 return blocks_val;
86 }
87
get_sbc_codec_create_bitpool_val()88 uint8_t get_sbc_codec_create_bitpool_val() {
89 return bitpool_val;
90 }
91
get_sbc_codec_destroy_called()92 int get_sbc_codec_destroy_called() {
93 return destroy_called;
94 }
95
set_sbc_codec_decoded_out(size_t ret)96 void set_sbc_codec_decoded_out(size_t ret) {
97 decode_out_decoded_return_val = ret;
98 }
99
set_sbc_codec_decoded_fail(int fail)100 void set_sbc_codec_decoded_fail(int fail) {
101 decode_fail = fail;
102 }
103
set_sbc_codec_encoded_out(size_t ret)104 void set_sbc_codec_encoded_out(size_t ret) {
105 encode_out_encoded_return_val = ret;
106 }
107
set_sbc_codec_encoded_fail(int fail)108 void set_sbc_codec_encoded_fail(int fail) {
109 encode_fail = fail;
110 }
111
decode(struct cras_audio_codec * codec,const void * input,size_t input_len,void * output,size_t output_len,size_t * count)112 int decode(struct cras_audio_codec* codec,
113 const void* input,
114 size_t input_len,
115 void* output,
116 size_t output_len,
117 size_t* count) {
118 *count = decode_out_decoded_return_val;
119 return decode_fail ? -1 : input_len;
120 }
121
encode(struct cras_audio_codec * codec,const void * input,size_t input_len,void * output,size_t output_len,size_t * count)122 int encode(struct cras_audio_codec* codec,
123 const void* input,
124 size_t input_len,
125 void* output,
126 size_t output_len,
127 size_t* count) {
128 // Written half the output buffer.
129 *count = encode_out_encoded_return_val;
130 return encode_fail ? -1 : input_len;
131 }
132
cras_sbc_codec_create(uint8_t freq,uint8_t mode,uint8_t subbands,uint8_t alloc,uint8_t blocks,uint8_t bitpool)133 struct cras_audio_codec* cras_sbc_codec_create(uint8_t freq,
134 uint8_t mode,
135 uint8_t subbands,
136 uint8_t alloc,
137 uint8_t blocks,
138 uint8_t bitpool) {
139 if (!create_fail) {
140 sbc_codec = (struct cras_audio_codec*)calloc(1, sizeof(*sbc_codec));
141 sbc_codec->decode = decode;
142 sbc_codec->encode = encode;
143 }
144
145 create_called++;
146 freq_val = freq;
147 mode_val = mode;
148 subbands_val = subbands;
149 alloc_val = alloc;
150 blocks_val = blocks;
151 bitpool_val = bitpool;
152 return sbc_codec;
153 }
154
cras_msbc_codec_create()155 struct cras_audio_codec* cras_msbc_codec_create() {
156 msbc_create_called++;
157 sbc_codec = (struct cras_audio_codec*)calloc(1, sizeof(*sbc_codec));
158 sbc_codec->decode = decode;
159 sbc_codec->encode = encode;
160 return sbc_codec;
161 }
162
cras_sbc_codec_destroy(struct cras_audio_codec * codec)163 void cras_sbc_codec_destroy(struct cras_audio_codec* codec) {
164 destroy_called++;
165 free(codec);
166 }
167
cras_sbc_get_codesize(struct cras_audio_codec * codec)168 int cras_sbc_get_codesize(struct cras_audio_codec* codec) {
169 return cras_sbc_get_codesize_val;
170 }
171
cras_sbc_get_frame_length(struct cras_audio_codec * codec)172 int cras_sbc_get_frame_length(struct cras_audio_codec* codec) {
173 return cras_sbc_get_frame_length_val;
174 }
175