summaryrefslogtreecommitdiff
path: root/cras/src/tests/hfp_info_unittest.cc
blob: 482c3a9957ad398e26b1d0659c4ed69ba9826af1 (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/* Copyright 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 <stdint.h>
#include <time.h>

extern "C" {
#include "cras_hfp_info.c"
#include "sbc_codec_stub.h"
}
static struct hfp_info* info;
static struct cras_iodev dev;
static cras_audio_format format;

static int cras_msbc_plc_create_called;
static int cras_msbc_plc_handle_good_frames_called;
static int cras_msbc_plc_handle_bad_frames_called;

static thread_callback thread_cb;
static void* cb_data;
static timespec ts;

void ResetStubData() {
  sbc_codec_stub_reset();
  cras_msbc_plc_create_called = 0;

  format.format = SND_PCM_FORMAT_S16_LE;
  format.num_channels = 1;
  format.frame_rate = 8000;
  dev.format = &format;
}

namespace {

TEST(HfpInfo, AddRmDev) {
  ResetStubData();

  info = hfp_info_create(HFP_CODEC_ID_CVSD);
  ASSERT_NE(info, (void*)NULL);
  dev.direction = CRAS_STREAM_OUTPUT;

  /* Test add dev */
  ASSERT_EQ(0, hfp_info_add_iodev(info, dev.direction, dev.format));
  ASSERT_TRUE(hfp_info_has_iodev(info));

  /* Test remove dev */
  ASSERT_EQ(0, hfp_info_rm_iodev(info, dev.direction));
  ASSERT_FALSE(hfp_info_has_iodev(info));

  hfp_info_destroy(info);
}

TEST(HfpInfo, AddRmDevInvalid) {
  ResetStubData();

  info = hfp_info_create(HFP_CODEC_ID_CVSD);
  ASSERT_NE(info, (void*)NULL);

  dev.direction = CRAS_STREAM_OUTPUT;

  /* Remove an iodev which doesn't exist */
  ASSERT_NE(0, hfp_info_rm_iodev(info, dev.direction));

  /* Adding an iodev twice returns error code */
  ASSERT_EQ(0, hfp_info_add_iodev(info, dev.direction, dev.format));
  ASSERT_NE(0, hfp_info_add_iodev(info, dev.direction, dev.format));

  hfp_info_destroy(info);
}

TEST(HfpInfo, AcquirePlaybackBuffer) {
  unsigned buffer_frames, buffer_frames2, queued;
  uint8_t* samples;

  ResetStubData();

  info = hfp_info_create(HFP_CODEC_ID_CVSD);
  ASSERT_NE(info, (void*)NULL);

  hfp_info_start(1, 48, info);
  dev.direction = CRAS_STREAM_OUTPUT;
  ASSERT_EQ(0, hfp_info_add_iodev(info, dev.direction, dev.format));

  buffer_frames = 500;
  hfp_buf_acquire(info, dev.direction, &samples, &buffer_frames);
  ASSERT_EQ(500, buffer_frames);

  hfp_buf_release(info, dev.direction, 500);
  ASSERT_EQ(500, hfp_buf_queued(info, dev.direction));

  /* Assert the amount of frames of available buffer + queued buf is
   * greater than or equal to the buffer size, 2 bytes per frame
   */
  queued = hfp_buf_queued(info, dev.direction);
  buffer_frames = 500;
  hfp_buf_acquire(info, dev.direction, &samples, &buffer_frames);
  ASSERT_GE(info->playback_buf->used_size / 2, buffer_frames + queued);

  /* Consume all queued data from read buffer */
  buf_increment_read(info->playback_buf, queued * 2);

  queued = hfp_buf_queued(info, dev.direction);
  ASSERT_EQ(0, queued);

  /* Assert consecutive acquire buffer will acquire full used size of buffer */
  buffer_frames = 500;
  hfp_buf_acquire(info, dev.direction, &samples, &buffer_frames);
  hfp_buf_release(info, dev.direction, buffer_frames);

  buffer_frames2 = 500;
  hfp_buf_acquire(info, dev.direction, &samples, &buffer_frames2);
  hfp_buf_release(info, dev.direction, buffer_frames2);

  ASSERT_GE(info->playback_buf->used_size / 2, buffer_frames + buffer_frames2);

  hfp_info_destroy(info);
}

