OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/bind.h" | |
6 #include "base/memory/shared_memory.h" | |
7 #include "content/public/renderer/renderer_ppapi_host.h" | |
8 #include "content/renderer/pepper/host_globals.h" | |
9 #include "content/renderer/pepper/pepper_audio_encoder_host.h" | |
10 #include "content/renderer/render_thread_impl.h" | |
11 #include "media/base/bind_to_current_loop.h" | |
12 #include "ppapi/c/pp_codecs.h" | |
13 #include "ppapi/c/pp_errors.h" | |
14 #include "ppapi/host/dispatch_host_message.h" | |
15 #include "ppapi/host/ppapi_host.h" | |
16 #include "ppapi/proxy/ppapi_messages.h" | |
17 #include "ppapi/shared_impl/media_stream_buffer.h" | |
18 #include "third_party/opus/src/include/opus.h" | |
19 | |
20 using ppapi::proxy::SerializedHandle; | |
21 | |
22 namespace content { | |
23 | |
24 namespace { | |
25 | |
26 // Buffer up to 150ms (15 x 10ms per frame). | |
27 const uint32_t kDefaultNumberOfAudioBuffers = 15; | |
28 | |
29 bool PP_HardwareAccelerationCompatible(bool accelerated, | |
30 PP_HardwareAcceleration requested) { | |
31 switch (requested) { | |
32 case PP_HARDWAREACCELERATION_ONLY: | |
33 return accelerated; | |
34 case PP_HARDWAREACCELERATION_NONE: | |
35 return !accelerated; | |
36 case PP_HARDWAREACCELERATION_WITHFALLBACK: | |
37 return true; | |
38 // No default case, to catch unhandled PP_HardwareAcceleration values. | |
39 } | |
40 return false; | |
41 } | |
42 | |
43 void StopAudioEncoder( | |
44 scoped_ptr<PepperAudioEncoderHost::AudioEncoderImpl> encoder, | |
45 scoped_ptr<ppapi::MediaStreamBufferManager> audio_buffer_manager, | |
46 scoped_ptr<ppapi::MediaStreamBufferManager> bitstream_buffer_manager) {} | |
47 | |
48 } // namespace | |
49 | |
50 // This class should be constructed and initialized on the main renderer | |
51 // thread, used and destructed on the media thread. | |
52 class PepperAudioEncoderHost::AudioEncoderImpl { | |
53 public: | |
54 // Callback used to signal encoded data. If |size| is negative, an error | |
55 // occured. | |
bbudge
2015/11/13 20:37:56
nit: spelling occurred
llandwerlin-old
2015/11/16 11:23:57
Done.
| |
56 using BitstreamBufferReadyCB = base::Callback<void(int32_t size)>; | |
57 | |
58 AudioEncoderImpl(); | |
59 ~AudioEncoderImpl(); | |
60 | |
61 // Used on the renderer thread. | |
62 static std::vector<PP_AudioProfileDescription> GetSupportedProfiles(); | |
63 bool Initialize(const ppapi::proxy::PPB_AudioEncodeParameters& parameters); | |
64 int32_t GetNumberOfSamplesPerFrame(); | |
65 | |
66 // Used on the media thread. | |
67 void Encode(uint8_t* input_data, | |
68 size_t input_size, | |
69 uint8_t* output_data, | |
70 size_t output_size, | |
71 BitstreamBufferReadyCB callback); | |
72 void RequestBitrateChange(uint32_t bitrate); | |
73 | |
74 private: | |
75 scoped_ptr<uint8[]> encoder_memory_; | |
76 OpusEncoder* opus_encoder_; | |
77 | |
78 // Initialization parameters, only valid if |encoder_memory_| is not | |
79 // nullptr. | |
80 ppapi::proxy::PPB_AudioEncodeParameters parameters_; | |
81 | |
82 DISALLOW_COPY_AND_ASSIGN(AudioEncoderImpl); | |
83 }; | |
84 | |
85 PepperAudioEncoderHost::AudioEncoderImpl::AudioEncoderImpl() | |
86 : opus_encoder_(nullptr) {} | |
87 | |
88 PepperAudioEncoderHost::AudioEncoderImpl::~AudioEncoderImpl() {} | |
89 | |
90 // static | |
91 std::vector<PP_AudioProfileDescription> | |
92 PepperAudioEncoderHost::AudioEncoderImpl::GetSupportedProfiles() { | |
93 std::vector<PP_AudioProfileDescription> profiles; | |
94 static const uint32_t sampling_rates[] = {8000, 12000, 16000, 24000, 48000}; | |
95 | |
96 for (uint32_t i = 0; i < arraysize(sampling_rates); ++i) { | |
97 PP_AudioProfileDescription profile; | |
98 profile.profile = PP_AUDIOPROFILE_OPUS; | |
99 profile.max_channels = 2; | |
100 profile.sample_size = PP_AUDIOBUFFER_SAMPLESIZE_16_BITS; | |
101 profile.sample_rate = sampling_rates[i]; | |
102 profile.hardware_accelerated = PP_FALSE; | |
103 profiles.push_back(profile); | |
104 } | |
105 return profiles; | |
106 } | |
107 | |
108 bool PepperAudioEncoderHost::AudioEncoderImpl::Initialize( | |
109 const ppapi::proxy::PPB_AudioEncodeParameters& parameters) { | |
110 if (parameters.output_profile != PP_AUDIOPROFILE_OPUS) | |
111 return false; | |
112 | |
113 DCHECK(!encoder_memory_); | |
114 | |
115 int32_t encoder_size = opus_encoder_get_size(parameters.channels); | |
116 if (encoder_size < 1) | |
117 return false; | |
118 | |
119 scoped_ptr<uint8[]> encoder_memory(new uint8[encoder_size]); | |
120 opus_encoder_ = reinterpret_cast<OpusEncoder*>(encoder_memory.get()); | |
121 | |
122 if (opus_encoder_init(opus_encoder_, parameters.input_sample_rate, | |
123 parameters.channels, OPUS_APPLICATION_AUDIO) != OPUS_OK) | |
124 return false; | |
125 | |
126 if (opus_encoder_ctl(opus_encoder_, | |
127 OPUS_SET_BITRATE(parameters.initial_bitrate <= 0 | |
128 ? OPUS_AUTO | |
129 : parameters.initial_bitrate)) != | |
130 OPUS_OK) | |
131 return false; | |
132 | |
133 encoder_memory_.swap(encoder_memory); | |
134 parameters_ = parameters; | |
135 | |
136 return true; | |
137 } | |
138 | |
139 int32_t PepperAudioEncoderHost::AudioEncoderImpl::GetNumberOfSamplesPerFrame() { | |
140 DCHECK(encoder_memory_); | |
141 // Opus supports 2.5, 5, 10, 20, 40 or 60ms audio frames. We take | |
142 // 10ms by default. | |
143 return parameters_.input_sample_rate / 100; | |
144 } | |
145 | |
146 void PepperAudioEncoderHost::AudioEncoderImpl::Encode( | |
147 uint8_t* input_data, | |
148 size_t input_size, | |
149 uint8_t* output_data, | |
150 size_t output_size, | |
151 BitstreamBufferReadyCB callback) { | |
152 DCHECK(encoder_memory_); | |
153 int32_t result = opus_encode( | |
154 opus_encoder_, reinterpret_cast<opus_int16*>(input_data), | |
155 (input_size / parameters_.channels) / parameters_.input_sample_size, | |
156 output_data, output_size); | |
157 callback.Run(result); | |
158 } | |
159 | |
160 void PepperAudioEncoderHost::AudioEncoderImpl::RequestBitrateChange( | |
161 uint32_t bitrate) { | |
162 DCHECK(encoder_memory_); | |
163 opus_encoder_ctl(opus_encoder_, OPUS_SET_BITRATE(bitrate)); | |
164 } | |
165 | |
166 PepperAudioEncoderHost::PepperAudioEncoderHost(RendererPpapiHost* host, | |
167 PP_Instance instance, | |
168 PP_Resource resource) | |
169 : ResourceHost(host->GetPpapiHost(), instance, resource), | |
170 renderer_ppapi_host_(host), | |
171 initialized_(false), | |
172 encoder_last_error_(PP_ERROR_FAILED), | |
173 media_task_runner_(RenderThreadImpl::current() | |
174 ->GetMediaThreadTaskRunner()), | |
175 weak_ptr_factory_(this) {} | |
176 | |
177 PepperAudioEncoderHost::~PepperAudioEncoderHost() { | |
178 Close(); | |
179 } | |
180 | |
181 int32_t PepperAudioEncoderHost::OnResourceMessageReceived( | |
182 const IPC::Message& msg, | |
183 ppapi::host::HostMessageContext* context) { | |
184 PPAPI_BEGIN_MESSAGE_MAP(PepperAudioEncoderHost, msg) | |
185 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( | |
186 PpapiHostMsg_AudioEncoder_GetSupportedProfiles, | |
187 OnHostMsgGetSupportedProfiles) | |
188 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_AudioEncoder_Initialize, | |
189 OnHostMsgInitialize) | |
190 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_AudioEncoder_Encode, | |
191 OnHostMsgEncode) | |
192 PPAPI_DISPATCH_HOST_RESOURCE_CALL( | |
193 PpapiHostMsg_AudioEncoder_RecycleBitstreamBuffer, | |
194 OnHostMsgRecycleBitstreamBuffer) | |
195 PPAPI_DISPATCH_HOST_RESOURCE_CALL( | |
196 PpapiHostMsg_AudioEncoder_RequestBitrateChange, | |
197 OnHostMsgRequestBitrateChange) | |
198 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_AudioEncoder_Close, | |
199 OnHostMsgClose) | |
200 PPAPI_END_MESSAGE_MAP() | |
201 return PP_ERROR_FAILED; | |
202 } | |
203 | |
204 int32_t PepperAudioEncoderHost::OnHostMsgGetSupportedProfiles( | |
205 ppapi::host::HostMessageContext* context) { | |
206 std::vector<PP_AudioProfileDescription> profiles; | |
207 GetSupportedProfiles(&profiles); | |
208 | |
209 host()->SendReply( | |
210 context->MakeReplyMessageContext(), | |
211 PpapiPluginMsg_AudioEncoder_GetSupportedProfilesReply(profiles)); | |
212 | |
213 return PP_OK_COMPLETIONPENDING; | |
214 } | |
215 | |
216 int32_t PepperAudioEncoderHost::OnHostMsgInitialize( | |
217 ppapi::host::HostMessageContext* context, | |
218 const ppapi::proxy::PPB_AudioEncodeParameters& parameters) { | |
219 if (initialized_) | |
220 return PP_ERROR_FAILED; | |
221 | |
222 if (!IsInitializationValid(parameters)) | |
223 return PP_ERROR_NOTSUPPORTED; | |
224 | |
225 parameters_ = parameters; | |
226 | |
227 if (parameters.acceleration == PP_HARDWAREACCELERATION_ONLY) | |
228 return PP_ERROR_FAILED; | |
229 | |
230 scoped_ptr<AudioEncoderImpl> encoder(new AudioEncoderImpl); | |
231 if (!encoder->Initialize(parameters)) | |
232 return PP_ERROR_FAILED; | |
233 if (!AllocateBuffers(parameters, encoder->GetNumberOfSamplesPerFrame())) | |
234 return PP_ERROR_NOMEMORY; | |
235 | |
236 initialized_ = true; | |
237 encoder_last_error_ = PP_OK; | |
238 encoder_.swap(encoder); | |
239 | |
240 ppapi::host::ReplyMessageContext reply_context = | |
241 context->MakeReplyMessageContext(); | |
242 reply_context.params.AppendHandle( | |
243 SerializedHandle(renderer_ppapi_host_->ShareSharedMemoryHandleWithRemote( | |
244 audio_buffer_manager_->shm()->handle()), | |
245 audio_buffer_manager_->shm()->mapped_size())); | |
246 reply_context.params.AppendHandle( | |
247 SerializedHandle(renderer_ppapi_host_->ShareSharedMemoryHandleWithRemote( | |
248 bitstream_buffer_manager_->shm()->handle()), | |
249 bitstream_buffer_manager_->shm()->mapped_size())); | |
250 host()->SendReply(reply_context, | |
251 PpapiPluginMsg_AudioEncoder_InitializeReply( | |
252 encoder_->GetNumberOfSamplesPerFrame(), | |
253 audio_buffer_manager_->number_of_buffers(), | |
254 audio_buffer_manager_->buffer_size(), | |
255 bitstream_buffer_manager_->number_of_buffers(), | |
256 bitstream_buffer_manager_->buffer_size())); | |
257 | |
258 return PP_OK_COMPLETIONPENDING; | |
259 } | |
260 | |
261 int32_t PepperAudioEncoderHost::OnHostMsgEncode( | |
262 ppapi::host::HostMessageContext* context, | |
263 int32_t buffer_id) { | |
264 if (encoder_last_error_) | |
265 return encoder_last_error_; | |
266 | |
267 if (buffer_id < 0 || buffer_id >= audio_buffer_manager_->number_of_buffers()) | |
268 return PP_ERROR_BADARGUMENT; | |
269 | |
270 audio_buffer_manager_->EnqueueBuffer(buffer_id); | |
271 | |
272 DoEncode(); | |
273 | |
274 return PP_OK_COMPLETIONPENDING; | |
275 } | |
276 | |
277 int32_t PepperAudioEncoderHost::OnHostMsgRecycleBitstreamBuffer( | |
278 ppapi::host::HostMessageContext* context, | |
279 int32_t buffer_id) { | |
280 if (encoder_last_error_) | |
281 return encoder_last_error_; | |
282 | |
283 if (buffer_id < 0 || | |
284 buffer_id >= bitstream_buffer_manager_->number_of_buffers()) | |
285 return PP_ERROR_BADARGUMENT; | |
286 | |
287 bitstream_buffer_manager_->EnqueueBuffer(buffer_id); | |
288 | |
289 DoEncode(); | |
290 | |
291 return PP_OK; | |
292 } | |
293 | |
294 int32_t PepperAudioEncoderHost::OnHostMsgRequestBitrateChange( | |
295 ppapi::host::HostMessageContext* context, | |
296 uint32_t bitrate) { | |
297 if (encoder_last_error_) | |
298 return encoder_last_error_; | |
299 | |
300 media_task_runner_->PostTask( | |
301 FROM_HERE, base::Bind(&AudioEncoderImpl::RequestBitrateChange, | |
302 base::Unretained(encoder_.get()), bitrate)); | |
303 | |
304 return PP_OK; | |
305 } | |
306 | |
307 int32_t PepperAudioEncoderHost::OnHostMsgClose( | |
308 ppapi::host::HostMessageContext* context) { | |
309 encoder_last_error_ = PP_ERROR_FAILED; | |
310 Close(); | |
311 | |
312 return PP_OK; | |
313 } | |
314 | |
315 void PepperAudioEncoderHost::GetSupportedProfiles( | |
316 std::vector<PP_AudioProfileDescription>* profiles) { | |
317 DCHECK(RenderThreadImpl::current()); | |
318 | |
319 *profiles = AudioEncoderImpl::GetSupportedProfiles(); | |
320 } | |
321 | |
322 bool PepperAudioEncoderHost::IsInitializationValid( | |
323 const ppapi::proxy::PPB_AudioEncodeParameters& parameters) { | |
324 DCHECK(RenderThreadImpl::current()); | |
325 | |
326 std::vector<PP_AudioProfileDescription> profiles; | |
327 GetSupportedProfiles(&profiles); | |
328 | |
329 for (const PP_AudioProfileDescription& profile : profiles) { | |
330 if (parameters.output_profile == profile.profile && | |
331 parameters.input_sample_size == profile.sample_size && | |
332 parameters.input_sample_rate == profile.sample_rate && | |
333 parameters.channels <= profile.max_channels && | |
334 PP_HardwareAccelerationCompatible( | |
335 profile.hardware_accelerated == PP_TRUE ? true : false, | |
336 parameters.acceleration)) | |
337 return true; | |
338 } | |
339 | |
340 return false; | |
341 } | |
342 | |
343 bool PepperAudioEncoderHost::AllocateBuffers( | |
344 const ppapi::proxy::PPB_AudioEncodeParameters& parameters, | |
345 int32_t samples_per_frame) { | |
346 DCHECK(RenderThreadImpl::current()); | |
347 | |
348 // Audio buffers size computation & allocation. | |
349 base::CheckedNumeric<size_t> audio_buffer_size = samples_per_frame; | |
350 audio_buffer_size *= parameters.channels; | |
351 audio_buffer_size *= parameters.input_sample_size; | |
352 | |
353 base::CheckedNumeric<size_t> total_audio_buffer_size = audio_buffer_size; | |
354 total_audio_buffer_size += sizeof(ppapi::MediaStreamBuffer::Audio); | |
355 if (!total_audio_buffer_size.IsValid()) | |
356 return false; | |
357 | |
358 base::CheckedNumeric<size_t> total_audio_memory_size = | |
359 total_audio_buffer_size.ValueOrDie(); | |
360 total_audio_memory_size *= kDefaultNumberOfAudioBuffers; | |
361 if (!total_audio_memory_size.IsValid()) | |
362 return false; | |
363 | |
364 scoped_ptr<base::SharedMemory> audio_memory( | |
365 RenderThreadImpl::current()->HostAllocateSharedMemoryBuffer( | |
366 total_audio_memory_size.ValueOrDie())); | |
367 if (!audio_memory) | |
368 return false; | |
369 | |
370 scoped_ptr<ppapi::MediaStreamBufferManager> audio_buffer_manager( | |
371 new ppapi::MediaStreamBufferManager(this)); | |
372 if (!audio_buffer_manager->SetBuffers(kDefaultNumberOfAudioBuffers, | |
373 total_audio_buffer_size.ValueOrDie(), | |
374 audio_memory.Pass(), false)) | |
375 return false; | |
376 | |
377 for (int32_t i = 0; i < audio_buffer_manager->number_of_buffers(); ++i) { | |
378 ppapi::MediaStreamBuffer::Audio* buffer = | |
379 &(audio_buffer_manager->GetBufferPointer(i)->audio); | |
380 buffer->header.size = total_audio_buffer_size.ValueOrDie(); | |
381 buffer->header.type = ppapi::MediaStreamBuffer::TYPE_AUDIO; | |
382 buffer->sample_rate = | |
383 static_cast<PP_AudioBuffer_SampleRate>(parameters.input_sample_rate); | |
384 buffer->number_of_channels = parameters.channels; | |
385 buffer->number_of_samples = samples_per_frame; | |
386 buffer->data_size = audio_buffer_size.ValueOrDie(); | |
387 } | |
388 | |
389 // Bitstream buffers size computation & allocation (individual bitstream | |
390 // buffers are twice the size of the raw data). | |
bbudge
2015/11/13 20:37:56
Why are the bitstream buffers twice the size? I'm
llandwerlin-old
2015/11/16 11:23:57
Yes, that's exactly why. Adding a comment here.
| |
391 base::CheckedNumeric<size_t> bitstream_buffer_size = audio_buffer_size; | |
392 bitstream_buffer_size *= 2; | |
393 if (!bitstream_buffer_size.IsValid()) | |
394 return false; | |
395 | |
396 base::CheckedNumeric<size_t> total_bitstream_memory_size = | |
397 bitstream_buffer_size; | |
398 total_bitstream_memory_size *= kDefaultNumberOfAudioBuffers; | |
399 if (!total_bitstream_memory_size.IsValid()) | |
400 return false; | |
401 | |
402 scoped_ptr<base::SharedMemory> bistream_memory( | |
bbudge
2015/11/13 20:37:56
nit: bitstream
llandwerlin-old
2015/11/16 11:23:57
Done.
| |
403 RenderThreadImpl::current()->HostAllocateSharedMemoryBuffer( | |
404 total_bitstream_memory_size.ValueOrDie())); | |
405 if (!bistream_memory) | |
406 return false; | |
407 | |
408 scoped_ptr<ppapi::MediaStreamBufferManager> bitstream_buffer_manager( | |
409 new ppapi::MediaStreamBufferManager(this)); | |
410 if (!bitstream_buffer_manager->SetBuffers(kDefaultNumberOfAudioBuffers, | |
411 bitstream_buffer_size.ValueOrDie(), | |
412 bistream_memory.Pass(), true)) | |
413 return false; | |
414 | |
415 audio_buffer_manager_.swap(audio_buffer_manager); | |
416 bitstream_buffer_manager_.swap(bitstream_buffer_manager); | |
417 | |
418 return true; | |
419 } | |
420 | |
421 void PepperAudioEncoderHost::DoEncode() { | |
422 DCHECK(RenderThreadImpl::current()); | |
423 DCHECK(encoder_); | |
424 | |
425 if (!audio_buffer_manager_->HasAvailableBuffer() || | |
426 !bitstream_buffer_manager_->HasAvailableBuffer()) | |
427 return; | |
428 | |
429 int32_t audio_buffer_id = audio_buffer_manager_->DequeueBuffer(); | |
430 int32_t bitstream_buffer_id = bitstream_buffer_manager_->DequeueBuffer(); | |
431 | |
432 ppapi::MediaStreamBuffer* audio_buffer = | |
433 audio_buffer_manager_->GetBufferPointer(audio_buffer_id); | |
434 uint8_t* bitstream_data = reinterpret_cast<uint8_t*>( | |
435 bitstream_buffer_manager_->GetBufferPointer(bitstream_buffer_id)); | |
436 | |
437 media_task_runner_->PostTask( | |
438 FROM_HERE, | |
439 base::Bind(&AudioEncoderImpl::Encode, base::Unretained(encoder_.get()), | |
440 static_cast<uint8_t*>(audio_buffer->audio.data), | |
441 audio_buffer->audio.data_size, bitstream_data, | |
442 bitstream_buffer_manager_->buffer_size(), | |
443 media::BindToCurrentLoop( | |
444 base::Bind(&PepperAudioEncoderHost::BitstreamBufferReady, | |
445 weak_ptr_factory_.GetWeakPtr(), audio_buffer_id, | |
446 bitstream_buffer_id)))); | |
447 } | |
448 | |
449 void PepperAudioEncoderHost::BitstreamBufferReady(int32_t audio_buffer_id, | |
450 int32_t bitstream_buffer_id, | |
451 int32_t size) { | |
452 DCHECK(RenderThreadImpl::current()); | |
453 | |
454 if (encoder_last_error_) | |
455 return; | |
456 | |
457 host()->SendUnsolicitedReply( | |
458 pp_resource(), PpapiPluginMsg_AudioEncoder_EncodeReply(audio_buffer_id)); | |
459 | |
460 if (size < 0) { | |
461 NotifyPepperError(PP_ERROR_FAILED); | |
462 return; | |
463 } | |
464 | |
465 host()->SendUnsolicitedReply( | |
466 pp_resource(), PpapiPluginMsg_AudioEncoder_BitstreamBufferReady( | |
467 bitstream_buffer_id, static_cast<uint32_t>(size))); | |
468 } | |
469 | |
470 void PepperAudioEncoderHost::NotifyPepperError(int32_t error) { | |
471 DCHECK(RenderThreadImpl::current()); | |
472 | |
473 encoder_last_error_ = error; | |
474 Close(); | |
475 host()->SendUnsolicitedReply( | |
476 pp_resource(), | |
477 PpapiPluginMsg_AudioEncoder_NotifyError(encoder_last_error_)); | |
478 } | |
479 | |
480 void PepperAudioEncoderHost::Close() { | |
481 DCHECK(RenderThreadImpl::current()); | |
482 | |
483 // Destroy the encoder and the audio/bitstream buffers on the media thread | |
484 // to avoid freeing memory the encoder might still be working on. | |
485 media_task_runner_->PostTask( | |
486 FROM_HERE, base::Bind(&StopAudioEncoder, base::Passed(encoder_.Pass()), | |
487 base::Passed(audio_buffer_manager_.Pass()), | |
488 base::Passed(bitstream_buffer_manager_.Pass()))); | |
489 } | |
490 | |
491 } // namespace content | |
OLD | NEW |