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 <gtest/gtest.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <sys/socket.h>
9 #include <unistd.h>
10
11 extern "C" {
12 #include <sbc/sbc.h>
13
14 #include "cras_a2dp_info.h"
15 #include "cras_sbc_codec.h"
16 #include "sbc_codec_stub.h"
17 }
18
19 static size_t a2dp_write_link_mtu_val;
20 static struct a2dp_info a2dp;
21 static a2dp_sbc_t sbc;
22
ResetStubData()23 void ResetStubData() {
24 sbc_codec_stub_reset();
25
26 a2dp_write_link_mtu_val = 40;
27
28 sbc.frequency = SBC_SAMPLING_FREQ_48000;
29 sbc.channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO;
30 sbc.allocation_method = SBC_ALLOCATION_LOUDNESS;
31 sbc.subbands = SBC_SUBBANDS_8;
32 sbc.block_length = SBC_BLOCK_LENGTH_16;
33 sbc.max_bitpool = 50;
34
35 a2dp.a2dp_buf_used = 0;
36 a2dp.frame_count = 0;
37 a2dp.samples = 0;
38 a2dp.nsamples = 0;
39 }
40
41 namespace {
42
TEST(A2dpInfoInit,InitA2dp)43 TEST(A2dpInfoInit, InitA2dp) {
44 ResetStubData();
45 init_a2dp(&a2dp, &sbc);
46
47 ASSERT_EQ(1, get_sbc_codec_create_called());
48 ASSERT_EQ(SBC_FREQ_48000, get_sbc_codec_create_freq_val());
49 ASSERT_EQ(SBC_MODE_JOINT_STEREO, get_sbc_codec_create_mode_val());
50 ASSERT_EQ(SBC_AM_LOUDNESS, get_sbc_codec_create_alloc_val());
51 ASSERT_EQ(SBC_SB_8, get_sbc_codec_create_subbands_val());
52 ASSERT_EQ(SBC_BLK_16, get_sbc_codec_create_blocks_val());
53 ASSERT_EQ(50, get_sbc_codec_create_bitpool_val());
54
55 ASSERT_NE(a2dp.codec, (void*)NULL);
56 ASSERT_EQ(a2dp.a2dp_buf_used, 13);
57 ASSERT_EQ(a2dp.frame_count, 0);
58 ASSERT_EQ(a2dp.seq_num, 0);
59 ASSERT_EQ(a2dp.samples, 0);
60
61 destroy_a2dp(&a2dp);
62 }
63
TEST(A2dpInfoInit,InitA2dpFail)64 TEST(A2dpInfoInit, InitA2dpFail) {
65 ResetStubData();
66 int err;
67 set_sbc_codec_create_fail(1);
68 err = init_a2dp(&a2dp, &sbc);
69
70 ASSERT_EQ(1, get_sbc_codec_create_called());
71 ASSERT_NE(0, err);
72 ASSERT_EQ(a2dp.codec, (void*)NULL);
73 }
74
TEST(A2dpInfoInit,DestroyA2dp)75 TEST(A2dpInfoInit, DestroyA2dp) {
76 ResetStubData();
77 init_a2dp(&a2dp, &sbc);
78 destroy_a2dp(&a2dp);
79
80 ASSERT_EQ(1, get_sbc_codec_destroy_called());
81 }
82
TEST(A2dpInfoInit,ResetA2dp)83 TEST(A2dpInfoInit, ResetA2dp) {
84 ResetStubData();
85 init_a2dp(&a2dp, &sbc);
86 a2dp.a2dp_buf_used = 99;
87 a2dp.samples = 10;
88 a2dp.seq_num = 11;
89 a2dp.frame_count = 12;
90
91 a2dp_reset(&a2dp);
92
93 ASSERT_EQ(a2dp.a2dp_buf_used, 13);
94 ASSERT_EQ(a2dp.frame_count, 0);
95 ASSERT_EQ(a2dp.seq_num, 0);
96 ASSERT_EQ(a2dp.samples, 0);
97
98 destroy_a2dp(&a2dp);
99 }
100
TEST(A2dpEncode,WriteA2dp)101 TEST(A2dpEncode, WriteA2dp) {
102 unsigned int processed;
103
104 ResetStubData();
105 init_a2dp(&a2dp, &sbc);
106
107 set_sbc_codec_encoded_out(4);
108 processed = a2dp_encode(&a2dp, NULL, 20, 4, (size_t)40);
109
110 ASSERT_EQ(20, processed);
111 ASSERT_EQ(4, a2dp.frame_count);
112
113 // 13 + 4 used a2dp buffer still below half mtu unwritten
114 ASSERT_EQ(17, a2dp.a2dp_buf_used);
115 ASSERT_EQ(5, a2dp.samples);
116 ASSERT_EQ(5, a2dp.nsamples);
117 ASSERT_EQ(0, a2dp.seq_num);
118
119 set_sbc_codec_encoded_out(15);
120 processed = a2dp_encode(&a2dp, NULL, 20, 4, (size_t)40);
121
122 ASSERT_EQ(32, a2dp.a2dp_buf_used);
123 ASSERT_EQ(10, a2dp.samples);
124 ASSERT_EQ(10, a2dp.nsamples);
125 ASSERT_EQ(0, a2dp.seq_num);
126 }
127
128 } // namespace
129
main(int argc,char ** argv)130 int main(int argc, char** argv) {
131 ::testing::InitGoogleTest(&argc, argv);
132 return RUN_ALL_TESTS();
133 }
134