summaryrefslogtreecommitdiff
path: root/cras/src/tests/empty_iodev_unittest.cc
blob: 148d53012eb26639d3c6356c7fe3e1091a39cc55 (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
/* Copyright 2019 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>

extern "C" {
#include "cras_audio_area.h"
#include "cras_empty_iodev.h"
#include "cras_iodev.h"
}

static struct timespec clock_gettime_retspec;
static struct cras_audio_format fake_format;
static cras_audio_area mock_audio_area;

namespace {

TEST(EmptyIodev, GetInputBuffer) {
  struct cras_iodev* iodev;
  struct timespec ts;
  cras_audio_area* area;
  unsigned nframes;

  iodev = empty_iodev_create(CRAS_STREAM_INPUT, CRAS_NODE_TYPE_FALLBACK_NORMAL);

  clock_gettime_retspec.tv_sec = 0;
  clock_gettime_retspec.tv_nsec = 10000000;
  fake_format.frame_rate = 48000;
  iodev->format = &fake_format;
  iodev->configure_dev(iodev);

  clock_gettime_retspec.tv_nsec = 20000000;
  nframes = iodev->frames_queued(iodev, &ts);
  ASSERT_EQ(480, nframes);

  /* If client takes too long to get input data, number of frames
   * returned shouldn't exceeds device's own buffer size. */
  nframes = 5000;
  clock_gettime_retspec.tv_sec = 1;
  iodev->get_buffer(iodev, &area, &nframes);
  ASSERT_EQ(4096, nframes);

  iodev->close_dev(iodev);
  empty_iodev_destroy(iodev);
}

}  // namespace

extern "C" {

void cras_iodev_free_format(struct cras_iodev* iodev) {}

int cras_iodev_default_no_stream_playback(struct cras_iodev* odev, int enable) {
  return 0;
}

void cras_iodev_init_audio_area(struct cras_iodev* iodev, int num_channels) {
  iodev->area = &mock_audio_area;
}

void cras_iodev_free_audio_area(struct cras_iodev* iodev) {}

void cras_audio_area_config_buf_pointers(struct cras_audio_area* area,
                                         const struct cras_audio_format* fmt,
                                         uint8_t* base_buffer) {}

int cras_iodev_list_rm_input(struct cras_iodev* input) {
  return 0;
}

int cras_iodev_list_rm_output(struct cras_iodev* output) {
  return 0;
}

void cras_iodev_free_resources(struct cras_iodev* iodev) {}

void cras_iodev_add_node(struct cras_iodev* iodev, struct cras_ionode* node) {
  iodev->nodes = node;
}

void cras_iodev_set_active_node(struct cras_iodev* iodev,
                                struct cras_ionode* node) {
  iodev->active_node = node;
}

//  From librt.
int clock_gettime(clockid_t clk_id, struct timespec* tp) {
  tp->tv_sec = clock_gettime_retspec.tv_sec;
  tp->tv_nsec = clock_gettime_retspec.tv_nsec;
  return 0;
}

}  // extern "C"

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