TEST(HfpInfo, AcquireCaptureBuffer) {
  unsigned buffer_frames, buffer_frames2;
  uint8_t* samples;

  ResetStubData();

  info = hfp_info_create(HFP_CODEC_ID_CVSD);
  ASSERT_NE(info, (void*)NULL);

  hfp_info_start(1, 48, info);
  dev.direction = CRAS_STREAM_INPUT;
  ASSERT_EQ(0, hfp_info_add_iodev(info, dev.direction, dev.format));

  /* Put fake data 100 bytes(50 frames) in capture buf for test */
  buf_increment_write(info->capture_buf, 100);

  /* Assert successfully acquire and release 100 bytes of data */
  buffer_frames = 50;
  hfp_buf_acquire(info, dev.direction, &samples, &buffer_frames);
  ASSERT_EQ(50, buffer_frames);

  hfp_buf_release(info, dev.direction, buffer_frames);
  ASSERT_EQ(0, hfp_buf_queued(info, dev.direction));

  /* Push fake data to capture buffer */
  buf_increment_write(info->capture_buf, info->capture_buf->used_size - 100);
  buf_increment_write(info->capture_buf, 100);

  /* Assert consecutive acquire call will consume the whole buffer */
  buffer_frames = 1000;
  hfp_buf_acquire(info, dev.direction, &samples, &buffer_frames);
  hfp_buf_release(info, dev.direction, buffer_frames);
  ASSERT_GE(1000, buffer_frames);

  buffer_frames2 = 1000;
  hfp_buf_acquire(info, dev.direction, &samples, &buffer_frames2);
  hfp_buf_release(info, dev.direction, buffer_frames2);

  ASSERT_GE(info->capture_buf->used_size / 2, buffer_frames + buffer_frames2);

  hfp_info_destroy(info);
}

TEST(HfpInfo, HfpReadWriteFD) {
  int rc;
  int sock[2];
  uint8_t sample[480];
  uint8_t* buf;
  unsigned buffer_count;

  ResetStubData();

  ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, sock));

  info = hfp_info_create(HFP_CODEC_ID_CVSD);
  ASSERT_NE(info, (void*)NULL);

  dev.direction = CRAS_STREAM_INPUT;
  hfp_info_start(sock[1], 48, info);
  ASSERT_EQ(0, hfp_info_add_iodev(info, dev.direction, dev.format));

  /* Mock the sco fd and send some fake data */
  send(sock[0], sample, 48, 0);

  rc = hfp_read(info);
  ASSERT_EQ(48, rc);

  rc = hfp_buf_queued(info, dev.direction);
  ASSERT_EQ(48 / 2, rc);

  /* Fill the write buffer*/
  buffer_count = info->capture_buf->used_size;
  buf = buf_write_pointer_size(info->capture_buf, &buffer_count);
  buf_increment_write(info->capture_buf, buffer_count);
  ASSERT_NE((void*)NULL, buf);

  rc = hfp_read(info);
  ASSERT_EQ(0, rc);

  ASSERT_EQ(0, hfp_info_rm_iodev(info, dev.direction));
  dev.direction = CRAS_STREAM_OUTPUT;
  ASSERT_EQ(0, hfp_info_add_iodev(info, dev.direction, dev.format));

  /* Initial buffer is empty */
  rc = hfp_write(info);
  ASSERT_EQ(0, rc);

  buffer_count = 1024;
  buf = buf_write_pointer_size(info->playback_buf, &buffer_count);
  buf_increment_write(info->playback_buf, buffer_count);

  rc = hfp_write(info);
  ASSERT_EQ(48, rc);

  rc = recv(sock[0], sample, 48, 0);
  ASSERT_EQ(48, rc);

  hfp_info_destroy(info);
}

