OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/time.h" | |
9 #include "media/audio/audio_io.h" | 10 #include "media/audio/audio_io.h" |
10 | 11 |
11 AudioOutputDispatcher::AudioOutputDispatcher( | 12 AudioOutputDispatcher::AudioOutputDispatcher( |
12 AudioManager* audio_manager, const AudioParameters& params, | 13 AudioManager* audio_manager, const AudioParameters& params, |
13 int close_delay_ms) | 14 int close_delay_ms) |
14 : audio_manager_(audio_manager), | 15 : audio_manager_(audio_manager), |
15 message_loop_(audio_manager->GetMessageLoop()), | 16 message_loop_(audio_manager->GetMessageLoop()), |
16 params_(params), | 17 params_(params), |
18 pause_delay_milliseconds( 2 * params.samples_per_packet * | |
Sergey Ulanov
2011/04/12 05:09:25
nit: remove space before 2
| |
19 base::Time::kMillisecondsPerSecond / params.sample_rate), | |
17 paused_proxies_(0), | 20 paused_proxies_(0), |
18 ALLOW_THIS_IN_INITIALIZER_LIST(close_timer_( | 21 ALLOW_THIS_IN_INITIALIZER_LIST(close_timer_( |
19 base::TimeDelta::FromMilliseconds(close_delay_ms), | 22 base::TimeDelta::FromMilliseconds(close_delay_ms), |
20 this, &AudioOutputDispatcher::ClosePendingStreams)) { | 23 this, &AudioOutputDispatcher::ClosePendingStreams)) { |
21 } | 24 } |
22 | 25 |
23 AudioOutputDispatcher::~AudioOutputDispatcher() { | 26 AudioOutputDispatcher::~AudioOutputDispatcher() { |
Sergey Ulanov
2011/04/12 05:09:25
Cleanup pausing_streams here.
| |
24 } | 27 } |
25 | 28 |
26 bool AudioOutputDispatcher::StreamOpened() { | 29 bool AudioOutputDispatcher::StreamOpened() { |
27 DCHECK_EQ(MessageLoop::current(), message_loop_); | 30 DCHECK_EQ(MessageLoop::current(), message_loop_); |
28 paused_proxies_++; | 31 paused_proxies_++; |
29 | 32 |
30 // Ensure that there is at least one open stream. | 33 // Ensure that there is at least one open stream. |
31 if (streams_.empty() && !CreateAndOpenStream()) { | 34 if (idle_streams_.empty() && !CreateAndOpenStream()) { |
32 return false; | 35 return false; |
33 } | 36 } |
34 | 37 |
35 close_timer_.Reset(); | 38 close_timer_.Reset(); |
36 | 39 |
37 return true; | 40 return true; |
38 } | 41 } |
39 | 42 |
40 AudioOutputStream* AudioOutputDispatcher::StreamStarted() { | 43 AudioOutputStream* AudioOutputDispatcher::StreamStarted() { |
41 DCHECK_EQ(MessageLoop::current(), message_loop_); | 44 DCHECK_EQ(MessageLoop::current(), message_loop_); |
42 | 45 |
43 if (streams_.empty() && !CreateAndOpenStream()) { | 46 if (idle_streams_.empty() && !CreateAndOpenStream()) { |
44 return NULL; | 47 return NULL; |
45 } | 48 } |
46 | 49 |
47 AudioOutputStream* stream = streams_.back(); | 50 AudioOutputStream* stream = idle_streams_.back(); |
48 streams_.pop_back(); | 51 idle_streams_.pop_back(); |
49 | 52 |
50 DCHECK_GT(paused_proxies_, 0u); | 53 DCHECK_GT(paused_proxies_, 0u); |
51 paused_proxies_--; | 54 paused_proxies_--; |
52 | 55 |
53 close_timer_.Reset(); | 56 close_timer_.Reset(); |
54 | 57 |
55 // Schedule task to allocate streams for other proxies if we need to. | 58 // Schedule task to allocate streams for other proxies if we need to. |
56 message_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 59 message_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
57 this, &AudioOutputDispatcher::OpenTask)); | 60 this, &AudioOutputDispatcher::OpenTask)); |
58 | 61 |
59 return stream; | 62 return stream; |
60 } | 63 } |
61 | 64 |
62 void AudioOutputDispatcher::StreamStopped(AudioOutputStream* stream) { | 65 void AudioOutputDispatcher::StreamStopped(AudioOutputStream* stream) { |
63 DCHECK_EQ(MessageLoop::current(), message_loop_); | 66 DCHECK_EQ(MessageLoop::current(), message_loop_); |
67 | |
64 paused_proxies_++; | 68 paused_proxies_++; |
65 streams_.push_back(stream); | 69 |
70 pausing_streams_.push_front(stream); | |
71 | |
72 // Don't recycle stream until two buffers worth of time has elapsed | |
73 message_loop_->PostDelayedTask( | |
74 FROM_HERE, | |
75 NewRunnableMethod(this, &AudioOutputDispatcher::StopStreamTask), | |
76 pause_delay_milliseconds); | |
77 } | |
78 | |
79 void AudioOutputDispatcher::StopStreamTask() { | |
80 if (pausing_streams_.empty()) | |
81 return; | |
82 AudioOutputStream* stream = pausing_streams_.back(); | |
83 pausing_streams_.pop_back(); | |
84 idle_streams_.push_back(stream); | |
66 close_timer_.Reset(); | 85 close_timer_.Reset(); |
67 } | 86 } |
68 | 87 |
69 void AudioOutputDispatcher::StreamClosed() { | 88 void AudioOutputDispatcher::StreamClosed() { |
70 DCHECK_EQ(MessageLoop::current(), message_loop_); | 89 DCHECK_EQ(MessageLoop::current(), message_loop_); |
71 | 90 |
72 DCHECK_GT(paused_proxies_, 0u); | 91 DCHECK_GT(paused_proxies_, 0u); |
73 paused_proxies_--; | 92 paused_proxies_--; |
74 | 93 |
75 while (streams_.size() > paused_proxies_) { | 94 while (idle_streams_.size() > paused_proxies_) { |
76 streams_.back()->Close(); | 95 idle_streams_.back()->Close(); |
77 streams_.pop_back(); | 96 idle_streams_.pop_back(); |
78 } | 97 } |
79 } | 98 } |
80 | 99 |
81 MessageLoop* AudioOutputDispatcher::message_loop() { | 100 MessageLoop* AudioOutputDispatcher::message_loop() { |
82 return message_loop_; | 101 return message_loop_; |
83 } | 102 } |
84 | 103 |
85 bool AudioOutputDispatcher::CreateAndOpenStream() { | 104 bool AudioOutputDispatcher::CreateAndOpenStream() { |
86 AudioOutputStream* stream = | 105 AudioOutputStream* stream = |
87 audio_manager_->MakeAudioOutputStream(params_); | 106 audio_manager_->MakeAudioOutputStream(params_); |
88 if (!stream) { | 107 if (!stream) { |
89 return false; | 108 return false; |
90 } | 109 } |
91 if (!stream->Open()) { | 110 if (!stream->Open()) { |
92 stream->Close(); | 111 stream->Close(); |
93 return false; | 112 return false; |
94 } | 113 } |
95 streams_.push_back(stream); | 114 idle_streams_.push_back(stream); |
96 return true; | 115 return true; |
97 } | 116 } |
98 | 117 |
99 void AudioOutputDispatcher::OpenTask() { | 118 void AudioOutputDispatcher::OpenTask() { |
100 // Make sure that we have at least one stream allocated if there | 119 // Make sure that we have at least one stream allocated if there |
101 // are paused streams. | 120 // are paused streams. |
102 if (paused_proxies_ > 0 && streams_.empty()) { | 121 if (paused_proxies_ > 0 && idle_streams_.empty()) { |
103 CreateAndOpenStream(); | 122 CreateAndOpenStream(); |
104 } | 123 } |
105 | 124 |
106 close_timer_.Reset(); | 125 close_timer_.Reset(); |
107 } | 126 } |
108 | 127 |
109 // This method is called by |close_timer_|. | 128 // This method is called by |close_timer_|. |
110 void AudioOutputDispatcher::ClosePendingStreams() { | 129 void AudioOutputDispatcher::ClosePendingStreams() { |
111 while (!streams_.empty()) { | 130 while (!pausing_streams_.empty()) { |
112 streams_.back()->Close(); | 131 pausing_streams_.back()->Close(); |
113 streams_.pop_back(); | 132 pausing_streams_.pop_back(); |
Sergey Ulanov
2011/04/12 05:09:25
If you close |pausing_streams_| here you may be cl
| |
133 } | |
134 while (!idle_streams_.empty()) { | |
135 idle_streams_.back()->Close(); | |
136 idle_streams_.pop_back(); | |
114 } | 137 } |
115 } | 138 } |
OLD | NEW |