OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2010 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. | |
4 | |
5 #include "media/audio/audio_output_dispatcher.h" | |
6 | |
7 #include "base/compiler_specific.h" | |
8 #include "base/message_loop.h" | |
9 #include "media/audio/audio_io.h" | |
10 | |
11 AudioOutputDispatcher::AudioOutputDispatcher( | |
12 AudioManager* audio_manager, const AudioParameters& params, | |
13 int close_delay_ms) | |
14 : audio_manager_(audio_manager), | |
15 message_loop_(audio_manager->GetMessageLoop()), | |
16 params_(params), | |
17 paused_proxies_(0), | |
18 ALLOW_THIS_IN_INITIALIZER_LIST(close_timer_( | |
19 base::TimeDelta::FromMilliseconds(close_delay_ms), | |
20 this, &AudioOutputDispatcher::ClosePendingStreams)) { | |
21 } | |
22 | |
23 AudioOutputDispatcher::~AudioOutputDispatcher() { | |
24 } | |
25 | |
26 bool AudioOutputDispatcher::StreamOpened() { | |
27 DCHECK_EQ(MessageLoop::current(), message_loop_); | |
28 paused_proxies_++; | |
29 | |
30 // Ensure that there is at least one open stream. | |
31 if (streams_.size() == 0) { | |
scherkus (not reviewing)
2010/11/24 02:01:59
combine these two conditions
Sergey Ulanov
2010/11/24 03:49:54
Done.
| |
32 if (!CreateAndOpenStream()) { | |
33 return false; | |
34 } | |
35 } | |
36 | |
37 close_timer_.Reset(); | |
38 | |
39 return true; | |
40 } | |
41 | |
42 AudioOutputStream* AudioOutputDispatcher::StreamStarted() { | |
43 DCHECK_EQ(MessageLoop::current(), message_loop_); | |
44 | |
45 if (streams_.size() == 0) { | |
scherkus (not reviewing)
2010/11/24 02:01:59
combine these two conditions
Sergey Ulanov
2010/11/24 03:49:54
Done.
| |
46 if (!CreateAndOpenStream()) { | |
47 return NULL; | |
48 } | |
49 } | |
50 | |
51 AudioOutputStream* stream = streams_.back(); | |
52 streams_.pop_back(); | |
53 | |
54 paused_proxies_--; | |
55 DCHECK_GE(paused_proxies_, 0); | |
56 | |
57 close_timer_.Reset(); | |
58 | |
59 // Schedule task to allocate streams for other proxies if we need to. | |
60 message_loop_->PostTask(FROM_HERE, NewRunnableMethod( | |
61 this, &AudioOutputDispatcher::OpenTask)); | |
62 | |
63 return stream; | |
64 } | |
65 | |
66 void AudioOutputDispatcher::StreamStopped(AudioOutputStream* stream) { | |
67 DCHECK_EQ(MessageLoop::current(), message_loop_); | |
68 paused_proxies_++; | |
69 streams_.push_back(stream); | |
70 close_timer_.Reset(); | |
71 } | |
72 | |
73 void AudioOutputDispatcher::StreamClosed() { | |
74 DCHECK_EQ(MessageLoop::current(), message_loop_); | |
75 | |
76 paused_proxies_--; | |
77 DCHECK_GE(paused_proxies_, 0); | |
78 | |
79 while (static_cast<int>(streams_.size()) > paused_proxies_) { | |
scherkus (not reviewing)
2010/11/24 02:01:59
hmm maybe paused_proxies_ should be size_t ?
Sergey Ulanov
2010/11/24 03:49:54
Done.
| |
80 streams_.back()->Close(); | |
81 streams_.pop_back(); | |
82 } | |
83 } | |
84 | |
85 MessageLoop* AudioOutputDispatcher::message_loop() { | |
86 return message_loop_; | |
87 } | |
88 | |
89 bool AudioOutputDispatcher::CreateAndOpenStream() { | |
90 AudioOutputStream* stream = | |
91 audio_manager_->MakeAudioOutputStream(params_); | |
92 if (!stream) { | |
93 return false; | |
94 } | |
95 if (!stream->Open()) { | |
96 stream->Close(); | |
97 return false; | |
98 } | |
99 streams_.push_back(stream); | |
100 return true; | |
101 } | |
102 | |
103 void AudioOutputDispatcher::OpenTask() { | |
104 // Make sure that we have at least one stream allocated if there | |
105 // are paused streams. | |
106 if (paused_proxies_ > 0 && streams_.size() == 0) { | |
107 CreateAndOpenStream(); | |
108 } | |
109 | |
110 close_timer_.Reset(); | |
111 } | |
112 | |
113 // This method is called by |close_timer_|. | |
114 void AudioOutputDispatcher::ClosePendingStreams() { | |
115 while (static_cast<int>(streams_.size()) > 0) { | |
scherkus (not reviewing)
2010/11/24 02:01:59
nit: instead of cast etc... what about
while (!str
Sergey Ulanov
2010/11/24 03:49:54
Done.
| |
116 streams_.back()->Close(); | |
117 streams_.pop_back(); | |
118 } | |
119 } | |
OLD | NEW |