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/audio_output_dispatcher.h" | 5 #include "media/audio/audio_output_dispatcher.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
11 #include "media/audio/audio_io.h" | 11 #include "media/audio/audio_io.h" |
12 | 12 |
13 AudioOutputDispatcher::AudioOutputDispatcher( | 13 AudioOutputDispatcher::AudioOutputDispatcher( |
14 AudioManager* audio_manager, const AudioParameters& params, | 14 AudioManager* audio_manager, const AudioParameters& params, |
15 base::TimeDelta close_delay) | 15 base::TimeDelta close_delay) |
16 : audio_manager_(audio_manager), | 16 : audio_manager_(audio_manager), |
17 message_loop_(audio_manager->GetMessageLoop()), | 17 message_loop_(MessageLoop::current()), |
18 params_(params), | 18 params_(params), |
19 pause_delay_(base::TimeDelta::FromMilliseconds( | 19 pause_delay_(base::TimeDelta::FromMilliseconds( |
20 2 * params.samples_per_packet * | 20 2 * params.samples_per_packet * |
21 base::Time::kMillisecondsPerSecond / params.sample_rate)), | 21 base::Time::kMillisecondsPerSecond / params.sample_rate)), |
22 paused_proxies_(0), | 22 paused_proxies_(0), |
23 ALLOW_THIS_IN_INITIALIZER_LIST(weak_this_(this)), | 23 ALLOW_THIS_IN_INITIALIZER_LIST(weak_this_(this)), |
24 close_timer_(FROM_HERE, | 24 close_timer_(FROM_HERE, |
25 close_delay, | 25 close_delay, |
26 weak_this_.GetWeakPtr(), | 26 weak_this_.GetWeakPtr(), |
27 &AudioOutputDispatcher::ClosePendingStreams) { | 27 &AudioOutputDispatcher::ClosePendingStreams) { |
28 DCHECK_EQ(MessageLoop::current(), message_loop_); | 28 // We expect to be instantiated on the audio thread. Otherwise the |
| 29 // message_loop_ member will point to the wrong message loop! |
| 30 DCHECK(audio_manager->GetMessageLoop()->BelongsToCurrentThread()); |
29 } | 31 } |
30 | 32 |
31 AudioOutputDispatcher::~AudioOutputDispatcher() { | 33 AudioOutputDispatcher::~AudioOutputDispatcher() { |
32 DCHECK_EQ(MessageLoop::current(), message_loop_); | 34 DCHECK_EQ(MessageLoop::current(), message_loop_); |
33 } | 35 } |
34 | 36 |
35 bool AudioOutputDispatcher::StreamOpened() { | 37 bool AudioOutputDispatcher::StreamOpened() { |
36 DCHECK_EQ(MessageLoop::current(), message_loop_); | 38 DCHECK_EQ(MessageLoop::current(), message_loop_); |
37 paused_proxies_++; | 39 paused_proxies_++; |
38 | 40 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 for (; it != idle_streams_.end(); ++it) | 128 for (; it != idle_streams_.end(); ++it) |
127 (*it)->Close(); | 129 (*it)->Close(); |
128 idle_streams_.clear(); | 130 idle_streams_.clear(); |
129 | 131 |
130 it = pausing_streams_.begin(); | 132 it = pausing_streams_.begin(); |
131 for (; it != pausing_streams_.end(); ++it) | 133 for (; it != pausing_streams_.end(); ++it) |
132 (*it)->Close(); | 134 (*it)->Close(); |
133 pausing_streams_.clear(); | 135 pausing_streams_.clear(); |
134 } | 136 } |
135 | 137 |
136 MessageLoop* AudioOutputDispatcher::message_loop() { | |
137 return message_loop_; | |
138 } | |
139 | |
140 bool AudioOutputDispatcher::CreateAndOpenStream() { | 138 bool AudioOutputDispatcher::CreateAndOpenStream() { |
141 AudioOutputStream* stream = audio_manager_->MakeAudioOutputStream(params_); | 139 AudioOutputStream* stream = audio_manager_->MakeAudioOutputStream(params_); |
142 if (!stream) | 140 if (!stream) |
143 return false; | 141 return false; |
144 | 142 |
145 if (!stream->Open()) { | 143 if (!stream->Open()) { |
146 stream->Close(); | 144 stream->Close(); |
147 return false; | 145 return false; |
148 } | 146 } |
149 idle_streams_.push_back(stream); | 147 idle_streams_.push_back(stream); |
(...skipping 13 matching lines...) Expand all Loading... |
163 | 161 |
164 // This method is called by |close_timer_|. | 162 // This method is called by |close_timer_|. |
165 void AudioOutputDispatcher::ClosePendingStreams() { | 163 void AudioOutputDispatcher::ClosePendingStreams() { |
166 DCHECK_EQ(MessageLoop::current(), message_loop_); | 164 DCHECK_EQ(MessageLoop::current(), message_loop_); |
167 | 165 |
168 while (!idle_streams_.empty()) { | 166 while (!idle_streams_.empty()) { |
169 idle_streams_.back()->Close(); | 167 idle_streams_.back()->Close(); |
170 idle_streams_.pop_back(); | 168 idle_streams_.pop_back(); |
171 } | 169 } |
172 } | 170 } |
OLD | NEW |