1 /* Copyright (c) 2012 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 <pthread.h>
7 #include <stdbool.h>
8 #include <stdlib.h>
9 #include <sys/param.h>
10 #include <sys/time.h>
11 #include <syslog.h>
12 #include <time.h>
13
14 #include "audio_thread.h"
15 #include "audio_thread_log.h"
16 #include "buffer_share.h"
17 #include "cras_audio_area.h"
18 #include "cras_audio_thread_monitor.h"
19 #include "cras_device_monitor.h"
20 #include "cras_dsp.h"
21 #include "cras_dsp_pipeline.h"
22 #include "cras_fmt_conv.h"
23 #include "cras_iodev.h"
24 #include "cras_iodev_list.h"
25 #include "cras_mix.h"
26 #include "cras_ramp.h"
27 #include "cras_rstream.h"
28 #include "cras_server_metrics.h"
29 #include "cras_system_state.h"
30 #include "cras_util.h"
31 #include "dev_stream.h"
32 #include "input_data.h"
33 #include "utlist.h"
34 #include "rate_estimator.h"
35 #include "softvol_curve.h"
36
37 static const float RAMP_UNMUTE_DURATION_SECS = 0.5;
38 static const float RAMP_NEW_STREAM_DURATION_SECS = 0.01;
39 static const float RAMP_MUTE_DURATION_SECS = 0.1;
40 static const float RAMP_VOLUME_CHANGE_DURATION_SECS = 0.1;
41
42 /*
43 * It is the lastest time for the device to wake up when it is in the normal
44 * run state. It represents how many remaining frames in the device buffer.
45 */
46 static const struct timespec dev_normal_run_wake_up_time = {
47 0, 1 * 1000 * 1000 /* 1 msec. */
48 };
49
50 /*
51 * It is the lastest time for the device to wake up when it is in the no stream
52 * state. It represents how many remaining frames in the device buffer.
53 */
54 static const struct timespec dev_no_stream_wake_up_time = {
55 0, 5 * 1000 * 1000 /* 5 msec. */
56 };
57
58 /*
59 * Check issu b/72496547 and commit message for the history of
60 * rate estimator tuning.
61 */
62 static const struct timespec rate_estimation_window_sz = {
63 5, 0 /* 5 sec. */
64 };
65 static const double rate_estimation_smooth_factor = 0.3f;
66
67 static void cras_iodev_alloc_dsp(struct cras_iodev *iodev);
68
default_no_stream_playback(struct cras_iodev * odev)69 static int default_no_stream_playback(struct cras_iodev *odev)
70 {
71 int rc;
72 unsigned int hw_level, fr_to_write;
73 unsigned int target_hw_level = odev->min_cb_level * 2;
74 struct timespec hw_tstamp;
75
76 /* The default action for no stream playback is to fill zeros. */
77 rc = cras_iodev_frames_queued(odev, &hw_tstamp);
78 if (rc < 0)
79 return rc;
80 hw_level = rc;
81
82 /* If underrun happened, handle underrun and get hw_level again. */
83 if (hw_level == 0) {
84 rc = cras_iodev_output_underrun(odev);
85 if (rc < 0)
86 return rc;
87
88 rc = cras_iodev_frames_queued(odev, &hw_tstamp);
89 if (rc < 0)
90 return rc;
91 hw_level = rc;
92 }
93
94 ATLOG(atlog, AUDIO_THREAD_ODEV_DEFAULT_NO_STREAMS, odev->info.idx,
95 hw_level, target_hw_level);
96
97 fr_to_write = cras_iodev_buffer_avail(odev, hw_level);
98 if (hw_level <= target_hw_level) {
99 fr_to_write = MIN(target_hw_level - hw_level, fr_to_write);
100 return cras_iodev_fill_odev_zeros(odev, fr_to_write);
101 }
102 return 0;
103 }
104
cras_iodev_start(struct cras_iodev * iodev)105 static int cras_iodev_start(struct cras_iodev *iodev)
106 {
107 int rc;
108 if (!cras_iodev_is_open(iodev))
109 return -EPERM;
110 if (!iodev->start) {
111 syslog(LOG_ERR,
112 "start called on device %s not supporting start ops",
113 iodev->info.name);
114 return -EINVAL;
115 }
116 rc = iodev->start(iodev);
117 if (rc)
118 return rc;
119 iodev->state = CRAS_IODEV_STATE_NORMAL_RUN;
120 return 0;
121 }
122
123 /* Gets the number of frames ready for this device to play.
124 * It is the minimum number of available samples in dev_streams.
125 */
dev_playback_frames(struct cras_iodev * odev)126 static unsigned int dev_playback_frames(struct cras_iodev *odev)
127 {
128 struct dev_stream *curr;
129 int frames = 0;
130
131 DL_FOREACH (odev->streams, curr) {
132 int dev_frames;
133
134 /* Skip stream which hasn't started running yet. */
135 if (!dev_stream_is_running(curr))
136 continue;
137
138 /* If this is a single output dev stream, updates the latest
139 * number of frames for playback. */
140 if (dev_stream_attached_devs(curr) == 1)
141 dev_stream_update_frames(curr);
142
143 dev_frames = dev_stream_playback_frames(curr);
144 /* Do not handle stream error or end of draining in this
145 * function because they should be handled in write_streams. */
146 if (dev_frames < 0)
147 continue;
148 if (!dev_frames) {
149 if (cras_rstream_get_is_draining(curr->stream))
150 continue;
151 else
152 return 0;
153 }
154 if (frames == 0)
155 frames = dev_frames;
156 else
157 frames = MIN(dev_frames, frames);
158 }
159 return frames;
160 }
161
162 /* Let device enter/leave no stream playback.
163 * Args:
164 * iodev[in] - The output device.
165 * enable[in] - 1 to enter no stream playback, 0 to leave.
166 * Returns:
167 * 0 on success. Negative error code on failure.
168 */
cras_iodev_no_stream_playback_transition(struct cras_iodev * odev,int enable)169 static int cras_iodev_no_stream_playback_transition(struct cras_iodev *odev,
170 int enable)
171 {
172 int rc;
173
174 if (odev->direction != CRAS_STREAM_OUTPUT)
175 return -EINVAL;
176
177 /* This function is for transition between normal run and
178 * no stream run state.
179 */
180 if ((odev->state != CRAS_IODEV_STATE_NORMAL_RUN) &&
181 (odev->state != CRAS_IODEV_STATE_NO_STREAM_RUN))
182 return -EINVAL;
183
184 if (enable) {
185 ATLOG(atlog, AUDIO_THREAD_ODEV_NO_STREAMS, odev->info.idx, 0,
186 0);
187 } else {
188 ATLOG(atlog, AUDIO_THREAD_ODEV_LEAVE_NO_STREAMS, odev->info.idx,
189 0, 0);
190 }
191
192 rc = odev->no_stream(odev, enable);
193 if (rc < 0)
194 return rc;
195 if (enable)
196 odev->state = CRAS_IODEV_STATE_NO_STREAM_RUN;
197 else
198 odev->state = CRAS_IODEV_STATE_NORMAL_RUN;
199 return 0;
200 }
201
202 /* Determines if the output device should mute. It considers system mute,
203 * system volume, and active node volume on the device. */
output_should_mute(struct cras_iodev * odev)204 static int output_should_mute(struct cras_iodev *odev)
205 {
206 /* System mute has highest priority. */
207 if (cras_system_get_mute())
208 return 1;
209
210 /* consider system volume and active node volume. */
211 return cras_iodev_is_zero_volume(odev);
212 }
213
cras_iodev_is_zero_volume(const struct cras_iodev * odev)214 int cras_iodev_is_zero_volume(const struct cras_iodev *odev)
215 {
216 size_t system_volume;
217 unsigned int adjusted_node_volume;
218
219 system_volume = cras_system_get_volume();
220 if (odev->active_node) {
221 adjusted_node_volume = cras_iodev_adjust_node_volume(
222 odev->active_node, system_volume);
223 return (adjusted_node_volume == 0);
224 }
225 return (system_volume == 0);
226 }
227
228 /* Output device state transition diagram:
229 *
230 * ----------------
231 * -------------<-----------| S0 Closed |------<-------.
232 * | ---------------- |
233 * | | iodev_list enables |
234 * | | device and adds to |
235 * | V audio thread | iodev_list removes
236 * | ---------------- | device from
237 * | | S1 Open | | audio_thread and
238 * | ---------------- | closes device
239 * | Device with dummy start | |
240 * | ops transits into | Sample is ready |
241 * | no stream state right V |
242 * | after open. ---------------- |
243 * | | S2 Normal | |
244 * | ---------------- |
245 * | | ^ |
246 * | There is no stream | | Sample is ready |
247 * | V | |
248 * | ---------------- |
249 * ------------->-----------| S3 No Stream |------->------
250 * ----------------
251 *
252 * Device in open_devs can be in one of S1, S2, S3.
253 *
254 * cras_iodev_output_event_sample_ready change device state from S1 or S3 into
255 * S2.
256 */
cras_iodev_output_event_sample_ready(struct cras_iodev * odev)257 static int cras_iodev_output_event_sample_ready(struct cras_iodev *odev)
258 {
259 if (odev->state == CRAS_IODEV_STATE_OPEN ||
260 odev->state == CRAS_IODEV_STATE_NO_STREAM_RUN) {
261 /* Starts ramping up if device should not be muted.
262 * Both mute and volume are taken into consideration.
263 */
264 if (odev->ramp && !output_should_mute(odev))
265 cras_iodev_start_ramp(
266 odev,
267 CRAS_IODEV_RAMP_REQUEST_UP_START_PLAYBACK);
268 }
269
270 if (odev->state == CRAS_IODEV_STATE_OPEN) {
271 /* S1 => S2:
272 * If device is not started yet, and there is sample ready from
273 * stream, fill 1 min_cb_level of zeros first and fill sample
274 * from stream later.
275 * Starts the device here to finish state transition. */
276 cras_iodev_fill_odev_zeros(odev, odev->min_cb_level);
277 ATLOG(atlog, AUDIO_THREAD_ODEV_START, odev->info.idx,
278 odev->min_cb_level, 0);
279 return cras_iodev_start(odev);
280 } else if (odev->state == CRAS_IODEV_STATE_NO_STREAM_RUN) {
281 /* S3 => S2:
282 * Device in no stream state get sample ready. Leave no stream
283 * state and transit to normal run state.*/
284 return cras_iodev_no_stream_playback_transition(odev, 0);
285 } else {
286 syslog(LOG_ERR,
287 "Device %s in state %d received sample ready event",
288 odev->info.name, odev->state);
289 return -EINVAL;
290 }
291 return 0;
292 }
293
294 /*
295 * Exported Interface.
296 */
297
298 /* Finds the supported sample rate that best suits the requested rate, "rrate".
299 * Exact matches have highest priority, then integer multiples, then the default
300 * rate for the device. */
get_best_rate(struct cras_iodev * iodev,size_t rrate)301 static size_t get_best_rate(struct cras_iodev *iodev, size_t rrate)
302 {
303 size_t i;
304 size_t best;
305
306 if (iodev->supported_rates[0] == 0) /* No rates supported */
307 return 0;
308
309 for (i = 0, best = 0; iodev->supported_rates[i] != 0; i++) {
310 if (rrate == iodev->supported_rates[i] && rrate >= 44100)
311 return rrate;
312 if (best == 0 && (rrate % iodev->supported_rates[i] == 0 ||
313 iodev->supported_rates[i] % rrate == 0))
314 best = iodev->supported_rates[i];
315 }
316
317 if (best)
318 return best;
319 return iodev->supported_rates[0];
320 }
321
322 /* Finds the best match for the channel count. The following match rules
323 * will apply in order and return the value once matched:
324 * 1. Match the exact given channel count.
325 * 2. Match the preferred channel count.
326 * 3. The first channel count in the list.
327 */
get_best_channel_count(struct cras_iodev * iodev,size_t count)328 static size_t get_best_channel_count(struct cras_iodev *iodev, size_t count)
329 {
330 static const size_t preferred_channel_count = 2;
331 size_t i;
332
333 assert(iodev->supported_channel_counts[0] != 0);
334
335 for (i = 0; iodev->supported_channel_counts[i] != 0; i++) {
336 if (iodev->supported_channel_counts[i] == count)
337 return count;
338 }
339
340 /* If provided count is not supported, search for preferred
341 * channel count to which we're good at converting.
342 */
343 for (i = 0; iodev->supported_channel_counts[i] != 0; i++) {
344 if (iodev->supported_channel_counts[i] ==
345 preferred_channel_count)
346 return preferred_channel_count;
347 }
348
349 return iodev->supported_channel_counts[0];
350 }
351
352 /* finds the best match for the current format. If no exact match is
353 * found, use the first. */
get_best_pcm_format(struct cras_iodev * iodev,snd_pcm_format_t fmt)354 static snd_pcm_format_t get_best_pcm_format(struct cras_iodev *iodev,
355 snd_pcm_format_t fmt)
356 {
357 size_t i;
358
359 for (i = 0; iodev->supported_formats[i] != 0; i++) {
360 if (fmt == iodev->supported_formats[i])
361 return fmt;
362 }
363
364 return iodev->supported_formats[0];
365 }
366
367 /* Applies the DSP to the samples for the iodev if applicable. */
apply_dsp(struct cras_iodev * iodev,uint8_t * buf,size_t frames)368 static int apply_dsp(struct cras_iodev *iodev, uint8_t *buf, size_t frames)
369 {
370 struct cras_dsp_context *ctx;
371 struct pipeline *pipeline;
372 int rc;
373
374 ctx = iodev->dsp_context;
375 if (!ctx)
376 return 0;
377
378 pipeline = cras_dsp_get_pipeline(ctx);
379 if (!pipeline)
380 return 0;
381
382 rc = cras_dsp_pipeline_apply(pipeline, buf, iodev->format->format,
383 frames);
384
385 cras_dsp_put_pipeline(ctx);
386 return rc;
387 }
388
cras_iodev_free_dsp(struct cras_iodev * iodev)389 static void cras_iodev_free_dsp(struct cras_iodev *iodev)
390 {
391 if (iodev->dsp_context) {
392 cras_dsp_context_free(iodev->dsp_context);
393 iodev->dsp_context = NULL;
394 }
395 }
396
397 /* Modifies the number of channels in device format to the one that will be
398 * presented to the device after any channel changes from the DSP. */
adjust_dev_channel_for_dsp(const struct cras_iodev * iodev)399 static inline void adjust_dev_channel_for_dsp(const struct cras_iodev *iodev)
400 {
401 struct cras_dsp_context *ctx = iodev->dsp_context;
402
403 if (!ctx || !cras_dsp_get_pipeline(ctx))
404 return;
405
406 if (iodev->direction == CRAS_STREAM_OUTPUT)
407 iodev->format->num_channels = cras_dsp_num_output_channels(ctx);
408 else
409 iodev->format->num_channels = cras_dsp_num_input_channels(ctx);
410
411 cras_dsp_put_pipeline(ctx);
412 }
413
414 /* Updates channel layout based on the number of channels set by a
415 * client stream. Set a default value to format if the update call
416 * fails.
417 */
update_channel_layout(struct cras_iodev * iodev)418 static void update_channel_layout(struct cras_iodev *iodev)
419 {
420 int rc;
421
422 if (iodev->update_channel_layout == NULL)
423 return;
424
425 rc = iodev->update_channel_layout(iodev);
426 if (rc < 0)
427 cras_audio_format_set_default_channel_layout(iodev->format);
428 }
429
430 /*
431 * For the specified format, removes any channels from the channel layout that
432 * are higher than the supported number of channels. Should be used when the
433 * number of channels of the format been reduced.
434 */
trim_channel_layout(struct cras_audio_format * fmt)435 static void trim_channel_layout(struct cras_audio_format *fmt)
436 {
437 int i;
438 for (i = 0; i < CRAS_CH_MAX; i++)
439 if (fmt->channel_layout[i] >= fmt->num_channels)
440 fmt->channel_layout[i] = -1;
441 }
442
cras_iodev_set_format(struct cras_iodev * iodev,const struct cras_audio_format * fmt)443 int cras_iodev_set_format(struct cras_iodev *iodev,
444 const struct cras_audio_format *fmt)
445 {
446 size_t actual_rate, actual_num_channels;
447 snd_pcm_format_t actual_format;
448 int rc;
449
450 /* If this device isn't already using a format, try to match the one
451 * requested in "fmt". */
452 if (iodev->format == NULL) {
453 iodev->format = malloc(sizeof(struct cras_audio_format));
454 if (!iodev->format)
455 return -ENOMEM;
456 *iodev->format = *fmt;
457
458 if (iodev->update_supported_formats) {
459 rc = iodev->update_supported_formats(iodev);
460 if (rc) {
461 syslog(LOG_ERR, "Failed to update formats");
462 goto error;
463 }
464 }
465
466 /* Finds the actual rate of device before allocating DSP
467 * because DSP needs to use the rate of device, not rate of
468 * stream. */
469 actual_rate = get_best_rate(iodev, fmt->frame_rate);
470 iodev->format->frame_rate = actual_rate;
471
472 cras_iodev_alloc_dsp(iodev);
473 cras_iodev_update_dsp(iodev);
474 if (iodev->dsp_context)
475 adjust_dev_channel_for_dsp(iodev);
476
477 actual_num_channels = get_best_channel_count(
478 iodev, iodev->format->num_channels);
479 actual_format = get_best_pcm_format(iodev, fmt->format);
480 if (actual_rate == 0 || actual_num_channels == 0 ||
481 actual_format == 0) {
482 /* No compatible frame rate found. */
483 rc = -EINVAL;
484 goto error;
485 }
486 iodev->format->format = actual_format;
487 if (iodev->format->num_channels != actual_num_channels) {
488 /* If the DSP for this device doesn't match, drop it. */
489 iodev->format->num_channels = actual_num_channels;
490 trim_channel_layout(iodev->format);
491 cras_iodev_free_dsp(iodev);
492 }
493
494 update_channel_layout(iodev);
495
496 if (!iodev->rate_est)
497 iodev->rate_est = rate_estimator_create(
498 actual_rate, &rate_estimation_window_sz,
499 rate_estimation_smooth_factor);
500 else
501 rate_estimator_reset_rate(iodev->rate_est, actual_rate);
502 }
503
504 return 0;
505
506 error:
507 free(iodev->format);
508 iodev->format = NULL;
509 return rc;
510 }
511
512 /*
513 * Configures the external dsp module and adds it to the existing dsp pipeline.
514 */
add_ext_dsp_module_to_pipeline(struct cras_iodev * iodev)515 static void add_ext_dsp_module_to_pipeline(struct cras_iodev *iodev)
516 {
517 struct pipeline *pipeline;
518
519 pipeline = iodev->dsp_context ?
520 cras_dsp_get_pipeline(iodev->dsp_context) :
521 NULL;
522
523 if (!pipeline) {
524 cras_iodev_alloc_dsp(iodev);
525 cras_dsp_load_dummy_pipeline(iodev->dsp_context,
526 iodev->format->num_channels);
527 pipeline = cras_dsp_get_pipeline(iodev->dsp_context);
528 }
529 /* dsp_context mutex locked. Now it's safe to modify dsp
530 * pipeline resources. */
531
532 if (iodev->ext_dsp_module)
533 iodev->ext_dsp_module->configure(iodev->ext_dsp_module,
534 iodev->buffer_size,
535 iodev->format->num_channels,
536 iodev->format->frame_rate);
537
538 cras_dsp_pipeline_set_sink_ext_module(pipeline, iodev->ext_dsp_module);
539
540 /* Unlock dsp_context mutex. */
541 cras_dsp_put_pipeline(iodev->dsp_context);
542 }
543
544 /*
545 * Releases the ext_dsp_module if it ever added to iodev's dsp pipeline.
546 */
release_ext_dsp_module_from_pipeline(struct cras_iodev * iodev)547 static void release_ext_dsp_module_from_pipeline(struct cras_iodev *iodev)
548 {
549 struct pipeline *pipeline;
550
551 if (iodev->dsp_context == NULL)
552 return;
553
554 pipeline = cras_dsp_get_pipeline(iodev->dsp_context);
555 if (pipeline == NULL)
556 return;
557 /* dsp_context mutex locked. */
558
559 cras_dsp_pipeline_set_sink_ext_module(pipeline, NULL);
560
561 /* Unlock dsp_context mutex. */
562 cras_dsp_put_pipeline(iodev->dsp_context);
563 }
564
cras_iodev_set_ext_dsp_module(struct cras_iodev * iodev,struct ext_dsp_module * ext)565 void cras_iodev_set_ext_dsp_module(struct cras_iodev *iodev,
566 struct ext_dsp_module *ext)
567 {
568 iodev->ext_dsp_module = ext;
569
570 if (!cras_iodev_is_open(iodev))
571 return;
572
573 if (iodev->ext_dsp_module)
574 add_ext_dsp_module_to_pipeline(iodev);
575 else
576 release_ext_dsp_module_from_pipeline(iodev);
577 }
578
cras_iodev_update_dsp(struct cras_iodev * iodev)579 void cras_iodev_update_dsp(struct cras_iodev *iodev)
580 {
581 char swap_lr_disabled = 1;
582
583 if (!iodev->dsp_context)
584 return;
585
586 cras_dsp_set_variable_string(iodev->dsp_context, "dsp_name",
587 iodev->dsp_name ?: "");
588
589 if (iodev->active_node && iodev->active_node->left_right_swapped)
590 swap_lr_disabled = 0;
591
592 cras_dsp_set_variable_boolean(iodev->dsp_context, "swap_lr_disabled",
593 swap_lr_disabled);
594
595 cras_dsp_load_pipeline(iodev->dsp_context);
596 }
597
cras_iodev_dsp_set_swap_mode_for_node(struct cras_iodev * iodev,struct cras_ionode * node,int enable)598 int cras_iodev_dsp_set_swap_mode_for_node(struct cras_iodev *iodev,
599 struct cras_ionode *node, int enable)
600 {
601 if (node->left_right_swapped == enable)
602 return 0;
603
604 /* Sets left_right_swapped property on the node. It will be used
605 * when cras_iodev_update_dsp is called. */
606 node->left_right_swapped = enable;
607
608 /* Possibly updates dsp if the node is active on the device and there
609 * is dsp context. If dsp context is not created yet,
610 * cras_iodev_update_dsp returns right away. */
611 if (iodev->active_node == node)
612 cras_iodev_update_dsp(iodev);
613 return 0;
614 }
615
cras_iodev_free_format(struct cras_iodev * iodev)616 void cras_iodev_free_format(struct cras_iodev *iodev)
617 {
618 free(iodev->format);
619 iodev->format = NULL;
620 }
621
cras_iodev_init_audio_area(struct cras_iodev * iodev,int num_channels)622 void cras_iodev_init_audio_area(struct cras_iodev *iodev, int num_channels)
623 {
624 if (iodev->area)
625 cras_iodev_free_audio_area(iodev);
626
627 iodev->area = cras_audio_area_create(num_channels);
628 cras_audio_area_config_channels(iodev->area, iodev->format);
629 }
630
cras_iodev_free_audio_area(struct cras_iodev * iodev)631 void cras_iodev_free_audio_area(struct cras_iodev *iodev)
632 {
633 if (!iodev->area)
634 return;
635
636 cras_audio_area_destroy(iodev->area);
637 iodev->area = NULL;
638 }
639
cras_iodev_free_resources(struct cras_iodev * iodev)640 void cras_iodev_free_resources(struct cras_iodev *iodev)
641 {
642 cras_iodev_free_dsp(iodev);
643 rate_estimator_destroy(iodev->rate_est);
644 if (iodev->ramp)
645 cras_ramp_destroy(iodev->ramp);
646 }
647
cras_iodev_alloc_dsp(struct cras_iodev * iodev)648 static void cras_iodev_alloc_dsp(struct cras_iodev *iodev)
649 {
650 const char *purpose;
651
652 if (iodev->direction == CRAS_STREAM_OUTPUT)
653 purpose = "playback";
654 else
655 purpose = "capture";
656
657 cras_iodev_free_dsp(iodev);
658 iodev->dsp_context =
659 cras_dsp_context_new(iodev->format->frame_rate, purpose);
660 }
661
cras_iodev_fill_time_from_frames(size_t frames,size_t frame_rate,struct timespec * ts)662 void cras_iodev_fill_time_from_frames(size_t frames, size_t frame_rate,
663 struct timespec *ts)
664 {
665 uint64_t to_play_usec;
666
667 ts->tv_sec = 0;
668 /* adjust sleep time to target our callback threshold */
669 to_play_usec = (uint64_t)frames * 1000000L / (uint64_t)frame_rate;
670
671 while (to_play_usec > 1000000) {
672 ts->tv_sec++;
673 to_play_usec -= 1000000;
674 }
675 ts->tv_nsec = to_play_usec * 1000;
676 }
677
678 /* This is called when a node is plugged/unplugged */
cras_iodev_set_node_plugged(struct cras_ionode * node,int plugged)679 void cras_iodev_set_node_plugged(struct cras_ionode *node, int plugged)
680 {
681 if (node->plugged == plugged)
682 return;
683 node->plugged = plugged;
684 if (plugged) {
685 gettimeofday(&node->plugged_time, NULL);
686 } else if (node == node->dev->active_node) {
687 cras_iodev_list_disable_dev(node->dev, false);
688 }
689 cras_iodev_list_notify_nodes_changed();
690 }
691
cras_iodev_add_node(struct cras_iodev * iodev,struct cras_ionode * node)692 void cras_iodev_add_node(struct cras_iodev *iodev, struct cras_ionode *node)
693 {
694 DL_APPEND(iodev->nodes, node);
695 cras_iodev_list_notify_nodes_changed();
696 }
697
cras_iodev_rm_node(struct cras_iodev * iodev,struct cras_ionode * node)698 void cras_iodev_rm_node(struct cras_iodev *iodev, struct cras_ionode *node)
699 {
700 DL_DELETE(iodev->nodes, node);
701 cras_iodev_list_notify_nodes_changed();
702 }
703
cras_iodev_set_active_node(struct cras_iodev * iodev,struct cras_ionode * node)704 void cras_iodev_set_active_node(struct cras_iodev *iodev,
705 struct cras_ionode *node)
706 {
707 iodev->active_node = node;
708 cras_iodev_list_notify_active_node_changed(iodev->direction);
709 }
710
cras_iodev_get_software_volume_scaler(struct cras_iodev * iodev)711 float cras_iodev_get_software_volume_scaler(struct cras_iodev *iodev)
712 {
713 unsigned int volume;
714
715 volume = cras_iodev_adjust_active_node_volume(iodev,
716 cras_system_get_volume());
717
718 if (iodev->active_node && iodev->active_node->softvol_scalers)
719 return iodev->active_node->softvol_scalers[volume];
720 return softvol_get_scaler(volume);
721 }
722
cras_iodev_get_software_gain_scaler(const struct cras_iodev * iodev)723 float cras_iodev_get_software_gain_scaler(const struct cras_iodev *iodev)
724 {
725 float scaler = 1.0f;
726 if (cras_iodev_software_volume_needed(iodev)) {
727 long gain = cras_iodev_adjust_active_node_gain(
728 iodev, cras_system_get_capture_gain());
729 scaler = convert_softvol_scaler_from_dB(gain);
730 }
731 return scaler;
732 }
733
cras_iodev_get_valid_frames(struct cras_iodev * odev,struct timespec * hw_tstamp)734 int cras_iodev_get_valid_frames(struct cras_iodev *odev,
735 struct timespec *hw_tstamp)
736 {
737 int rc;
738
739 if (odev->direction != CRAS_STREAM_OUTPUT)
740 return -EINVAL;
741
742 if (odev->get_valid_frames) {
743 rc = odev->get_valid_frames(odev, hw_tstamp);
744 if (rc < 0)
745 return rc;
746
747 if (rc < odev->min_buffer_level)
748 return 0;
749
750 return rc - odev->min_buffer_level;
751 } else {
752 return cras_iodev_frames_queued(odev, hw_tstamp);
753 }
754 }
755
cras_iodev_add_stream(struct cras_iodev * iodev,struct dev_stream * stream)756 int cras_iodev_add_stream(struct cras_iodev *iodev, struct dev_stream *stream)
757 {
758 /*
759 * For input stream, start stream right after adding stream.
760 * For output stream, start stream after its first fetch such that it does not
761 * block other existing streams.
762 */
763 DL_APPEND(iodev->streams, stream);
764 if (!iodev->buf_state)
765 iodev->buf_state = buffer_share_create(iodev->buffer_size);
766 if (stream->stream->direction == CRAS_STREAM_INPUT)
767 cras_iodev_start_stream(iodev, stream);
768 return 0;
769 }
770
cras_iodev_start_stream(struct cras_iodev * iodev,struct dev_stream * stream)771 void cras_iodev_start_stream(struct cras_iodev *iodev,
772 struct dev_stream *stream)
773 {
774 unsigned int cb_threshold = dev_stream_cb_threshold(stream);
775
776 if (dev_stream_is_running(stream))
777 return;
778 /*
779 * TRIGGER_ONLY streams do not want to receive data, so do not add them
780 * to buffer_share, otherwise they'll affect other streams to receive.
781 */
782 if (!(stream->stream->flags & TRIGGER_ONLY))
783 buffer_share_add_id(iodev->buf_state, stream->stream->stream_id,
784 NULL);
785 iodev->min_cb_level = MIN(iodev->min_cb_level, cb_threshold);
786 iodev->max_cb_level = MAX(iodev->max_cb_level, cb_threshold);
787 iodev->largest_cb_level = MAX(iodev->largest_cb_level, cb_threshold);
788 dev_stream_set_running(stream);
789 }
790
cras_iodev_rm_stream(struct cras_iodev * iodev,const struct cras_rstream * rstream)791 struct dev_stream *cras_iodev_rm_stream(struct cras_iodev *iodev,
792 const struct cras_rstream *rstream)
793 {
794 struct dev_stream *out;
795 struct dev_stream *ret = NULL;
796 unsigned int cb_threshold;
797 struct timespec earliest_next_cb_ts;
798 int set_earliest = 0;
799
800 iodev->min_cb_level = iodev->buffer_size / 2;
801 iodev->max_cb_level = 0;
802 DL_FOREACH (iodev->streams, out) {
803 if (out->stream == rstream) {
804 buffer_share_rm_id(iodev->buf_state,
805 rstream->stream_id);
806 ret = out;
807 DL_DELETE(iodev->streams, out);
808 continue;
809 }
810 if (!dev_stream_is_running(out))
811 continue;
812 cb_threshold = dev_stream_cb_threshold(out);
813 iodev->min_cb_level = MIN(iodev->min_cb_level, cb_threshold);
814 iodev->max_cb_level = MAX(iodev->max_cb_level, cb_threshold);
815 if (!set_earliest) {
816 set_earliest = 1;
817 earliest_next_cb_ts = out->stream->next_cb_ts;
818 }
819 if (timespec_after(&earliest_next_cb_ts,
820 &out->stream->next_cb_ts))
821 earliest_next_cb_ts = out->stream->next_cb_ts;
822 }
823
824 if (!iodev->streams) {
825 buffer_share_destroy(iodev->buf_state);
826 iodev->buf_state = NULL;
827 iodev->min_cb_level = iodev->buffer_size / 2;
828 /* Let output device transit into no stream state if it's
829 * in normal run state now. Leave input device in normal
830 * run state. */
831 if ((iodev->direction == CRAS_STREAM_OUTPUT) &&
832 (iodev->state == CRAS_IODEV_STATE_NORMAL_RUN))
833 cras_iodev_no_stream_playback_transition(iodev, 1);
834 }
835
836 if (!set_earliest)
837 return ret;
838
839 DL_FOREACH (iodev->streams, out) {
840 if (!dev_stream_is_running(out))
841 out->stream->next_cb_ts = earliest_next_cb_ts;
842 }
843
844 return ret;
845 }
846
cras_iodev_stream_offset(struct cras_iodev * iodev,struct dev_stream * stream)847 unsigned int cras_iodev_stream_offset(struct cras_iodev *iodev,
848 struct dev_stream *stream)
849 {
850 return buffer_share_id_offset(iodev->buf_state,
851 stream->stream->stream_id);
852 }
853
cras_iodev_stream_written(struct cras_iodev * iodev,struct dev_stream * stream,unsigned int nwritten)854 void cras_iodev_stream_written(struct cras_iodev *iodev,
855 struct dev_stream *stream, unsigned int nwritten)
856 {
857 buffer_share_offset_update(iodev->buf_state, stream->stream->stream_id,
858 nwritten);
859 }
860
cras_iodev_all_streams_written(struct cras_iodev * iodev)861 unsigned int cras_iodev_all_streams_written(struct cras_iodev *iodev)
862 {
863 if (!iodev->buf_state)
864 return 0;
865 return buffer_share_get_new_write_point(iodev->buf_state);
866 }
867
cras_iodev_max_stream_offset(const struct cras_iodev * iodev)868 unsigned int cras_iodev_max_stream_offset(const struct cras_iodev *iodev)
869 {
870 unsigned int max = 0;
871 struct dev_stream *curr;
872
873 DL_FOREACH (iodev->streams, curr) {
874 /* Skip stream which hasn't started running yet. */
875 if (!dev_stream_is_running(curr))
876 continue;
877
878 max = MAX(max, buffer_share_id_offset(iodev->buf_state,
879 curr->stream->stream_id));
880 }
881
882 return max;
883 }
884
cras_iodev_open(struct cras_iodev * iodev,unsigned int cb_level,const struct cras_audio_format * fmt)885 int cras_iodev_open(struct cras_iodev *iodev, unsigned int cb_level,
886 const struct cras_audio_format *fmt)
887 {
888 struct cras_loopback *loopback;
889 int rc;
890
891 if (iodev->pre_open_iodev_hook)
892 iodev->pre_open_iodev_hook();
893
894 DL_FOREACH (iodev->loopbacks, loopback) {
895 if (loopback->hook_control)
896 loopback->hook_control(true, loopback->cb_data);
897 }
898
899 if (iodev->open_dev) {
900 rc = iodev->open_dev(iodev);
901 if (rc)
902 return rc;
903 }
904
905 if (iodev->format == NULL) {
906 rc = cras_iodev_set_format(iodev, fmt);
907 if (rc) {
908 iodev->close_dev(iodev);
909 return rc;
910 }
911 }
912
913 rc = iodev->configure_dev(iodev);
914 if (rc < 0) {
915 iodev->close_dev(iodev);
916 return rc;
917 }
918
919 /*
920 * Convert cb_level from input format to device format
921 */
922 cb_level = cras_frames_at_rate(fmt->frame_rate, cb_level,
923 iodev->format->frame_rate);
924 /* Make sure the min_cb_level doesn't get too large. */
925 iodev->min_cb_level = MIN(iodev->buffer_size / 2, cb_level);
926 iodev->max_cb_level = 0;
927 iodev->largest_cb_level = 0;
928
929 iodev->reset_request_pending = 0;
930 iodev->state = CRAS_IODEV_STATE_OPEN;
931 iodev->highest_hw_level = 0;
932 iodev->input_dsp_offset = 0;
933
934 if (iodev->direction == CRAS_STREAM_OUTPUT) {
935 /* If device supports start ops, device can be in open state.
936 * Otherwise, device starts running right after opening. */
937 if (iodev->start)
938 iodev->state = CRAS_IODEV_STATE_OPEN;
939 else
940 iodev->state = CRAS_IODEV_STATE_NO_STREAM_RUN;
941 } else {
942 iodev->input_data = input_data_create(iodev);
943 /* If this is the echo reference dev, its ext_dsp_module will
944 * be set to APM reverse module. Do not override it to its
945 * input data. */
946 if (iodev->ext_dsp_module == NULL)
947 iodev->ext_dsp_module = &iodev->input_data->ext;
948
949 /* Input device starts running right after opening.
950 * No stream state is only for output device. Input device
951 * should be in normal run state. */
952 iodev->state = CRAS_IODEV_STATE_NORMAL_RUN;
953 /* Initialize the input_streaming flag to zero.*/
954 iodev->input_streaming = 0;
955
956 /*
957 * The device specific gain scaler to be used in audio thread.
958 * It's expected to stick to 1.0f if device has hardware gain
959 * control. For alsa device, this gain value can be configured
960 * through UCM labels DefaultNodeGain.
961 */
962 iodev->software_gain_scaler =
963 cras_iodev_get_software_gain_scaler(iodev);
964 }
965
966 add_ext_dsp_module_to_pipeline(iodev);
967 clock_gettime(CLOCK_MONOTONIC_RAW, &iodev->open_ts);
968
969 return 0;
970 }
971
cras_iodev_state(const struct cras_iodev * iodev)972 enum CRAS_IODEV_STATE cras_iodev_state(const struct cras_iodev *iodev)
973 {
974 return iodev->state;
975 }
976
cras_iodev_close(struct cras_iodev * iodev)977 int cras_iodev_close(struct cras_iodev *iodev)
978 {
979 struct cras_loopback *loopback;
980 int rc;
981
982 if (!cras_iodev_is_open(iodev))
983 return 0;
984
985 cras_server_metrics_device_runtime(iodev);
986
987 if (iodev->input_data) {
988 if (iodev->ext_dsp_module == &iodev->input_data->ext)
989 iodev->ext_dsp_module = NULL;
990 input_data_destroy(&iodev->input_data);
991 }
992
993 rc = iodev->close_dev(iodev);
994 if (rc)
995 return rc;
996 iodev->state = CRAS_IODEV_STATE_CLOSE;
997 if (iodev->ramp)
998 cras_ramp_reset(iodev->ramp);
999
1000 if (iodev->post_close_iodev_hook)
1001 iodev->post_close_iodev_hook();
1002
1003 DL_FOREACH (iodev->loopbacks, loopback) {
1004 if (loopback->hook_control)
1005 loopback->hook_control(false, loopback->cb_data);
1006 }
1007
1008 return 0;
1009 }
1010
cras_iodev_put_input_buffer(struct cras_iodev * iodev)1011 int cras_iodev_put_input_buffer(struct cras_iodev *iodev)
1012 {
1013 unsigned int min_frames;
1014 unsigned int dsp_frames;
1015 struct input_data *data = iodev->input_data;
1016 int rc;
1017
1018 if (iodev->streams)
1019 min_frames = buffer_share_get_new_write_point(iodev->buf_state);
1020 else
1021 min_frames = data->area->frames;
1022
1023 // Update the max number of frames has applied input dsp.
1024 dsp_frames = MAX(iodev->input_frames_read, iodev->input_dsp_offset);
1025 iodev->input_dsp_offset = dsp_frames - min_frames;
1026
1027 input_data_set_all_streams_read(data, min_frames);
1028 rate_estimator_add_frames(iodev->rate_est, -min_frames);
1029 rc = iodev->put_buffer(iodev, min_frames);
1030 if (rc < 0)
1031 return rc;
1032 return min_frames;
1033 }
1034
cras_iodev_put_output_buffer(struct cras_iodev * iodev,uint8_t * frames,unsigned int nframes,int * is_non_empty,struct cras_fmt_conv * remix_converter)1035 int cras_iodev_put_output_buffer(struct cras_iodev *iodev, uint8_t *frames,
1036 unsigned int nframes, int *is_non_empty,
1037 struct cras_fmt_conv *remix_converter)
1038 {
1039 const struct cras_audio_format *fmt = iodev->format;
1040 struct cras_ramp_action ramp_action = {
1041 .type = CRAS_RAMP_ACTION_NONE,
1042 .scaler = 0.0f,
1043 .increment = 0.0f,
1044 .target = 1.0f,
1045 };
1046 float software_volume_scaler = 1.0;
1047 int software_volume_needed = cras_iodev_software_volume_needed(iodev);
1048 int rc;
1049 struct cras_loopback *loopback;
1050
1051 /* Calculate whether the final output was non-empty, if requested. */
1052 if (is_non_empty) {
1053 unsigned int i;
1054 for (i = 0; i < nframes * cras_get_format_bytes(fmt); i++) {
1055 if (frames[i]) {
1056 *is_non_empty = 1;
1057 break;
1058 }
1059 }
1060 }
1061
1062 DL_FOREACH (iodev->loopbacks, loopback) {
1063 if (loopback->type == LOOPBACK_POST_MIX_PRE_DSP)
1064 loopback->hook_data(frames, nframes, iodev->format,
1065 loopback->cb_data);
1066 }
1067
1068 rc = apply_dsp(iodev, frames, nframes);
1069 if (rc)
1070 return rc;
1071
1072 DL_FOREACH (iodev->loopbacks, loopback) {
1073 if (loopback->type == LOOPBACK_POST_DSP)
1074 loopback->hook_data(frames, nframes, iodev->format,
1075 loopback->cb_data);
1076 }
1077
1078 if (iodev->ramp) {
1079 ramp_action = cras_ramp_get_current_action(iodev->ramp);
1080 }
1081
1082 /* Mute samples if adjusted volume is 0 or system is muted, plus
1083 * that this device is not ramping. */
1084 if (output_should_mute(iodev) &&
1085 ramp_action.type != CRAS_RAMP_ACTION_PARTIAL) {
1086 const unsigned int frame_bytes = cras_get_format_bytes(fmt);
1087 cras_mix_mute_buffer(frames, frame_bytes, nframes);
1088 }
1089
1090 /* Compute scaler for software volume if needed. */
1091 if (software_volume_needed) {
1092 software_volume_scaler =
1093 cras_iodev_get_software_volume_scaler(iodev);
1094 }
1095
1096 if (ramp_action.type == CRAS_RAMP_ACTION_PARTIAL) {
1097 /* Scale with increment for ramp and possibly
1098 * software volume using cras_scale_buffer_increment.*/
1099 float starting_scaler = ramp_action.scaler;
1100 float increment = ramp_action.increment;
1101 float target = ramp_action.target;
1102
1103 if (software_volume_needed) {
1104 starting_scaler *= software_volume_scaler;
1105 increment *= software_volume_scaler;
1106 target *= software_volume_scaler;
1107 }
1108
1109 cras_scale_buffer_increment(fmt->format, frames, nframes,
1110 starting_scaler, increment, target,
1111 fmt->num_channels);
1112 cras_ramp_update_ramped_frames(iodev->ramp, nframes);
1113 } else if (!output_should_mute(iodev) && software_volume_needed) {
1114 /* Just scale for software volume using
1115 * cras_scale_buffer. */
1116 unsigned int nsamples = nframes * fmt->num_channels;
1117 cras_scale_buffer(fmt->format, frames, nsamples,
1118 software_volume_scaler);
1119 }
1120
1121 if (remix_converter)
1122 cras_channel_remix_convert(remix_converter, iodev->format,
1123 frames, nframes);
1124 if (iodev->rate_est)
1125 rate_estimator_add_frames(iodev->rate_est, nframes);
1126
1127 return iodev->put_buffer(iodev, nframes);
1128 }
1129
cras_iodev_get_input_buffer(struct cras_iodev * iodev,unsigned int * frames)1130 int cras_iodev_get_input_buffer(struct cras_iodev *iodev, unsigned int *frames)
1131 {
1132 const unsigned int frame_bytes = cras_get_format_bytes(iodev->format);
1133 struct input_data *data = iodev->input_data;
1134 int rc;
1135 uint8_t *hw_buffer;
1136 unsigned frame_requested = *frames;
1137
1138 rc = iodev->get_buffer(iodev, &data->area, frames);
1139 if (rc < 0 || *frames == 0)
1140 return rc;
1141
1142 if (*frames > frame_requested) {
1143 syslog(LOG_ERR,
1144 "frames returned from get_buffer is greater than "
1145 "requested: %u > %u",
1146 *frames, frame_requested);
1147 return -EINVAL;
1148 }
1149
1150 iodev->input_frames_read = *frames;
1151
1152 /* TODO(hychao) - This assumes interleaved audio. */
1153 hw_buffer = data->area->channels[0].buf;
1154
1155 /*
1156 * input_dsp_offset records the position where input dsp has applied to
1157 * last time. It's possible the requested |frames| count is smaller
1158 * than the tracked offset. That could happen when client stream uses
1159 * small buffer size and runs APM processing (which requires 10 ms
1160 * equivalent of data to process).
1161 * Only apply input dsp to the part of read buffer beyond where we've
1162 * already applied dsp.
1163 */
1164 if (*frames > iodev->input_dsp_offset) {
1165 rc = apply_dsp(iodev,
1166 hw_buffer +
1167 iodev->input_dsp_offset * frame_bytes,
1168 *frames - iodev->input_dsp_offset);
1169 if (rc)
1170 return rc;
1171 }
1172
1173 if (cras_system_get_capture_mute())
1174 cras_mix_mute_buffer(hw_buffer, frame_bytes, *frames);
1175
1176 return rc;
1177 }
1178
cras_iodev_get_output_buffer(struct cras_iodev * iodev,struct cras_audio_area ** area,unsigned * frames)1179 int cras_iodev_get_output_buffer(struct cras_iodev *iodev,
1180 struct cras_audio_area **area,
1181 unsigned *frames)
1182 {
1183 int rc;
1184 unsigned frame_requested = *frames;
1185
1186 rc = iodev->get_buffer(iodev, area, frames);
1187 if (*frames > frame_requested) {
1188 syslog(LOG_ERR,
1189 "frames returned from get_buffer is greater than "
1190 "requested: %u > %u",
1191 *frames, frame_requested);
1192 return -EINVAL;
1193 }
1194 return rc;
1195 }
1196
cras_iodev_update_rate(struct cras_iodev * iodev,unsigned int level,struct timespec * level_tstamp)1197 int cras_iodev_update_rate(struct cras_iodev *iodev, unsigned int level,
1198 struct timespec *level_tstamp)
1199 {
1200 /* If output underruns, reset to avoid incorrect estimated rate. */
1201 if ((iodev->direction == CRAS_STREAM_OUTPUT) && !level)
1202 rate_estimator_reset_rate(iodev->rate_est,
1203 iodev->format->frame_rate);
1204
1205 return rate_estimator_check(iodev->rate_est, level, level_tstamp);
1206 }
1207
cras_iodev_reset_rate_estimator(const struct cras_iodev * iodev)1208 int cras_iodev_reset_rate_estimator(const struct cras_iodev *iodev)
1209 {
1210 rate_estimator_reset_rate(iodev->rate_est, iodev->format->frame_rate);
1211 return 0;
1212 }
1213
cras_iodev_get_est_rate_ratio(const struct cras_iodev * iodev)1214 double cras_iodev_get_est_rate_ratio(const struct cras_iodev *iodev)
1215 {
1216 return rate_estimator_get_rate(iodev->rate_est) /
1217 iodev->format->frame_rate;
1218 }
1219
cras_iodev_get_dsp_delay(const struct cras_iodev * iodev)1220 int cras_iodev_get_dsp_delay(const struct cras_iodev *iodev)
1221 {
1222 struct cras_dsp_context *ctx;
1223 struct pipeline *pipeline;
1224 int delay;
1225
1226 ctx = iodev->dsp_context;
1227 if (!ctx)
1228 return 0;
1229
1230 pipeline = cras_dsp_get_pipeline(ctx);
1231 if (!pipeline)
1232 return 0;
1233
1234 delay = cras_dsp_pipeline_get_delay(pipeline);
1235
1236 cras_dsp_put_pipeline(ctx);
1237 return delay;
1238 }
1239
cras_iodev_frames_queued(struct cras_iodev * iodev,struct timespec * hw_tstamp)1240 int cras_iodev_frames_queued(struct cras_iodev *iodev,
1241 struct timespec *hw_tstamp)
1242 {
1243 int rc;
1244
1245 rc = iodev->frames_queued(iodev, hw_tstamp);
1246 if (rc == -EPIPE)
1247 cras_audio_thread_event_severe_underrun();
1248
1249 if (rc < 0)
1250 return rc;
1251
1252 if (iodev->direction == CRAS_STREAM_INPUT) {
1253 if (rc > 0)
1254 iodev->input_streaming = 1;
1255 return rc;
1256 }
1257
1258 if (rc < iodev->min_buffer_level)
1259 return 0;
1260
1261 return rc - iodev->min_buffer_level;
1262 }
1263
cras_iodev_buffer_avail(struct cras_iodev * iodev,unsigned hw_level)1264 int cras_iodev_buffer_avail(struct cras_iodev *iodev, unsigned hw_level)
1265 {
1266 if (iodev->direction == CRAS_STREAM_INPUT)
1267 return hw_level;
1268
1269 if (hw_level + iodev->min_buffer_level > iodev->buffer_size)
1270 return 0;
1271
1272 return iodev->buffer_size - iodev->min_buffer_level - hw_level;
1273 }
1274
cras_iodev_fill_odev_zeros(struct cras_iodev * odev,unsigned int frames)1275 int cras_iodev_fill_odev_zeros(struct cras_iodev *odev, unsigned int frames)
1276 {
1277 struct cras_audio_area *area = NULL;
1278 unsigned int frame_bytes, frames_written;
1279 int rc;
1280 uint8_t *buf;
1281
1282 if (odev->direction != CRAS_STREAM_OUTPUT)
1283 return -EINVAL;
1284
1285 ATLOG(atlog, AUDIO_THREAD_FILL_ODEV_ZEROS, odev->info.idx, frames, 0);
1286
1287 frame_bytes = cras_get_format_bytes(odev->format);
1288 while (frames > 0) {
1289 frames_written = frames;
1290 rc = cras_iodev_get_output_buffer(odev, &area, &frames_written);
1291 if (rc < 0) {
1292 syslog(LOG_ERR, "fill zeros fail: %d", rc);
1293 return rc;
1294 }
1295
1296 /* This assumes consecutive channel areas. */
1297 buf = area->channels[0].buf;
1298 memset(buf, 0, frames_written * frame_bytes);
1299 cras_iodev_put_output_buffer(odev, buf, frames_written, NULL,
1300 NULL);
1301 frames -= frames_written;
1302 }
1303
1304 return 0;
1305 }
1306
cras_iodev_output_underrun(struct cras_iodev * odev)1307 int cras_iodev_output_underrun(struct cras_iodev *odev)
1308 {
1309 cras_audio_thread_event_underrun();
1310 if (odev->output_underrun)
1311 return odev->output_underrun(odev);
1312 else
1313 return cras_iodev_fill_odev_zeros(odev, odev->min_cb_level);
1314 }
1315
cras_iodev_odev_should_wake(const struct cras_iodev * odev)1316 int cras_iodev_odev_should_wake(const struct cras_iodev *odev)
1317 {
1318 if (odev->direction != CRAS_STREAM_OUTPUT)
1319 return 0;
1320
1321 if (odev->is_free_running && odev->is_free_running(odev))
1322 return 0;
1323
1324 /* Do not wake up for device not started yet. */
1325 return (odev->state == CRAS_IODEV_STATE_NORMAL_RUN ||
1326 odev->state == CRAS_IODEV_STATE_NO_STREAM_RUN);
1327 }
1328
cras_iodev_frames_to_play_in_sleep(struct cras_iodev * odev,unsigned int * hw_level,struct timespec * hw_tstamp)1329 unsigned int cras_iodev_frames_to_play_in_sleep(struct cras_iodev *odev,
1330 unsigned int *hw_level,
1331 struct timespec *hw_tstamp)
1332 {
1333 int rc = cras_iodev_frames_queued(odev, hw_tstamp);
1334 unsigned int level = (rc < 0) ? 0 : rc;
1335 unsigned int wakeup_frames;
1336 *hw_level = level;
1337
1338 if (odev->streams) {
1339 /*
1340 * We have two cases in this scope. The first one is if there are frames
1341 * waiting to be played, audio thread will wake up when hw_level drops
1342 * to min_cb_level. This situation only happens when hardware buffer is
1343 * smaller than the client stream buffer. The second one is waking up
1344 * when hw_level drops to dev_normal_run_wake_up_time. It is a default
1345 * behavior. This wake up time is the bottom line to avoid underrun.
1346 * Normally, the audio thread does not wake up at that time because the
1347 * streams should wake it up before then.
1348 */
1349 if (*hw_level > odev->min_cb_level && dev_playback_frames(odev))
1350 return *hw_level - odev->min_cb_level;
1351
1352 wakeup_frames = cras_time_to_frames(
1353 &dev_normal_run_wake_up_time, odev->format->frame_rate);
1354 if (level > wakeup_frames)
1355 return level - wakeup_frames;
1356 else
1357 return level;
1358 }
1359
1360 /*
1361 * When this device has no stream, schedule audio thread to wake up when
1362 * hw_level drops to dev_no_stream_wake_up_time so audio thread can
1363 * fill zeros to it. We also need to consider min_cb_level in order to avoid
1364 * busyloop when device buffer size is smaller than wake up time.
1365 */
1366 wakeup_frames = cras_time_to_frames(&dev_no_stream_wake_up_time,
1367 odev->format->frame_rate);
1368 if (level > MIN(odev->min_cb_level, wakeup_frames))
1369 return level - MIN(odev->min_cb_level, wakeup_frames);
1370 else
1371 return 0;
1372 }
1373
cras_iodev_default_no_stream_playback(struct cras_iodev * odev,int enable)1374 int cras_iodev_default_no_stream_playback(struct cras_iodev *odev, int enable)
1375 {
1376 if (enable)
1377 return default_no_stream_playback(odev);
1378 return 0;
1379 }
1380
cras_iodev_prepare_output_before_write_samples(struct cras_iodev * odev)1381 int cras_iodev_prepare_output_before_write_samples(struct cras_iodev *odev)
1382 {
1383 int may_enter_normal_run;
1384 enum CRAS_IODEV_STATE state;
1385
1386 if (odev->direction != CRAS_STREAM_OUTPUT)
1387 return -EINVAL;
1388
1389 state = cras_iodev_state(odev);
1390
1391 may_enter_normal_run = (state == CRAS_IODEV_STATE_OPEN ||
1392 state == CRAS_IODEV_STATE_NO_STREAM_RUN);
1393
1394 if (may_enter_normal_run && dev_playback_frames(odev))
1395 return cras_iodev_output_event_sample_ready(odev);
1396
1397 /* no_stream ops is called every cycle in no_stream state. */
1398 if (state == CRAS_IODEV_STATE_NO_STREAM_RUN)
1399 return odev->no_stream(odev, 1);
1400
1401 return 0;
1402 }
1403
cras_iodev_get_num_underruns(const struct cras_iodev * iodev)1404 unsigned int cras_iodev_get_num_underruns(const struct cras_iodev *iodev)
1405 {
1406 if (iodev->get_num_underruns)
1407 return iodev->get_num_underruns(iodev);
1408 return 0;
1409 }
1410
cras_iodev_get_num_severe_underruns(const struct cras_iodev * iodev)1411 unsigned int cras_iodev_get_num_severe_underruns(const struct cras_iodev *iodev)
1412 {
1413 if (iodev->get_num_severe_underruns)
1414 return iodev->get_num_severe_underruns(iodev);
1415 return 0;
1416 }
1417
cras_iodev_reset_request(struct cras_iodev * iodev)1418 int cras_iodev_reset_request(struct cras_iodev *iodev)
1419 {
1420 /* Ignore requests if there is a pending request.
1421 * This function sends the request from audio thread to main
1422 * thread when audio thread finds a device is in a bad state
1423 * e.g. severe underrun. Before main thread receives the
1424 * request and resets device, audio thread might try to send
1425 * multiple requests because it finds device is still in bad
1426 * state. We should ignore requests in this cause. Otherwise,
1427 * main thread will reset device multiple times.
1428 * The flag is cleared in cras_iodev_open.
1429 * */
1430 if (iodev->reset_request_pending)
1431 return 0;
1432 iodev->reset_request_pending = 1;
1433 return cras_device_monitor_reset_device(iodev->info.idx);
1434 }
1435
ramp_mute_callback(void * data)1436 static void ramp_mute_callback(void *data)
1437 {
1438 struct cras_iodev *odev = (struct cras_iodev *)data;
1439 cras_device_monitor_set_device_mute_state(odev->info.idx);
1440 }
1441
1442 /* Used in audio thread. Check the docstrings of CRAS_IODEV_RAMP_REQUEST. */
cras_iodev_start_ramp(struct cras_iodev * odev,enum CRAS_IODEV_RAMP_REQUEST request)1443 int cras_iodev_start_ramp(struct cras_iodev *odev,
1444 enum CRAS_IODEV_RAMP_REQUEST request)
1445 {
1446 cras_ramp_cb cb = NULL;
1447 void *cb_data = NULL;
1448 int rc;
1449 float from, to, duration_secs;
1450
1451 /* Ignores request if device is closed. */
1452 if (!cras_iodev_is_open(odev))
1453 return 0;
1454
1455 switch (request) {
1456 case CRAS_IODEV_RAMP_REQUEST_UP_UNMUTE:
1457 from = 0.0;
1458 to = 1.0;
1459 duration_secs = RAMP_UNMUTE_DURATION_SECS;
1460 break;
1461 case CRAS_IODEV_RAMP_REQUEST_UP_START_PLAYBACK:
1462 from = 0.0;
1463 to = 1.0;
1464 duration_secs = RAMP_NEW_STREAM_DURATION_SECS;
1465 break;
1466 /* Unmute -> mute. Callback to set mute state should be called after
1467 * ramping is done. */
1468 case CRAS_IODEV_RAMP_REQUEST_DOWN_MUTE:
1469 from = 1.0;
1470 to = 0.0;
1471 duration_secs = RAMP_MUTE_DURATION_SECS;
1472 cb = ramp_mute_callback;
1473 cb_data = (void *)odev;
1474 break;
1475 default:
1476 return -EINVAL;
1477 }
1478
1479 /* Starts ramping. */
1480 rc = cras_mute_ramp_start(odev->ramp, from, to,
1481 duration_secs * odev->format->frame_rate, cb,
1482 cb_data);
1483
1484 if (rc)
1485 return rc;
1486
1487 /* Mute -> unmute case, unmute state should be set after ramping is
1488 * started so device can start playing with samples close to 0. */
1489 if (request == CRAS_IODEV_RAMP_REQUEST_UP_UNMUTE)
1490 cras_device_monitor_set_device_mute_state(odev->info.idx);
1491
1492 return 0;
1493 }
1494
cras_iodev_start_volume_ramp(struct cras_iodev * odev,unsigned int old_volume,unsigned int new_volume)1495 int cras_iodev_start_volume_ramp(struct cras_iodev *odev,
1496 unsigned int old_volume,
1497 unsigned int new_volume)
1498 {
1499 float old_scaler, new_scaler;
1500 float from, to;
1501
1502 if (old_volume == new_volume)
1503 return 0;
1504 if (!cras_iodev_is_open(odev))
1505 return 0;
1506 if (!odev->format)
1507 return -EINVAL;
1508 if (odev->active_node && odev->active_node->softvol_scalers) {
1509 old_scaler = odev->active_node->softvol_scalers[old_volume];
1510 new_scaler = odev->active_node->softvol_scalers[new_volume];
1511 } else {
1512 old_scaler = softvol_get_scaler(old_volume);
1513 new_scaler = softvol_get_scaler(new_volume);
1514 }
1515 if (new_scaler == 0.0) {
1516 return -EINVAL;
1517 }
1518 /* We will soon set odev's volume to new_volume from old_volume.
1519 * Because we're using softvol, we were previously scaling our volume by
1520 * old_scaler. If we want to avoid a jump in volume, we need to start
1521 * our ramp so that (from * new_scaler) = old_scaler. */
1522 from = old_scaler / new_scaler;
1523 to = 1.0;
1524
1525 return cras_volume_ramp_start(odev->ramp, from, to,
1526 RAMP_VOLUME_CHANGE_DURATION_SECS *
1527 odev->format->frame_rate,
1528 NULL, NULL);
1529 }
1530
cras_iodev_set_mute(struct cras_iodev * iodev)1531 int cras_iodev_set_mute(struct cras_iodev *iodev)
1532 {
1533 if (!cras_iodev_is_open(iodev))
1534 return 0;
1535
1536 if (iodev->set_mute)
1537 iodev->set_mute(iodev);
1538 return 0;
1539 }
1540
cras_iodev_update_highest_hw_level(struct cras_iodev * iodev,unsigned int hw_level)1541 void cras_iodev_update_highest_hw_level(struct cras_iodev *iodev,
1542 unsigned int hw_level)
1543 {
1544 iodev->highest_hw_level = MAX(iodev->highest_hw_level, hw_level);
1545 }
1546
1547 /*
1548 * Makes an input device drop the given number of frames.
1549 * Args:
1550 * iodev - The device.
1551 * frames - How many frames will be dropped in a device.
1552 * Returns:
1553 * The number of frames have been dropped. Negative error code on failure.
1554 */
cras_iodev_drop_frames(struct cras_iodev * iodev,unsigned int frames)1555 static int cras_iodev_drop_frames(struct cras_iodev *iodev, unsigned int frames)
1556 {
1557 struct timespec hw_tstamp;
1558 int rc;
1559
1560 if (iodev->direction != CRAS_STREAM_INPUT)
1561 return -EINVAL;
1562
1563 rc = cras_iodev_frames_queued(iodev, &hw_tstamp);
1564 if (rc < 0)
1565 return rc;
1566
1567 frames = MIN(frames, rc);
1568
1569 rc = iodev->get_buffer(iodev, &iodev->input_data->area, &frames);
1570 if (rc < 0)
1571 return rc;
1572
1573 rc = iodev->put_buffer(iodev, frames);
1574 if (rc < 0)
1575 return rc;
1576
1577 /*
1578 * Tell rate estimator that some frames have been dropped to avoid calculating
1579 * the wrong rate.
1580 */
1581 rate_estimator_add_frames(iodev->rate_est, -frames);
1582
1583 ATLOG(atlog, AUDIO_THREAD_DEV_DROP_FRAMES, iodev->info.idx, frames, 0);
1584
1585 return frames;
1586 }
1587
cras_iodev_drop_frames_by_time(struct cras_iodev * iodev,struct timespec ts)1588 int cras_iodev_drop_frames_by_time(struct cras_iodev *iodev, struct timespec ts)
1589 {
1590 int frames_to_set;
1591 double est_rate;
1592 int rc;
1593
1594 est_rate = iodev->format->frame_rate *
1595 cras_iodev_get_est_rate_ratio(iodev);
1596 frames_to_set = cras_time_to_frames(&ts, est_rate);
1597
1598 rc = cras_iodev_drop_frames(iodev, frames_to_set);
1599
1600 return rc;
1601 }
1602