TEST(HfpInfo, StartHfpInfo) {
  int sock[2];

  ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, sock));

  info = hfp_info_create(HFP_CODEC_ID_CVSD);
  ASSERT_NE(info, (void*)NULL);

  hfp_info_start(sock[0], 48, info);
  ASSERT_EQ(1, hfp_info_running(info));
  ASSERT_EQ(cb_data, (void*)info);

  hfp_info_stop(info);
  ASSERT_EQ(0, hfp_info_running(info));
  ASSERT_EQ(NULL, cb_data);

  hfp_info_destroy(info);
}

TEST(HfpInfo, StartHfpInfoAndRead) {
  int rc;
  int sock[2];
  uint8_t sample[480];

  ResetStubData();

  ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, sock));

  info = hfp_info_create(HFP_CODEC_ID_CVSD);
  ASSERT_NE(info, (void*)NULL);

  /* Start and send two chunk of fake data */
  hfp_info_start(sock[1], 48, info);
  send(sock[0], sample, 48, 0);
  send(sock[0], sample, 48, 0);

  /* Trigger thread callback */
  thread_cb((struct hfp_info*)cb_data);

  dev.direction = CRAS_STREAM_INPUT;
  ASSERT_EQ(0, hfp_info_add_iodev(info, dev.direction, dev.format));

  /* Expect no data read, since no idev present at previous thread callback */
  rc = hfp_buf_queued(info, dev.direction);
  ASSERT_EQ(0, rc);

  /* Trigger thread callback after idev added. */
  ts.tv_sec = 0;
  ts.tv_nsec = 5000000;
  thread_cb((struct hfp_info*)cb_data);

  rc = hfp_buf_queued(info, dev.direction);
  ASSERT_EQ(48 / 2, rc);

  /* Assert wait time is unchanged. */
  ASSERT_EQ(0, ts.tv_sec);
  ASSERT_EQ(5000000, ts.tv_nsec);

  hfp_info_stop(info);
  ASSERT_EQ(0, hfp_info_running(info));

  hfp_info_destroy(info);
}

TEST(HfpInfo, StartHfpInfoAndWrite) {
  int rc;
  int sock[2];
  uint8_t sample[480];

  ResetStubData();

  ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, sock));

  info = hfp_info_create(HFP_CODEC_ID_CVSD);
  ASSERT_NE(info, (void*)NULL);

  hfp_info_start(sock[1], 48, info);
  send(sock[0], sample, 48, 0);
  send(sock[0], sample, 48, 0);

  /* Trigger thread callback */
  thread_cb((struct hfp_info*)cb_data);

  /* Without odev in presence, zero packet should be sent. */
  rc = recv(sock[0], sample, 48, 0);
  ASSERT_EQ(48, rc);

  dev.direction = CRAS_STREAM_OUTPUT;
  ASSERT_EQ(0, hfp_info_add_iodev(info, dev.direction, dev.format));

  /* Assert queued samples unchanged before output device added */
  ASSERT_EQ(0, hfp_buf_queued(info, dev.direction));

  /* Put some fake data and trigger thread callback again */
  buf_increment_write(info->playback_buf, 1008);
  thread_cb((struct hfp_info*)cb_data);

  /* Assert some samples written */
  rc = recv(sock[0], sample, 48, 0);
  ASSERT_EQ(48, rc);
  ASSERT_EQ(480, hfp_buf_queued(info, dev.direction));

  hfp_info_stop(info);
  hfp_info_destroy(info);
}

