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/audio_output_dispatcher_impl.h"
10 #include "base/compiler_specific.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/time/time.h"
13 #include "media/audio/audio_output_proxy.h"
17 AudioOutputDispatcherImpl::AudioOutputDispatcherImpl(
18 AudioManager
* audio_manager
,
19 const AudioParameters
& params
,
20 const std::string
& output_device_id
,
21 const base::TimeDelta
& close_delay
)
22 : AudioOutputDispatcher(audio_manager
,
26 close_timer_(FROM_HERE
,
29 &AudioOutputDispatcherImpl::CloseAllIdleStreams
),
31 audio_manager
->CreateAudioLog(AudioLogFactory::AUDIO_OUTPUT_STREAM
)),
32 audio_stream_id_(0) {}
34 AudioOutputDispatcherImpl::~AudioOutputDispatcherImpl() {
35 DCHECK_EQ(idle_proxies_
, 0u);
36 DCHECK(proxy_to_physical_map_
.empty());
37 DCHECK(idle_streams_
.empty());
40 bool AudioOutputDispatcherImpl::OpenStream() {
41 DCHECK(task_runner_
->BelongsToCurrentThread());
43 // Ensure that there is at least one open stream.
44 if (idle_streams_
.empty() && !CreateAndOpenStream())
52 bool AudioOutputDispatcherImpl::StartStream(
53 AudioOutputStream::AudioSourceCallback
* callback
,
54 AudioOutputProxy
* stream_proxy
) {
55 DCHECK(task_runner_
->BelongsToCurrentThread());
56 DCHECK(proxy_to_physical_map_
.find(stream_proxy
) ==
57 proxy_to_physical_map_
.end());
59 if (idle_streams_
.empty() && !CreateAndOpenStream())
62 AudioOutputStream
* physical_stream
= idle_streams_
.back();
63 idle_streams_
.pop_back();
65 DCHECK_GT(idle_proxies_
, 0u);
69 stream_proxy
->GetVolume(&volume
);
70 physical_stream
->SetVolume(volume
);
71 const int stream_id
= audio_stream_ids_
[physical_stream
];
72 audio_log_
->OnSetVolume(stream_id
, volume
);
73 physical_stream
->Start(callback
);
74 audio_log_
->OnStarted(stream_id
);
75 proxy_to_physical_map_
[stream_proxy
] = physical_stream
;
81 void AudioOutputDispatcherImpl::StopStream(AudioOutputProxy
* stream_proxy
) {
82 DCHECK(task_runner_
->BelongsToCurrentThread());
84 AudioStreamMap::iterator it
= proxy_to_physical_map_
.find(stream_proxy
);
85 DCHECK(it
!= proxy_to_physical_map_
.end());
86 AudioOutputStream
* physical_stream
= it
->second
;
87 proxy_to_physical_map_
.erase(it
);
89 physical_stream
->Stop();
90 audio_log_
->OnStopped(audio_stream_ids_
[physical_stream
]);
92 idle_streams_
.push_back(physical_stream
);
97 void AudioOutputDispatcherImpl::StreamVolumeSet(AudioOutputProxy
* stream_proxy
,
99 DCHECK(task_runner_
->BelongsToCurrentThread());
100 AudioStreamMap::iterator it
= proxy_to_physical_map_
.find(stream_proxy
);
101 if (it
!= proxy_to_physical_map_
.end()) {
102 AudioOutputStream
* physical_stream
= it
->second
;
103 physical_stream
->SetVolume(volume
);
104 audio_log_
->OnSetVolume(audio_stream_ids_
[physical_stream
], volume
);
108 void AudioOutputDispatcherImpl::CloseStream(AudioOutputProxy
* stream_proxy
) {
109 DCHECK(task_runner_
->BelongsToCurrentThread());
111 DCHECK_GT(idle_proxies_
, 0u);
114 // Leave at least a single stream running until the close timer fires to help
115 // cycle time when streams are opened and closed repeatedly.
116 CloseIdleStreams(std::max(idle_proxies_
, static_cast<size_t>(1)));
117 close_timer_
.Reset();
120 void AudioOutputDispatcherImpl::Shutdown() {
121 DCHECK(task_runner_
->BelongsToCurrentThread());
123 // Close all idle streams immediately. The |close_timer_| will handle
124 // invalidating any outstanding tasks upon its destruction.
125 CloseAllIdleStreams();
127 // No AudioOutputProxy objects should hold a reference to us when we get
129 DCHECK(HasOneRef()) << "Only the AudioManager should hold a reference";
132 bool AudioOutputDispatcherImpl::HasOutputProxies() const {
133 return idle_proxies_
|| !proxy_to_physical_map_
.empty();
136 bool AudioOutputDispatcherImpl::CreateAndOpenStream() {
137 DCHECK(task_runner_
->BelongsToCurrentThread());
138 AudioOutputStream
* stream
= audio_manager_
->MakeAudioOutputStream(
139 params_
, device_id_
);
143 if (!stream
->Open()) {
148 const int stream_id
= audio_stream_id_
++;
149 audio_stream_ids_
[stream
] = stream_id
;
150 audio_log_
->OnCreated(
151 stream_id
, params_
, device_id_
);
153 idle_streams_
.push_back(stream
);
157 void AudioOutputDispatcherImpl::CloseAllIdleStreams() {
158 DCHECK(task_runner_
->BelongsToCurrentThread());
162 void AudioOutputDispatcherImpl::CloseIdleStreams(size_t keep_alive
) {
163 DCHECK(task_runner_
->BelongsToCurrentThread());
164 if (idle_streams_
.size() <= keep_alive
)
166 for (size_t i
= keep_alive
; i
< idle_streams_
.size(); ++i
) {
167 AudioOutputStream
* stream
= idle_streams_
[i
];
170 AudioStreamIDMap::iterator it
= audio_stream_ids_
.find(stream
);
171 DCHECK(it
!= audio_stream_ids_
.end());
172 audio_log_
->OnClosed(it
->second
);
173 audio_stream_ids_
.erase(it
);
175 idle_streams_
.erase(idle_streams_
.begin() + keep_alive
, idle_streams_
.end());