| 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/renderer/pepper/pepper_platform_audio_output_impl.h" | 5 #include "content/renderer/pepper/pepper_platform_audio_output_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop_proxy.h" | 9 #include "base/message_loop_proxy.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "content/common/child_process.h" | 11 #include "content/common/child_process.h" |
| 12 #include "content/common/media/audio_messages.h" | 12 #include "content/common/media/audio_messages.h" |
| 13 #include "content/renderer/media/audio_hardware.h" | 13 #include "content/renderer/media/audio_hardware.h" |
| 14 #include "content/renderer/media/audio_message_filter.h" |
| 14 #include "content/renderer/render_thread_impl.h" | 15 #include "content/renderer/render_thread_impl.h" |
| 15 | 16 |
| 16 namespace content { | 17 namespace content { |
| 17 | 18 |
| 18 // static | 19 // static |
| 19 PepperPlatformAudioOutputImpl* PepperPlatformAudioOutputImpl::Create( | 20 PepperPlatformAudioOutputImpl* PepperPlatformAudioOutputImpl::Create( |
| 20 int sample_rate, | 21 int sample_rate, |
| 21 int frames_per_buffer, | 22 int frames_per_buffer, |
| 22 webkit::ppapi::PluginDelegate::PlatformAudioOutputClient* client) { | 23 webkit::ppapi::PluginDelegate::PlatformAudioOutputClient* client) { |
| 23 scoped_refptr<PepperPlatformAudioOutputImpl> audio_output( | 24 scoped_refptr<PepperPlatformAudioOutputImpl> audio_output( |
| 24 new PepperPlatformAudioOutputImpl); | 25 new PepperPlatformAudioOutputImpl()); |
| 25 if (audio_output->Initialize(sample_rate, frames_per_buffer, client)) { | 26 if (audio_output->Initialize(sample_rate, frames_per_buffer, client)) { |
| 26 // Balanced by Release invoked in | 27 // Balanced by Release invoked in |
| 27 // PepperPlatformAudioOutputImpl::ShutDownOnIOThread(). | 28 // PepperPlatformAudioOutputImpl::ShutDownOnIOThread(). |
| 28 return audio_output.release(); | 29 return audio_output.release(); |
| 29 } | 30 } |
| 30 return NULL; | 31 return NULL; |
| 31 } | 32 } |
| 32 | 33 |
| 33 bool PepperPlatformAudioOutputImpl::StartPlayback() { | 34 bool PepperPlatformAudioOutputImpl::StartPlayback() { |
| 34 if (filter_) { | 35 if (ipc_) { |
| 35 ChildProcess::current()->io_message_loop()->PostTask( | 36 ChildProcess::current()->io_message_loop()->PostTask( |
| 36 FROM_HERE, | 37 FROM_HERE, |
| 37 base::Bind(&PepperPlatformAudioOutputImpl::StartPlaybackOnIOThread, | 38 base::Bind(&PepperPlatformAudioOutputImpl::StartPlaybackOnIOThread, |
| 38 this)); | 39 this)); |
| 39 return true; | 40 return true; |
| 40 } | 41 } |
| 41 return false; | 42 return false; |
| 42 } | 43 } |
| 43 | 44 |
| 44 bool PepperPlatformAudioOutputImpl::StopPlayback() { | 45 bool PepperPlatformAudioOutputImpl::StopPlayback() { |
| 45 if (filter_) { | 46 if (ipc_) { |
| 46 ChildProcess::current()->io_message_loop()->PostTask( | 47 ChildProcess::current()->io_message_loop()->PostTask( |
| 47 FROM_HERE, | 48 FROM_HERE, |
| 48 base::Bind(&PepperPlatformAudioOutputImpl::StopPlaybackOnIOThread, | 49 base::Bind(&PepperPlatformAudioOutputImpl::StopPlaybackOnIOThread, |
| 49 this)); | 50 this)); |
| 50 return true; | 51 return true; |
| 51 } | 52 } |
| 52 return false; | 53 return false; |
| 53 } | 54 } |
| 54 | 55 |
| 55 void PepperPlatformAudioOutputImpl::ShutDown() { | 56 void PepperPlatformAudioOutputImpl::ShutDown() { |
| 56 // Called on the main thread to stop all audio callbacks. We must only change | 57 // Called on the main thread to stop all audio callbacks. We must only change |
| 57 // the client on the main thread, and the delegates from the I/O thread. | 58 // the client on the main thread, and the delegates from the I/O thread. |
| 58 client_ = NULL; | 59 client_ = NULL; |
| 59 ChildProcess::current()->io_message_loop()->PostTask( | 60 ChildProcess::current()->io_message_loop()->PostTask( |
| 60 FROM_HERE, | 61 FROM_HERE, |
| 61 base::Bind(&PepperPlatformAudioOutputImpl::ShutDownOnIOThread, this)); | 62 base::Bind(&PepperPlatformAudioOutputImpl::ShutDownOnIOThread, this)); |
| 62 } | 63 } |
| 63 | 64 |
| 64 void PepperPlatformAudioOutputImpl::OnStateChanged(AudioStreamState state) {} | 65 void PepperPlatformAudioOutputImpl::OnStateChanged( |
| 66 media::AudioOutputIPCDelegate::State state) { |
| 67 } |
| 65 | 68 |
| 66 void PepperPlatformAudioOutputImpl::OnStreamCreated( | 69 void PepperPlatformAudioOutputImpl::OnStreamCreated( |
| 67 base::SharedMemoryHandle handle, | 70 base::SharedMemoryHandle handle, |
| 68 base::SyncSocket::Handle socket_handle, | 71 base::SyncSocket::Handle socket_handle, |
| 69 uint32 length) { | 72 int length) { |
| 70 #if defined(OS_WIN) | 73 #if defined(OS_WIN) |
| 71 DCHECK(handle); | 74 DCHECK(handle); |
| 72 DCHECK(socket_handle); | 75 DCHECK(socket_handle); |
| 73 #else | 76 #else |
| 74 DCHECK_NE(-1, handle.fd); | 77 DCHECK_NE(-1, handle.fd); |
| 75 DCHECK_NE(-1, socket_handle); | 78 DCHECK_NE(-1, socket_handle); |
| 76 #endif | 79 #endif |
| 77 DCHECK(length); | 80 DCHECK(length); |
| 78 | 81 |
| 79 if (base::MessageLoopProxy::current() == main_message_loop_proxy_) { | 82 if (base::MessageLoopProxy::current() == main_message_loop_proxy_) { |
| 80 // Must dereference the client only on the main thread. Shutdown may have | 83 // Must dereference the client only on the main thread. Shutdown may have |
| 81 // occurred while the request was in-flight, so we need to NULL check. | 84 // occurred while the request was in-flight, so we need to NULL check. |
| 82 if (client_) | 85 if (client_) |
| 83 client_->StreamCreated(handle, length, socket_handle); | 86 client_->StreamCreated(handle, length, socket_handle); |
| 84 } else { | 87 } else { |
| 85 main_message_loop_proxy_->PostTask(FROM_HERE, | 88 main_message_loop_proxy_->PostTask(FROM_HERE, |
| 86 base::Bind(&PepperPlatformAudioOutputImpl::OnStreamCreated, this, | 89 base::Bind(&PepperPlatformAudioOutputImpl::OnStreamCreated, this, |
| 87 handle, socket_handle, length)); | 90 handle, socket_handle, length)); |
| 88 } | 91 } |
| 89 } | 92 } |
| 90 | 93 |
| 94 void PepperPlatformAudioOutputImpl::OnIPCClosed() { |
| 95 ipc_ = NULL; |
| 96 } |
| 97 |
| 91 PepperPlatformAudioOutputImpl::~PepperPlatformAudioOutputImpl() { | 98 PepperPlatformAudioOutputImpl::~PepperPlatformAudioOutputImpl() { |
| 92 // Make sure we have been shut down. Warning: this will usually happen on | 99 // Make sure we have been shut down. Warning: this will usually happen on |
| 93 // the I/O thread! | 100 // the I/O thread! |
| 94 DCHECK_EQ(0, stream_id_); | 101 DCHECK_EQ(0, stream_id_); |
| 95 DCHECK(!client_); | 102 DCHECK(!client_); |
| 96 } | 103 } |
| 97 | 104 |
| 98 PepperPlatformAudioOutputImpl::PepperPlatformAudioOutputImpl() | 105 PepperPlatformAudioOutputImpl::PepperPlatformAudioOutputImpl() |
| 99 : client_(NULL), | 106 : client_(NULL), |
| 100 stream_id_(0), | 107 stream_id_(0), |
| 101 main_message_loop_proxy_(base::MessageLoopProxy::current()) { | 108 main_message_loop_proxy_(base::MessageLoopProxy::current()) { |
| 102 filter_ = RenderThreadImpl::current()->audio_message_filter(); | 109 ipc_ = RenderThreadImpl::current()->audio_message_filter(); |
| 103 } | 110 } |
| 104 | 111 |
| 105 bool PepperPlatformAudioOutputImpl::Initialize( | 112 bool PepperPlatformAudioOutputImpl::Initialize( |
| 106 int sample_rate, | 113 int sample_rate, |
| 107 int frames_per_buffer, | 114 int frames_per_buffer, |
| 108 webkit::ppapi::PluginDelegate::PlatformAudioOutputClient* client) { | 115 webkit::ppapi::PluginDelegate::PlatformAudioOutputClient* client) { |
| 109 DCHECK(client); | 116 DCHECK(client); |
| 110 // Make sure we don't call init more than once. | 117 // Make sure we don't call init more than once. |
| 111 DCHECK_EQ(0, stream_id_); | 118 DCHECK_EQ(0, stream_id_); |
| 112 | 119 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 129 | 136 |
| 130 ChildProcess::current()->io_message_loop()->PostTask( | 137 ChildProcess::current()->io_message_loop()->PostTask( |
| 131 FROM_HERE, | 138 FROM_HERE, |
| 132 base::Bind(&PepperPlatformAudioOutputImpl::InitializeOnIOThread, | 139 base::Bind(&PepperPlatformAudioOutputImpl::InitializeOnIOThread, |
| 133 this, params)); | 140 this, params)); |
| 134 return true; | 141 return true; |
| 135 } | 142 } |
| 136 | 143 |
| 137 void PepperPlatformAudioOutputImpl::InitializeOnIOThread( | 144 void PepperPlatformAudioOutputImpl::InitializeOnIOThread( |
| 138 const media::AudioParameters& params) { | 145 const media::AudioParameters& params) { |
| 139 stream_id_ = filter_->AddDelegate(this); | 146 stream_id_ = ipc_->AddDelegate(this); |
| 140 filter_->Send(new AudioHostMsg_CreateStream(stream_id_, params)); | 147 ipc_->CreateStream(stream_id_, params); |
| 141 } | 148 } |
| 142 | 149 |
| 143 void PepperPlatformAudioOutputImpl::StartPlaybackOnIOThread() { | 150 void PepperPlatformAudioOutputImpl::StartPlaybackOnIOThread() { |
| 144 if (stream_id_) | 151 if (stream_id_) |
| 145 filter_->Send(new AudioHostMsg_PlayStream(stream_id_)); | 152 ipc_->PlayStream(stream_id_); |
| 146 } | 153 } |
| 147 | 154 |
| 148 void PepperPlatformAudioOutputImpl::StopPlaybackOnIOThread() { | 155 void PepperPlatformAudioOutputImpl::StopPlaybackOnIOThread() { |
| 149 if (stream_id_) | 156 if (stream_id_) |
| 150 filter_->Send(new AudioHostMsg_PauseStream(stream_id_)); | 157 ipc_->PauseStream(stream_id_); |
| 151 } | 158 } |
| 152 | 159 |
| 153 void PepperPlatformAudioOutputImpl::ShutDownOnIOThread() { | 160 void PepperPlatformAudioOutputImpl::ShutDownOnIOThread() { |
| 154 // Make sure we don't call shutdown more than once. | 161 // Make sure we don't call shutdown more than once. |
| 155 if (!stream_id_) | 162 if (!stream_id_) |
| 156 return; | 163 return; |
| 157 | 164 |
| 158 filter_->Send(new AudioHostMsg_CloseStream(stream_id_)); | 165 ipc_->CloseStream(stream_id_); |
| 159 filter_->RemoveDelegate(stream_id_); | 166 ipc_->RemoveDelegate(stream_id_); |
| 160 stream_id_ = 0; | 167 stream_id_ = 0; |
| 161 | 168 |
| 162 Release(); // Release for the delegate, balances out the reference taken in | 169 Release(); // Release for the delegate, balances out the reference taken in |
| 163 // PepperPluginDelegateImpl::CreateAudio. | 170 // PepperPluginDelegateImpl::CreateAudio. |
| 164 } | 171 } |
| 165 | 172 |
| 166 } // namespace content | 173 } // namespace content |
| OLD | NEW |