• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 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 <stdint.h>
6 #include <stdio.h>
7 #include <sys/socket.h>
8 #include <unistd.h>
9 #include <gtest/gtest.h>
10 
11 extern "C" {
12   #include <sbc/sbc.h>
13 
14   #include "cras_sbc_codec.h"
15   #include "cras_a2dp_info.h"
16 }
17 
18 static size_t cras_sbc_codec_create_called;
19 static size_t cras_sbc_codec_destroy_called;
20 static uint8_t codec_create_freq_val;
21 static uint8_t codec_create_mode_val;
22 static uint8_t codec_create_subbands_val;
23 static uint8_t codec_create_alloc_val;
24 static uint8_t codec_create_blocks_val;
25 static uint8_t codec_create_bitpool_val;
26 static int cras_sbc_get_frame_length_val;
27 static int cras_sbc_get_codesize_val;
28 static size_t a2dp_write_link_mtu_val;
29 static size_t encode_out_encoded_return_val;
30 static struct cras_audio_codec *sbc_codec;
31 static int cras_sbc_codec_create_fail;
32 static struct a2dp_info a2dp;
33 static a2dp_sbc_t sbc;
34 
ResetStubData()35 void ResetStubData() {
36   cras_sbc_codec_create_called = 0;
37   cras_sbc_codec_destroy_called = 0;
38 
39   codec_create_freq_val = 0;
40   codec_create_mode_val = 0;
41   codec_create_subbands_val = 0;
42   codec_create_alloc_val = 0;
43   codec_create_blocks_val = 0;
44   codec_create_bitpool_val = 0;
45 
46   cras_sbc_get_frame_length_val = 5;
47   cras_sbc_get_codesize_val = 5;
48   sbc_codec = NULL;
49   cras_sbc_codec_create_fail = 0;
50 
51   a2dp_write_link_mtu_val = 40;
52   encode_out_encoded_return_val = 0;
53 
54   sbc.frequency = SBC_SAMPLING_FREQ_48000;
55   sbc.channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO;
56   sbc.allocation_method = SBC_ALLOCATION_LOUDNESS;
57   sbc.subbands = SBC_SUBBANDS_8;
58   sbc.block_length = SBC_BLOCK_LENGTH_16;
59   sbc.max_bitpool = 50;
60 
61   a2dp.a2dp_buf_used = 0;
62   a2dp.frame_count = 0;
63   a2dp.samples = 0;
64   a2dp.nsamples = 0;
65 }
66 
67 namespace {
68 
TEST(A2dpInfoInit,InitA2dp)69 TEST(A2dpInfoInit, InitA2dp) {
70   ResetStubData();
71   init_a2dp(&a2dp, &sbc);
72 
73   ASSERT_EQ(1, cras_sbc_codec_create_called);
74   ASSERT_EQ(SBC_FREQ_48000, codec_create_freq_val);
75   ASSERT_EQ(SBC_MODE_JOINT_STEREO, codec_create_mode_val);
76   ASSERT_EQ(SBC_AM_LOUDNESS, codec_create_alloc_val);
77   ASSERT_EQ(SBC_SB_8, codec_create_subbands_val);
78   ASSERT_EQ(SBC_BLK_16, codec_create_blocks_val);
79   ASSERT_EQ(50, codec_create_bitpool_val);
80 
81   ASSERT_NE(a2dp.codec, (void *)NULL);
82   ASSERT_EQ(a2dp.a2dp_buf_used, 13);
83   ASSERT_EQ(a2dp.frame_count, 0);
84   ASSERT_EQ(a2dp.seq_num, 0);
85   ASSERT_EQ(a2dp.samples, 0);
86 
87   destroy_a2dp(&a2dp);
88 }
89 
TEST(A2dpInfoInit,InitA2dpFail)90 TEST(A2dpInfoInit, InitA2dpFail) {
91   ResetStubData();
92   int err;
93   cras_sbc_codec_create_fail = 1;
94   err = init_a2dp(&a2dp, &sbc);
95 
96   ASSERT_EQ(1, cras_sbc_codec_create_called);
97   ASSERT_NE(0, err);
98   ASSERT_EQ(a2dp.codec, (void *)NULL);
99 }
100 
TEST(A2dpInfoInit,DestroyA2dp)101 TEST(A2dpInfoInit, DestroyA2dp) {
102   ResetStubData();
103   init_a2dp(&a2dp, &sbc);
104   destroy_a2dp(&a2dp);
105 
106   ASSERT_EQ(1, cras_sbc_codec_destroy_called);
107 }
108 
TEST(A2dpInfoInit,DrainA2dp)109 TEST(A2dpInfoInit, DrainA2dp) {
110   ResetStubData();
111   init_a2dp(&a2dp, &sbc);
112   a2dp.a2dp_buf_used = 99;
113   a2dp.samples = 10;
114   a2dp.seq_num = 11;
115   a2dp.frame_count = 12;
116 
117   a2dp_drain(&a2dp);
118 
119   ASSERT_EQ(a2dp.a2dp_buf_used, 13);
120   ASSERT_EQ(a2dp.frame_count, 0);
121   ASSERT_EQ(a2dp.seq_num, 0);
122   ASSERT_EQ(a2dp.samples, 0);
123 
124   destroy_a2dp(&a2dp);
125 }
126 
TEST(A2dpEncode,WriteA2dp)127 TEST(A2dpEncode, WriteA2dp) {
128   unsigned int processed;
129 
130   ResetStubData();
131   init_a2dp(&a2dp, &sbc);
132 
133   encode_out_encoded_return_val = 4;
134   processed = a2dp_encode(&a2dp, NULL, 20, 4, (size_t)40);
135 
136   ASSERT_EQ(20, processed);
137   ASSERT_EQ(4, a2dp.frame_count);
138 
139   // 13 + 4 used a2dp buffer still below half mtu unwritten
140   ASSERT_EQ(17, a2dp.a2dp_buf_used);
141   ASSERT_EQ(5, a2dp.samples);
142   ASSERT_EQ(5, a2dp.nsamples);
143   ASSERT_EQ(0, a2dp.seq_num);
144 
145   encode_out_encoded_return_val = 15;
146   processed = a2dp_encode(&a2dp, NULL, 20, 4, (size_t)40);
147 
148   ASSERT_EQ(32, a2dp.a2dp_buf_used);
149   ASSERT_EQ(10, a2dp.samples);
150   ASSERT_EQ(10, a2dp.nsamples);
151   ASSERT_EQ(0, a2dp.seq_num);
152 }
153 
154 } // namespace
155 
main(int argc,char ** argv)156 int main(int argc, char **argv) {
157   ::testing::InitGoogleTest(&argc, argv);
158   return RUN_ALL_TESTS();
159 }
160 
decode(struct cras_audio_codec * codec,const void * input,size_t input_len,void * output,size_t output_len,size_t * count)161 int decode(struct cras_audio_codec *codec, const void *input,
162 	   size_t input_len, void *output, size_t output_len,
163 	   size_t *count)
164 {
165   return input_len;
166 }
167 
encode(struct cras_audio_codec * codec,const void * input,size_t input_len,void * output,size_t output_len,size_t * count)168 int encode(struct cras_audio_codec *codec, const void *input,
169 	   size_t input_len, void *output, size_t output_len,
170 	   size_t *count)
171 {
172   // Written half the output buffer.
173   *count = encode_out_encoded_return_val;
174   return input_len;
175 }
176 
cras_sbc_codec_create(uint8_t freq,uint8_t mode,uint8_t subbands,uint8_t alloc,uint8_t blocks,uint8_t bitpool)177 struct cras_audio_codec *cras_sbc_codec_create(uint8_t freq,
178 		uint8_t mode, uint8_t subbands, uint8_t alloc,
179 		uint8_t blocks, uint8_t bitpool)
180 {
181   if (!cras_sbc_codec_create_fail) {
182     sbc_codec = (struct cras_audio_codec *)calloc(1, sizeof(*sbc_codec));
183     sbc_codec->decode = decode;
184     sbc_codec->encode = encode;
185   }
186 
187   cras_sbc_codec_create_called++;
188   codec_create_freq_val = freq;
189   codec_create_mode_val = mode;
190   codec_create_subbands_val = subbands;
191   codec_create_alloc_val = alloc;
192   codec_create_blocks_val = blocks;
193   codec_create_bitpool_val = bitpool;
194   return sbc_codec;
195 }
196 
cras_sbc_codec_destroy(struct cras_audio_codec * codec)197 void cras_sbc_codec_destroy(struct cras_audio_codec *codec)
198 {
199   cras_sbc_codec_destroy_called++;
200   free(codec);
201 }
202 
cras_sbc_get_codesize(struct cras_audio_codec * codec)203 int cras_sbc_get_codesize(struct cras_audio_codec *codec)
204 {
205   return cras_sbc_get_codesize_val;
206 }
207 
cras_sbc_get_frame_length(struct cras_audio_codec * codec)208 int cras_sbc_get_frame_length(struct cras_audio_codec *codec)
209 {
210   return cras_sbc_get_frame_length_val;
211 }
212