Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1335)

Side by Side Diff: media/audio/win/audio_low_latency_output_win.cc

Issue 2101303004: Pass delay and timestamp to AudioSourceCallback::OnMoreData. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Changes based on comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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();
Mikhail 2016/09/08 07:44:12 wouldn't it be more accurate to use the following
miu 2016/09/08 20:46:48 Good catch! Yes, that sounds like a great idea. WD
James West 2016/09/13 07:40:50 I agree that's the correct way to do it. I impleme
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 int 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::FromMicroseconds(
533 audio_delay_frames * base::Time::kMicrosecondsPerSecond /
534 format_.Format.nSamplesPerSec);
532 } 535 }
533 536
534 // Read a data packet from the registered client source and 537 // Read a data packet from the registered client source and
535 // deliver a delay estimate in the same callback to the client. 538 // deliver a delay estimate in the same callback to the client.
536 539
537 int frames_filled = 540 int frames_filled =
538 source_->OnMoreData(audio_bus_.get(), audio_delay_bytes, 0); 541 source_->OnMoreData(target_playout_time, 0, audio_bus_.get());
539 uint32_t num_filled_bytes = frames_filled * format_.Format.nBlockAlign; 542 uint32_t num_filled_bytes = frames_filled * format_.Format.nBlockAlign;
540 DCHECK_LE(num_filled_bytes, packet_size_bytes_); 543 DCHECK_LE(num_filled_bytes, packet_size_bytes_);
541 544
542 // Note: If this ever changes to output raw float the data must be 545 // Note: If this ever changes to output raw float the data must be
543 // clipped and sanitized since it may come from an untrusted 546 // clipped and sanitized since it may come from an untrusted
544 // source such as NaCl. 547 // source such as NaCl.
545 const int bytes_per_sample = format_.Format.wBitsPerSample >> 3; 548 const int bytes_per_sample = format_.Format.wBitsPerSample >> 3;
546 audio_bus_->Scale(volume_); 549 audio_bus_->Scale(volume_);
547 audio_bus_->ToInterleaved( 550 audio_bus_->ToInterleaved(
548 frames_filled, bytes_per_sample, audio_data); 551 frames_filled, bytes_per_sample, audio_data);
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
634 DVLOG(1) << "IAudioClient::GetBufferSize: " << std::hex << hr; 637 DVLOG(1) << "IAudioClient::GetBufferSize: " << std::hex << hr;
635 return hr; 638 return hr;
636 } 639 }
637 640
638 *endpoint_buffer_size = buffer_size_in_frames; 641 *endpoint_buffer_size = buffer_size_in_frames;
639 DVLOG(2) << "endpoint buffer size: " << buffer_size_in_frames; 642 DVLOG(2) << "endpoint buffer size: " << buffer_size_in_frames;
640 return hr; 643 return hr;
641 } 644 }
642 645
643 void WASAPIAudioOutputStream::StopThread() { 646 void WASAPIAudioOutputStream::StopThread() {
644 if (render_thread_ ) { 647 if (render_thread_) {
645 if (render_thread_->HasBeenStarted()) { 648 if (render_thread_->HasBeenStarted()) {
646 // Wait until the thread completes and perform cleanup. 649 // Wait until the thread completes and perform cleanup.
647 SetEvent(stop_render_event_.Get()); 650 SetEvent(stop_render_event_.Get());
648 render_thread_->Join(); 651 render_thread_->Join();
649 } 652 }
650 653
651 render_thread_.reset(); 654 render_thread_.reset();
652 655
653 // Ensure that we don't quit the main thread loop immediately next 656 // Ensure that we don't quit the main thread loop immediately next
654 // time Start() is called. 657 // time Start() is called.
655 ResetEvent(stop_render_event_.Get()); 658 ResetEvent(stop_render_event_.Get());
656 } 659 }
657 660
658 source_ = NULL; 661 source_ = NULL;
659 } 662 }
660 663
661 } // namespace media 664 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698