1 /* Copyright 2017 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6 #include <time.h>
7
8 #include <unordered_map>
9
10 extern "C" {
11 #include "cras_iodev.h"
12 #include "dev_stream.h"
13 #include "utlist.h"
14 }
15
16 namespace {
17 struct cb_data {
18 int frames_queued_ret;
19 timespec frames_queued_ts;
20 };
21 std::unordered_map<cras_iodev*, cb_data> frames_queued_map;
22 std::unordered_map<cras_iodev*, cb_data> valid_frames_map;
23 std::unordered_map<cras_iodev*, timespec> drop_time_map;
24 std::unordered_map<const cras_iodev*, double> est_rate_ratio_map;
25 std::unordered_map<const cras_iodev*, int> update_rate_map;
26 std::unordered_map<const cras_ionode*, int> on_internal_card_map;
27 } // namespace
28
iodev_stub_reset()29 void iodev_stub_reset() {
30 frames_queued_map.clear();
31 valid_frames_map.clear();
32 drop_time_map.clear();
33 est_rate_ratio_map.clear();
34 update_rate_map.clear();
35 on_internal_card_map.clear();
36 }
37
iodev_stub_est_rate_ratio(cras_iodev * iodev,double ratio)38 void iodev_stub_est_rate_ratio(cras_iodev* iodev, double ratio) {
39 est_rate_ratio_map.insert({iodev, ratio});
40 }
41
iodev_stub_update_rate(cras_iodev * iodev,int data)42 void iodev_stub_update_rate(cras_iodev* iodev, int data) {
43 update_rate_map.insert({iodev, data});
44 }
45
iodev_stub_on_internal_card(cras_ionode * node,int data)46 void iodev_stub_on_internal_card(cras_ionode* node, int data) {
47 on_internal_card_map.insert({node, data});
48 }
49
iodev_stub_frames_queued(cras_iodev * iodev,int ret,timespec ts)50 void iodev_stub_frames_queued(cras_iodev* iodev, int ret, timespec ts) {
51 cb_data data = {ret, ts};
52 frames_queued_map.insert({iodev, data});
53 }
54
iodev_stub_valid_frames(cras_iodev * iodev,int ret,timespec ts)55 void iodev_stub_valid_frames(cras_iodev* iodev, int ret, timespec ts) {
56 cb_data data = {ret, ts};
57 valid_frames_map.insert({iodev, data});
58 }
59
iodev_stub_get_drop_time(cras_iodev * iodev,timespec * ts)60 bool iodev_stub_get_drop_time(cras_iodev* iodev, timespec* ts) {
61 auto elem = drop_time_map.find(iodev);
62 if (elem != drop_time_map.end()) {
63 *ts = elem->second;
64 return true;
65 }
66 return false;
67 }
68
69 extern "C" {
70
cras_iodev_add_stream(struct cras_iodev * iodev,struct dev_stream * stream)71 int cras_iodev_add_stream(struct cras_iodev* iodev, struct dev_stream* stream) {
72 DL_APPEND(iodev->streams, stream);
73 return 0;
74 }
75
cras_iodev_get_valid_frames(struct cras_iodev * iodev,struct timespec * tstamp)76 int cras_iodev_get_valid_frames(struct cras_iodev* iodev,
77 struct timespec* tstamp) {
78 auto elem = valid_frames_map.find(iodev);
79 if (elem != valid_frames_map.end()) {
80 *tstamp = elem->second.frames_queued_ts;
81 return elem->second.frames_queued_ret;
82 }
83 clock_gettime(CLOCK_MONOTONIC_RAW, tstamp);
84 return 0;
85 }
86
cras_iodev_get_est_rate_ratio(const struct cras_iodev * iodev)87 double cras_iodev_get_est_rate_ratio(const struct cras_iodev* iodev) {
88 auto elem = est_rate_ratio_map.find(iodev);
89 if (elem != est_rate_ratio_map.end()) {
90 return elem->second;
91 }
92 return 1.0f;
93 }
94
cras_iodev_get_dsp_delay(const struct cras_iodev * iodev)95 int cras_iodev_get_dsp_delay(const struct cras_iodev* iodev) {
96 return 0;
97 }
98
cras_iodev_frames_queued(struct cras_iodev * iodev,struct timespec * tstamp)99 int cras_iodev_frames_queued(struct cras_iodev* iodev,
100 struct timespec* tstamp) {
101 auto elem = frames_queued_map.find(iodev);
102 if (elem != frames_queued_map.end()) {
103 *tstamp = elem->second.frames_queued_ts;
104 return elem->second.frames_queued_ret;
105 }
106 clock_gettime(CLOCK_MONOTONIC_RAW, tstamp);
107 return 0;
108 }
109
cras_iodev_rm_stream(struct cras_iodev * iodev,const struct cras_rstream * stream)110 struct dev_stream* cras_iodev_rm_stream(struct cras_iodev* iodev,
111 const struct cras_rstream* stream) {
112 return NULL;
113 }
114
cras_iodev_update_rate(struct cras_iodev * iodev,unsigned int level,struct timespec * level_tstamp)115 int cras_iodev_update_rate(struct cras_iodev* iodev,
116 unsigned int level,
117 struct timespec* level_tstamp) {
118 auto elem = update_rate_map.find(iodev);
119 if (elem != update_rate_map.end()) {
120 return elem->second;
121 }
122 return 0;
123 }
124
cras_iodev_state(const struct cras_iodev * iodev)125 enum CRAS_IODEV_STATE cras_iodev_state(const struct cras_iodev* iodev) {
126 return iodev->state;
127 }
128
cras_iodev_all_streams_written(struct cras_iodev * iodev)129 unsigned int cras_iodev_all_streams_written(struct cras_iodev* iodev) {
130 return 0;
131 }
132
cras_iodev_put_input_buffer(struct cras_iodev * iodev)133 int cras_iodev_put_input_buffer(struct cras_iodev* iodev) {
134 return 0;
135 }
136
cras_iodev_put_output_buffer(struct cras_iodev * iodev,uint8_t * frames,unsigned int nframes,int * non_empty,struct cras_fmt_conv * output_converter)137 int cras_iodev_put_output_buffer(struct cras_iodev* iodev,
138 uint8_t* frames,
139 unsigned int nframes,
140 int* non_empty,
141 struct cras_fmt_conv* output_converter) {
142 return 0;
143 }
144
cras_iodev_get_input_buffer(struct cras_iodev * iodev,unsigned * frames)145 int cras_iodev_get_input_buffer(struct cras_iodev* iodev, unsigned* frames) {
146 return 0;
147 }
148
cras_iodev_get_output_buffer(struct cras_iodev * iodev,struct cras_audio_area ** area,unsigned * frames)149 int cras_iodev_get_output_buffer(struct cras_iodev* iodev,
150 struct cras_audio_area** area,
151 unsigned* frames) {
152 return 0;
153 }
154
cras_iodev_get_software_gain_scaler(const struct cras_iodev * iodev)155 float cras_iodev_get_software_gain_scaler(const struct cras_iodev* iodev) {
156 return 0.0;
157 }
158
cras_iodev_stream_written(struct cras_iodev * iodev,struct dev_stream * stream,unsigned int nwritten)159 void cras_iodev_stream_written(struct cras_iodev* iodev,
160 struct dev_stream* stream,
161 unsigned int nwritten) {}
162
cras_iodev_prepare_output_before_write_samples(struct cras_iodev * odev)163 int cras_iodev_prepare_output_before_write_samples(struct cras_iodev* odev) {
164 return 0;
165 }
166
cras_iodev_buffer_avail(struct cras_iodev * iodev,unsigned hw_level)167 int cras_iodev_buffer_avail(struct cras_iodev* iodev, unsigned hw_level) {
168 return 0;
169 }
170
cras_iodev_max_stream_offset(const struct cras_iodev * iodev)171 unsigned int cras_iodev_max_stream_offset(const struct cras_iodev* iodev) {
172 return 0;
173 }
174
cras_iodev_odev_should_wake(const struct cras_iodev * odev)175 int cras_iodev_odev_should_wake(const struct cras_iodev* odev) {
176 return 1;
177 }
178
cras_iodev_output_underrun(struct cras_iodev * odev,unsigned int hw_level,unsigned int frames_written)179 int cras_iodev_output_underrun(struct cras_iodev* odev,
180 unsigned int hw_level,
181 unsigned int frames_written) {
182 return 0;
183 }
184
cras_iodev_reset_request(struct cras_iodev * iodev)185 int cras_iodev_reset_request(struct cras_iodev* iodev) {
186 return 0;
187 }
188
cras_iodev_stream_offset(struct cras_iodev * iodev,struct dev_stream * stream)189 unsigned int cras_iodev_stream_offset(struct cras_iodev* iodev,
190 struct dev_stream* stream) {
191 return 0;
192 }
193
cras_iodev_get_num_underruns(const struct cras_iodev * iodev)194 unsigned int cras_iodev_get_num_underruns(const struct cras_iodev* iodev) {
195 return 0;
196 }
197
cras_iodev_frames_to_play_in_sleep(struct cras_iodev * odev,unsigned int * hw_level,struct timespec * hw_tstamp)198 unsigned int cras_iodev_frames_to_play_in_sleep(struct cras_iodev* odev,
199 unsigned int* hw_level,
200 struct timespec* hw_tstamp) {
201 clock_gettime(CLOCK_MONOTONIC_RAW, hw_tstamp);
202 *hw_level = 0;
203 return 0;
204 }
205
cras_iodev_update_highest_hw_level(struct cras_iodev * iodev,unsigned int hw_level)206 void cras_iodev_update_highest_hw_level(struct cras_iodev* iodev,
207 unsigned int hw_level) {}
208
cras_iodev_start_stream(struct cras_iodev * iodev,struct dev_stream * stream)209 void cras_iodev_start_stream(struct cras_iodev* iodev,
210 struct dev_stream* stream) {}
211
cras_iodev_drop_frames_by_time(struct cras_iodev * iodev,struct timespec ts)212 int cras_iodev_drop_frames_by_time(struct cras_iodev* iodev,
213 struct timespec ts) {
214 drop_time_map.insert({iodev, ts});
215 return 0;
216 }
217
cras_iodev_is_on_internal_card(const struct cras_ionode * node)218 bool cras_iodev_is_on_internal_card(const struct cras_ionode* node) {
219 auto elem = on_internal_card_map.find(node);
220 if (elem != on_internal_card_map.end()) {
221 return elem->second;
222 }
223 return 1;
224 }
225 } // extern "C"
226