| 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" |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 if (client_) | 90 if (client_) |
| 91 client_->StreamCreated(handle, length, socket_handle); | 91 client_->StreamCreated(handle, length, socket_handle); |
| 92 } else { | 92 } else { |
| 93 main_message_loop_proxy_->PostTask(FROM_HERE, | 93 main_message_loop_proxy_->PostTask(FROM_HERE, |
| 94 base::Bind(&PepperPlatformAudioOutputImpl::OnStreamCreated, this, | 94 base::Bind(&PepperPlatformAudioOutputImpl::OnStreamCreated, this, |
| 95 handle, socket_handle, length)); | 95 handle, socket_handle, length)); |
| 96 } | 96 } |
| 97 } | 97 } |
| 98 | 98 |
| 99 void PepperPlatformAudioOutputImpl::OnIPCClosed() { | 99 void PepperPlatformAudioOutputImpl::OnIPCClosed() { |
| 100 ipc_ = NULL; | 100 ipc_.reset(); |
| 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(!ipc_); |
| 107 DCHECK(!client_); | 107 DCHECK(!client_); |
| 108 } | 108 } |
| 109 | 109 |
| 110 PepperPlatformAudioOutputImpl::PepperPlatformAudioOutputImpl() | 110 PepperPlatformAudioOutputImpl::PepperPlatformAudioOutputImpl() |
| 111 : client_(NULL), | 111 : client_(NULL), |
| 112 stream_id_(0), | |
| 113 main_message_loop_proxy_(base::MessageLoopProxy::current()) { | 112 main_message_loop_proxy_(base::MessageLoopProxy::current()) { |
| 114 ipc_ = RenderThreadImpl::current()->audio_message_filter(); | |
| 115 } | 113 } |
| 116 | 114 |
| 117 bool PepperPlatformAudioOutputImpl::Initialize( | 115 bool PepperPlatformAudioOutputImpl::Initialize( |
| 118 int sample_rate, | 116 int sample_rate, |
| 119 int frames_per_buffer, | 117 int frames_per_buffer, |
| 120 int source_render_view_id, | 118 int source_render_view_id, |
| 121 webkit::ppapi::PluginDelegate::PlatformAudioOutputClient* client) { | 119 webkit::ppapi::PluginDelegate::PlatformAudioOutputClient* client) { |
| 122 DCHECK(client); | 120 DCHECK(client); |
| 123 client_ = client; | 121 client_ = client; |
| 124 | 122 |
| 123 RenderThreadImpl* const render_thread = RenderThreadImpl::current(); |
| 124 ipc_ = render_thread->audio_message_filter()-> |
| 125 CreateAudioOutputIPC(source_render_view_id); |
| 126 CHECK(ipc_); |
| 127 |
| 125 media::AudioParameters::Format format; | 128 media::AudioParameters::Format format; |
| 126 const int kMaxFramesForLowLatency = 2047; | 129 const int kMaxFramesForLowLatency = 2047; |
| 127 | 130 |
| 128 media::AudioHardwareConfig* hardware_config = | 131 media::AudioHardwareConfig* hardware_config = |
| 129 RenderThreadImpl::current()->GetAudioHardwareConfig(); | 132 render_thread->GetAudioHardwareConfig(); |
| 130 | 133 |
| 131 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | 134 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
| 132 if (!cmd_line->HasSwitch(switches::kDisableAudioOutputResampler)) { | 135 if (!cmd_line->HasSwitch(switches::kDisableAudioOutputResampler)) { |
| 133 // Rely on AudioOutputResampler to handle any inconsistencies between the | 136 // Rely on AudioOutputResampler to handle any inconsistencies between the |
| 134 // hardware params required for low latency and the requested params. | 137 // hardware params required for low latency and the requested params. |
| 135 format = media::AudioParameters::AUDIO_PCM_LOW_LATENCY; | 138 format = media::AudioParameters::AUDIO_PCM_LOW_LATENCY; |
| 136 } else if (sample_rate == hardware_config->GetOutputSampleRate() && | 139 } else if (sample_rate == hardware_config->GetOutputSampleRate() && |
| 137 frames_per_buffer <= kMaxFramesForLowLatency && | 140 frames_per_buffer <= kMaxFramesForLowLatency && |
| 138 frames_per_buffer % hardware_config->GetOutputBufferSize() == 0) { | 141 frames_per_buffer % hardware_config->GetOutputBufferSize() == 0) { |
| 139 // Use the low latency back end if the client request is compatible, and | 142 // Use the low latency back end if the client request is compatible, and |
| 140 // the sample count is low enough to justify using AUDIO_PCM_LOW_LATENCY. | 143 // the sample count is low enough to justify using AUDIO_PCM_LOW_LATENCY. |
| 141 format = media::AudioParameters::AUDIO_PCM_LOW_LATENCY; | 144 format = media::AudioParameters::AUDIO_PCM_LOW_LATENCY; |
| 142 } else { | 145 } else { |
| 143 format = media::AudioParameters::AUDIO_PCM_LINEAR; | 146 format = media::AudioParameters::AUDIO_PCM_LINEAR; |
| 144 } | 147 } |
| 145 | 148 |
| 146 media::AudioParameters params(format, media::CHANNEL_LAYOUT_STEREO, | 149 media::AudioParameters params(format, media::CHANNEL_LAYOUT_STEREO, |
| 147 sample_rate, 16, frames_per_buffer); | 150 sample_rate, 16, frames_per_buffer); |
| 148 | 151 |
| 149 ChildProcess::current()->io_message_loop()->PostTask( | 152 ChildProcess::current()->io_message_loop()->PostTask( |
| 150 FROM_HERE, | 153 FROM_HERE, |
| 151 base::Bind(&PepperPlatformAudioOutputImpl::InitializeOnIOThread, | 154 base::Bind(&PepperPlatformAudioOutputImpl::InitializeOnIOThread, |
| 152 this, params, source_render_view_id)); | 155 this, params)); |
| 153 return true; | 156 return true; |
| 154 } | 157 } |
| 155 | 158 |
| 156 void PepperPlatformAudioOutputImpl::InitializeOnIOThread( | 159 void PepperPlatformAudioOutputImpl::InitializeOnIOThread( |
| 157 const media::AudioParameters& params, int source_render_view_id) { | 160 const media::AudioParameters& params) { |
| 158 // Make sure we don't call init more than once. | 161 DCHECK(ChildProcess::current()->io_message_loop_proxy()-> |
| 159 DCHECK_EQ(0, stream_id_); | 162 BelongsToCurrentThread()); |
| 160 stream_id_ = ipc_->AddDelegate(this); | 163 if (ipc_) |
| 161 DCHECK_NE(0, stream_id_); | 164 ipc_->CreateStream(this, params); |
| 162 | |
| 163 ipc_->CreateStream(stream_id_, params); | |
| 164 ipc_->AssociateStreamWithProducer(stream_id_, source_render_view_id); | |
| 165 } | 165 } |
| 166 | 166 |
| 167 void PepperPlatformAudioOutputImpl::StartPlaybackOnIOThread() { | 167 void PepperPlatformAudioOutputImpl::StartPlaybackOnIOThread() { |
| 168 if (stream_id_) | 168 DCHECK(ChildProcess::current()->io_message_loop_proxy()-> |
| 169 ipc_->PlayStream(stream_id_); | 169 BelongsToCurrentThread()); |
| 170 if (ipc_) |
| 171 ipc_->PlayStream(); |
| 170 } | 172 } |
| 171 | 173 |
| 172 void PepperPlatformAudioOutputImpl::StopPlaybackOnIOThread() { | 174 void PepperPlatformAudioOutputImpl::StopPlaybackOnIOThread() { |
| 173 if (stream_id_) | 175 DCHECK(ChildProcess::current()->io_message_loop_proxy()-> |
| 174 ipc_->PauseStream(stream_id_); | 176 BelongsToCurrentThread()); |
| 177 if (ipc_) |
| 178 ipc_->PauseStream(); |
| 175 } | 179 } |
| 176 | 180 |
| 177 void PepperPlatformAudioOutputImpl::ShutDownOnIOThread() { | 181 void PepperPlatformAudioOutputImpl::ShutDownOnIOThread() { |
| 182 DCHECK(ChildProcess::current()->io_message_loop_proxy()-> |
| 183 BelongsToCurrentThread()); |
| 184 |
| 178 // Make sure we don't call shutdown more than once. | 185 // Make sure we don't call shutdown more than once. |
| 179 if (!stream_id_) | 186 if (!ipc_) |
| 180 return; | 187 return; |
| 181 | 188 |
| 182 ipc_->CloseStream(stream_id_); | 189 ipc_->CloseStream(); |
| 183 ipc_->RemoveDelegate(stream_id_); | 190 ipc_.reset(); |
| 184 stream_id_ = 0; | |
| 185 | 191 |
| 186 Release(); // Release for the delegate, balances out the reference taken in | 192 Release(); // Release for the delegate, balances out the reference taken in |
| 187 // PepperPluginDelegateImpl::CreateAudio. | 193 // PepperPluginDelegateImpl::CreateAudio. |
| 188 } | 194 } |
| 189 | 195 |
| 190 } // namespace content | 196 } // namespace content |
| OLD | NEW |