iodev_unittest.cc revision 5b1717f92baa7ab7006e04431bd6b33a24d8a23f
1838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid// Use of this source code is governed by a BSD-style license that can be
3838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid// found in the LICENSE file.
4838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
5838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid#include <stdio.h>
6838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid#include <gtest/gtest.h>
7838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
8838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reidextern "C" {
9838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid#include "cras_iodev.h"
10838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid#include "cras_rstream.h"
11838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid#include "utlist.h"
12838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid}
13838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
14838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reidnamespace {
15838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
16838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reidstatic struct timespec clock_gettime_retspec;
17838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
18838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid//  Test fill_time_from_frames
19838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan ReidTEST(IoDevTestSuite, FillTimeFromFramesNormal) {
20838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  struct timespec ts;
21838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
22838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  cras_iodev_fill_time_from_frames(24000, 12000, 48000, &ts);
23838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_EQ(0, ts.tv_sec);
24838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_GE(ts.tv_nsec, 249900000);
25838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_LE(ts.tv_nsec, 250100000);
26838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid}
27838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
28838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan ReidTEST(IoDevTestSuite, FillTimeFromFramesLong) {
29838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  struct timespec ts;
30838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
31838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  cras_iodev_fill_time_from_frames(120000, 12000, 48000, &ts);
32838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_EQ(2, ts.tv_sec);
33838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_GE(ts.tv_nsec, 249900000);
34838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_LE(ts.tv_nsec, 250100000);
35838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid}
36838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
37838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan ReidTEST(IoDevTestSuite, FillTimeFromFramesShort) {
38838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  struct timespec ts;
39838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
40838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  cras_iodev_fill_time_from_frames(12000, 12000, 48000, &ts);
41838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_EQ(0, ts.tv_sec);
42838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_EQ(0, ts.tv_nsec);
43838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid}
44838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
45838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid//  Test set_playback_timestamp.
46838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan ReidTEST(IoDevTestSuite, SetPlaybackTimeStampSimple) {
47838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  struct timespec ts;
48838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
49838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  clock_gettime_retspec.tv_sec = 1;
50838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  clock_gettime_retspec.tv_nsec = 0;
51838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  cras_iodev_set_playback_timestamp(48000, 24000, &ts);
52838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_EQ(1, ts.tv_sec);
53838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_GE(ts.tv_nsec, 499900000);
54838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_LE(ts.tv_nsec, 500100000);
55838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid}
56838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
57838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan ReidTEST(IoDevTestSuite, SetPlaybackTimeStampWrap) {
58838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  struct timespec ts;
59838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
60838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  clock_gettime_retspec.tv_sec = 1;
61838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  clock_gettime_retspec.tv_nsec = 750000000;
62838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  cras_iodev_set_playback_timestamp(48000, 24000, &ts);
63838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_EQ(2, ts.tv_sec);
64838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_GE(ts.tv_nsec, 249900000);
65838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_LE(ts.tv_nsec, 250100000);
66838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid}
67838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
68838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan ReidTEST(IoDevTestSuite, SetPlaybackTimeStampWrapTwice) {
69838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  struct timespec ts;
70838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
71838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  clock_gettime_retspec.tv_sec = 1;
72838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  clock_gettime_retspec.tv_nsec = 750000000;
73838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  cras_iodev_set_playback_timestamp(48000, 72000, &ts);
74838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_EQ(3, ts.tv_sec);
75838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_GE(ts.tv_nsec, 249900000);
76838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_LE(ts.tv_nsec, 250100000);
77838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid}
78838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
79838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid//  Test set_capture_timestamp.
80838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan ReidTEST(IoDevTestSuite, SetCaptureTimeStampSimple) {
81838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  struct timespec ts;
82838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
83838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  clock_gettime_retspec.tv_sec = 1;
84838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  clock_gettime_retspec.tv_nsec = 750000000;
85838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  cras_iodev_set_capture_timestamp(48000, 24000, &ts);
86838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_EQ(1, ts.tv_sec);
87838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_GE(ts.tv_nsec, 249900000);
88838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_LE(ts.tv_nsec, 250100000);
89838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid}
90838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
91838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan ReidTEST(IoDevTestSuite, SetCaptureTimeStampWrap) {
92838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  struct timespec ts;
93838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
94838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  clock_gettime_retspec.tv_sec = 1;
95838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  clock_gettime_retspec.tv_nsec = 0;
96838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  cras_iodev_set_capture_timestamp(48000, 24000, &ts);
97838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_EQ(0, ts.tv_sec);
98838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_GE(ts.tv_nsec, 499900000);
99838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_LE(ts.tv_nsec, 500100000);
100838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid}
101838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
102838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan ReidTEST(IoDevTestSuite, SetCaptureTimeStampWrapPartial) {
103838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  struct timespec ts;
104838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
105838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  clock_gettime_retspec.tv_sec = 2;
106838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  clock_gettime_retspec.tv_nsec = 750000000;
107838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  cras_iodev_set_capture_timestamp(48000, 72000, &ts);
108838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_EQ(1, ts.tv_sec);
109838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_GE(ts.tv_nsec, 249900000);
110838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_LE(ts.tv_nsec, 250100000);
111838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid}
112838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
113838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan ReidTEST(IoDevTestSuite, TestConfigParamsOneStream) {
114838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  struct cras_iodev iodev;
115838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  struct cras_rstream stream1;
116838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  struct cras_io_stream iostream1;
117838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
118838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  memset(&iodev, 0, sizeof(iodev));
119838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
120838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  stream1.buffer_frames = 10;
121838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  stream1.cb_threshold = 3;
122838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
123838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  iostream1.stream = &stream1;
124838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  DL_APPEND(iodev.streams, &iostream1);
12532771830509e3baa322a5746edb3a990ea5d631fDylan Reid  iodev.buffer_size = 1024;
126838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
127838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  cras_iodev_config_params_for_streams(&iodev);
128838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_EQ(iodev.used_size, 10);
129838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_EQ(iodev.cb_threshold, 3);
130838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid}
131838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
1320534603ac205e739953e3d100d09d3a93ae93c3dDylan ReidTEST(IoDevTestSuite, TestConfigParamsOneStreamLimitThreshold) {
1330534603ac205e739953e3d100d09d3a93ae93c3dDylan Reid  struct cras_iodev iodev;
1340534603ac205e739953e3d100d09d3a93ae93c3dDylan Reid  struct cras_rstream stream1;
1350534603ac205e739953e3d100d09d3a93ae93c3dDylan Reid  struct cras_io_stream iostream1;
1360534603ac205e739953e3d100d09d3a93ae93c3dDylan Reid
1370534603ac205e739953e3d100d09d3a93ae93c3dDylan Reid  memset(&iodev, 0, sizeof(iodev));
1380534603ac205e739953e3d100d09d3a93ae93c3dDylan Reid
1390534603ac205e739953e3d100d09d3a93ae93c3dDylan Reid  stream1.buffer_frames = 10;
1400534603ac205e739953e3d100d09d3a93ae93c3dDylan Reid  stream1.cb_threshold = 10;
1410534603ac205e739953e3d100d09d3a93ae93c3dDylan Reid
1420534603ac205e739953e3d100d09d3a93ae93c3dDylan Reid  iostream1.stream = &stream1;
1430534603ac205e739953e3d100d09d3a93ae93c3dDylan Reid  DL_APPEND(iodev.streams, &iostream1);
1440534603ac205e739953e3d100d09d3a93ae93c3dDylan Reid  iodev.buffer_size = 1024;
1450534603ac205e739953e3d100d09d3a93ae93c3dDylan Reid
1460534603ac205e739953e3d100d09d3a93ae93c3dDylan Reid  cras_iodev_config_params_for_streams(&iodev);
1470534603ac205e739953e3d100d09d3a93ae93c3dDylan Reid  EXPECT_EQ(iodev.used_size, 10);
1480534603ac205e739953e3d100d09d3a93ae93c3dDylan Reid  EXPECT_EQ(iodev.cb_threshold, 5);
1490534603ac205e739953e3d100d09d3a93ae93c3dDylan Reid
1500534603ac205e739953e3d100d09d3a93ae93c3dDylan Reid  iodev.direction = CRAS_STREAM_INPUT;
1510534603ac205e739953e3d100d09d3a93ae93c3dDylan Reid  cras_iodev_config_params_for_streams(&iodev);
1520534603ac205e739953e3d100d09d3a93ae93c3dDylan Reid  EXPECT_EQ(iodev.used_size, 10);
1530534603ac205e739953e3d100d09d3a93ae93c3dDylan Reid  EXPECT_EQ(iodev.cb_threshold, 10);
1540534603ac205e739953e3d100d09d3a93ae93c3dDylan Reid}
1550534603ac205e739953e3d100d09d3a93ae93c3dDylan Reid
15632771830509e3baa322a5746edb3a990ea5d631fDylan ReidTEST(IoDevTestSuite, TestConfigParamsOneStreamUsedGreaterBuffer) {
15732771830509e3baa322a5746edb3a990ea5d631fDylan Reid  struct cras_iodev iodev;
15832771830509e3baa322a5746edb3a990ea5d631fDylan Reid  struct cras_rstream stream1;
15932771830509e3baa322a5746edb3a990ea5d631fDylan Reid  struct cras_io_stream iostream1;
16032771830509e3baa322a5746edb3a990ea5d631fDylan Reid
16132771830509e3baa322a5746edb3a990ea5d631fDylan Reid  memset(&iodev, 0, sizeof(iodev));
16232771830509e3baa322a5746edb3a990ea5d631fDylan Reid
16332771830509e3baa322a5746edb3a990ea5d631fDylan Reid  stream1.buffer_frames = 1280;
16432771830509e3baa322a5746edb3a990ea5d631fDylan Reid  stream1.cb_threshold = 1400;
16532771830509e3baa322a5746edb3a990ea5d631fDylan Reid
16632771830509e3baa322a5746edb3a990ea5d631fDylan Reid  iostream1.stream = &stream1;
16732771830509e3baa322a5746edb3a990ea5d631fDylan Reid  DL_APPEND(iodev.streams, &iostream1);
16832771830509e3baa322a5746edb3a990ea5d631fDylan Reid  iodev.buffer_size = 1024;
16932771830509e3baa322a5746edb3a990ea5d631fDylan Reid
17032771830509e3baa322a5746edb3a990ea5d631fDylan Reid  cras_iodev_config_params_for_streams(&iodev);
17132771830509e3baa322a5746edb3a990ea5d631fDylan Reid  EXPECT_EQ(iodev.used_size, 1024);
17232771830509e3baa322a5746edb3a990ea5d631fDylan Reid  EXPECT_EQ(iodev.cb_threshold, 512);
17332771830509e3baa322a5746edb3a990ea5d631fDylan Reid}
17432771830509e3baa322a5746edb3a990ea5d631fDylan Reid
175838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan ReidTEST(IoDevTestSuite, TestConfigParamsTwoStreamsFirstLonger) {
176838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  struct cras_iodev iodev;
177838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  struct cras_rstream stream1;
178838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  struct cras_io_stream iostream1;
179838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  struct cras_rstream stream2;
180838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  struct cras_io_stream iostream2;
181838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
182838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  memset(&iodev, 0, sizeof(iodev));
183838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
184838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  stream1.buffer_frames = 10;
185838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  stream1.cb_threshold = 3;
186838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  stream2.buffer_frames = 8;
187838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  stream2.cb_threshold = 5;
188838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
189838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  iostream1.stream = &stream1;
190838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  iostream2.stream = &stream2;
191838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  DL_APPEND(iodev.streams, &iostream1);
192838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  DL_APPEND(iodev.streams, &iostream2);
19332771830509e3baa322a5746edb3a990ea5d631fDylan Reid  iodev.buffer_size = 1024;
194838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
195838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  cras_iodev_config_params_for_streams(&iodev);
196838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_EQ(iodev.used_size, 8);
19732771830509e3baa322a5746edb3a990ea5d631fDylan Reid  EXPECT_EQ(iodev.cb_threshold, 4);
198838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid}
199838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
200838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan ReidTEST(IoDevTestSuite, TestConfigParamsTwoStreamsSecondLonger) {
201838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  struct cras_iodev iodev;
202838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  struct cras_rstream stream1;
203838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  struct cras_io_stream iostream1;
204838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  struct cras_rstream stream2;
205838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  struct cras_io_stream iostream2;
206838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
207838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  memset(&iodev, 0, sizeof(iodev));
208838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
209838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  stream1.buffer_frames = 10;
210838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  stream1.cb_threshold = 3;
211838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  stream2.buffer_frames = 80;
212838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  stream2.cb_threshold = 5;
213838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
214838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  iostream1.stream = &stream1;
215838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  iostream2.stream = &stream2;
216838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  DL_APPEND(iodev.streams, &iostream1);
217838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  DL_APPEND(iodev.streams, &iostream2);
21832771830509e3baa322a5746edb3a990ea5d631fDylan Reid  iodev.buffer_size = 1024;
219838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
220838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  cras_iodev_config_params_for_streams(&iodev);
221838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_EQ(iodev.used_size, 10);
222838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  EXPECT_EQ(iodev.cb_threshold, 3);
223838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid}
224838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
2258284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan ReidTEST(IoDevTestSuite, TestPluggedTiming) {
2268284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan Reid  struct cras_iodev iodev;
2278284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan Reid  struct timeval tv;
2288284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan Reid  struct timeval tv2;
2298284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan Reid
2308284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan Reid  memset(&iodev, 0, sizeof(iodev));
2318284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan Reid
2328284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan Reid  cras_iodev_plug_event(&iodev, 1);
2338284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan Reid  EXPECT_EQ(1, cras_iodev_is_plugged_in(&iodev));
2348284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan Reid  tv = cras_iodev_last_plugged_time(&iodev);
2358284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan Reid  EXPECT_GT(tv.tv_sec, 1);
2368284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan Reid
2378284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan Reid  cras_iodev_plug_event(&iodev, 0);
2388284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan Reid  EXPECT_EQ(0, cras_iodev_is_plugged_in(&iodev));
2398284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan Reid
2408284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan Reid  usleep(1000); // insure different times for plug events.
2418284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan Reid
2428284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan Reid  cras_iodev_plug_event(&iodev, 1);
2438284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan Reid  EXPECT_EQ(1, cras_iodev_is_plugged_in(&iodev));
2448284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan Reid  tv2 = cras_iodev_last_plugged_time(&iodev);
2458284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan Reid  EXPECT_TRUE(tv2.tv_sec > tv.tv_sec ||
2468284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan Reid              (tv2.tv_sec == tv.tv_sec && tv2.tv_usec > tv.tv_usec));
2478284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan Reid}
2488284c1b74b0b4ee012aa7e1566940abf81ae2ef5Dylan Reid
249045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Changclass IoDevSetFormatTestSuite : public testing::Test {
250045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  protected:
251045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang    virtual void SetUp() {
252045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang      sample_rates_[0] = 44100;
253045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang      sample_rates_[1] = 48000;
254045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang      sample_rates_[2] = 0;
255045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang
256045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang      channel_counts_[0] = 2;
257045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang      channel_counts_[1] = 0;
258045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang
259045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang      memset(&iodev_, 0, sizeof(iodev_));
260045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang      iodev_.supported_rates = sample_rates_;
261045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang      iodev_.supported_channel_counts = channel_counts_;
262045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang    }
263045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang
2645b1717f92baa7ab7006e04431bd6b33a24d8a23fChih-Chung Chang    virtual void TearDown() {
2655b1717f92baa7ab7006e04431bd6b33a24d8a23fChih-Chung Chang      cras_iodev_free_format(&iodev_);
2665b1717f92baa7ab7006e04431bd6b33a24d8a23fChih-Chung Chang    }
2675b1717f92baa7ab7006e04431bd6b33a24d8a23fChih-Chung Chang
268045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang    struct cras_iodev iodev_;
269045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang    size_t sample_rates_[3];
270045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang    size_t channel_counts_[2];
271045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang};
272045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang
273045156126f648a9f0910152a66572a13447bfdc6Chih-Chung ChangTEST_F(IoDevSetFormatTestSuite, SupportedFormatSecondary) {
274045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  struct cras_audio_format fmt;
275045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  int rc;
276045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang
277045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  fmt.format = SND_PCM_FORMAT_S16_LE;
278045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  fmt.frame_rate = 48000;
279045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  fmt.num_channels = 2;
280045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  rc = cras_iodev_set_format(&iodev_, &fmt);
281045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  EXPECT_EQ(0, rc);
282045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  EXPECT_EQ(SND_PCM_FORMAT_S16_LE, fmt.format);
283045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  EXPECT_EQ(48000, fmt.frame_rate);
284045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  EXPECT_EQ(2, fmt.num_channels);
285045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang}
286045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang
287045156126f648a9f0910152a66572a13447bfdc6Chih-Chung ChangTEST_F(IoDevSetFormatTestSuite, SupportedFormatPrimary) {
288045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  struct cras_audio_format fmt;
289045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  int rc;
290045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang
291045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  fmt.format = SND_PCM_FORMAT_S16_LE;
292045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  fmt.frame_rate = 44100;
293045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  fmt.num_channels = 2;
294045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  rc = cras_iodev_set_format(&iodev_, &fmt);
295045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  EXPECT_EQ(0, rc);
296045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  EXPECT_EQ(SND_PCM_FORMAT_S16_LE, fmt.format);
297045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  EXPECT_EQ(44100, fmt.frame_rate);
298045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  EXPECT_EQ(2, fmt.num_channels);
299045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang}
300045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang
301045156126f648a9f0910152a66572a13447bfdc6Chih-Chung ChangTEST_F(IoDevSetFormatTestSuite, SupportedFormatDivisor) {
302045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  struct cras_audio_format fmt;
303045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  int rc;
304045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang
305045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  fmt.format = SND_PCM_FORMAT_S16_LE;
306045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  fmt.frame_rate = 96000;
307045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  fmt.num_channels = 2;
308045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  rc = cras_iodev_set_format(&iodev_, &fmt);
309045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  EXPECT_EQ(0, rc);
310045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  EXPECT_EQ(SND_PCM_FORMAT_S16_LE, fmt.format);
311045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  EXPECT_EQ(48000, fmt.frame_rate);
312045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  EXPECT_EQ(2, fmt.num_channels);
313045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang}
314045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang
315045156126f648a9f0910152a66572a13447bfdc6Chih-Chung ChangTEST_F(IoDevSetFormatTestSuite, UnsupportedChannelCount) {
316045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  struct cras_audio_format fmt;
317045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  int rc;
318045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang
319045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  fmt.format = SND_PCM_FORMAT_S16_LE;
320045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  fmt.frame_rate = 96000;
321045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  fmt.num_channels = 1;
322045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  rc = cras_iodev_set_format(&iodev_, &fmt);
323045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  EXPECT_EQ(0, rc);
324045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  EXPECT_EQ(SND_PCM_FORMAT_S16_LE, fmt.format);
325045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  EXPECT_EQ(48000, fmt.frame_rate);
326045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  EXPECT_EQ(2, fmt.num_channels);
327045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang}
328045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang
329045156126f648a9f0910152a66572a13447bfdc6Chih-Chung ChangTEST_F(IoDevSetFormatTestSuite, SupportedFormatFallbackDefault) {
330045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  struct cras_audio_format fmt;
331045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  int rc;
332045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang
333045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  fmt.format = SND_PCM_FORMAT_S16_LE;
334045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  fmt.frame_rate = 96008;
335045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  fmt.num_channels = 2;
336045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  rc = cras_iodev_set_format(&iodev_, &fmt);
337045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  EXPECT_EQ(0, rc);
338045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  EXPECT_EQ(SND_PCM_FORMAT_S16_LE, fmt.format);
339045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  EXPECT_EQ(44100, fmt.frame_rate);
340045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang  EXPECT_EQ(2, fmt.num_channels);
341045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang}
342045156126f648a9f0910152a66572a13447bfdc6Chih-Chung Chang
343838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reidextern "C" {
344838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
345838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid//  From libpthread.
346838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reidint pthread_create(pthread_t *thread, const pthread_attr_t *attr,
347838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid                   void *(*start_routine)(void*), void *arg) {
348838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  return 0;
349838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid}
350838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
351838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reidint pthread_join(pthread_t thread, void **value_ptr) {
352838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  return 0;
353838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid}
354838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
355838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid//  From librt.
356838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reidint clock_gettime(clockid_t clk_id, struct timespec *tp) {
357838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  tp->tv_sec = clock_gettime_retspec.tv_sec;
358838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  tp->tv_nsec = clock_gettime_retspec.tv_nsec;
359838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  return 0;
360838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid}
361838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
362386ab71e324831d632544f8bcecc8d0ba1890f01Dylan Reid// From cras_system_state.
363386ab71e324831d632544f8bcecc8d0ba1890f01Dylan Reidvoid cras_system_state_stream_added() {
364386ab71e324831d632544f8bcecc8d0ba1890f01Dylan Reid}
365386ab71e324831d632544f8bcecc8d0ba1890f01Dylan Reid
366386ab71e324831d632544f8bcecc8d0ba1890f01Dylan Reidvoid cras_system_state_stream_removed() {
367386ab71e324831d632544f8bcecc8d0ba1890f01Dylan Reid}
368386ab71e324831d632544f8bcecc8d0ba1890f01Dylan Reid
3695b1717f92baa7ab7006e04431bd6b33a24d8a23fChih-Chung Chang// From cras_dsp
3705b1717f92baa7ab7006e04431bd6b33a24d8a23fChih-Chung Changstruct cras_dsp_context *cras_dsp_context_new(int channels, int sample_rate,
3715b1717f92baa7ab7006e04431bd6b33a24d8a23fChih-Chung Chang                                              const char *purpose)
3725b1717f92baa7ab7006e04431bd6b33a24d8a23fChih-Chung Chang{
3735b1717f92baa7ab7006e04431bd6b33a24d8a23fChih-Chung Chang  return NULL;
3745b1717f92baa7ab7006e04431bd6b33a24d8a23fChih-Chung Chang}
3755b1717f92baa7ab7006e04431bd6b33a24d8a23fChih-Chung Chang
3765b1717f92baa7ab7006e04431bd6b33a24d8a23fChih-Chung Changvoid cras_dsp_context_free(struct cras_dsp_context *ctx)
3775b1717f92baa7ab7006e04431bd6b33a24d8a23fChih-Chung Chang{
3785b1717f92baa7ab7006e04431bd6b33a24d8a23fChih-Chung Chang}
3795b1717f92baa7ab7006e04431bd6b33a24d8a23fChih-Chung Chang
3805b1717f92baa7ab7006e04431bd6b33a24d8a23fChih-Chung Changvoid cras_dsp_load_pipeline(struct cras_dsp_context *ctx)
3815b1717f92baa7ab7006e04431bd6b33a24d8a23fChih-Chung Chang{
3825b1717f92baa7ab7006e04431bd6b33a24d8a23fChih-Chung Chang}
3835b1717f92baa7ab7006e04431bd6b33a24d8a23fChih-Chung Chang
384838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid}  // extern "C"
385838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid}  //  namespace
386838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid
387838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reidint main(int argc, char **argv) {
388838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  ::testing::InitGoogleTest(&argc, argv);
389838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid  return RUN_ALL_TESTS();
390838597c9ca04f19584c6fbcc78ee5f417c1528bfDylan Reid}
391