Index: content/renderer/pepper/pepper_audio_encoder_host.cc |
diff --git a/content/renderer/pepper/pepper_audio_encoder_host.cc b/content/renderer/pepper/pepper_audio_encoder_host.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2bb702610148bb26b9c93517ef84a6a2195b60e4 |
--- /dev/null |
+++ b/content/renderer/pepper/pepper_audio_encoder_host.cc |
@@ -0,0 +1,576 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/bind.h" |
+#include "base/memory/shared_memory.h" |
+#include "base/numerics/safe_math.h" |
+#include "content/public/renderer/renderer_ppapi_host.h" |
+#include "content/renderer/pepper/host_globals.h" |
+#include "content/renderer/pepper/pepper_audio_encoder_host.h" |
+#include "content/renderer/render_thread_impl.h" |
+#include "media/base/bind_to_current_loop.h" |
+#include "ppapi/c/pp_codecs.h" |
+#include "ppapi/c/pp_errors.h" |
+#include "ppapi/host/dispatch_host_message.h" |
+#include "ppapi/host/ppapi_host.h" |
+#include "ppapi/proxy/ppapi_messages.h" |
+#include "ppapi/shared_impl/media_stream_buffer.h" |
+#include "third_party/opus/src/include/opus.h" |
+ |
+using ppapi::proxy::SerializedHandle; |
+ |
+namespace content { |
+ |
+namespace { |
+ |
+// Buffer up to 150ms (15 x 10ms per frame). |
+const uint32_t kDefaultNumberOfAudioBuffers = 15; |
+ |
+// Class used to pass audio data between the different threads. |
bbudge
2015/11/04 00:59:59
nit: s/the different//
llandwerlin-old
2015/11/04 14:44:27
Done.
|
+class AudioData { |
+ public: |
+ AudioData(const AudioData& other) : data(other.data), size(other.size) {} |
+ AudioData(uint8_t* data, size_t size) : data(data), size(size) {} |
+ ~AudioData() {} |
+ |
+ uint8_t* data; |
+ size_t size; |
+}; |
+ |
+using BitstreamBufferReadyCB = base::Callback<void(int32_t size)>; |
+ |
+bool PP_HardwareAccelerationCompatible(bool accelerated, |
+ PP_HardwareAcceleration requested) { |
+ switch (requested) { |
+ case PP_HARDWAREACCELERATION_ONLY: |
+ return accelerated; |
+ case PP_HARDWAREACCELERATION_NONE: |
+ return !accelerated; |
+ case PP_HARDWAREACCELERATION_WITHFALLBACK: |
+ return true; |
+ // No default case, to catch unhandled PP_HardwareAcceleration values. |
+ } |
+ return false; |
+} |
+ |
+base::CheckedNumeric<size_t> GetAudioBufferSize( |
+ const ppapi::proxy::PPB_AudioEncodeParameters& audio_parameters, |
+ uint32_t number_of_samples) { |
+ base::CheckedNumeric<size_t> buffer_size = number_of_samples; |
+ buffer_size *= audio_parameters.channels; |
+ buffer_size *= audio_parameters.input_sample_size; |
+ |
+ return buffer_size; |
+} |
+ |
+AudioData GetAudioDataFromAudioBufferManager( |
+ ppapi::MediaStreamBufferManager* buffer_manager, |
+ uint32_t* buffer_id) { |
+ DCHECK(buffer_manager->HasAvailableBuffer()); |
+ int32_t id = buffer_manager->DequeueBuffer(); |
+ ppapi::MediaStreamBuffer* buffer = buffer_manager->GetBufferPointer(id); |
+ *buffer_id = id; |
+ return AudioData(static_cast<uint8_t*>(buffer->audio.data), |
+ buffer->audio.data_size); |
+} |
+ |
+AudioData GetBitstreamDataFromAudioBufferManager( |
bbudge
2015/11/04 00:59:59
s/GetBitstreamDataFromAudioBufferManager/GetBitstr
llandwerlin-old
2015/11/04 14:44:27
Done.
|
+ ppapi::MediaStreamBufferManager* buffer_manager, |
+ uint32_t* buffer_id) { |
+ DCHECK(buffer_manager->HasAvailableBuffer()); |
+ int32_t id = buffer_manager->DequeueBuffer(); |
+ uint8_t* buffer = |
+ reinterpret_cast<uint8_t*>(buffer_manager->GetBufferPointer(id)); |
+ *buffer_id = id; |
+ return AudioData(buffer, buffer_manager->buffer_size()); |
+} |
+ |
+} // namespace |
+ |
+// This class should be constructed, used, and destructed on the main |
+// (renderer) thread. |
+class PepperAudioEncoderHost::AudioEncoderImpl { |
+ public: |
+ // Callback used to signal encoded data. If |size| is negative, an error |
+ // occured. |
+ |
+ AudioEncoderImpl(); |
+ ~AudioEncoderImpl(); |
+ |
+ std::vector<PP_AudioProfileDescription> GetSupportedProfiles(); |
+ bool Initialize(const ppapi::proxy::PPB_AudioEncodeParameters& parameters); |
+ int32_t GetNumberOfSamplesPerFrame(); |
+ void Encode(const AudioData& input, |
+ const AudioData& output, |
+ BitstreamBufferReadyCB callback); |
+ void RequestBitrateChange(uint32_t bitrate); |
+ |
+ private: |
+ void DoEncode(const AudioData& input, |
+ const AudioData& output, |
+ BitstreamBufferReadyCB callback); |
+ void DoRequestBitrateChange(uint32_t bitrate); |
+ void OnEncodeDone(BitstreamBufferReadyCB callback, int32_t output_size); |
+ void Stop(base::WaitableEvent* event); |
+ |
+ // Task calling from/into PepperAudioEncoderHost. |
+ scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; |
+ |
+ // Task doing the encoding. |
+ scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_; |
+ |
+ // Initialization parameters. |
+ ppapi::proxy::PPB_AudioEncodeParameters parameters_; |
+ |
+ scoped_ptr<uint8[]> encoder_memory_; |
+ OpusEncoder* opus_encoder_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(AudioEncoderImpl); |
+}; |
+ |
+PepperAudioEncoderHost::AudioEncoderImpl::AudioEncoderImpl() |
+ : main_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
+ media_task_runner_(RenderThreadImpl::current() |
+ ->GetMediaThreadTaskRunner()) {} |
+ |
+PepperAudioEncoderHost::AudioEncoderImpl::~AudioEncoderImpl() { |
+ if (encoder_memory_) { |
+ base::WaitableEvent event(false, false); |
+ media_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&AudioEncoderImpl::Stop, base::Unretained(this), &event)); |
+ event.Wait(); |
+ } |
+} |
+ |
+std::vector<PP_AudioProfileDescription> |
+PepperAudioEncoderHost::AudioEncoderImpl::GetSupportedProfiles() { |
+ std::vector<PP_AudioProfileDescription> profiles; |
+ static const uint32_t sampling_rates[] = {8000, 12000, 16000, 24000, 48000}; |
+ |
+ for (uint32_t i = 0; i < arraysize(sampling_rates); ++i) { |
+ PP_AudioProfileDescription profile; |
+ profile.profile = PP_AUDIOPROFILE_OPUS; |
+ profile.max_channels = 2; |
+ profile.sample_size = PP_AUDIOBUFFER_SAMPLESIZE_16_BITS; |
+ profile.sample_rate = sampling_rates[i]; |
+ profile.hardware_accelerated = PP_FALSE; |
+ profiles.push_back(profile); |
+ } |
+ return profiles; |
+} |
+ |
+bool PepperAudioEncoderHost::AudioEncoderImpl::Initialize( |
+ const ppapi::proxy::PPB_AudioEncodeParameters& parameters) { |
+ if (parameters.output_profile != PP_AUDIOPROFILE_OPUS) |
+ return false; |
+ |
+ DCHECK(!encoder_memory_); |
+ |
+ int32_t encoder_size = opus_encoder_get_size(parameters.channels); |
+ if (encoder_size < 1) |
+ return false; |
+ |
+ encoder_memory_.reset(new uint8[encoder_size]); |
+ opus_encoder_ = reinterpret_cast<OpusEncoder*>(encoder_memory_.get()); |
+ |
+ if (opus_encoder_init(opus_encoder_, parameters.input_sample_rate, |
+ parameters.channels, OPUS_APPLICATION_AUDIO) != OPUS_OK) |
+ return false; |
+ |
+ if (opus_encoder_ctl(opus_encoder_, |
+ OPUS_SET_BITRATE(parameters.initial_bitrate <= 0 |
+ ? OPUS_AUTO |
+ : parameters.initial_bitrate)) != |
+ OPUS_OK) |
+ return false; |
+ |
+ parameters_ = parameters; |
+ |
+ return true; |
+} |
+ |
+int32_t PepperAudioEncoderHost::AudioEncoderImpl::GetNumberOfSamplesPerFrame() { |
+ // Opus supports 2.5, 5, 10, 20, 40 or 60ms audio frames. We take |
+ // 10ms by default. |
+ return parameters_.input_sample_rate / 100; |
+} |
+ |
+void PepperAudioEncoderHost::AudioEncoderImpl::Encode( |
+ const AudioData& input, |
+ const AudioData& output, |
+ BitstreamBufferReadyCB callback) { |
+ media_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&AudioEncoderImpl::DoEncode, base::Unretained(this), input, |
+ output, base::Bind(&AudioEncoderImpl::OnEncodeDone, |
+ base::Unretained(this), callback))); |
+} |
+ |
+void PepperAudioEncoderHost::AudioEncoderImpl::DoEncode( |
+ const AudioData& input, |
+ const AudioData& output, |
+ BitstreamBufferReadyCB callback) { |
+ int32_t result = opus_encode( |
+ opus_encoder_, reinterpret_cast<opus_int16*>(input.data), |
+ (input.size / parameters_.channels) / parameters_.input_sample_size, |
+ output.data, output.size); |
+ main_task_runner_->PostTask( |
+ FROM_HERE, base::Bind(&AudioEncoderImpl::OnEncodeDone, |
+ base::Unretained(this), callback, result)); |
+} |
+ |
+void PepperAudioEncoderHost::AudioEncoderImpl::RequestBitrateChange( |
+ uint32_t bitrate) { |
+ media_task_runner_->PostTask( |
+ FROM_HERE, base::Bind(&AudioEncoderImpl::RequestBitrateChange, |
+ base::Unretained(this), bitrate)); |
+} |
+ |
+void PepperAudioEncoderHost::AudioEncoderImpl::DoRequestBitrateChange( |
+ uint32_t bitrate) { |
+ opus_encoder_ctl(opus_encoder_, OPUS_SET_BITRATE(bitrate)); |
+} |
+ |
+void PepperAudioEncoderHost::AudioEncoderImpl::OnEncodeDone( |
+ BitstreamBufferReadyCB callback, |
+ int32_t size) { |
+ callback.Run(size); |
+} |
+ |
+void PepperAudioEncoderHost::AudioEncoderImpl::Stop( |
+ base::WaitableEvent* event) { |
+ event->Signal(); |
+} |
+ |
+PepperAudioEncoderHost::PepperAudioEncoderHost(RendererPpapiHost* host, |
+ PP_Instance instance, |
+ PP_Resource resource) |
+ : ResourceHost(host->GetPpapiHost(), instance, resource), |
+ renderer_ppapi_host_(host), |
+ initialized_(false), |
+ encoder_last_error_(PP_ERROR_FAILED), |
+ audio_buffer_manager_(this), |
+ bitstream_buffer_manager_(this) {} |
+ |
+PepperAudioEncoderHost::~PepperAudioEncoderHost() { |
+ Close(); |
+} |
+ |
+int32_t PepperAudioEncoderHost::OnResourceMessageReceived( |
+ const IPC::Message& msg, |
+ ppapi::host::HostMessageContext* context) { |
+ PPAPI_BEGIN_MESSAGE_MAP(PepperAudioEncoderHost, msg) |
+ PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( |
+ PpapiHostMsg_AudioEncoder_GetSupportedProfiles, |
+ OnHostMsgGetSupportedProfiles) |
+ PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_AudioEncoder_Initialize, |
+ OnHostMsgInitialize) |
+ PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( |
+ PpapiHostMsg_AudioEncoder_GetAudioBuffers, OnHostMsgGetAudioBuffers) |
+ PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_AudioEncoder_Encode, |
+ OnHostMsgEncode) |
+ PPAPI_DISPATCH_HOST_RESOURCE_CALL( |
+ PpapiHostMsg_AudioEncoder_RecycleBitstreamBuffer, |
+ OnHostMsgRecycleBitstreamBuffer) |
+ PPAPI_DISPATCH_HOST_RESOURCE_CALL( |
+ PpapiHostMsg_AudioEncoder_RequestBitrateChange, |
+ OnHostMsgRequestBitrateChange) |
+ PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_AudioEncoder_Close, |
+ OnHostMsgClose) |
+ PPAPI_END_MESSAGE_MAP() |
+ return PP_ERROR_FAILED; |
+} |
+ |
+int32_t PepperAudioEncoderHost::OnHostMsgGetSupportedProfiles( |
+ ppapi::host::HostMessageContext* context) { |
+ std::vector<PP_AudioProfileDescription> profiles; |
+ GetSupportedProfiles(&profiles); |
+ |
+ host()->SendReply( |
+ context->MakeReplyMessageContext(), |
+ PpapiPluginMsg_AudioEncoder_GetSupportedProfilesReply(profiles)); |
+ |
+ return PP_OK_COMPLETIONPENDING; |
+} |
+ |
+int32_t PepperAudioEncoderHost::OnHostMsgInitialize( |
+ ppapi::host::HostMessageContext* context, |
+ const ppapi::proxy::PPB_AudioEncodeParameters& parameters) { |
+ if (initialized_) |
+ return PP_ERROR_FAILED; |
+ |
+ if (!IsInitializationValid(parameters)) |
+ return PP_ERROR_NOTSUPPORTED; |
+ |
+ encode_parameters_ = parameters; |
+ |
+ int32_t error = PP_ERROR_FAILED; |
+ if (parameters.acceleration == PP_HARDWAREACCELERATION_NONE || |
+ parameters.acceleration == PP_HARDWAREACCELERATION_WITHFALLBACK) { |
+ encoder_.reset(new AudioEncoderImpl); |
+ if (encoder_->Initialize(parameters)) { |
bbudge
2015/11/04 00:59:59
It's unfortunate that you validate 'parameters' af
llandwerlin-old
2015/11/04 14:44:27
The parameters are validated above line 304, those
bbudge
2015/11/10 00:08:25
OK, I missed that the parameters have to match a s
|
+ base::CheckedNumeric<size_t> bitstream_buffer_size = GetAudioBufferSize( |
+ parameters, encoder_->GetNumberOfSamplesPerFrame()); |
+ bitstream_buffer_size *= 2; |
+ if (bitstream_buffer_size.IsValid() && |
+ AllocateBitstreamBuffers(bitstream_buffer_size.ValueOrDie())) { |
+ initialized_ = true; |
+ encoder_last_error_ = PP_OK; |
+ |
+ ppapi::host::ReplyMessageContext reply_context = |
+ context->MakeReplyMessageContext(); |
+ reply_context.params.AppendHandle(SerializedHandle( |
+ renderer_ppapi_host_->ShareSharedMemoryHandleWithRemote( |
+ bitstream_buffer_manager_.shm()->handle()), |
+ bitstream_buffer_manager_.shm()->mapped_size())); |
+ host()->SendReply(reply_context, |
+ PpapiPluginMsg_AudioEncoder_InitializeReply( |
+ encoder_->GetNumberOfSamplesPerFrame(), |
+ bitstream_buffer_manager_.number_of_buffers(), |
+ bitstream_buffer_manager_.buffer_size())); |
+ |
+ return PP_OK_COMPLETIONPENDING; |
+ } |
+ error = PP_ERROR_NOMEMORY; |
+ } else |
+ error = PP_ERROR_FAILED; |
+ } |
+ |
+ encoder_ = nullptr; |
+ |
+ return error; |
+} |
+ |
+int32_t PepperAudioEncoderHost::OnHostMsgGetAudioBuffers( |
+ ppapi::host::HostMessageContext* context) { |
+ if (encoder_last_error_) |
+ return encoder_last_error_; |
+ |
bbudge
2015/11/04 00:59:59
You should probably detect when this has been call
llandwerlin-old
2015/11/04 14:44:27
Done.
|
+ if (!AllocateAudioBuffers(encoder_->GetNumberOfSamplesPerFrame())) |
+ return PP_ERROR_NOMEMORY; |
+ |
+ ppapi::host::ReplyMessageContext reply_context = |
+ context->MakeReplyMessageContext(); |
+ reply_context.params.AppendHandle( |
+ SerializedHandle(renderer_ppapi_host_->ShareSharedMemoryHandleWithRemote( |
+ audio_buffer_manager_.shm()->handle()), |
+ audio_buffer_manager_.shm()->mapped_size())); |
+ |
+ host()->SendReply(reply_context, |
+ PpapiPluginMsg_AudioEncoder_GetAudioBuffersReply( |
+ audio_buffer_manager_.number_of_buffers(), |
+ audio_buffer_manager_.buffer_size())); |
+ |
+ return PP_OK_COMPLETIONPENDING; |
+} |
+ |
+int32_t PepperAudioEncoderHost::OnHostMsgEncode( |
+ ppapi::host::HostMessageContext* context, |
+ uint32_t buffer_id) { |
+ if (encoder_last_error_) |
+ return encoder_last_error_; |
+ |
+ if (buffer_id >= |
+ static_cast<uint32_t>(audio_buffer_manager_.number_of_buffers())) |
+ return PP_ERROR_BADARGUMENT; |
+ |
+ audio_buffer_manager_.EnqueueBuffer(buffer_id); |
+ |
+ DoEncode(); |
+ |
+ return PP_OK_COMPLETIONPENDING; |
+} |
+ |
+int32_t PepperAudioEncoderHost::OnHostMsgRecycleBitstreamBuffer( |
+ ppapi::host::HostMessageContext* context, |
+ uint32_t buffer_id) { |
+ if (encoder_last_error_) |
+ return encoder_last_error_; |
+ |
+ if (buffer_id >= |
+ static_cast<uint32_t>(bitstream_buffer_manager_.number_of_buffers())) |
+ return PP_ERROR_BADARGUMENT; |
+ |
+ bitstream_buffer_manager_.EnqueueBuffer(buffer_id); |
+ |
+ DoEncode(); |
+ |
+ return PP_OK; |
+} |
+ |
+int32_t PepperAudioEncoderHost::OnHostMsgRequestBitrateChange( |
+ ppapi::host::HostMessageContext* context, |
+ uint32_t bitrate) { |
+ if (encoder_last_error_) |
+ return encoder_last_error_; |
+ |
+ encoder_->RequestBitrateChange(bitrate); |
+ |
+ return PP_OK; |
+} |
+ |
+int32_t PepperAudioEncoderHost::OnHostMsgClose( |
+ ppapi::host::HostMessageContext* context) { |
+ encoder_last_error_ = PP_ERROR_FAILED; |
+ Close(); |
+ |
+ return PP_OK; |
+} |
+ |
+void PepperAudioEncoderHost::GetSupportedProfiles( |
+ std::vector<PP_AudioProfileDescription>* profiles) { |
+ DCHECK(RenderThreadImpl::current()); |
+ |
+ AudioEncoderImpl software_encoder; |
+ *profiles = software_encoder.GetSupportedProfiles(); |
+} |
+ |
+bool PepperAudioEncoderHost::IsInitializationValid( |
+ const ppapi::proxy::PPB_AudioEncodeParameters& parameters) { |
+ DCHECK(RenderThreadImpl::current()); |
+ |
+ std::vector<PP_AudioProfileDescription> profiles; |
+ GetSupportedProfiles(&profiles); |
+ |
+ for (const PP_AudioProfileDescription& profile : profiles) { |
+ if (parameters.output_profile == profile.profile && |
+ parameters.input_sample_size == profile.sample_size && |
+ parameters.input_sample_rate == profile.sample_rate && |
+ parameters.channels <= profile.max_channels && |
+ PP_HardwareAccelerationCompatible( |
+ profile.hardware_accelerated == PP_TRUE ? true : false, |
+ parameters.acceleration)) |
+ return true; |
+ } |
+ |
+ return false; |
+} |
+ |
+bool PepperAudioEncoderHost::AllocateAudioBuffers(uint32_t number_of_samples) { |
+ DCHECK(RenderThreadImpl::current()); |
+ DCHECK(initialized_); |
+ |
+ // Buffers have already been allocated. |
+ if (audio_buffer_manager_.number_of_buffers() > 0) |
+ return false; |
bbudge
2015/11/04 00:59:59
remove this check (see comment at call site above.
llandwerlin-old
2015/11/04 14:44:27
Done.
|
+ |
+ base::CheckedNumeric<size_t> buffer_size = |
+ GetAudioBufferSize(encode_parameters_, number_of_samples); |
+ if (!buffer_size.IsValid()) |
+ return false; |
+ |
+ base::CheckedNumeric<size_t> total_buffer_size = buffer_size.ValueOrDie(); |
+ total_buffer_size += sizeof(ppapi::MediaStreamBuffer::Audio); |
+ if (!total_buffer_size.IsValid()) |
+ return false; |
+ |
+ base::CheckedNumeric<size_t> total_memory_size = |
+ total_buffer_size.ValueOrDie(); |
+ total_memory_size *= kDefaultNumberOfAudioBuffers; |
+ if (!total_memory_size.IsValid()) |
+ return false; |
+ |
+ if (!audio_buffer_manager_.SetBuffers( |
+ kDefaultNumberOfAudioBuffers, total_buffer_size.ValueOrDie(), |
+ RenderThreadImpl::current() |
+ ->HostAllocateSharedMemoryBuffer(total_memory_size.ValueOrDie()) |
+ .Pass(), |
+ false)) |
+ return false; |
+ |
+ for (int32_t i = 0; i < audio_buffer_manager_.number_of_buffers(); ++i) { |
+ ppapi::MediaStreamBuffer::Audio* buffer = |
+ &(audio_buffer_manager_.GetBufferPointer(i)->audio); |
+ buffer->header.size = total_buffer_size.ValueOrDie(); |
+ buffer->header.type = ppapi::MediaStreamBuffer::TYPE_AUDIO; |
+ buffer->sample_rate = static_cast<PP_AudioBuffer_SampleRate>( |
+ encode_parameters_.input_sample_rate); |
+ buffer->number_of_channels = encode_parameters_.channels; |
+ buffer->number_of_samples = number_of_samples; |
+ buffer->data_size = buffer_size.ValueOrDie(); |
+ } |
+ |
+ return true; |
+} |
+ |
+bool PepperAudioEncoderHost::AllocateBitstreamBuffers(size_t buffer_size) { |
bbudge
2015/11/04 00:59:59
If you follow some of my other suggestions for the
|
+ DCHECK(RenderThreadImpl::current()); |
+ // We assume AllocateBitstreamBuffers is only called once. |
+ DCHECK(!initialized_); |
+ |
+ // Buffers have already been allocated. |
+ if (bitstream_buffer_manager_.number_of_buffers() > 0) |
+ return false; |
+ |
+ base::CheckedNumeric<size_t> total_size = buffer_size; |
+ total_size *= kDefaultNumberOfAudioBuffers; |
+ if (!total_size.IsValid()) |
+ return false; |
+ |
+ if (!bitstream_buffer_manager_.SetBuffers( |
+ kDefaultNumberOfAudioBuffers, buffer_size, |
+ RenderThreadImpl::current() |
+ ->HostAllocateSharedMemoryBuffer(total_size.ValueOrDie()) |
+ .Pass(), |
+ true)) |
+ return false; |
+ |
+ return true; |
+} |
+ |
+void PepperAudioEncoderHost::DoEncode() { |
+ if (!audio_buffer_manager_.HasAvailableBuffer() || |
+ !bitstream_buffer_manager_.HasAvailableBuffer()) |
+ return; |
+ |
+ uint32_t audio_buffer_id, bitstream_buffer_id; |
+ AudioData input(GetAudioDataFromAudioBufferManager(&audio_buffer_manager_, |
+ &audio_buffer_id)); |
+ AudioData output(GetBitstreamDataFromAudioBufferManager( |
+ &bitstream_buffer_manager_, &bitstream_buffer_id)); |
+ encoder_->Encode( |
+ input, output, |
+ base::Bind(&PepperAudioEncoderHost::BitstreamBufferReady, |
+ base::Unretained(this), audio_buffer_id, bitstream_buffer_id)); |
+} |
+ |
+void PepperAudioEncoderHost::BitstreamBufferReady(uint32_t audio_buffer_id, |
+ uint32_t bitstream_buffer_id, |
+ int32_t size) { |
+ DCHECK(RenderThreadImpl::current()); |
+ |
+ if (encoder_last_error_) |
+ return; |
+ |
+ host()->SendUnsolicitedReply( |
+ pp_resource(), PpapiPluginMsg_AudioEncoder_EncodeReply(audio_buffer_id)); |
+ |
+ if (size < 0) { |
+ NotifyPepperError(PP_ERROR_FAILED); |
+ return; |
+ } |
+ |
+ host()->SendUnsolicitedReply( |
+ pp_resource(), PpapiPluginMsg_AudioEncoder_BitstreamBufferReady( |
+ bitstream_buffer_id, static_cast<uint32_t>(size))); |
+} |
+ |
+void PepperAudioEncoderHost::NotifyPepperError(int32_t error) { |
+ DCHECK(RenderThreadImpl::current()); |
+ |
+ encoder_last_error_ = error; |
+ Close(); |
+ host()->SendUnsolicitedReply( |
+ pp_resource(), |
+ PpapiPluginMsg_AudioEncoder_NotifyError(encoder_last_error_)); |
+} |
+ |
+void PepperAudioEncoderHost::Close() { |
+ DCHECK(RenderThreadImpl::current()); |
+ |
+ encoder_ = nullptr; |
+} |
+ |
+} // namespace content |