void send_mSBC_packet(int fd, unsigned seq, int broken_pkt) {
  /* These three bytes are h2 header, frame count and mSBC sync word.
   * The second octet of H2 header is composed by 4 bits fixed 0x8 and 4 bits
   * sequence number 0000, 0011, 1100, 1111.
   */
  uint8_t headers[4][3] = {{0x01, 0x08, 0xAD},
                           {0x01, 0x38, 0xAD},
                           {0x01, 0xc8, 0xAD},
                           {0x01, 0xf8, 0xAD}};
  /* These three bytes are HCI SCO Data packet header, we only care the
   * Packet_Status_Flag bits, which are the bit 4 to 5 in the second octet.
   */
  uint8_t sco_header[] = {0x01, 0x01, 0x3c};
  uint8_t zero_frame[] = {
      0xad, 0x00, 0x00, 0xc5, 0x00, 0x00, 0x00, 0x00, 0x77, 0x6d, 0xb6, 0xdd,
      0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77, 0x6d, 0xb6,
      0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77, 0x6d,
      0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77,
      0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6c};
  if (broken_pkt)
    sco_header[1] = 0x11;

  send(fd, sco_header, 3, 0);
  send(fd, headers[seq % 4], 3, 0);
  send(fd, zero_frame, 57, 0);
}

TEST(HfpInfo, StartHfpInfoAndReadMsbc) {
  int sock[2];
  int pkt_count = 0;
  int rc;
  uint8_t sample[480];
  ResetStubData();

  ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, sock));

  set_sbc_codec_decoded_out(MSBC_CODE_SIZE);

  info = hfp_info_create(HFP_CODEC_ID_MSBC);
  ASSERT_NE(info, (void*)NULL);
  ASSERT_EQ(2, get_msbc_codec_create_called());
  ASSERT_EQ(1, cras_msbc_plc_create_called);

  /* Start and send an mSBC packets with all zero samples */
  hfp_info_start(sock[1], 63, info);
  send_mSBC_packet(sock[0], pkt_count++, 0);

  /* Trigger thread callback */
  thread_cb((struct hfp_info*)cb_data);

  /* Expect one empty mSBC packet is send, because no odev in presence. */
  rc = recv(sock[0], sample, MSBC_PKT_SIZE, 0);
  ASSERT_EQ(MSBC_PKT_SIZE, rc);

  dev.direction = CRAS_STREAM_INPUT;
  ASSERT_EQ(0, hfp_info_add_iodev(info, dev.direction, dev.format));

  /* Expect no data read, since no idev present at previous thread callback */
  ASSERT_EQ(0, hfp_buf_queued(info, dev.direction));

  send_mSBC_packet(sock[0], pkt_count, 0);

  /* Trigger thread callback after idev added. */
  thread_cb((struct hfp_info*)cb_data);
  rc = recv(sock[0], sample, MSBC_PKT_SIZE, 0);
  ASSERT_EQ(MSBC_PKT_SIZE, rc);

  ASSERT_EQ(pkt_count * MSBC_CODE_SIZE / 2,
            hfp_buf_queued(info, dev.direction));
  ASSERT_EQ(2, cras_msbc_plc_handle_good_frames_called);
  pkt_count++;
  /* When the third packet is lost, we should call the handle_bad_packet and
   * still have right size of samples queued
   */
  pkt_count++;
  send_mSBC_packet(sock[0], pkt_count, 0);
  thread_cb((struct hfp_info*)cb_data);
  rc = recv(sock[0], sample, MSBC_PKT_SIZE, 0);
  ASSERT_EQ(MSBC_PKT_SIZE, rc);

  /* Packet 1, 2, 4 are all good frames */
  ASSERT_EQ(3, cras_msbc_plc_handle_good_frames_called);
  ASSERT_EQ(1, cras_msbc_plc_handle_bad_frames_called);
  ASSERT_EQ(pkt_count * MSBC_CODE_SIZE / 2,
            hfp_buf_queued(info, dev.direction));
  pkt_count++;
  /* If the erroneous data reporting marks the packet as broken, we should
   * also call the handle_bad_packet and have the right size of samples queued.
   */
  send_mSBC_packet(sock[0], pkt_count, 1);

  set_sbc_codec_decoded_fail(1);

  thread_cb((struct hfp_info*)cb_data);
  rc = recv(sock[0], sample, MSBC_PKT_SIZE, 0);
  ASSERT_EQ(MSBC_PKT_SIZE, rc);

  ASSERT_EQ(3, cras_msbc_plc_handle_good_frames_called);
  ASSERT_EQ(2, cras_msbc_plc_handle_bad_frames_called);
  ASSERT_EQ(pkt_count * MSBC_CODE_SIZE / 2,
            hfp_buf_queued(info, dev.direction));
  pkt_count++;
  /* If we can't decode the packet, we should also call the handle_bad_packet
   * and have the right size of samples queued
   */
  send_mSBC_packet(sock[0], pkt_count, 0);

  set_sbc_codec_decoded_fail(1);

  thread_cb((struct hfp_info*)cb_data);
  rc = recv(sock[0], sample, MSBC_PKT_SIZE, 0);
  ASSERT_EQ(MSBC_PKT_SIZE, rc);

  ASSERT_EQ(3, cras_msbc_plc_handle_good_frames_called);
  ASSERT_EQ(3, cras_msbc_plc_handle_bad_frames_called);
  ASSERT_EQ(pkt_count * MSBC_CODE_SIZE / 2,
            hfp_buf_queued(info, dev.direction));

  hfp_info_stop(info);
  ASSERT_EQ(0, hfp_info_running(info));

  hfp_info_destroy(info);
}

