summaryrefslogtreecommitdiff
path: root/cras/src/tests/alsa_helpers_unittest.cc
blob: 32df30af396c86e38ba279cd2d8bcaacb83d2e7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <gtest/gtest.h>

#include <vector>

extern "C" {
// For static function test.
#include "cras_alsa_helpers.c"
}

static int snd_pcm_sw_params_set_tstamp_type_called;
static int snd_pcm_sw_params_set_tstamp_mode_called;
static snd_pcm_uframes_t snd_pcm_htimestamp_avail_ret_val;
static timespec snd_pcm_htimestamp_tstamp_ret_val;
static std::vector<int> snd_pcm_sw_params_ret_vals;

static void ResetStubData() {
  snd_pcm_sw_params_set_tstamp_type_called = 0;
  snd_pcm_sw_params_set_tstamp_mode_called = 0;
  snd_pcm_htimestamp_avail_ret_val = 0;
  snd_pcm_htimestamp_tstamp_ret_val.tv_sec = 0;
  snd_pcm_htimestamp_tstamp_ret_val.tv_nsec = 0;
  snd_pcm_sw_params_ret_vals.clear();
}

namespace {

static snd_pcm_chmap_query_t* create_chmap_cap(snd_pcm_chmap_type type,
                                               size_t channels) {
  snd_pcm_chmap_query_t* c;
  c = (snd_pcm_chmap_query_t*)calloc(channels + 2, sizeof(int));
  c->type = type;
  c->map.channels = channels;
  return c;
}

TEST(AlsaHelper, MatchChannelMapCapabilityStereo) {
  snd_pcm_chmap_query_t** caps;
  snd_pcm_chmap_query_t* c;
  struct cras_audio_format* fmt;

  caps = (snd_pcm_chmap_query_t**)calloc(4, sizeof(*caps));

  /* Layout (CRAS_CH_RL, CRAS_CH_RR) corresponds to
   * ALSA channel map (5, 6)
   */
  int8_t channel_layout[CRAS_CH_MAX] = {-1, -1, 0,  1,  -1, -1,
                                        -1, -1, -1, -1, -1};

  fmt = cras_audio_format_create(SND_PCM_FORMAT_S16_LE, 44100, 2);
  cras_audio_format_set_channel_layout(fmt, channel_layout);

  /* Create a list of capabilities */
  c = create_chmap_cap(SND_CHMAP_TYPE_FIXED, 3);
  c->map.pos[0] = 3;
  c->map.pos[1] = 4;
  c->map.pos[2] = 5;
  caps[0] = c;

  c = create_chmap_cap(SND_CHMAP_TYPE_VAR, 2);
  c->map.pos[0] = 5;
  c->map.pos[1] = 6;
  caps[1] = c;

  c = create_chmap_cap(SND_CHMAP_TYPE_VAR, 2);
  c->map.pos[0] = 9;
  c->map.pos[1] = 10;
  caps[2] = c;

  caps[3] = NULL;

  /* Test if there's a cap matches fmt */
  c = cras_chmap_caps_match(caps, fmt);
  ASSERT_NE((void*)NULL, c);

  caps[1]->map.pos[0] = 5;
  caps[1]->map.pos[1] = 7;

  c = cras_chmap_caps_match(caps, fmt);
  ASSERT_EQ((void*)NULL, c);

  free(caps[0]);
  free(caps[1]);
  free(caps[2]);
  free(caps[3]);
  free(caps);
  cras_audio_format_destroy(fmt);
}

TEST(AlsaHelper, MatchChannelMapCapability51) {
  snd_pcm_chmap_query_t** caps = NULL;
  snd_pcm_chmap_query_t* c = NULL;
  struct cras_audio_format* fmt;

  caps = (snd_pcm_chmap_query_t**)calloc(4, sizeof(*caps));

  /* Layout (CRAS_CH_FL, CRAS_CH_FR, CRAS_CH_RL, CRAS_CH_RR, CRAS_CH_FC)
   * corresponds to ALSA channel map (3, 4, 5, 6, 7)
   */
  int8_t channel_layout[CRAS_CH_MAX] = {0, 1, 2, 3, 4, 5, -1, -1, -1, -1, -1};

  fmt = cras_audio_format_create(SND_PCM_FORMAT_S16_LE, 44100, 6);
  cras_audio_format_set_channel_layout(fmt, channel_layout);

  /* Create a list of capabilities */
  c = create_chmap_cap(SND_CHMAP_TYPE_FIXED, 6);
  c->map.pos[0] = 3;
  c->map.pos[1] = 4;
  c->map.pos[2] = 5;
  c->map.pos[3] = 6;
  c->map.pos[4] = 7;
  c->map.pos[5] = 8;
  caps[0] = c;

  c = create_chmap_cap(SND_CHMAP_TYPE_VAR, 2);
  c->map.pos[0] = 6;
  c->map.pos[1] = 4;
  caps[1] = c;

  c = create_chmap_cap(SND_CHMAP_TYPE_VAR, 6);
  c->map.pos[0] = 9;
  c->map.pos[1] = 10;
  c->map.pos[2] = 5;
  c->map.pos[3] = 6;
  c->map.pos[4] = 7;
  c->map.pos[5] = 8;
  caps[2] = c;
  caps[3] = NULL;

  /* Test if there's a cap matches fmt */
  c = cras_chmap_caps_match(caps, fmt);
  ASSERT_NE((void*)NULL, c);

  caps[0]->map.pos[0] = 7;
  caps[0]->map.pos[1] = 8;
  caps[0]->map.pos[4] = 3;
  caps[0]->map.pos[5] = 4;
  c = cras_chmap_caps_match(caps, fmt);
  ASSERT_EQ((void*)NULL, c);

  caps[0]->type = SND_CHMAP_TYPE_PAIRED;
  c = cras_chmap_caps_match(caps, fmt);
  ASSERT_NE((void*)NULL, c);

  caps[0]->map.pos[0] = 8;
  caps[0]->map.pos[1] = 7;
  c = cras_chmap_caps_match(caps, fmt);
  ASSERT_EQ((void*)NULL, c);

  caps[0]->type = SND_CHMAP_TYPE_VAR;
  c = cras_chmap_caps_match(caps, fmt);
  ASSERT_NE((void*)NULL, c);

  free(caps[0]);
  free(caps[1]);
  free(caps[2]);
  free(caps[3]);
  free(caps);
  cras_audio_format_destroy(fmt);
}

TEST(AlsaHelper, Htimestamp) {
  snd_pcm_t* mock_handle = reinterpret_cast<snd_pcm_t*>(0x1);
  snd_pcm_uframes_t used;
  snd_pcm_uframes_t severe_underrun_frames = 480;
  struct timespec tstamp;
  const char* dev_name = "dev_name";

  ResetStubData();
  tstamp.tv_sec = 0;
  tstamp.tv_nsec = 0;
  snd_pcm_htimestamp_avail_ret_val = 20000;
  snd_pcm_htimestamp_tstamp_ret_val.tv_sec = 10;
  snd_pcm_htimestamp_tstamp_ret_val.tv_nsec = 10000;

  cras_alsa_get_avail_frames(mock_handle, 48000, severe_underrun_frames,
                             dev_name, &used, &tstamp);
  EXPECT_EQ(used, snd_pcm_htimestamp_avail_ret_val);
  EXPECT_EQ(tstamp.tv_sec, snd_pcm_htimestamp_tstamp_ret_val.tv_sec);
  EXPECT_EQ(tstamp.tv_nsec, snd_pcm_htimestamp_tstamp_ret_val.tv_nsec);
}

TEST(AlsaHelper, GetAvailFramesSevereUnderrun) {
  snd_pcm_t* mock_handle = reinterpret_cast<snd_pcm_t*>(0x1);
  snd_pcm_uframes_t avail;
  snd_pcm_uframes_t severe_underrun_frames = 480;
  snd_pcm_uframes_t buffer_size = 48000;
  struct timespec tstamp;
  int rc;
  const char* dev_name = "dev_name";

  ResetStubData();
  snd_pcm_htimestamp_avail_ret_val = buffer_size + severe_underrun_frames + 1;
  rc = cras_alsa_get_avail_frames(mock_handle, buffer_size,
                                  severe_underrun_frames, dev_name, &avail,
                                  &tstamp);
  // Returns -EPIPE when severe underrun happens.
  EXPECT_EQ(rc, -EPIPE);

  ResetStubData();
  snd_pcm_htimestamp_avail_ret_val = buffer_size + severe_underrun_frames;
  rc = cras_alsa_get_avail_frames(mock_handle, buffer_size,
                                  severe_underrun_frames, dev_name, &avail,
                                  &tstamp);
  // Underrun which is not severe enough will be masked.
  // avail will be adjusted to buffer_size.
  EXPECT_EQ(avail, buffer_size);
  EXPECT_EQ(rc, 0);

  ResetStubData();
  snd_pcm_htimestamp_avail_ret_val = buffer_size - 1;
  rc = cras_alsa_get_avail_frames(mock_handle, buffer_size,
                                  severe_underrun_frames, dev_name, &avail,
                                  &tstamp);
  // When avail < buffer_size, there is no underrun.
  EXPECT_EQ(avail, buffer_size - 1);
  EXPECT_EQ(rc, 0);
}
}  // namespace

