OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/audio/win/audio_low_latency_output_win.h" | 5 #include "media/audio/win/audio_low_latency_output_win.h" |
6 | 6 |
7 #include <Functiondiscoverykeys_devpkey.h> | 7 #include <Functiondiscoverykeys_devpkey.h> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
14 #include "base/time/time.h" | |
14 #include "base/trace_event/trace_event.h" | 15 #include "base/trace_event/trace_event.h" |
15 #include "base/win/scoped_propvariant.h" | 16 #include "base/win/scoped_propvariant.h" |
16 #include "media/audio/audio_device_description.h" | 17 #include "media/audio/audio_device_description.h" |
17 #include "media/audio/win/audio_manager_win.h" | 18 #include "media/audio/win/audio_manager_win.h" |
18 #include "media/audio/win/avrt_wrapper_win.h" | 19 #include "media/audio/win/avrt_wrapper_win.h" |
19 #include "media/audio/win/core_audio_util_win.h" | 20 #include "media/audio/win/core_audio_util_win.h" |
20 #include "media/base/limits.h" | 21 #include "media/base/limits.h" |
21 #include "media/base/media_switches.h" | 22 #include "media/base/media_switches.h" |
22 | 23 |
23 using base::win::ScopedComPtr; | 24 using base::win::ScopedComPtr; |
(...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
504 << std::hex << hr; | 505 << std::hex << hr; |
505 return false; | 506 return false; |
506 } | 507 } |
507 | 508 |
508 // Derive the audio delay which corresponds to the delay between | 509 // Derive the audio delay which corresponds to the delay between |
509 // a render event and the time when the first audio sample in a | 510 // a render event and the time when the first audio sample in a |
510 // packet is played out through the speaker. This delay value | 511 // packet is played out through the speaker. This delay value |
511 // can typically be utilized by an acoustic echo-control (AEC) | 512 // can typically be utilized by an acoustic echo-control (AEC) |
512 // unit at the render side. | 513 // unit at the render side. |
513 UINT64 position = 0; | 514 UINT64 position = 0; |
514 uint32_t audio_delay_bytes = 0; | 515 UINT64 qpc_position = 0; |
515 hr = audio_clock_->GetPosition(&position, NULL); | 516 base::TimeDelta delay; |
517 base::TimeTicks delay_timestamp; | |
518 hr = audio_clock_->GetPosition(&position, &qpc_position); | |
516 if (SUCCEEDED(hr)) { | 519 if (SUCCEEDED(hr)) { |
517 // Stream position of the sample that is currently playing | 520 // Number of frames already played out through the speaker. |
518 // through the speaker. | 521 const uint64_t played_out_frames = |
519 double pos_sample_playing_frames = format_.Format.nSamplesPerSec * | 522 format_.Format.nSamplesPerSec * position / device_frequency; |
chcunningham
2016/09/23 20:53:30
int division
jameswest
2016/09/29 00:52:24
As discussed, this integer division is safe.
| |
520 (static_cast<double>(position) / device_frequency); | |
521 | 523 |
522 // Stream position of the last sample written to the endpoint | 524 // Number of frames that have been written to the buffer but not yet |
523 // buffer. Note that, the packet we are about to receive in | 525 // played out. |
524 // the upcoming callback is also included. | 526 const uint64_t delay_frames = num_written_frames_ - played_out_frames; |
525 size_t pos_last_sample_written_frames = | |
526 num_written_frames_ + packet_size_frames_; | |
527 | 527 |
528 // Derive the actual delay value which will be fed to the | 528 // Convert the delay from frames to time. |
529 // render client using the OnMoreData() callback. | 529 delay = base::TimeDelta::FromMicroseconds( |
530 audio_delay_bytes = (pos_last_sample_written_frames - | 530 delay_frames * base::Time::kMicrosecondsPerSecond / |
531 pos_sample_playing_frames) * format_.Format.nBlockAlign; | 531 format_.Format.nSamplesPerSec); |
532 | |
533 delay_timestamp = base::TimeTicks::FromQPCValue(qpc_position); | |
534 } else { | |
535 // Use a delay of zero. | |
536 delay_timestamp = base::TimeTicks::Now(); | |
532 } | 537 } |
533 | 538 |
534 // Read a data packet from the registered client source and | 539 // Read a data packet from the registered client source and |
535 // deliver a delay estimate in the same callback to the client. | 540 // deliver a delay estimate in the same callback to the client. |
536 | 541 |
537 int frames_filled = | 542 int frames_filled = |
538 source_->OnMoreData(audio_bus_.get(), audio_delay_bytes, 0); | 543 source_->OnMoreData(delay, delay_timestamp, 0, audio_bus_.get()); |
539 uint32_t num_filled_bytes = frames_filled * format_.Format.nBlockAlign; | 544 uint32_t num_filled_bytes = frames_filled * format_.Format.nBlockAlign; |
540 DCHECK_LE(num_filled_bytes, packet_size_bytes_); | 545 DCHECK_LE(num_filled_bytes, packet_size_bytes_); |
541 | 546 |
542 // Note: If this ever changes to output raw float the data must be | 547 // Note: If this ever changes to output raw float the data must be |
543 // clipped and sanitized since it may come from an untrusted | 548 // clipped and sanitized since it may come from an untrusted |
544 // source such as NaCl. | 549 // source such as NaCl. |
545 const int bytes_per_sample = format_.Format.wBitsPerSample >> 3; | 550 const int bytes_per_sample = format_.Format.wBitsPerSample >> 3; |
546 audio_bus_->Scale(volume_); | 551 audio_bus_->Scale(volume_); |
547 audio_bus_->ToInterleaved( | 552 audio_bus_->ToInterleaved( |
548 frames_filled, bytes_per_sample, audio_data); | 553 frames_filled, bytes_per_sample, audio_data); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
634 DVLOG(1) << "IAudioClient::GetBufferSize: " << std::hex << hr; | 639 DVLOG(1) << "IAudioClient::GetBufferSize: " << std::hex << hr; |
635 return hr; | 640 return hr; |
636 } | 641 } |
637 | 642 |
638 *endpoint_buffer_size = buffer_size_in_frames; | 643 *endpoint_buffer_size = buffer_size_in_frames; |
639 DVLOG(2) << "endpoint buffer size: " << buffer_size_in_frames; | 644 DVLOG(2) << "endpoint buffer size: " << buffer_size_in_frames; |
640 return hr; | 645 return hr; |
641 } | 646 } |
642 | 647 |
643 void WASAPIAudioOutputStream::StopThread() { | 648 void WASAPIAudioOutputStream::StopThread() { |
644 if (render_thread_ ) { | 649 if (render_thread_) { |
645 if (render_thread_->HasBeenStarted()) { | 650 if (render_thread_->HasBeenStarted()) { |
646 // Wait until the thread completes and perform cleanup. | 651 // Wait until the thread completes and perform cleanup. |
647 SetEvent(stop_render_event_.Get()); | 652 SetEvent(stop_render_event_.Get()); |
648 render_thread_->Join(); | 653 render_thread_->Join(); |
649 } | 654 } |
650 | 655 |
651 render_thread_.reset(); | 656 render_thread_.reset(); |
652 | 657 |
653 // Ensure that we don't quit the main thread loop immediately next | 658 // Ensure that we don't quit the main thread loop immediately next |
654 // time Start() is called. | 659 // time Start() is called. |
655 ResetEvent(stop_render_event_.Get()); | 660 ResetEvent(stop_render_event_.Get()); |
656 } | 661 } |
657 | 662 |
658 source_ = NULL; | 663 source_ = NULL; |
659 } | 664 } |
660 | 665 |
661 } // namespace media | 666 } // namespace media |
OLD | NEW |