OLD | NEW |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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 "base/message_loop.h" | 5 #include "base/message_loop.h" |
6 #include "base/process.h" | 6 #include "base/process.h" |
7 #include "chrome/browser/renderer_host/audio_renderer_host.h" | 7 #include "chrome/browser/renderer_host/audio_renderer_host.h" |
8 | 8 |
9 AudioRendererHost::IPCAudioSource::IPCAudioSource( | 9 AudioRendererHost::IPCAudioSource::IPCAudioSource( |
10 AudioRendererHost* host, | 10 AudioRendererHost* host, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 // TODO(cpu): make sure it is safe to close() in this method. | 61 // TODO(cpu): make sure it is safe to close() in this method. |
62 stream_->Close(); | 62 stream_->Close(); |
63 } | 63 } |
64 | 64 |
65 void AudioRendererHost::IPCAudioSource::NotifyPacketReady() { | 65 void AudioRendererHost::IPCAudioSource::NotifyPacketReady() { |
66 // TODO(hclam): wake the thread waiting for buffer. | 66 // TODO(hclam): wake the thread waiting for buffer. |
67 } | 67 } |
68 | 68 |
69 AudioRendererHost::AudioRendererHost(MessageLoop* message_loop) | 69 AudioRendererHost::AudioRendererHost(MessageLoop* message_loop) |
70 : next_id_(INVALID_ID+1), | 70 : next_id_(INVALID_ID+1), |
71 message_loop_(message_loop) { | 71 io_loop_(message_loop) { |
| 72 // Make sure we perform actual initialization operations in the thread where |
| 73 // this object should live. |
| 74 io_loop_->PostTask(FROM_HERE, |
| 75 NewRunnableMethod(this, &AudioRendererHost::OnInitialized)); |
72 } | 76 } |
73 | 77 |
74 AudioRendererHost::~AudioRendererHost() { | 78 AudioRendererHost::~AudioRendererHost() { |
75 DestroyAllStreams(); | |
76 } | 79 } |
77 | 80 |
78 int AudioRendererHost::CreateStream( | 81 int AudioRendererHost::CreateStream( |
79 IPC::Message::Sender* sender, base::ProcessHandle handle, | 82 IPC::Message::Sender* sender, base::ProcessHandle handle, |
80 AudioManager::Format format, int channels, int sample_rate, | 83 AudioManager::Format format, int channels, int sample_rate, |
81 int bits_per_sample, size_t packet_size) { | 84 int bits_per_sample, size_t packet_size) { |
82 DCHECK(MessageLoop::current() == message_loop_); | 85 DCHECK(MessageLoop::current() == io_loop_); |
83 | 86 |
84 // Create the stream in the first place. | 87 // Create the stream in the first place. |
85 AudioOutputStream* stream = AudioManager::GetAudioManager()->MakeAudioStream( | 88 AudioOutputStream* stream = AudioManager::GetAudioManager()->MakeAudioStream( |
86 format, channels, sample_rate, bits_per_sample); | 89 format, channels, sample_rate, bits_per_sample); |
87 if (!stream) | 90 if (!stream) |
88 return INVALID_ID; | 91 return INVALID_ID; |
89 | 92 |
90 // Try to open the stream if we can create it. | 93 // Try to open the stream if we can create it. |
91 if (stream->Open(packet_size)) { | 94 if (stream->Open(packet_size)) { |
92 // Create the containing IPCAudioSource and save it to the map. | 95 // Create the containing IPCAudioSource and save it to the map. |
93 IPCAudioSource* source = | 96 IPCAudioSource* source = |
94 new IPCAudioSource(this, next_id_++, stream, sender, handle, | 97 new IPCAudioSource(this, next_id_++, stream, sender, handle, |
95 packet_size); | 98 packet_size); |
96 sources_.AddWithID(source, source->id()); | 99 sources_.AddWithID(source, source->id()); |
97 return source->id(); | 100 return source->id(); |
98 } | 101 } |
99 return INVALID_ID; | 102 return INVALID_ID; |
100 } | 103 } |
101 | 104 |
102 bool AudioRendererHost::Start(int stream_id) { | 105 bool AudioRendererHost::Start(int stream_id) { |
103 DCHECK(MessageLoop::current() == message_loop_); | 106 DCHECK(MessageLoop::current() == io_loop_); |
104 IPCAudioSource* source = sources_.Lookup(stream_id); | 107 IPCAudioSource* source = sources_.Lookup(stream_id); |
105 if (source) { | 108 if (source) { |
106 source->stream()->Start(source); | 109 source->stream()->Start(source); |
107 return true; | 110 return true; |
108 } | 111 } |
109 return false; | 112 return false; |
110 } | 113 } |
111 | 114 |
112 bool AudioRendererHost::Stop(int stream_id) { | 115 bool AudioRendererHost::Stop(int stream_id) { |
113 DCHECK(MessageLoop::current() == message_loop_); | 116 DCHECK(MessageLoop::current() == io_loop_); |
114 IPCAudioSource* source = sources_.Lookup(stream_id); | 117 IPCAudioSource* source = sources_.Lookup(stream_id); |
115 if (source) { | 118 if (source) { |
116 source->stream()->Stop(); | 119 source->stream()->Stop(); |
117 return true; | 120 return true; |
118 } | 121 } |
119 return false; | 122 return false; |
120 } | 123 } |
121 | 124 |
122 bool AudioRendererHost::Close(int stream_id) { | 125 bool AudioRendererHost::Close(int stream_id) { |
123 DCHECK(MessageLoop::current() == message_loop_); | 126 DCHECK(MessageLoop::current() == io_loop_); |
124 IPCAudioSource* source = sources_.Lookup(stream_id); | 127 IPCAudioSource* source = sources_.Lookup(stream_id); |
125 if (source) { | 128 if (source) { |
126 source->stream()->Close(); | 129 source->stream()->Close(); |
127 return true; | 130 return true; |
128 } | 131 } |
129 return false; | 132 return false; |
130 } | 133 } |
131 | 134 |
132 bool AudioRendererHost::SetVolume( | 135 bool AudioRendererHost::SetVolume( |
133 int stream_id, double left_channel, double right_channel) { | 136 int stream_id, double left_channel, double right_channel) { |
134 DCHECK(MessageLoop::current() == message_loop_); | 137 DCHECK(MessageLoop::current() == io_loop_); |
135 IPCAudioSource* source = sources_.Lookup(stream_id); | 138 IPCAudioSource* source = sources_.Lookup(stream_id); |
136 if (source) { | 139 if (source) { |
137 source->stream()->SetVolume(left_channel, right_channel); | 140 source->stream()->SetVolume(left_channel, right_channel); |
138 } | 141 } |
139 return false; | 142 return false; |
140 } | 143 } |
141 | 144 |
142 bool AudioRendererHost::GetVolume( | 145 bool AudioRendererHost::GetVolume( |
143 int stream_id, double* left_channel, double* right_channel) { | 146 int stream_id, double* left_channel, double* right_channel) { |
144 DCHECK(MessageLoop::current() == message_loop_); | 147 DCHECK(MessageLoop::current() == io_loop_); |
145 IPCAudioSource* source = sources_.Lookup(stream_id); | 148 IPCAudioSource* source = sources_.Lookup(stream_id); |
146 if (source) { | 149 if (source) { |
147 source->stream()->GetVolume(left_channel, right_channel); | 150 source->stream()->GetVolume(left_channel, right_channel); |
148 return true; | 151 return true; |
149 } | 152 } |
150 return false; | 153 return false; |
151 } | 154 } |
152 | 155 |
153 void AudioRendererHost::NotifyPacketReady(int stream_id) { | 156 void AudioRendererHost::NotifyPacketReady(int stream_id) { |
154 DCHECK(MessageLoop::current() == message_loop_); | 157 DCHECK(MessageLoop::current() == io_loop_); |
155 IPCAudioSource* source = sources_.Lookup(stream_id); | 158 IPCAudioSource* source = sources_.Lookup(stream_id); |
156 if (source) { | 159 if (source) { |
157 source->NotifyPacketReady(); | 160 source->NotifyPacketReady(); |
158 } | 161 } |
159 } | 162 } |
160 | 163 |
161 void AudioRendererHost::DestroyAllStreams() { | 164 void AudioRendererHost::DestroyAllStreams() { |
162 DCHECK(MessageLoop::current() == message_loop_); | 165 DCHECK(MessageLoop::current() == io_loop_); |
163 // TODO(hclam): iterate on the map, close and delete every stream, and clear | 166 // TODO(hclam): iterate on the map, close and delete every stream, and clear |
164 // the map. | 167 // the map. |
165 } | 168 } |
166 | 169 |
167 void AudioRendererHost::DestroySource(int stream_id) { | 170 void AudioRendererHost::DestroySource(int stream_id) { |
168 DCHECK(MessageLoop::current() == message_loop_); | 171 DCHECK(MessageLoop::current() == io_loop_); |
169 IPCAudioSource* source = sources_.Lookup(stream_id); | 172 IPCAudioSource* source = sources_.Lookup(stream_id); |
170 if (source) { | 173 if (source) { |
171 sources_.Remove(stream_id); | 174 sources_.Remove(stream_id); |
172 delete source; | 175 delete source; |
173 } | 176 } |
174 } | 177 } |
| 178 |
| 179 void AudioRendererHost::Destroy() { |
| 180 // Post a message to the thread where this object should live and do the |
| 181 // actual operations there. |
| 182 io_loop_->PostTask( |
| 183 FROM_HERE, NewRunnableMethod(this, &AudioRendererHost::OnDestroyed)); |
| 184 } |
| 185 |
| 186 void AudioRendererHost::OnInitialized() { |
| 187 DCHECK(MessageLoop::current() == io_loop_); |
| 188 |
| 189 // Increase the ref count of this object so it is active until we do |
| 190 // Release(). |
| 191 AddRef(); |
| 192 |
| 193 // Also create the AudioManager singleton in this thread. |
| 194 // TODO(hclam): figure out a better location to initialize the AudioManager |
| 195 // singleton. |
| 196 AudioManager::GetAudioManager(); |
| 197 } |
| 198 |
| 199 void AudioRendererHost::OnDestroyed() { |
| 200 DCHECK(MessageLoop::current() == io_loop_); |
| 201 |
| 202 // Destroy audio streams only in the thread it should happen. |
| 203 // TODO(hclam): make sure we don't call IPC::Message::Sender inside |
| 204 // IPCAudioSource because it is most likely be destroyed. |
| 205 DestroyAllStreams(); |
| 206 |
| 207 // Decrease the reference to this object, which may lead to self-destruction. |
| 208 Release(); |
| 209 } |
OLD | NEW |