Plugin Power Saver: Fix a nullptr crash with plugin placeholder resize.
[chromium-blink-merge.git] / media / audio / virtual_audio_output_stream.cc
blob29e3804fe21daa3d6488b8aa80b5463cbc796db0
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
3 // found in the LICENSE file.
5 #include "media/audio/virtual_audio_output_stream.h"
7 #include "base/logging.h"
8 #include "media/audio/virtual_audio_input_stream.h"
10 namespace media {
12 VirtualAudioOutputStream::VirtualAudioOutputStream(
13 const AudioParameters& params, VirtualAudioInputStream* target,
14 const AfterCloseCallback& after_close_cb)
15 : params_(params), target_input_stream_(target),
16 after_close_cb_(after_close_cb), callback_(NULL), volume_(1.0f) {
17 DCHECK(params_.IsValid());
18 DCHECK(target);
20 // VAOS can be constructed on any thread, but will DCHECK that all
21 // AudioOutputStream methods are called from the same thread.
22 thread_checker_.DetachFromThread();
25 VirtualAudioOutputStream::~VirtualAudioOutputStream() {
26 DCHECK(!callback_);
29 bool VirtualAudioOutputStream::Open() {
30 DCHECK(thread_checker_.CalledOnValidThread());
31 return true;
34 void VirtualAudioOutputStream::Start(AudioSourceCallback* callback) {
35 DCHECK(thread_checker_.CalledOnValidThread());
36 DCHECK(!callback_);
37 callback_ = callback;
38 target_input_stream_->AddOutputStream(this, params_);
41 void VirtualAudioOutputStream::Stop() {
42 DCHECK(thread_checker_.CalledOnValidThread());
43 if (callback_) {
44 target_input_stream_->RemoveOutputStream(this, params_);
45 callback_ = NULL;
49 void VirtualAudioOutputStream::Close() {
50 DCHECK(thread_checker_.CalledOnValidThread());
52 Stop();
54 // If a non-null AfterCloseCallback was provided to the constructor, invoke it
55 // here. The callback is moved to a stack-local first since |this| could be
56 // destroyed during Run().
57 if (!after_close_cb_.is_null()) {
58 const AfterCloseCallback cb = after_close_cb_;
59 after_close_cb_.Reset();
60 cb.Run(this);
64 void VirtualAudioOutputStream::SetVolume(double volume) {
65 DCHECK(thread_checker_.CalledOnValidThread());
66 volume_ = volume;
69 void VirtualAudioOutputStream::GetVolume(double* volume) {
70 DCHECK(thread_checker_.CalledOnValidThread());
71 *volume = volume_;
74 double VirtualAudioOutputStream::ProvideInput(AudioBus* audio_bus,
75 base::TimeDelta buffer_delay) {
76 // Note: This method may be invoked on any one thread, depending on the
77 // platform.
78 DCHECK(callback_);
80 DCHECK_GE(buffer_delay, base::TimeDelta());
81 const int64 upstream_delay_in_bytes =
82 params_.GetBytesPerSecond() * buffer_delay /
83 base::TimeDelta::FromSeconds(1);
84 const int frames = callback_->OnMoreData(
85 audio_bus, static_cast<uint32>(upstream_delay_in_bytes));
86 if (frames < audio_bus->frames())
87 audio_bus->ZeroFramesPartial(frames, audio_bus->frames() - frames);
89 return frames > 0 ? volume_ : 0;
92 } // namespace media