Chromium Code Reviews| Index: ppapi/proxy/audio_encoder_resource.cc |
| diff --git a/ppapi/proxy/audio_encoder_resource.cc b/ppapi/proxy/audio_encoder_resource.cc |
| index d1576fb616b6dadd2d7ed8d72fb00fb525be23ed..4b06eebac67aa3d99e8208a87c13709ab487d8c0 100644 |
| --- a/ppapi/proxy/audio_encoder_resource.cc |
| +++ b/ppapi/proxy/audio_encoder_resource.cc |
| @@ -2,14 +2,40 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| +#include "base/memory/shared_memory.h" |
| +#include "ppapi/c/pp_array_output.h" |
| +#include "ppapi/c/pp_codecs.h" |
| +#include "ppapi/proxy/audio_buffer_resource.h" |
| #include "ppapi/proxy/audio_encoder_resource.h" |
| +#include "ppapi/proxy/ppapi_messages.h" |
| +#include "ppapi/shared_impl/array_writer.h" |
| +#include "ppapi/shared_impl/media_stream_buffer.h" |
| +#include "ppapi/thunk/enter.h" |
| namespace ppapi { |
| namespace proxy { |
| +namespace { |
| + |
| +void RunCallback(scoped_refptr<TrackedCallback>* callback, int32_t error) { |
| + if (TrackedCallback::IsPending(*callback)) { |
| + scoped_refptr<TrackedCallback> temp; |
| + callback->swap(temp); |
| + temp->Run(error); |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| AudioEncoderResource::AudioEncoderResource(Connection connection, |
| PP_Instance instance) |
| - : PluginResource(connection, instance) { |
| + : PluginResource(connection, instance), |
| + encoder_last_error_(PP_ERROR_FAILED), |
| + initialized_(false), |
| + closed_(false), |
|
bbudge
2015/11/17 00:53:40
initialize number_of_samples_ and get_buffer_data_
bbudge
2015/11/17 00:53:40
closed_ is written but never read, so you can remo
|
| + audio_buffer_manager_(this), |
| + bitstream_buffer_manager_(this) { |
| + SendCreate(RENDERER, PpapiHostMsg_AudioEncoder_Create()); |
| } |
| AudioEncoderResource::~AudioEncoderResource() { |
| @@ -22,7 +48,15 @@ thunk::PPB_AudioEncoder_API* AudioEncoderResource::AsPPB_AudioEncoder_API() { |
| int32_t AudioEncoderResource::GetSupportedProfiles( |
| const PP_ArrayOutput& output, |
| const scoped_refptr<TrackedCallback>& callback) { |
| - return PP_ERROR_NOTSUPPORTED; |
| + if (TrackedCallback::IsPending(get_supported_profiles_callback_)) |
| + return PP_ERROR_INPROGRESS; |
| + |
| + get_supported_profiles_callback_ = callback; |
| + Call<PpapiPluginMsg_AudioEncoder_GetSupportedProfilesReply>( |
| + RENDERER, PpapiHostMsg_AudioEncoder_GetSupportedProfiles(), |
| + base::Bind(&AudioEncoderResource::OnPluginMsgGetSupportedProfilesReply, |
| + this, output)); |
| + return PP_OK_COMPLETIONPENDING; |
| } |
| int32_t AudioEncoderResource::Initialize( |
| @@ -33,39 +67,320 @@ int32_t AudioEncoderResource::Initialize( |
| uint32_t initial_bitrate, |
| PP_HardwareAcceleration acceleration, |
| const scoped_refptr<TrackedCallback>& callback) { |
| - return PP_ERROR_NOTSUPPORTED; |
| + if (initialized_) |
| + return PP_ERROR_FAILED; |
| + if (TrackedCallback::IsPending(initialize_callback_)) |
| + return PP_ERROR_INPROGRESS; |
| + |
| + initialize_callback_ = callback; |
| + |
| + PPB_AudioEncodeParameters parameters; |
| + parameters.channels = channels; |
| + parameters.input_sample_rate = input_sample_rate; |
| + parameters.input_sample_size = input_sample_size; |
| + parameters.output_profile = output_profile; |
| + parameters.initial_bitrate = initial_bitrate; |
| + parameters.acceleration = acceleration; |
| + |
| + Call<PpapiPluginMsg_AudioEncoder_InitializeReply>( |
| + RENDERER, PpapiHostMsg_AudioEncoder_Initialize(parameters), |
| + base::Bind(&AudioEncoderResource::OnPluginMsgInitializeReply, this)); |
| + return PP_OK_COMPLETIONPENDING; |
| } |
| int32_t AudioEncoderResource::GetNumberOfSamples() { |
| - return PP_ERROR_NOTSUPPORTED; |
| + if (encoder_last_error_) |
| + return encoder_last_error_; |
| + return number_of_samples_; |
| } |
| int32_t AudioEncoderResource::GetBuffer( |
| PP_Resource* audio_buffer, |
| const scoped_refptr<TrackedCallback>& callback) { |
| - return PP_ERROR_NOTSUPPORTED; |
| + if (encoder_last_error_) |
| + return encoder_last_error_; |
| + if (TrackedCallback::IsPending(get_buffer_callback_)) |
| + return PP_ERROR_INPROGRESS; |
| + |
| + get_buffer_data_ = audio_buffer; |
| + get_buffer_callback_ = callback; |
| + |
| + TryWriteAudioBuffer(); |
| + |
| + return PP_OK_COMPLETIONPENDING; |
| } |
| int32_t AudioEncoderResource::Encode( |
| PP_Resource audio_buffer, |
| const scoped_refptr<TrackedCallback>& callback) { |
| - return PP_ERROR_NOTSUPPORTED; |
| + if (encoder_last_error_) |
| + return encoder_last_error_; |
| + |
| + AudioBufferMap::iterator it = audio_buffers_.find(audio_buffer); |
| + if (it == audio_buffers_.end()) |
| + // TODO(llandwerlin): accept MediaStreamAudioTrack's audio buffers. |
| + return PP_ERROR_BADRESOURCE; |
| + |
| + scoped_refptr<AudioBufferResource> buffer_resource = it->second; |
| + |
| + encode_callbacks_.insert( |
| + std::make_pair(buffer_resource->GetBufferIndex(), callback)); |
| + |
| + Post(RENDERER, |
| + PpapiHostMsg_AudioEncoder_Encode(buffer_resource->GetBufferIndex())); |
| + |
| + // Invalidate the buffer to prevent the plugin from modifying it. |
| + buffer_resource->Invalidate(); |
|
bbudge
2015/11/17 00:53:40
The real reason to call Invalidate() is so the res
llandwerlin-old
2015/11/17 15:22:30
Done.
|
| + audio_buffers_.erase(it); |
| + |
| + return PP_OK_COMPLETIONPENDING; |
| } |
| int32_t AudioEncoderResource::GetBitstreamBuffer( |
| PP_AudioBitstreamBuffer* bitstream_buffer, |
| const scoped_refptr<TrackedCallback>& callback) { |
| - return PP_ERROR_NOTSUPPORTED; |
| + if (encoder_last_error_) |
| + return encoder_last_error_; |
| + if (TrackedCallback::IsPending(get_bitstream_buffer_callback_)) |
| + return PP_ERROR_INPROGRESS; |
| + |
| + get_bitstream_buffer_callback_ = callback; |
| + get_bitstream_buffer_data_ = bitstream_buffer; |
| + |
| + TryWriteBitstreamBuffer(); |
| + |
| + return PP_OK_COMPLETIONPENDING; |
| } |
| void AudioEncoderResource::RecycleBitstreamBuffer( |
| const PP_AudioBitstreamBuffer* bitstream_buffer) { |
| + if (encoder_last_error_) |
| + return; |
| + |
| + BufferMap::const_iterator it = |
| + bitstream_buffer_map_.find(bitstream_buffer->buffer); |
| + if (it != bitstream_buffer_map_.end()) |
| + Post(RENDERER, |
| + PpapiHostMsg_AudioEncoder_RecycleBitstreamBuffer(it->second)); |
| } |
| void AudioEncoderResource::RequestBitrateChange(uint32_t bitrate) { |
| + if (encoder_last_error_) |
| + return; |
| + Post(RENDERER, PpapiHostMsg_AudioEncoder_RequestBitrateChange(bitrate)); |
| } |
| void AudioEncoderResource::Close() { |
| + if (encoder_last_error_) |
| + return; |
| + Post(RENDERER, PpapiHostMsg_AudioEncoder_Close()); |
| + closed_ = true; |
| + if (!encoder_last_error_ || !initialized_) |
| + NotifyError(PP_ERROR_ABORTED); |
| + ReleaseBuffers(); |
| +} |
| + |
| +void AudioEncoderResource::OnReplyReceived( |
| + const ResourceMessageReplyParams& params, |
| + const IPC::Message& msg) { |
| + PPAPI_BEGIN_MESSAGE_MAP(AudioEncoderResource, msg) |
| + PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL( |
| + PpapiPluginMsg_AudioEncoder_BitstreamBufferReady, |
| + OnPluginMsgBitstreamBufferReady) |
| + PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(PpapiPluginMsg_AudioEncoder_EncodeReply, |
| + OnPluginMsgEncodeReply) |
| + PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(PpapiPluginMsg_AudioEncoder_NotifyError, |
| + OnPluginMsgNotifyError) |
| + PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED( |
| + PluginResource::OnReplyReceived(params, msg)) |
| + PPAPI_END_MESSAGE_MAP() |
| +} |
| + |
| +void AudioEncoderResource::OnPluginMsgGetSupportedProfilesReply( |
| + const PP_ArrayOutput& output, |
| + const ResourceMessageReplyParams& params, |
| + const std::vector<PP_AudioProfileDescription>& profiles) { |
| + int32_t error = params.result(); |
| + if (error) { |
| + NotifyError(error); |
|
bbudge
2015/11/17 00:53:40
It seems wrong to NotifyError in this case.
llandwerlin-old
2015/11/17 15:22:31
It seems that if the host is not able to answer th
|
| + return; |
| + } |
| + |
| + ArrayWriter writer(output); |
| + if (!writer.is_valid()) { |
| + RunCallback(&get_supported_profiles_callback_, PP_ERROR_BADARGUMENT); |
|
bbudge
2015/11/17 00:53:40
In this case, we usually return PP_ERROR_FAILED si
llandwerlin-old
2015/11/17 15:22:30
Done.
|
| + return; |
| + } |
| + |
| + bool write_result = writer.StoreVector(profiles); |
| + |
| + if (!write_result) { |
| + RunCallback(&get_supported_profiles_callback_, PP_ERROR_FAILED); |
| + return; |
| + } |
|
bbudge
2015/11/17 00:53:40
You could eliminate some duplication like this:
llandwerlin-old
2015/11/17 15:22:31
Done.
|
| + |
| + RunCallback(&get_supported_profiles_callback_, |
| + base::checked_cast<int32_t>(profiles.size())); |
| +} |
| + |
| +void AudioEncoderResource::OnPluginMsgInitializeReply( |
| + const ResourceMessageReplyParams& params, |
| + int32_t number_of_samples, |
| + int32_t audio_buffer_count, |
| + int32_t audio_buffer_size, |
| + int32_t bitstream_buffer_count, |
| + int32_t bitstream_buffer_size) { |
| + DCHECK(!initialized_); |
| + |
| + int32_t error = params.result(); |
| + if (error) { |
| + NotifyError(error); |
|
bbudge
2015/11/17 00:53:41
I guess this works but you could also just call:
R
llandwerlin-old
2015/11/17 15:22:31
Done.
|
| + return; |
| + } |
| + |
| + // Get audio buffers shared memory buffer. |
| + base::SharedMemoryHandle buffer_handle; |
| + if (!params.TakeSharedMemoryHandleAtIndex(0, &buffer_handle)) { |
| + NotifyError(PP_ERROR_FAILED); |
|
bbudge
2015/11/17 00:53:40
PP_ERROR_NOMEMORY
here and below
llandwerlin-old
2015/11/17 15:22:31
Done.
|
| + return; |
| + } |
| + |
| + if (!audio_buffer_manager_.SetBuffers( |
| + audio_buffer_count, audio_buffer_size, |
| + make_scoped_ptr(new base::SharedMemory(buffer_handle, false)), |
| + true)) { |
| + NotifyError(PP_ERROR_FAILED); |
| + return; |
| + } |
| + |
| + // Get bitstreamer buffers shared memory buffer. |
|
bbudge
2015/11/17 00:53:40
s/bitstreamer/bitstream
llandwerlin-old
2015/11/17 15:22:31
Done.
|
| + if (!params.TakeSharedMemoryHandleAtIndex(1, &buffer_handle)) { |
| + NotifyError(PP_ERROR_FAILED); |
| + return; |
| + } |
| + |
| + if (!bitstream_buffer_manager_.SetBuffers( |
| + bitstream_buffer_count, bitstream_buffer_size, |
| + make_scoped_ptr(new base::SharedMemory(buffer_handle, false)), |
| + false)) { |
| + NotifyError(PP_ERROR_FAILED); |
| + return; |
| + } |
| + |
| + for (int32_t i = 0; i < bitstream_buffer_manager_.number_of_buffers(); i++) |
| + bitstream_buffer_map_.insert( |
| + std::make_pair(bitstream_buffer_manager_.GetBufferPointer(i), i)); |
| + |
| + encoder_last_error_ = PP_OK; |
| + number_of_samples_ = number_of_samples; |
| + initialized_ = true; |
| + |
| + RunCallback(&initialize_callback_, encoder_last_error_); |
|
bbudge
2015/11/17 00:53:40
RunCallback(&initialize_callback_, PP_OK);
llandwerlin-old
2015/11/17 15:22:31
Done.
|
| + |
| + if (TrackedCallback::IsPending(get_buffer_callback_)) |
|
bbudge
2015/11/17 00:53:40
This should never be true so you could remove it.
llandwerlin-old
2015/11/17 15:22:31
Thanks, done.
|
| + TryWriteAudioBuffer(); |
| +} |
| + |
| +void AudioEncoderResource::OnPluginMsgEncodeReply( |
| + const ResourceMessageReplyParams& params, |
| + int32_t buffer_id) { |
| + // We need to ensure there are still callbacks to be called before |
| + // processing this message. We might receive a EncodeReply message |
| + // after having sent a Close message to the renderer. In this case, |
| + // we don't have any callback left to call. |
| + if (encode_callbacks_.empty()) |
| + return; |
| + encoder_last_error_ = params.result(); |
|
bbudge
2015/11/17 00:53:40
Looking at the host, this is an unsolicited reply
llandwerlin-old
2015/11/17 15:22:31
Thanks, done.
|
| + |
| + EncodeMap::iterator it = encode_callbacks_.find(buffer_id); |
| + DCHECK(encode_callbacks_.end() != it); |
| + |
| + scoped_refptr<TrackedCallback> callback = it->second; |
| + encode_callbacks_.erase(it); |
| + RunCallback(&callback, encoder_last_error_); |
| + |
| + audio_buffer_manager_.EnqueueBuffer(buffer_id); |
| + // If the plugin is waiting for an audio buffer, we can give the one |
| + // that just became available again. |
| + if (TrackedCallback::IsPending(get_buffer_callback_)) |
| + TryWriteAudioBuffer(); |
| +} |
| + |
| +void AudioEncoderResource::OnPluginMsgBitstreamBufferReady( |
| + const ResourceMessageReplyParams& params, |
| + int32_t buffer_id, |
| + int32_t buffer_size) { |
| + bitstream_buffer_manager_.EnqueueBuffer(buffer_id); |
| + bitstream_queued_buffer_sizes_.push_back(buffer_size); |
| + |
| + if (TrackedCallback::IsPending(get_bitstream_buffer_callback_)) |
| + TryWriteBitstreamBuffer(); |
| +} |
| + |
| +void AudioEncoderResource::OnPluginMsgNotifyError( |
| + const ResourceMessageReplyParams& params, |
| + int32_t error) { |
| + NotifyError(error); |
| +} |
| + |
| +void AudioEncoderResource::NotifyError(int32_t error) { |
|
bbudge
2015/11/17 00:53:40
DCHECK(error)
might be a good idea here.
llandwerlin-old
2015/11/17 15:22:31
Done.
|
| + encoder_last_error_ = error; |
| + RunCallback(&get_supported_profiles_callback_, error); |
| + RunCallback(&initialize_callback_, error); |
| + RunCallback(&get_buffer_callback_, error); |
| + get_buffer_data_ = nullptr; |
| + RunCallback(&get_bitstream_buffer_callback_, error); |
| + get_bitstream_buffer_data_ = nullptr; |
| + for (EncodeMap::iterator it = encode_callbacks_.begin(); |
| + it != encode_callbacks_.end(); ++it) { |
| + scoped_refptr<TrackedCallback> callback = it->second; |
| + RunCallback(&callback, error); |
|
bbudge
2015/11/17 00:53:41
RunCallback(&it->second, error); // doesn't seem
llandwerlin-old
2015/11/17 15:22:31
Done.
|
| + } |
| + encode_callbacks_.clear(); |
| +} |
| + |
| +void AudioEncoderResource::TryWriteAudioBuffer() { |
|
bbudge
2015/11/17 00:53:40
I'm finding this method name confusing. How about
llandwerlin-old
2015/11/17 15:22:31
Done.
|
| + DCHECK(TrackedCallback::IsPending(get_buffer_callback_)); |
| + |
| + if (!audio_buffer_manager_.HasAvailableBuffer()) |
| + return; |
| + |
| + int32_t buffer_id = audio_buffer_manager_.DequeueBuffer(); |
| + scoped_refptr<AudioBufferResource> resource = new AudioBufferResource( |
| + pp_instance(), buffer_id, |
| + audio_buffer_manager_.GetBufferPointer(buffer_id)); |
| + audio_buffers_.insert( |
| + AudioBufferMap::value_type(resource->pp_resource(), resource)); |
| + |
| + *get_buffer_data_ = resource->GetReference(); |
|
bbudge
2015/11/17 00:53:40
This line deserves a comment:
// Take a reference
llandwerlin-old
2015/11/17 15:22:31
Done.
|
| + get_buffer_data_ = nullptr; |
| + RunCallback(&get_buffer_callback_, PP_OK); |
| +} |
| + |
| +void AudioEncoderResource::TryWriteBitstreamBuffer() { |
| + DCHECK(TrackedCallback::IsPending(get_bitstream_buffer_callback_)); |
| + |
| + if (!bitstream_buffer_manager_.HasAvailableBuffer()) |
| + return; |
| + |
| + int32_t buffer_id = bitstream_buffer_manager_.DequeueBuffer(); |
| + int32_t buffer_size = bitstream_queued_buffer_sizes_.front(); |
| + bitstream_queued_buffer_sizes_.pop_front(); |
| + |
| + get_bitstream_buffer_data_->buffer = |
| + static_cast<void*>(bitstream_buffer_manager_.GetBufferPointer(buffer_id)); |
|
bbudge
2015/11/17 00:53:40
Is this cast needed?
|
| + get_bitstream_buffer_data_->size = static_cast<uint32_t>(buffer_size); |
| + get_bitstream_buffer_data_ = nullptr; |
| + RunCallback(&get_bitstream_buffer_callback_, PP_OK); |
| +} |
| + |
| +void AudioEncoderResource::ReleaseBuffers() { |
| + for (AudioBufferMap::iterator it = audio_buffers_.begin(); |
| + it != audio_buffers_.end(); ++it) { |
| + it->second->Invalidate(); |
| + it->second = nullptr; |
|
bbudge
2015/11/17 00:53:40
This line isn't strictly needed. clear() will caus
llandwerlin-old
2015/11/17 15:22:31
Done.
|
| + } |
| + audio_buffers_.clear(); |
| } |
| } // namespace proxy |