1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/audio/audio_output_dispatcher.h"
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/message_loop.h"
#include "base/time.h"
#include "media/audio/audio_io.h"
AudioOutputDispatcher::AudioOutputDispatcher(
AudioManager* audio_manager, const AudioParameters& params,
base::TimeDelta close_delay)
: audio_manager_(audio_manager),
message_loop_(MessageLoop::current()),
params_(params),
pause_delay_(base::TimeDelta::FromMilliseconds(
2 * params.samples_per_packet *
base::Time::kMillisecondsPerSecond / params.sample_rate)),
paused_proxies_(0),
ALLOW_THIS_IN_INITIALIZER_LIST(weak_this_(this)),
close_timer_(FROM_HERE,
close_delay,
weak_this_.GetWeakPtr(),
&AudioOutputDispatcher::ClosePendingStreams) {
// We expect to be instantiated on the audio thread. Otherwise the
// message_loop_ member will point to the wrong message loop!
DCHECK(audio_manager->GetMessageLoop()->BelongsToCurrentThread());
}
AudioOutputDispatcher::~AudioOutputDispatcher() {
DCHECK_EQ(MessageLoop::current(), message_loop_);
}
bool AudioOutputDispatcher::StreamOpened() {
DCHECK_EQ(MessageLoop::current(), message_loop_);
paused_proxies_++;
// Ensure that there is at least one open stream.
if (idle_streams_.empty() && !CreateAndOpenStream()) {
return false;
}
close_timer_.Reset();
return true;
}
AudioOutputStream* AudioOutputDispatcher::StreamStarted() {
DCHECK_EQ(MessageLoop::current(), message_loop_);
if (idle_streams_.empty() && !CreateAndOpenStream()) {
return NULL;
}
AudioOutputStream* stream = idle_streams_.back();
idle_streams_.pop_back();
DCHECK_GT(paused_proxies_, 0u);
paused_proxies_--;
close_timer_.Reset();
// Schedule task to allocate streams for other proxies if we need to.
message_loop_->PostTask(FROM_HERE, base::Bind(
&AudioOutputDispatcher::OpenTask, weak_this_.GetWeakPtr()));
return stream;
}
void AudioOutputDispatcher::StreamStopped(AudioOutputStream* stream) {
DCHECK_EQ(MessageLoop::current(), message_loop_);
paused_proxies_++;
pausing_streams_.push_front(stream);
// Don't recycle stream until two buffers worth of time has elapsed.
message_loop_->PostDelayedTask(
FROM_HERE,
base::Bind(&AudioOutputDispatcher::StopStreamTask,
weak_this_.GetWeakPtr()),
pause_delay_);
}
void AudioOutputDispatcher::StopStreamTask() {
DCHECK_EQ(MessageLoop::current(), message_loop_);
if (pausing_streams_.empty())
return;
AudioOutputStream* stream = pausing_streams_.back();
pausing_streams_.pop_back();
idle_streams_.push_back(stream);
close_timer_.Reset();
}
void AudioOutputDispatcher::StreamClosed() {
DCHECK_EQ(MessageLoop::current(), message_loop_);
while (!pausing_streams_.empty()) {
idle_streams_.push_back(pausing_streams_.back());
pausing_streams_.pop_back();
}
DCHECK_GT(paused_proxies_, 0u);
paused_proxies_--;
while (idle_streams_.size() > paused_proxies_) {
idle_streams_.back()->Close();
idle_streams_.pop_back();
}
}
void AudioOutputDispatcher::Shutdown() {
DCHECK_EQ(MessageLoop::current(), message_loop_);
// Cancel any pending tasks to close paused streams or create new ones.
weak_this_.InvalidateWeakPtrs();
// No AudioOutputProxy objects should hold a reference to us when we get
// to this stage.
DCHECK(HasOneRef()) << "Only the AudioManager should hold a reference";
AudioOutputStreamList::iterator it = idle_streams_.begin();
for (; it != idle_streams_.end(); ++it)
(*it)->Close();
idle_streams_.clear();
it = pausing_streams_.begin();
for (; it != pausing_streams_.end(); ++it)
(*it)->Close();
pausing_streams_.clear();
}
bool AudioOutputDispatcher::CreateAndOpenStream() {
AudioOutputStream* stream = audio_manager_->MakeAudioOutputStream(params_);
if (!stream)
return false;
if (!stream->Open()) {
stream->Close();
return false;
}
idle_streams_.push_back(stream);
return true;
}
void AudioOutputDispatcher::OpenTask() {
// Make sure that we have at least one stream allocated if there
// are paused streams.
if (paused_proxies_ > 0 && idle_streams_.empty() &&
pausing_streams_.empty()) {
CreateAndOpenStream();
}
close_timer_.Reset();
}
// This method is called by |close_timer_|.
void AudioOutputDispatcher::ClosePendingStreams() {
DCHECK_EQ(MessageLoop::current(), message_loop_);
while (!idle_streams_.empty()) {
idle_streams_.back()->Close();
idle_streams_.pop_back();
}
}
|