extern "C" {

int snd_pcm_sw_params_current(snd_pcm_t* pcm, snd_pcm_sw_params_t* params) {
  return 0;
}

int snd_pcm_sw_params_get_boundary(const snd_pcm_sw_params_t* params,
                                   snd_pcm_uframes_t* val) {
  return 0;
}

int snd_pcm_sw_params_set_stop_threshold(snd_pcm_t* pcm,
                                         snd_pcm_sw_params_t* params,
                                         snd_pcm_uframes_t val) {
  return 0;
}

int snd_pcm_sw_params_set_start_threshold(snd_pcm_t* pcm,
                                          snd_pcm_sw_params_t* params,
                                          snd_pcm_uframes_t val) {
  return 0;
}

int snd_pcm_sw_params_set_period_event(snd_pcm_t* pcm,
                                       snd_pcm_sw_params_t* params,
                                       int val) {
  return 0;
}

int snd_pcm_sw_params_set_tstamp_mode(snd_pcm_t* pcm,
                                      snd_pcm_sw_params_t* params,
                                      snd_pcm_tstamp_t val) {
  snd_pcm_sw_params_set_tstamp_mode_called++;
  return 0;
}

int snd_pcm_sw_params_set_tstamp_type(snd_pcm_t* pcm,
                                      snd_pcm_sw_params_t* params,
                                      snd_pcm_tstamp_type_t val) {
  snd_pcm_sw_params_set_tstamp_type_called++;
  return 0;
}

int snd_pcm_sw_params(snd_pcm_t* pcm, snd_pcm_sw_params_t* params) {
  int rc;

  if (snd_pcm_sw_params_ret_vals.size() == 0)
    return 0;
  rc = snd_pcm_sw_params_ret_vals.back();
  snd_pcm_sw_params_ret_vals.pop_back();
  return rc;
}

snd_pcm_sframes_t snd_pcm_avail(snd_pcm_t* pcm) {
  return snd_pcm_htimestamp_avail_ret_val;
}

int snd_pcm_htimestamp(snd_pcm_t* pcm,
                       snd_pcm_uframes_t* avail,
                       snd_htimestamp_t* tstamp) {
  *avail = snd_pcm_htimestamp_avail_ret_val;
  *tstamp = snd_pcm_htimestamp_tstamp_ret_val;
  return 0;
}
}

int main(int argc, char** argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}