1 /* Copyright (c) 2014 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 <syslog.h> 7 8 #include "audio_thread_log.h" 9 #include "byte_buffer.h" 10 #include "cras_fmt_conv.h" 11 #include "dev_stream.h" 12 #include "cras_audio_area.h" 13 #include "cras_mix.h" 14 #include "cras_shm.h" 15 16 /* 17 * Sleep this much time past the buffer size to be sure at least 18 * the buffer size is captured when the audio thread wakes up. 19 */ 20 static const unsigned int capture_extra_sleep_frames = 20; 21 /* Adjust device's sample rate by this step faster or slower. Used 22 * to make sure multiple active device has stable buffer level. 23 */ 24 static const int coarse_rate_adjust_step = 3; 25 26 /* 27 * Allow capture callback to fire this much earlier than the scheduled 28 * next_cb_ts to avoid an extra wake of audio thread. 29 */ 30 static const struct timespec capture_callback_fuzz_ts = { 31 .tv_sec = 0, 32 .tv_nsec = 1000000, /* 1 ms. */ 33 }; 34 35 struct dev_stream *dev_stream_create(struct cras_rstream *stream, 36 unsigned int dev_id, 37 const struct cras_audio_format *dev_fmt, 38 void *dev_ptr, 39 struct timespec *cb_ts) 40 { 41 struct dev_stream *out; 42 struct cras_audio_format *stream_fmt = &stream->format; 43 int rc = 0; 44 unsigned int max_frames, dev_frames, buf_bytes; 45 const struct cras_audio_format *ofmt; 46 47 out = calloc(1, sizeof(*out)); 48 out->dev_id = dev_id; 49 out->stream = stream; 50 out->dev_rate = dev_fmt->frame_rate; 51 52 if (stream->direction == CRAS_STREAM_OUTPUT) { 53 max_frames = MAX(stream->buffer_frames, 54 cras_frames_at_rate(stream_fmt->frame_rate, 55 stream->buffer_frames, 56 dev_fmt->frame_rate)); 57 rc = config_format_converter(&out->conv, 58 stream->direction, 59 stream_fmt, 60 dev_fmt, 61 max_frames); 62 } else { 63 max_frames = MAX(stream->buffer_frames, 64 cras_frames_at_rate(dev_fmt->frame_rate, 65 stream->buffer_frames, 66 stream_fmt->frame_rate)); 67 rc = config_format_converter(&out->conv, 68 stream->direction, 69 dev_fmt, 70 stream_fmt, 71 max_frames); 72 } 73 if (rc) { 74 free(out); 75 return NULL; 76 } 77 78 ofmt = cras_fmt_conv_out_format(out->conv); 79 80 dev_frames = (stream->direction == CRAS_STREAM_OUTPUT) 81 ? cras_fmt_conv_in_frames_to_out(out->conv, 82 stream->buffer_frames) 83 : cras_fmt_conv_out_frames_to_in(out->conv, 84 stream->buffer_frames); 85 86 out->conv_buffer_size_frames = 2 * MAX(dev_frames, 87 stream->buffer_frames); 88 89 /* Create conversion buffer and area using the output format 90 * of the format converter. Note that this format might not be 91 * identical to stream_fmt for capture. */ 92 buf_bytes = out->conv_buffer_size_frames * cras_get_format_bytes(ofmt); 93 out->conv_buffer = byte_buffer_create(buf_bytes); 94 out->conv_area = cras_audio_area_create(ofmt->num_channels); 95 96 cras_frames_to_time(cras_rstream_get_cb_threshold(stream), 97 stream_fmt->frame_rate, 98 &stream->sleep_interval_ts); 99 stream->next_cb_ts = *cb_ts; 100 101 if (stream->direction != CRAS_STREAM_OUTPUT) { 102 struct timespec extra_sleep; 103 104 cras_frames_to_time(capture_extra_sleep_frames, 105 stream->format.frame_rate, &extra_sleep); 106 add_timespecs(&stream->next_cb_ts, &stream->sleep_interval_ts); 107 add_timespecs(&stream->next_cb_ts, &extra_sleep); 108 } 109 110 cras_rstream_dev_attach(stream, dev_id, dev_ptr); 111 112 return out; 113 } 114 115 void dev_stream_destroy(struct dev_stream *dev_stream) 116 { 117 cras_rstream_dev_detach(dev_stream->stream, dev_stream->dev_id); 118 if (dev_stream->conv) { 119 cras_audio_area_destroy(dev_stream->conv_area); 120 cras_fmt_conv_destroy(dev_stream->conv); 121 byte_buffer_destroy(dev_stream->conv_buffer); 122 } 123 free(dev_stream); 124 } 125 126 void dev_stream_set_dev_rate(struct dev_stream *dev_stream, 127 unsigned int dev_rate, 128 double dev_rate_ratio, 129 double master_rate_ratio, 130 int coarse_rate_adjust) 131 { 132 if (dev_stream->dev_id == dev_stream->stream->master_dev.dev_id) { 133 cras_fmt_conv_set_linear_resample_rates( 134 dev_stream->conv, 135 dev_rate, 136 dev_rate); 137 cras_frames_to_time_precise( 138 cras_rstream_get_cb_threshold(dev_stream->stream), 139 dev_stream->stream->format.frame_rate * dev_rate_ratio, 140 &dev_stream->stream->sleep_interval_ts); 141 } else { 142 double new_rate = dev_rate * dev_rate_ratio / 143 master_rate_ratio + 144 coarse_rate_adjust_step * coarse_rate_adjust; 145 cras_fmt_conv_set_linear_resample_rates( 146 dev_stream->conv, 147 dev_rate, 148 new_rate); 149 } 150 151 } 152 153 int dev_stream_mix(struct dev_stream *dev_stream, 154 const struct cras_audio_format *fmt, 155 uint8_t *dst, 156 unsigned int num_to_write) 157 { 158 struct cras_rstream *rstream = dev_stream->stream; 159 uint8_t *src; 160 uint8_t *target = dst; 161 unsigned int fr_written, fr_read; 162 unsigned int buffer_offset; 163 int fr_in_buf; 164 unsigned int num_samples; 165 size_t frames = 0; 166 unsigned int dev_frames; 167 float mix_vol; 168 169 fr_in_buf = dev_stream_playback_frames(dev_stream); 170 if (fr_in_buf <= 0) 171 return fr_in_buf; 172 if (fr_in_buf < num_to_write) 173 num_to_write = fr_in_buf; 174 175 buffer_offset = cras_rstream_dev_offset(rstream, dev_stream->dev_id); 176 177 /* Stream volume scaler. */ 178 mix_vol = cras_rstream_get_volume_scaler(dev_stream->stream); 179 180 fr_written = 0; 181 fr_read = 0; 182 while (fr_written < num_to_write) { 183 unsigned int read_frames; 184 src = cras_rstream_get_readable_frames( 185 rstream, buffer_offset + fr_read, &frames); 186 if (frames == 0) 187 break; 188 if (cras_fmt_conversion_needed(dev_stream->conv)) { 189 read_frames = frames; 190 dev_frames = cras_fmt_conv_convert_frames( 191 dev_stream->conv, 192 src, 193 dev_stream->conv_buffer->bytes, 194 &read_frames, 195 num_to_write - fr_written); 196 src = dev_stream->conv_buffer->bytes; 197 } else { 198 dev_frames = MIN(frames, num_to_write - fr_written); 199 read_frames = dev_frames; 200 } 201 num_samples = dev_frames * fmt->num_channels; 202 cras_mix_add(fmt->format, target, src, num_samples, 1, 203 cras_rstream_get_mute(rstream), mix_vol); 204 target += dev_frames * cras_get_format_bytes(fmt); 205 fr_written += dev_frames; 206 fr_read += read_frames; 207 } 208 209 cras_rstream_dev_offset_update(rstream, fr_read, dev_stream->dev_id); 210 ATLOG(atlog, AUDIO_THREAD_DEV_STREAM_MIX, 211 fr_written, fr_read, 0); 212 213 return fr_written; 214 } 215 216 /* Copy from the captured buffer to the temporary format converted buffer. */ 217 static unsigned int capture_with_fmt_conv(struct dev_stream *dev_stream, 218 const uint8_t *source_samples, 219 unsigned int num_frames) 220 { 221 const struct cras_audio_format *source_format; 222 const struct cras_audio_format *dst_format; 223 uint8_t *buffer; 224 unsigned int total_read = 0; 225 unsigned int write_frames; 226 unsigned int read_frames; 227 unsigned int source_frame_bytes; 228 unsigned int dst_frame_bytes; 229 230 source_format = cras_fmt_conv_in_format(dev_stream->conv); 231 source_frame_bytes = cras_get_format_bytes(source_format); 232 dst_format = cras_fmt_conv_out_format(dev_stream->conv); 233 dst_frame_bytes = cras_get_format_bytes(dst_format); 234 235 dev_stream->conv_area->num_channels = dst_format->num_channels; 236 237 while (total_read < num_frames) { 238 buffer = buf_write_pointer_size(dev_stream->conv_buffer, 239 &write_frames); 240 write_frames /= dst_frame_bytes; 241 if (write_frames == 0) 242 break; 243 244 read_frames = num_frames - total_read; 245 write_frames = cras_fmt_conv_convert_frames( 246 dev_stream->conv, 247 source_samples, 248 buffer, 249 &read_frames, 250 write_frames); 251 total_read += read_frames; 252 source_samples += read_frames * source_frame_bytes; 253 buf_increment_write(dev_stream->conv_buffer, 254 write_frames * dst_frame_bytes); 255 } 256 257 return total_read; 258 } 259 260 /* Copy from the converted buffer to the stream shm. These have the same format 261 * at this point. */ 262 static unsigned int capture_copy_converted_to_stream( 263 struct dev_stream *dev_stream, 264 struct cras_rstream *rstream, 265 float software_gain_scaler) 266 { 267 struct cras_audio_shm *shm; 268 uint8_t *stream_samples; 269 uint8_t *converted_samples; 270 unsigned int num_frames; 271 unsigned int total_written = 0; 272 unsigned int write_frames; 273 unsigned int frame_bytes; 274 unsigned int offset; 275 const struct cras_audio_format *fmt; 276 277 shm = cras_rstream_input_shm(rstream); 278 279 fmt = cras_fmt_conv_out_format(dev_stream->conv); 280 frame_bytes = cras_get_format_bytes(fmt); 281 282 offset = cras_rstream_dev_offset(rstream, dev_stream->dev_id); 283 284 stream_samples = cras_shm_get_writeable_frames( 285 shm, 286 cras_rstream_get_cb_threshold(rstream), 287 &rstream->audio_area->frames); 288 num_frames = MIN(rstream->audio_area->frames - offset, 289 buf_queued_bytes(dev_stream->conv_buffer) / 290 frame_bytes); 291 292 ATLOG(atlog, AUDIO_THREAD_CONV_COPY, 293 shm->area->write_buf_idx, 294 rstream->audio_area->frames, 295 offset); 296 297 while (total_written < num_frames) { 298 converted_samples = 299 buf_read_pointer_size(dev_stream->conv_buffer, 300 &write_frames); 301 write_frames /= frame_bytes; 302 write_frames = MIN(write_frames, num_frames - total_written); 303 304 cras_audio_area_config_buf_pointers(dev_stream->conv_area, 305 fmt, 306 converted_samples); 307 cras_audio_area_config_channels(dev_stream->conv_area, fmt); 308 dev_stream->conv_area->frames = write_frames; 309 310 cras_audio_area_config_buf_pointers(rstream->audio_area, 311 &rstream->format, 312 stream_samples); 313 314 cras_audio_area_copy(rstream->audio_area, offset, 315 &rstream->format, 316 dev_stream->conv_area, 0, 317 software_gain_scaler); 318 319 buf_increment_read(dev_stream->conv_buffer, 320 write_frames * frame_bytes); 321 total_written += write_frames; 322 cras_rstream_dev_offset_update(rstream, write_frames, 323 dev_stream->dev_id); 324 offset = cras_rstream_dev_offset(rstream, dev_stream->dev_id); 325 } 326 327 ATLOG(atlog, AUDIO_THREAD_CAPTURE_WRITE, 328 rstream->stream_id, 329 total_written, 330 cras_shm_frames_written(shm)); 331 return total_written; 332 } 333 334 unsigned int dev_stream_capture(struct dev_stream *dev_stream, 335 const struct cras_audio_area *area, 336 unsigned int area_offset, 337 float software_gain_scaler) 338 { 339 struct cras_rstream *rstream = dev_stream->stream; 340 struct cras_audio_shm *shm; 341 uint8_t *stream_samples; 342 unsigned int nread; 343 344 /* Check if format conversion is needed. */ 345 if (cras_fmt_conversion_needed(dev_stream->conv)) { 346 unsigned int format_bytes; 347 348 format_bytes = cras_get_format_bytes( 349 cras_fmt_conv_in_format(dev_stream->conv)); 350 nread = capture_with_fmt_conv( 351 dev_stream, 352 area->channels[0].buf + area_offset * format_bytes, 353 area->frames - area_offset); 354 capture_copy_converted_to_stream(dev_stream, rstream, 355 software_gain_scaler); 356 } else { 357 unsigned int offset = 358 cras_rstream_dev_offset(rstream, dev_stream->dev_id); 359 360 /* Set up the shm area and copy to it. */ 361 shm = cras_rstream_input_shm(rstream); 362 stream_samples = cras_shm_get_writeable_frames( 363 shm, 364 cras_rstream_get_cb_threshold(rstream), 365 &rstream->audio_area->frames); 366 cras_audio_area_config_buf_pointers(rstream->audio_area, 367 &rstream->format, 368 stream_samples); 369 370 nread = cras_audio_area_copy(rstream->audio_area, offset, 371 &rstream->format, area, 372 area_offset, 373 software_gain_scaler); 374 375 ATLOG(atlog, AUDIO_THREAD_CAPTURE_WRITE, 376 rstream->stream_id, 377 nread, 378 cras_shm_frames_written(shm)); 379 cras_rstream_dev_offset_update(rstream, nread, 380 dev_stream->dev_id); 381 } 382 383 return nread; 384 } 385 386 int dev_stream_attached_devs(const struct dev_stream *dev_stream) 387 { 388 return dev_stream->stream->num_attached_devs; 389 } 390 391 void dev_stream_update_frames(const struct dev_stream *dev_stream) 392 { 393 cras_rstream_update_queued_frames(dev_stream->stream); 394 } 395 396 int dev_stream_playback_frames(const struct dev_stream *dev_stream) 397 { 398 int frames; 399 400 frames = cras_rstream_playable_frames(dev_stream->stream, 401 dev_stream->dev_id); 402 if (frames < 0) 403 return frames; 404 405 if (!dev_stream->conv) 406 return frames; 407 408 return cras_fmt_conv_in_frames_to_out(dev_stream->conv, frames); 409 } 410 411 unsigned int dev_stream_cb_threshold(const struct dev_stream *dev_stream) 412 { 413 const struct cras_rstream *rstream = dev_stream->stream; 414 unsigned int cb_threshold = cras_rstream_get_cb_threshold(rstream); 415 416 if (rstream->direction == CRAS_STREAM_OUTPUT) 417 return cras_fmt_conv_in_frames_to_out(dev_stream->conv, 418 cb_threshold); 419 else 420 return cras_fmt_conv_out_frames_to_in(dev_stream->conv, 421 cb_threshold); 422 } 423 424 unsigned int dev_stream_capture_avail(const struct dev_stream *dev_stream) 425 { 426 struct cras_audio_shm *shm; 427 struct cras_rstream *rstream = dev_stream->stream; 428 unsigned int frames_avail; 429 unsigned int conv_buf_level; 430 unsigned int format_bytes; 431 unsigned int wlimit; 432 unsigned int dev_offset = 433 cras_rstream_dev_offset(rstream, dev_stream->dev_id); 434 435 shm = cras_rstream_input_shm(rstream); 436 437 wlimit = cras_rstream_get_max_write_frames(rstream); 438 wlimit -= dev_offset; 439 cras_shm_get_writeable_frames(shm, wlimit, &frames_avail); 440 441 if (!dev_stream->conv) 442 return frames_avail; 443 444 format_bytes = cras_get_format_bytes( 445 cras_fmt_conv_out_format(dev_stream->conv)); 446 447 /* Sample rate conversion may cause some sample left in conv_buffer 448 * take this buffer into account. */ 449 conv_buf_level = buf_queued_bytes(dev_stream->conv_buffer) / 450 format_bytes; 451 if (frames_avail < conv_buf_level) 452 return 0; 453 else 454 frames_avail -= conv_buf_level; 455 456 frames_avail = MIN(frames_avail, 457 buf_available_bytes(dev_stream->conv_buffer) / 458 format_bytes); 459 460 return cras_fmt_conv_out_frames_to_in(dev_stream->conv, frames_avail); 461 } 462 463 /* TODO(dgreid) remove this hack to reset the time if needed. */ 464 static void check_next_wake_time(struct dev_stream *dev_stream) 465 { 466 struct cras_rstream *rstream = dev_stream->stream; 467 struct timespec now; 468 469 clock_gettime(CLOCK_MONOTONIC_RAW, &now); 470 if (timespec_after(&now, &rstream->next_cb_ts)) { 471 rstream->next_cb_ts = now; 472 add_timespecs(&rstream->next_cb_ts, 473 &rstream->sleep_interval_ts); 474 } 475 } 476 477 int dev_stream_playback_update_rstream(struct dev_stream *dev_stream) 478 { 479 cras_rstream_update_output_read_pointer(dev_stream->stream); 480 return 0; 481 } 482 483 static int late_enough_for_capture_callback(struct dev_stream *dev_stream) 484 { 485 struct timespec now; 486 struct cras_rstream *rstream = dev_stream->stream; 487 clock_gettime(CLOCK_MONOTONIC_RAW, &now); 488 add_timespecs(&now, &capture_callback_fuzz_ts); 489 return timespec_after(&now, &rstream->next_cb_ts); 490 } 491 492 int dev_stream_capture_update_rstream(struct dev_stream *dev_stream) 493 { 494 struct cras_rstream *rstream = dev_stream->stream; 495 unsigned int frames_ready = cras_rstream_get_cb_threshold(rstream); 496 int rc; 497 498 cras_rstream_update_input_write_pointer(rstream); 499 500 /* 501 * For stream without BULK_AUDIO_OK flag, if it isn't time for 502 * this stream then skip it. 503 */ 504 if (!(rstream->flags & BULK_AUDIO_OK) && 505 !late_enough_for_capture_callback(dev_stream)) 506 return 0; 507 508 /* If there is not enough data for one callback, skip it. */ 509 if (!cras_rstream_input_level_met(rstream)) 510 return 0; 511 512 /* Enough data for this stream. */ 513 if (rstream->flags & BULK_AUDIO_OK) 514 frames_ready = cras_rstream_level(rstream); 515 516 ATLOG(atlog, AUDIO_THREAD_CAPTURE_POST, 517 rstream->stream_id, 518 frames_ready, 519 rstream->shm.area->read_buf_idx); 520 521 rc = cras_rstream_audio_ready(rstream, frames_ready); 522 523 if (rc < 0) 524 return rc; 525 526 /* Update next callback time according to perfect schedule. */ 527 add_timespecs(&rstream->next_cb_ts, 528 &rstream->sleep_interval_ts); 529 /* Reset schedule if the schedule is missed. */ 530 check_next_wake_time(dev_stream); 531 532 return 0; 533 } 534 535 void cras_set_playback_timestamp(size_t frame_rate, 536 size_t frames, 537 struct cras_timespec *ts) 538 { 539 cras_clock_gettime(CLOCK_MONOTONIC_RAW, ts); 540 541 /* For playback, want now + samples left to be played. 542 * ts = time next written sample will be played to DAC, 543 */ 544 ts->tv_nsec += frames * 1000000000ULL / frame_rate; 545 while (ts->tv_nsec > 1000000000ULL) { 546 ts->tv_sec++; 547 ts->tv_nsec -= 1000000000ULL; 548 } 549 } 550 551 void cras_set_capture_timestamp(size_t frame_rate, 552 size_t frames, 553 struct cras_timespec *ts) 554 { 555 long tmp; 556 557 cras_clock_gettime(CLOCK_MONOTONIC_RAW, ts); 558 559 /* For capture, now - samples left to be read. 560 * ts = time next sample to be read was captured at ADC. 561 */ 562 tmp = frames * (1000000000L / frame_rate); 563 while (tmp > 1000000000L) { 564 tmp -= 1000000000L; 565 ts->tv_sec--; 566 } 567 if (ts->tv_nsec >= tmp) 568 ts->tv_nsec -= tmp; 569 else { 570 tmp -= ts->tv_nsec; 571 ts->tv_nsec = 1000000000L - tmp; 572 ts->tv_sec--; 573 } 574 } 575 576 void dev_stream_set_delay(const struct dev_stream *dev_stream, 577 unsigned int delay_frames) 578 { 579 struct cras_rstream *rstream = dev_stream->stream; 580 struct cras_audio_shm *shm; 581 unsigned int stream_frames; 582 583 if (rstream->direction == CRAS_STREAM_OUTPUT) { 584 shm = cras_rstream_output_shm(rstream); 585 stream_frames = cras_fmt_conv_out_frames_to_in(dev_stream->conv, 586 delay_frames); 587 cras_set_playback_timestamp(rstream->format.frame_rate, 588 stream_frames + 589 cras_shm_get_frames(shm), 590 &shm->area->ts); 591 } else { 592 shm = cras_rstream_input_shm(rstream); 593 stream_frames = cras_fmt_conv_in_frames_to_out(dev_stream->conv, 594 delay_frames); 595 if (cras_shm_frames_written(shm) == 0) 596 cras_set_capture_timestamp( 597 rstream->format.frame_rate, 598 stream_frames, 599 &shm->area->ts); 600 } 601 } 602 603 int dev_stream_can_fetch(struct dev_stream *dev_stream) 604 { 605 struct cras_rstream *rstream = dev_stream->stream; 606 struct cras_audio_shm *shm; 607 608 shm = cras_rstream_output_shm(rstream); 609 610 /* Don't fetch if the previous request hasn't got response. */ 611 return !cras_shm_callback_pending(shm) && 612 cras_shm_is_buffer_available(shm); 613 } 614 615 int dev_stream_request_playback_samples(struct dev_stream *dev_stream, 616 const struct timespec *now) 617 { 618 struct cras_rstream *rstream = dev_stream->stream; 619 int rc; 620 621 rc = cras_rstream_request_audio(dev_stream->stream, now); 622 if (rc < 0) 623 return rc; 624 625 add_timespecs(&rstream->next_cb_ts, 626 &rstream->sleep_interval_ts); 627 check_next_wake_time(dev_stream); 628 cras_shm_set_callback_pending(cras_rstream_output_shm(rstream), 1); 629 630 return 0; 631 } 632 633 int dev_stream_poll_stream_fd(const struct dev_stream *dev_stream) 634 { 635 const struct cras_rstream *stream = dev_stream->stream; 636 637 if (!stream_uses_output(stream) || 638 !cras_shm_callback_pending(&stream->shm) || 639 cras_rstream_get_is_draining(stream)) 640 return -1; 641 642 return stream->fd; 643 } 644 645 /* 646 * Needed frames from this device such that written frames in shm meets 647 * cb_threshold. 648 */ 649 static int get_input_needed_frames(struct dev_stream *dev_stream, 650 unsigned int curr_level) 651 { 652 struct cras_rstream *rstream = dev_stream->stream; 653 unsigned int rstream_level = cras_rstream_level(rstream); 654 unsigned int dev_offset = cras_rstream_dev_offset( 655 rstream, dev_stream->dev_id); 656 unsigned int needed_for_stream; 657 658 /* 659 * rstream_level + def_offset is the number of frames written to shm 660 * from this device. 661 */ 662 if (rstream_level + dev_offset > rstream->cb_threshold) { 663 /* Enough frames from this device for this stream. */ 664 return 0; 665 } 666 667 /* 668 * Needed number of frames in shm such that written frames in shm meets 669 * cb_threshold. 670 */ 671 needed_for_stream = rstream->cb_threshold - rstream_level - dev_offset; 672 673 /* Convert the number of frames from stream format to device format. */ 674 return cras_fmt_conv_out_frames_to_in(dev_stream->conv, 675 needed_for_stream); 676 677 } 678 679 /* 680 * Gets proper wake up time for an input stream. It considers both 681 * time for samples to reach one callback level, and the time for next callback. 682 */ 683 static int get_input_wake_time(struct dev_stream *dev_stream, 684 unsigned int curr_level, 685 struct timespec *level_tstamp, 686 struct timespec *wake_time_out) 687 { 688 struct cras_rstream *rstream = dev_stream->stream; 689 struct timespec time_for_sample; 690 int needed_frames_from_device; 691 692 needed_frames_from_device = get_input_needed_frames( 693 dev_stream, curr_level); 694 695 /* 696 * If current frames in the device can provide needed amount for stream, 697 * there is no need to wait. 698 */ 699 if (curr_level >= needed_frames_from_device) 700 needed_frames_from_device = 0; 701 702 else 703 needed_frames_from_device -= curr_level; 704 705 cras_frames_to_time(needed_frames_from_device, 706 dev_stream->dev_rate, 707 &time_for_sample); 708 709 add_timespecs(&time_for_sample, level_tstamp); 710 711 /* Select the time that is later so both sample and time conditions 712 * are met. */ 713 if (timespec_after(&time_for_sample, &rstream->next_cb_ts)) { 714 *wake_time_out = time_for_sample; 715 } else { 716 *wake_time_out = rstream->next_cb_ts; 717 } 718 719 return 0; 720 } 721 722 int dev_stream_wake_time(struct dev_stream *dev_stream, 723 unsigned int curr_level, 724 struct timespec *level_tstamp, 725 struct timespec *wake_time_out) 726 { 727 if (dev_stream->stream->direction == CRAS_STREAM_OUTPUT) { 728 /* 729 * TODO(cychiang) Implement the method for output stream. 730 * The logic should be similar to what 731 * get_next_stream_wake_from_list in audio_thread.c is doing. 732 */ 733 return -EINVAL; 734 } 735 736 return get_input_wake_time(dev_stream, curr_level, level_tstamp, 737 wake_time_out); 738 } 739