summaryrefslogtreecommitdiff
path: root/cras/src/tests/dev_io_stubs.cc
blob: fc6c353e8071848bf1dec68e3464442d634122ea (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
// Copyright 2017 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 <algorithm>
#include <memory>
#include <stdint.h>
#include <stdio.h>
#include <time.h>

extern "C" {
#include "dev_stream.h"
#include "cras_rstream.h"
#include "cras_iodev.h"
#include "cras_shm.h"
#include "cras_types.h"
#include "utlist.h"
}

#include "dev_io_stubs.h"

ShmPtr create_shm(size_t cb_threshold) {
  uint32_t frame_bytes = 4;
  uint32_t used_size = cb_threshold * 2 * frame_bytes;
  uint32_t shm_size = sizeof(cras_audio_shm_area) + used_size * 2;
  ShmPtr shm(reinterpret_cast<cras_audio_shm_area*>(calloc(1, shm_size)),
              free);
  shm->config.used_size = used_size;
  shm->config.frame_bytes = frame_bytes;
  shm->volume_scaler = 1.0;
  return shm;
}

RstreamPtr create_rstream(cras_stream_id_t id,
                          CRAS_STREAM_DIRECTION direction,
                          size_t cb_threshold,
                          const cras_audio_format* format,
                          cras_audio_shm_area* shm) {
  RstreamPtr rstream(
      reinterpret_cast<cras_rstream*>(calloc(1, sizeof(cras_rstream))), free);
  rstream->stream_id = id;
  rstream->direction = direction;
  rstream->fd = RSTREAM_FAKE_POLL_FD;
  rstream->buffer_frames = cb_threshold * 2;
  rstream->cb_threshold = cb_threshold;
  rstream->shm.area = shm;
  rstream->shm.config = shm->config;
  rstream->format = *format;
  cras_frames_to_time(cb_threshold,
                      rstream->format.frame_rate,
                      &rstream->sleep_interval_ts);
  return rstream;
}

DevStreamPtr create_dev_stream(unsigned int dev_id, cras_rstream* rstream) {
  DevStreamPtr dstream(
      reinterpret_cast<dev_stream*>(calloc(1, sizeof(dev_stream))),
      free);
  dstream->dev_id = dev_id;
  dstream->stream = rstream;
  dstream->dev_rate = rstream->format.frame_rate;
  return dstream;
}

StreamPtr create_stream(cras_stream_id_t id,
                        unsigned int dev_id,
                        CRAS_STREAM_DIRECTION direction,
                        size_t cb_threshold,
                        const cras_audio_format* format) {
  ShmPtr shm = create_shm(cb_threshold);
  RstreamPtr rstream = create_rstream(1, CRAS_STREAM_INPUT, cb_threshold,
                                      format, shm.get());
  DevStreamPtr dstream = create_dev_stream(1, rstream.get());
  StreamPtr s(new Stream(std::move(shm),
                         std::move(rstream),
                         std::move(dstream)));
  return s;
}

void AddFakeDataToStream(Stream* stream, unsigned int frames) {
  cras_shm_check_write_overrun(&stream->rstream->shm);
  cras_shm_buffer_written(&stream->rstream->shm, frames);
}

int delay_frames_stub(const struct cras_iodev* iodev) {
  return 0;
}

IonodePtr create_ionode(CRAS_NODE_TYPE type) {
  IonodePtr ionode(
      reinterpret_cast<cras_ionode*>(calloc(1, sizeof(cras_ionode))), free);
  ionode->type = type;
  return ionode;
}

IodevPtr create_open_iodev(CRAS_STREAM_DIRECTION direction,
                           size_t cb_threshold,
                           cras_audio_format* format,
                           cras_ionode* active_node) {
  IodevPtr iodev(reinterpret_cast<cras_iodev*>(calloc(1, sizeof(cras_iodev))),
                  free);
  iodev->is_enabled = 1;
  iodev->direction = direction;
  iodev->format = format;
  iodev->state = CRAS_IODEV_STATE_OPEN;
  iodev->delay_frames = delay_frames_stub;
  iodev->active_node = active_node;
  iodev->buffer_size = cb_threshold * 2;
  iodev->min_cb_level = UINT_MAX;
  iodev->max_cb_level = 0;
  return iodev;
}

DevicePtr create_device(CRAS_STREAM_DIRECTION direction,
                        size_t cb_threshold,
                        cras_audio_format* format,
                        CRAS_NODE_TYPE active_node_type) {
  IonodePtr node = create_ionode(active_node_type);
  IodevPtr dev = create_open_iodev(direction, cb_threshold, format, node.get());
  OpendevPtr odev(
      reinterpret_cast<open_dev*>(calloc(1, sizeof(open_dev))), free);
  odev->dev = dev.get();

  DevicePtr d(new Device(std::move(dev), std::move(node), std::move(odev)));
  return d;
}

void add_stream_to_dev(IodevPtr& dev, const StreamPtr& stream) {
  DL_APPEND(dev->streams, stream->dstream.get());
  dev->min_cb_level = std::min(stream->rstream->cb_threshold,
                               static_cast<size_t>(dev->min_cb_level));
  dev->max_cb_level = std::max(stream->rstream->cb_threshold,
                               static_cast<size_t>(dev->max_cb_level));
}

void fill_audio_format(cras_audio_format* format, unsigned int rate) {
  format->format = SND_PCM_FORMAT_S16_LE;
  format->frame_rate = rate;
  format->num_channels = 2;
  format->channel_layout[0] = 0;
  format->channel_layout[1] = 1;
  for (int i = 2; i < CRAS_CH_MAX; i++)
    format->channel_layout[i] = -1;
}