TEST(HfpInfo, StartHfpInfoAndWriteMsbc) {
  int rc;
  int sock[2];
  uint8_t sample[480];

  ResetStubData();

  set_sbc_codec_encoded_out(57);
  ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, sock));

  info = hfp_info_create(HFP_CODEC_ID_MSBC);
  ASSERT_NE(info, (void*)NULL);

  hfp_info_start(sock[1], 63, info);
  send(sock[0], sample, 63, 0);

  /* Trigger thread callback */
  thread_cb((struct hfp_info*)cb_data);

  dev.direction = CRAS_STREAM_OUTPUT;
  ASSERT_EQ(0, hfp_info_add_iodev(info, dev.direction, dev.format));

  /* Assert queued samples unchanged before output device added */
  ASSERT_EQ(0, hfp_buf_queued(info, dev.direction));

  /* Put some fake data and trigger thread callback again */
  send(sock[0], sample, 63, 0);
  buf_increment_write(info->playback_buf, 240);
  thread_cb((struct hfp_info*)cb_data);

  /* Assert some samples written */
  rc = recv(sock[0], sample, 60, 0);
  ASSERT_EQ(60, rc);
  ASSERT_EQ(0, hfp_buf_queued(info, dev.direction));

  hfp_info_stop(info);
  hfp_info_destroy(info);
}

}  // namespace

extern "C" {

struct audio_thread* cras_iodev_list_get_audio_thread() {
  return NULL;
}

void audio_thread_add_callback(int fd, thread_callback cb, void* data) {
  thread_cb = cb;
  cb_data = data;
  return;
}

int audio_thread_rm_callback_sync(struct audio_thread* thread, int fd) {
  thread_cb = NULL;
  cb_data = NULL;
  return 0;
}

void audio_thread_rm_callback(int fd) {}

struct cras_msbc_plc* cras_msbc_plc_create() {
  cras_msbc_plc_create_called++;
  return NULL;
}

void cras_msbc_plc_destroy(struct cras_msbc_plc* plc) {}

int cras_msbc_plc_handle_bad_frames(struct cras_msbc_plc* plc,
                                    struct cras_audio_codec* codec,
                                    uint8_t* output) {
  cras_msbc_plc_handle_bad_frames_called++;
  return MSBC_CODE_SIZE;
}

int cras_msbc_plc_handle_good_frames(struct cras_msbc_plc* plc,
                                     const uint8_t* input,
                                     uint8_t* output) {
  cras_msbc_plc_handle_good_frames_called++;
  return MSBC_CODE_SIZE;
}
}

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