| 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 base::TimeTicks target_playout_time = base::TimeTicks::Now(); |
| 515 hr = audio_clock_->GetPosition(&position, NULL); | 516 hr = audio_clock_->GetPosition(&position, NULL); |
| 516 if (SUCCEEDED(hr)) { | 517 if (SUCCEEDED(hr)) { |
| 517 // Stream position of the sample that is currently playing | 518 // Stream position of the sample that is currently playing |
| 518 // through the speaker. | 519 // through the speaker. |
| 519 double pos_sample_playing_frames = format_.Format.nSamplesPerSec * | 520 double pos_sample_playing_frames = format_.Format.nSamplesPerSec * |
| 520 (static_cast<double>(position) / device_frequency); | 521 (static_cast<double>(position) / device_frequency); |
| 521 | 522 |
| 522 // Stream position of the last sample written to the endpoint | 523 // Stream position of the last sample written to the endpoint |
| 523 // buffer. Note that, the packet we are about to receive in | 524 // buffer. Note that, the packet we are about to receive in |
| 524 // the upcoming callback is also included. | 525 // the upcoming callback is also included. |
| 525 size_t pos_last_sample_written_frames = | 526 size_t pos_last_sample_written_frames = |
| 526 num_written_frames_ + packet_size_frames_; | 527 num_written_frames_ + packet_size_frames_; |
| 527 | 528 |
| 528 // Derive the actual delay value which will be fed to the | 529 double audio_delay_frames = |
| 529 // render client using the OnMoreData() callback. | 530 pos_last_sample_written_frames - pos_sample_playing_frames; |
| 530 audio_delay_bytes = (pos_last_sample_written_frames - | 531 |
| 531 pos_sample_playing_frames) * format_.Format.nBlockAlign; | 532 target_playout_time += base::TimeDelta::FromSecondsD( |
| 533 audio_delay_frames / format_.Format.nSamplesPerSec); |
| 532 } | 534 } |
| 533 | 535 |
| 534 // Read a data packet from the registered client source and | 536 // Read a data packet from the registered client source and |
| 535 // deliver a delay estimate in the same callback to the client. | 537 // deliver a delay estimate in the same callback to the client. |
| 536 | 538 |
| 537 int frames_filled = | 539 int frames_filled = |
| 538 source_->OnMoreData(audio_bus_.get(), audio_delay_bytes, 0); | 540 source_->OnMoreData(target_playout_time, 0, audio_bus_.get()); |
| 539 uint32_t num_filled_bytes = frames_filled * format_.Format.nBlockAlign; | 541 uint32_t num_filled_bytes = frames_filled * format_.Format.nBlockAlign; |
| 540 DCHECK_LE(num_filled_bytes, packet_size_bytes_); | 542 DCHECK_LE(num_filled_bytes, packet_size_bytes_); |
| 541 | 543 |
| 542 // Note: If this ever changes to output raw float the data must be | 544 // Note: If this ever changes to output raw float the data must be |
| 543 // clipped and sanitized since it may come from an untrusted | 545 // clipped and sanitized since it may come from an untrusted |
| 544 // source such as NaCl. | 546 // source such as NaCl. |
| 545 const int bytes_per_sample = format_.Format.wBitsPerSample >> 3; | 547 const int bytes_per_sample = format_.Format.wBitsPerSample >> 3; |
| 546 audio_bus_->Scale(volume_); | 548 audio_bus_->Scale(volume_); |
| 547 audio_bus_->ToInterleaved( | 549 audio_bus_->ToInterleaved( |
| 548 frames_filled, bytes_per_sample, audio_data); | 550 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; | 636 DVLOG(1) << "IAudioClient::GetBufferSize: " << std::hex << hr; |
| 635 return hr; | 637 return hr; |
| 636 } | 638 } |
| 637 | 639 |
| 638 *endpoint_buffer_size = buffer_size_in_frames; | 640 *endpoint_buffer_size = buffer_size_in_frames; |
| 639 DVLOG(2) << "endpoint buffer size: " << buffer_size_in_frames; | 641 DVLOG(2) << "endpoint buffer size: " << buffer_size_in_frames; |
| 640 return hr; | 642 return hr; |
| 641 } | 643 } |
| 642 | 644 |
| 643 void WASAPIAudioOutputStream::StopThread() { | 645 void WASAPIAudioOutputStream::StopThread() { |
| 644 if (render_thread_ ) { | 646 if (render_thread_) { |
| 645 if (render_thread_->HasBeenStarted()) { | 647 if (render_thread_->HasBeenStarted()) { |
| 646 // Wait until the thread completes and perform cleanup. | 648 // Wait until the thread completes and perform cleanup. |
| 647 SetEvent(stop_render_event_.Get()); | 649 SetEvent(stop_render_event_.Get()); |
| 648 render_thread_->Join(); | 650 render_thread_->Join(); |
| 649 } | 651 } |
| 650 | 652 |
| 651 render_thread_.reset(); | 653 render_thread_.reset(); |
| 652 | 654 |
| 653 // Ensure that we don't quit the main thread loop immediately next | 655 // Ensure that we don't quit the main thread loop immediately next |
| 654 // time Start() is called. | 656 // time Start() is called. |
| 655 ResetEvent(stop_render_event_.Get()); | 657 ResetEvent(stop_render_event_.Get()); |
| 656 } | 658 } |
| 657 | 659 |
| 658 source_ = NULL; | 660 source_ = NULL; |
| 659 } | 661 } |
| 660 | 662 |
| 661 } // namespace media | 663 } // namespace media |
| OLD | NEW |