Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "content/browser/renderer_host/media/audio_renderer_host.h" | 5 #include "content/browser/renderer_host/media/audio_renderer_host.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/process.h" | 9 #include "base/process.h" |
| 10 #include "base/shared_memory.h" | 10 #include "base/shared_memory.h" |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 231 | 231 |
| 232 if (!entry->controller) { | 232 if (!entry->controller) { |
| 233 SendErrorMessage(stream_id); | 233 SendErrorMessage(stream_id); |
| 234 return; | 234 return; |
| 235 } | 235 } |
| 236 | 236 |
| 237 // If we have created the controller successfully, create an entry and add it | 237 // If we have created the controller successfully, create an entry and add it |
| 238 // to the map. | 238 // to the map. |
| 239 entry->stream_id = stream_id; | 239 entry->stream_id = stream_id; |
| 240 audio_entries_.insert(std::make_pair(stream_id, entry.release())); | 240 audio_entries_.insert(std::make_pair(stream_id, entry.release())); |
| 241 GetMediaObserver()->OnSetAudioStreamStatus(this, stream_id, "created"); | 241 OnSetAudioStreamStatus(stream_id, "created"); |
| 242 } | 242 } |
| 243 | 243 |
| 244 void AudioRendererHost::OnPlayStream(int stream_id) { | 244 void AudioRendererHost::OnPlayStream(int stream_id) { |
| 245 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 245 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 246 | 246 |
| 247 AudioEntry* entry = LookupById(stream_id); | 247 AudioEntry* entry = LookupById(stream_id); |
| 248 if (!entry) { | 248 if (!entry) { |
| 249 SendErrorMessage(stream_id); | 249 SendErrorMessage(stream_id); |
| 250 return; | 250 return; |
| 251 } | 251 } |
| 252 | 252 |
| 253 entry->controller->Play(); | 253 entry->controller->Play(); |
| 254 GetMediaObserver()->OnSetAudioStreamPlaying(this, stream_id, true); | 254 OnSetAudioStreamPlaying(stream_id, true); |
| 255 } | 255 } |
| 256 | 256 |
| 257 void AudioRendererHost::OnPauseStream(int stream_id) { | 257 void AudioRendererHost::OnPauseStream(int stream_id) { |
| 258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 259 | 259 |
| 260 AudioEntry* entry = LookupById(stream_id); | 260 AudioEntry* entry = LookupById(stream_id); |
| 261 if (!entry) { | 261 if (!entry) { |
| 262 SendErrorMessage(stream_id); | 262 SendErrorMessage(stream_id); |
| 263 return; | 263 return; |
| 264 } | 264 } |
| 265 | 265 |
| 266 entry->controller->Pause(); | 266 entry->controller->Pause(); |
| 267 GetMediaObserver()->OnSetAudioStreamPlaying(this, stream_id, false); | 267 OnSetAudioStreamPlaying(stream_id, false); |
| 268 } | 268 } |
| 269 | 269 |
| 270 void AudioRendererHost::OnFlushStream(int stream_id) { | 270 void AudioRendererHost::OnFlushStream(int stream_id) { |
| 271 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 271 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 272 | 272 |
| 273 AudioEntry* entry = LookupById(stream_id); | 273 AudioEntry* entry = LookupById(stream_id); |
| 274 if (!entry) { | 274 if (!entry) { |
| 275 SendErrorMessage(stream_id); | 275 SendErrorMessage(stream_id); |
| 276 return; | 276 return; |
| 277 } | 277 } |
| 278 | 278 |
| 279 entry->controller->Flush(); | 279 entry->controller->Flush(); |
| 280 GetMediaObserver()->OnSetAudioStreamStatus(this, stream_id, "flushed"); | 280 OnSetAudioStreamStatus(stream_id, "flushed"); |
| 281 } | 281 } |
| 282 | 282 |
| 283 void AudioRendererHost::OnCloseStream(int stream_id) { | 283 void AudioRendererHost::OnCloseStream(int stream_id) { |
| 284 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 284 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 285 | 285 |
| 286 GetMediaObserver()->OnSetAudioStreamStatus(this, stream_id, "closed"); | 286 OnSetAudioStreamStatus(stream_id, "closed"); |
| 287 | 287 |
| 288 AudioEntry* entry = LookupById(stream_id); | 288 AudioEntry* entry = LookupById(stream_id); |
| 289 | 289 |
| 290 if (entry) | 290 if (entry) |
| 291 CloseAndDeleteStream(entry); | 291 CloseAndDeleteStream(entry); |
| 292 } | 292 } |
| 293 | 293 |
| 294 void AudioRendererHost::OnSetVolume(int stream_id, double volume) { | 294 void AudioRendererHost::OnSetVolume(int stream_id, double volume) { |
| 295 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 295 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 296 | 296 |
| 297 AudioEntry* entry = LookupById(stream_id); | 297 AudioEntry* entry = LookupById(stream_id); |
| 298 if (!entry) { | 298 if (!entry) { |
| 299 SendErrorMessage(stream_id); | 299 SendErrorMessage(stream_id); |
| 300 return; | 300 return; |
| 301 } | 301 } |
| 302 | 302 |
| 303 // Make sure the volume is valid. | 303 // Make sure the volume is valid. |
| 304 if (volume < 0 || volume > 1.0) | 304 if (volume < 0 || volume > 1.0) |
| 305 return; | 305 return; |
| 306 entry->controller->SetVolume(volume); | 306 entry->controller->SetVolume(volume); |
| 307 GetMediaObserver()->OnSetAudioStreamVolume(this, stream_id, volume); | 307 OnSetAudioStreamVolume(stream_id, volume); |
| 308 } | 308 } |
| 309 | 309 |
| 310 void AudioRendererHost::SendErrorMessage(int32 stream_id) { | 310 void AudioRendererHost::SendErrorMessage(int32 stream_id) { |
| 311 Send(new AudioMsg_NotifyStreamStateChanged(stream_id, kAudioStreamError)); | 311 Send(new AudioMsg_NotifyStreamStateChanged(stream_id, kAudioStreamError)); |
| 312 } | 312 } |
| 313 | 313 |
| 314 void AudioRendererHost::DeleteEntries() { | 314 void AudioRendererHost::DeleteEntries() { |
| 315 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 315 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 316 | 316 |
| 317 for (AudioEntryMap::iterator i = audio_entries_.begin(); | 317 for (AudioEntryMap::iterator i = audio_entries_.begin(); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 341 void AudioRendererHost::DeleteEntry(AudioEntry* entry) { | 341 void AudioRendererHost::DeleteEntry(AudioEntry* entry) { |
| 342 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 342 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 343 | 343 |
| 344 // Delete the entry when this method goes out of scope. | 344 // Delete the entry when this method goes out of scope. |
| 345 scoped_ptr<AudioEntry> entry_deleter(entry); | 345 scoped_ptr<AudioEntry> entry_deleter(entry); |
| 346 | 346 |
| 347 // Erase the entry identified by |stream_id| from the map. | 347 // Erase the entry identified by |stream_id| from the map. |
| 348 audio_entries_.erase(entry->stream_id); | 348 audio_entries_.erase(entry->stream_id); |
| 349 | 349 |
| 350 // Notify the media observer. | 350 // Notify the media observer. |
| 351 GetMediaObserver()->OnDeleteAudioStream(this, entry->stream_id); | 351 OnDeleteAudioStream(entry->stream_id); |
| 352 } | 352 } |
| 353 | 353 |
| 354 void AudioRendererHost::DeleteEntryOnError(AudioEntry* entry) { | 354 void AudioRendererHost::DeleteEntryOnError(AudioEntry* entry) { |
| 355 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 355 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 356 | 356 |
| 357 // Sends the error message first before we close the stream because | 357 // Sends the error message first before we close the stream because |
| 358 // |entry| is destroyed in DeleteEntry(). | 358 // |entry| is destroyed in DeleteEntry(). |
| 359 SendErrorMessage(entry->stream_id); | 359 SendErrorMessage(entry->stream_id); |
| 360 | 360 |
| 361 GetMediaObserver()->OnSetAudioStreamStatus(this, entry->stream_id, "error"); | 361 OnSetAudioStreamStatus(entry->stream_id, "error"); |
| 362 CloseAndDeleteStream(entry); | 362 CloseAndDeleteStream(entry); |
| 363 } | 363 } |
| 364 | 364 |
| 365 AudioRendererHost::AudioEntry* AudioRendererHost::LookupById(int stream_id) { | 365 AudioRendererHost::AudioEntry* AudioRendererHost::LookupById(int stream_id) { |
| 366 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 366 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 367 | 367 |
| 368 AudioEntryMap::iterator i = audio_entries_.find(stream_id); | 368 AudioEntryMap::iterator i = audio_entries_.find(stream_id); |
| 369 if (i != audio_entries_.end() && !i->second->pending_close) | 369 if (i != audio_entries_.end() && !i->second->pending_close) |
| 370 return i->second; | 370 return i->second; |
| 371 return NULL; | 371 return NULL; |
| 372 } | 372 } |
| 373 | 373 |
| 374 AudioRendererHost::AudioEntry* AudioRendererHost::LookupByController( | 374 AudioRendererHost::AudioEntry* AudioRendererHost::LookupByController( |
| 375 media::AudioOutputController* controller) { | 375 media::AudioOutputController* controller) { |
| 376 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 376 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 377 | 377 |
| 378 // Iterate the map of entries. | 378 // Iterate the map of entries. |
| 379 // TODO(hclam): Implement a faster look up method. | 379 // TODO(hclam): Implement a faster look up method. |
| 380 for (AudioEntryMap::iterator i = audio_entries_.begin(); | 380 for (AudioEntryMap::iterator i = audio_entries_.begin(); |
| 381 i != audio_entries_.end(); ++i) { | 381 i != audio_entries_.end(); ++i) { |
| 382 if (!i->second->pending_close && controller == i->second->controller.get()) | 382 if (!i->second->pending_close && controller == i->second->controller.get()) |
| 383 return i->second; | 383 return i->second; |
| 384 } | 384 } |
| 385 return NULL; | 385 return NULL; |
| 386 } | 386 } |
| 387 | 387 |
| 388 void AudioRendererHost::OnSetAudioStreamPlaying(int stream_id, bool playing) { | |
|
jam
2012/03/06 21:26:17
nit: it seems that these batch of functions aren't
vrk (LEFT CHROMIUM)
2012/03/06 22:11:42
SGTM, thanks! Done.
| |
| 389 if (!GetMediaObserver()) | |
| 390 return; | |
| 391 GetMediaObserver()->OnSetAudioStreamPlaying(this, stream_id, playing); | |
| 392 } | |
| 393 | |
| 394 void AudioRendererHost::OnSetAudioStreamStatus(int stream_id, | |
| 395 const std::string& status) { | |
| 396 if (!GetMediaObserver()) | |
| 397 return; | |
| 398 GetMediaObserver()->OnSetAudioStreamStatus(this, stream_id, status); | |
| 399 } | |
| 400 | |
| 401 void AudioRendererHost::OnSetAudioStreamVolume(int stream_id, double volume) { | |
| 402 if (!GetMediaObserver()) | |
| 403 return; | |
| 404 GetMediaObserver()->OnSetAudioStreamVolume(this, stream_id, volume); | |
| 405 } | |
| 406 | |
| 407 void AudioRendererHost::OnDeleteAudioStream(int stream_id) { | |
| 408 if (!GetMediaObserver()) | |
| 409 return; | |
| 410 GetMediaObserver()->OnDeleteAudioStream(this, stream_id); | |
| 411 } | |
| 412 | |
| 388 content::MediaObserver* AudioRendererHost::GetMediaObserver() { | 413 content::MediaObserver* AudioRendererHost::GetMediaObserver() { |
| 389 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 414 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 390 if (!media_observer_) | 415 if (!media_observer_) |
| 391 media_observer_ = resource_context_->GetMediaObserver(); | 416 media_observer_ = resource_context_->GetMediaObserver(); |
| 392 return media_observer_; | 417 return media_observer_; |
| 393 } | 418 } |
| OLD | NEW |