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/render_thread_impl.h" | 14 #include "content/renderer/render_thread_impl.h" |
| 15 #include "content/renderer/render_view_impl.h" |
15 | 16 |
16 namespace content { | 17 namespace content { |
17 | 18 |
18 // static | 19 // static |
19 PepperPlatformAudioOutputImpl* PepperPlatformAudioOutputImpl::Create( | 20 PepperPlatformAudioOutputImpl* PepperPlatformAudioOutputImpl::Create( |
| 21 RenderViewImpl* render_view, |
20 int sample_rate, | 22 int sample_rate, |
21 int frames_per_buffer, | 23 int frames_per_buffer, |
22 webkit::ppapi::PluginDelegate::PlatformAudioOutputClient* client) { | 24 webkit::ppapi::PluginDelegate::PlatformAudioOutputClient* client) { |
23 scoped_refptr<PepperPlatformAudioOutputImpl> audio_output( | 25 scoped_refptr<PepperPlatformAudioOutputImpl> audio_output( |
24 new PepperPlatformAudioOutputImpl); | 26 new PepperPlatformAudioOutputImpl); |
25 if (audio_output->Initialize(sample_rate, frames_per_buffer, client)) { | 27 if (audio_output->Initialize(render_view, |
| 28 sample_rate, |
| 29 frames_per_buffer, |
| 30 client)) { |
26 // Balanced by Release invoked in | 31 // Balanced by Release invoked in |
27 // PepperPlatformAudioOutputImpl::ShutDownOnIOThread(). | 32 // PepperPlatformAudioOutputImpl::ShutDownOnIOThread(). |
28 return audio_output.release(); | 33 return audio_output.release(); |
29 } | 34 } |
30 return NULL; | 35 return NULL; |
31 } | 36 } |
32 | 37 |
33 bool PepperPlatformAudioOutputImpl::StartPlayback() { | 38 bool PepperPlatformAudioOutputImpl::StartPlayback() { |
34 if (filter_) { | 39 if (filter_) { |
35 ChildProcess::current()->io_message_loop()->PostTask( | 40 ChildProcess::current()->io_message_loop()->PostTask( |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 } | 101 } |
97 | 102 |
98 PepperPlatformAudioOutputImpl::PepperPlatformAudioOutputImpl() | 103 PepperPlatformAudioOutputImpl::PepperPlatformAudioOutputImpl() |
99 : client_(NULL), | 104 : client_(NULL), |
100 stream_id_(0), | 105 stream_id_(0), |
101 main_message_loop_proxy_(base::MessageLoopProxy::current()) { | 106 main_message_loop_proxy_(base::MessageLoopProxy::current()) { |
102 filter_ = RenderThreadImpl::current()->audio_message_filter(); | 107 filter_ = RenderThreadImpl::current()->audio_message_filter(); |
103 } | 108 } |
104 | 109 |
105 bool PepperPlatformAudioOutputImpl::Initialize( | 110 bool PepperPlatformAudioOutputImpl::Initialize( |
| 111 RenderViewImpl* render_view, |
106 int sample_rate, | 112 int sample_rate, |
107 int frames_per_buffer, | 113 int frames_per_buffer, |
108 webkit::ppapi::PluginDelegate::PlatformAudioOutputClient* client) { | 114 webkit::ppapi::PluginDelegate::PlatformAudioOutputClient* client) { |
109 DCHECK(client); | 115 DCHECK(client); |
110 // Make sure we don't call init more than once. | 116 // Make sure we don't call init more than once. |
111 DCHECK_EQ(0, stream_id_); | 117 DCHECK_EQ(0, stream_id_); |
112 | 118 |
| 119 render_view_ = render_view; |
113 client_ = client; | 120 client_ = client; |
114 | 121 |
115 media::AudioParameters::Format format; | 122 media::AudioParameters::Format format; |
116 const int kMaxFramesForLowLatency = 2400; | 123 const int kMaxFramesForLowLatency = 2400; |
117 // Use the low latency back end if the client request is compatible, and | 124 // Use the low latency back end if the client request is compatible, and |
118 // the sample count is low enough to justify using AUDIO_PCM_LOW_LATENCY. | 125 // the sample count is low enough to justify using AUDIO_PCM_LOW_LATENCY. |
119 if (sample_rate == audio_hardware::GetOutputSampleRate() && | 126 if (sample_rate == audio_hardware::GetOutputSampleRate() && |
120 frames_per_buffer <= kMaxFramesForLowLatency && | 127 frames_per_buffer <= kMaxFramesForLowLatency && |
121 frames_per_buffer % audio_hardware::GetOutputBufferSize() == 0) { | 128 frames_per_buffer % audio_hardware::GetOutputBufferSize() == 0) { |
122 format = media::AudioParameters::AUDIO_PCM_LOW_LATENCY; | 129 format = media::AudioParameters::AUDIO_PCM_LOW_LATENCY; |
123 } else { | 130 } else { |
124 format = media::AudioParameters::AUDIO_PCM_LINEAR; | 131 format = media::AudioParameters::AUDIO_PCM_LINEAR; |
125 } | 132 } |
126 | 133 |
127 media::AudioParameters params(format, CHANNEL_LAYOUT_STEREO, sample_rate, 16, | 134 media::AudioParameters params(format, CHANNEL_LAYOUT_STEREO, sample_rate, 16, |
128 frames_per_buffer); | 135 frames_per_buffer); |
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_ = filter_->AddDelegate(this); |
140 filter_->Send(new AudioHostMsg_CreateStream(stream_id_, params)); | 147 filter_->Send(new AudioHostMsg_CreateStream( |
| 148 render_view_->routing_id(), stream_id_, params)); |
141 } | 149 } |
142 | 150 |
143 void PepperPlatformAudioOutputImpl::StartPlaybackOnIOThread() { | 151 void PepperPlatformAudioOutputImpl::StartPlaybackOnIOThread() { |
144 if (stream_id_) | 152 if (stream_id_) |
145 filter_->Send(new AudioHostMsg_PlayStream(stream_id_)); | 153 filter_->Send(new AudioHostMsg_PlayStream(stream_id_)); |
146 } | 154 } |
147 | 155 |
148 void PepperPlatformAudioOutputImpl::StopPlaybackOnIOThread() { | 156 void PepperPlatformAudioOutputImpl::StopPlaybackOnIOThread() { |
149 if (stream_id_) | 157 if (stream_id_) |
150 filter_->Send(new AudioHostMsg_PauseStream(stream_id_)); | 158 filter_->Send(new AudioHostMsg_PauseStream(stream_id_)); |
151 } | 159 } |
152 | 160 |
153 void PepperPlatformAudioOutputImpl::ShutDownOnIOThread() { | 161 void PepperPlatformAudioOutputImpl::ShutDownOnIOThread() { |
154 // Make sure we don't call shutdown more than once. | 162 // Make sure we don't call shutdown more than once. |
155 if (!stream_id_) | 163 if (!stream_id_) |
156 return; | 164 return; |
157 | 165 |
158 filter_->Send(new AudioHostMsg_CloseStream(stream_id_)); | 166 filter_->Send(new AudioHostMsg_CloseStream(stream_id_)); |
159 filter_->RemoveDelegate(stream_id_); | 167 filter_->RemoveDelegate(stream_id_); |
160 stream_id_ = 0; | 168 stream_id_ = 0; |
161 | 169 |
162 Release(); // Release for the delegate, balances out the reference taken in | 170 Release(); // Release for the delegate, balances out the reference taken in |
163 // PepperPluginDelegateImpl::CreateAudio. | 171 // PepperPluginDelegateImpl::CreateAudio. |
164 } | 172 } |
165 | 173 |
166 } // namespace content | 174 } // namespace content |
OLD | NEW |