| 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/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
| 11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 #include "content/common/child_process.h" | 12 #include "content/common/child_process.h" |
| 13 #include "content/common/media/audio_messages.h" | 13 #include "content/common/media/audio_messages.h" |
| 14 #include "content/renderer/media/audio_message_filter.h" | 14 #include "content/renderer/media/audio_message_filter.h" |
| 15 #include "content/renderer/render_thread_impl.h" | 15 #include "content/renderer/render_thread_impl.h" |
| 16 #include "media/base/audio_hardware_config.h" | 16 #include "media/base/audio_hardware_config.h" |
| 17 #include "media/base/media_switches.h" | 17 #include "media/base/media_switches.h" |
| 18 | 18 |
| 19 namespace content { | 19 namespace content { |
| 20 | 20 |
| 21 // static | 21 // static |
| 22 PepperPlatformAudioOutputImpl* PepperPlatformAudioOutputImpl::Create( | 22 PepperPlatformAudioOutputImpl* PepperPlatformAudioOutputImpl::Create( |
| 23 int sample_rate, | 23 int sample_rate, |
| 24 int frames_per_buffer, | 24 int frames_per_buffer, |
| 25 int source_render_view_id, | 25 int source_render_view_id, |
| 26 webkit::ppapi::PluginDelegate::PlatformAudioOutputClient* client) { | 26 webkit::ppapi::PluginDelegate::PlatformAudioOutputClient* client) { |
| 27 scoped_refptr<PepperPlatformAudioOutputImpl> audio_output( | 27 scoped_refptr<PepperPlatformAudioOutputImpl> audio_output( |
| 28 new PepperPlatformAudioOutputImpl()); | 28 new PepperPlatformAudioOutputImpl(source_render_view_id)); |
| 29 if (audio_output->Initialize(sample_rate, frames_per_buffer, | 29 if (audio_output->Initialize(sample_rate, frames_per_buffer, client)) { |
| 30 source_render_view_id, client)) { | |
| 31 // Balanced by Release invoked in | 30 // Balanced by Release invoked in |
| 32 // PepperPlatformAudioOutputImpl::ShutDownOnIOThread(). | 31 // PepperPlatformAudioOutputImpl::ShutDownOnIOThread(). |
| 33 audio_output->AddRef(); | 32 audio_output->AddRef(); |
| 34 return audio_output.get(); | 33 return audio_output.get(); |
| 35 } | 34 } |
| 36 return NULL; | 35 return NULL; |
| 37 } | 36 } |
| 38 | 37 |
| 39 bool PepperPlatformAudioOutputImpl::StartPlayback() { | 38 bool PepperPlatformAudioOutputImpl::StartPlayback() { |
| 40 if (ipc_) { | 39 if (ipc_) { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 if (client_) | 89 if (client_) |
| 91 client_->StreamCreated(handle, length, socket_handle); | 90 client_->StreamCreated(handle, length, socket_handle); |
| 92 } else { | 91 } else { |
| 93 main_message_loop_proxy_->PostTask(FROM_HERE, | 92 main_message_loop_proxy_->PostTask(FROM_HERE, |
| 94 base::Bind(&PepperPlatformAudioOutputImpl::OnStreamCreated, this, | 93 base::Bind(&PepperPlatformAudioOutputImpl::OnStreamCreated, this, |
| 95 handle, socket_handle, length)); | 94 handle, socket_handle, length)); |
| 96 } | 95 } |
| 97 } | 96 } |
| 98 | 97 |
| 99 void PepperPlatformAudioOutputImpl::OnIPCClosed() { | 98 void PepperPlatformAudioOutputImpl::OnIPCClosed() { |
| 100 ipc_ = NULL; | 99 ipc_.reset(); |
| 100 stream_created_ = false; |
| 101 } | 101 } |
| 102 | 102 |
| 103 PepperPlatformAudioOutputImpl::~PepperPlatformAudioOutputImpl() { | 103 PepperPlatformAudioOutputImpl::~PepperPlatformAudioOutputImpl() { |
| 104 // Make sure we have been shut down. Warning: this will usually happen on | 104 // Make sure we have been shut down. Warning: this will usually happen on |
| 105 // the I/O thread! | 105 // the I/O thread! |
| 106 DCHECK_EQ(0, stream_id_); | 106 DCHECK(!stream_created_); |
| 107 DCHECK(!client_); | 107 DCHECK(!client_); |
| 108 } | 108 } |
| 109 | 109 |
| 110 PepperPlatformAudioOutputImpl::PepperPlatformAudioOutputImpl() | 110 PepperPlatformAudioOutputImpl::PepperPlatformAudioOutputImpl( |
| 111 int source_render_view_id) |
| 111 : client_(NULL), | 112 : client_(NULL), |
| 112 stream_id_(0), | 113 ipc_(AudioMessageFilter::Get()->CreateAudioOutputIPC( |
| 114 source_render_view_id)), |
| 115 stream_created_(false), |
| 113 main_message_loop_proxy_(base::MessageLoopProxy::current()) { | 116 main_message_loop_proxy_(base::MessageLoopProxy::current()) { |
| 114 ipc_ = RenderThreadImpl::current()->audio_message_filter(); | 117 CHECK(ipc_); |
| 115 } | 118 } |
| 116 | 119 |
| 117 bool PepperPlatformAudioOutputImpl::Initialize( | 120 bool PepperPlatformAudioOutputImpl::Initialize( |
| 118 int sample_rate, | 121 int sample_rate, |
| 119 int frames_per_buffer, | 122 int frames_per_buffer, |
| 120 int source_render_view_id, | |
| 121 webkit::ppapi::PluginDelegate::PlatformAudioOutputClient* client) { | 123 webkit::ppapi::PluginDelegate::PlatformAudioOutputClient* client) { |
| 122 DCHECK(client); | 124 DCHECK(client); |
| 123 client_ = client; | 125 client_ = client; |
| 124 | 126 |
| 125 media::AudioParameters::Format format; | 127 media::AudioParameters::Format format; |
| 126 const int kMaxFramesForLowLatency = 2047; | 128 const int kMaxFramesForLowLatency = 2047; |
| 127 | 129 |
| 128 media::AudioHardwareConfig* hardware_config = | 130 media::AudioHardwareConfig* hardware_config = |
| 129 RenderThreadImpl::current()->GetAudioHardwareConfig(); | 131 RenderThreadImpl::current()->GetAudioHardwareConfig(); |
| 130 | 132 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 142 } else { | 144 } else { |
| 143 format = media::AudioParameters::AUDIO_PCM_LINEAR; | 145 format = media::AudioParameters::AUDIO_PCM_LINEAR; |
| 144 } | 146 } |
| 145 | 147 |
| 146 media::AudioParameters params(format, media::CHANNEL_LAYOUT_STEREO, | 148 media::AudioParameters params(format, media::CHANNEL_LAYOUT_STEREO, |
| 147 sample_rate, 16, frames_per_buffer); | 149 sample_rate, 16, frames_per_buffer); |
| 148 | 150 |
| 149 ChildProcess::current()->io_message_loop()->PostTask( | 151 ChildProcess::current()->io_message_loop()->PostTask( |
| 150 FROM_HERE, | 152 FROM_HERE, |
| 151 base::Bind(&PepperPlatformAudioOutputImpl::InitializeOnIOThread, | 153 base::Bind(&PepperPlatformAudioOutputImpl::InitializeOnIOThread, |
| 152 this, params, source_render_view_id)); | 154 this, params)); |
| 153 return true; | 155 return true; |
| 154 } | 156 } |
| 155 | 157 |
| 156 void PepperPlatformAudioOutputImpl::InitializeOnIOThread( | 158 void PepperPlatformAudioOutputImpl::InitializeOnIOThread( |
| 157 const media::AudioParameters& params, int source_render_view_id) { | 159 const media::AudioParameters& params) { |
| 158 // Make sure we don't call init more than once. | 160 // Make sure we don't call init more than once. |
| 159 DCHECK_EQ(0, stream_id_); | 161 DCHECK(!stream_created_); |
| 160 stream_id_ = ipc_->AddDelegate(this); | |
| 161 DCHECK_NE(0, stream_id_); | |
| 162 | 162 |
| 163 ipc_->CreateStream(stream_id_, params); | 163 if (ipc_) { |
| 164 ipc_->AssociateStreamWithProducer(stream_id_, source_render_view_id); | 164 ipc_->CreateStream(this, params); |
| 165 stream_created_ = true; |
| 166 } |
| 165 } | 167 } |
| 166 | 168 |
| 167 void PepperPlatformAudioOutputImpl::StartPlaybackOnIOThread() { | 169 void PepperPlatformAudioOutputImpl::StartPlaybackOnIOThread() { |
| 168 if (stream_id_) | 170 if (stream_created_) |
| 169 ipc_->PlayStream(stream_id_); | 171 ipc_->PlayStream(); |
| 170 } | 172 } |
| 171 | 173 |
| 172 void PepperPlatformAudioOutputImpl::StopPlaybackOnIOThread() { | 174 void PepperPlatformAudioOutputImpl::StopPlaybackOnIOThread() { |
| 173 if (stream_id_) | 175 if (stream_created_) |
| 174 ipc_->PauseStream(stream_id_); | 176 ipc_->PauseStream(); |
| 175 } | 177 } |
| 176 | 178 |
| 177 void PepperPlatformAudioOutputImpl::ShutDownOnIOThread() { | 179 void PepperPlatformAudioOutputImpl::ShutDownOnIOThread() { |
| 178 // Make sure we don't call shutdown more than once. | 180 // Make sure we don't call shutdown more than once. |
| 179 if (!stream_id_) | 181 if (!stream_created_) |
| 180 return; | 182 return; |
| 181 | 183 |
| 182 ipc_->CloseStream(stream_id_); | 184 ipc_->CloseStream(); |
| 183 ipc_->RemoveDelegate(stream_id_); | 185 stream_created_ = false; |
| 184 stream_id_ = 0; | |
| 185 | 186 |
| 186 Release(); // Release for the delegate, balances out the reference taken in | 187 Release(); // Release for the delegate, balances out the reference taken in |
| 187 // PepperPluginDelegateImpl::CreateAudio. | 188 // PepperPluginDelegateImpl::CreateAudio. |
| 188 } | 189 } |
| 189 | 190 |
| 190 } // namespace content | 191 } // namespace content |
| OLD | NEW |