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/memory/shared_memory.h" |
| 6 #include "ppapi/c/pp_array_output.h" |
| 7 #include "ppapi/c/pp_codecs.h" |
| 8 #include "ppapi/proxy/audio_buffer_resource.h" |
| 9 #include "ppapi/proxy/audio_encoder_resource.h" |
| 10 #include "ppapi/proxy/ppapi_messages.h" |
| 11 #include "ppapi/shared_impl/array_writer.h" |
| 12 #include "ppapi/shared_impl/media_stream_buffer.h" |
| 13 #include "ppapi/thunk/enter.h" |
| 14 |
| 15 namespace ppapi { |
| 16 namespace proxy { |
| 17 |
| 18 namespace { |
| 19 |
| 20 void RunCallback(scoped_refptr<TrackedCallback>* callback, int32_t error) { |
| 21 if (!TrackedCallback::IsPending(*callback)) |
| 22 return; |
| 23 |
| 24 scoped_refptr<TrackedCallback> temp; |
| 25 callback->swap(temp); |
| 26 temp->Run(error); |
| 27 } |
| 28 |
| 29 } // namespace |
| 30 |
| 31 class AudioEncoderResource::BitstreamBufferManager { |
| 32 public: |
| 33 BitstreamBufferManager() : number_of_buffers_(0), buffer_size_(0) {} |
| 34 ~BitstreamBufferManager() {} |
| 35 |
| 36 bool Initialize(scoped_ptr<base::SharedMemory> shm, |
| 37 uint32_t number_of_buffers, |
| 38 uint32_t buffer_size) { |
| 39 uint32_t total_size = number_of_buffers * buffer_size; |
| 40 shm_ = shm.Pass(); |
| 41 if (!shm_ || !shm_->Map(base::checked_cast<size_t>(total_size))) |
| 42 return false; |
| 43 |
| 44 for (uint32_t i = 0; i < number_of_buffers; i++) |
| 45 buffer_map_.insert(std::make_pair( |
| 46 static_cast<uint8_t*>(shm_->memory()) + (i * buffer_size), |
| 47 static_cast<int32_t>(i))); |
| 48 |
| 49 number_of_buffers_ = number_of_buffers; |
| 50 buffer_size_ = buffer_size; |
| 51 return true; |
| 52 } |
| 53 int32_t GetBufferId(void* address) { |
| 54 BufferMap::const_iterator it = buffer_map_.find(address); |
| 55 if (it == buffer_map_.end()) |
| 56 return -1; |
| 57 return it->second; |
| 58 } |
| 59 uint8_t* GetBuffer(int32_t id) { |
| 60 return static_cast<uint8_t*>(shm_->memory()) + (id * buffer_size_); |
| 61 } |
| 62 std::pair<int32_t, uint32_t> DequeueBuffer() { |
| 63 if (enqueued_buffers_.empty()) |
| 64 return std::make_pair(-1, 0U); |
| 65 std::pair<int32_t, uint32_t> tuple = enqueued_buffers_.front(); |
| 66 enqueued_buffers_.pop_front(); |
| 67 return tuple; |
| 68 } |
| 69 void EnqueueBuffer(int32_t id, uint32_t size) { |
| 70 enqueued_buffers_.push_back(std::make_pair(id, size)); |
| 71 } |
| 72 bool AvailableBuffers() { return !enqueued_buffers_.empty(); } |
| 73 |
| 74 private: |
| 75 uint32_t number_of_buffers_; |
| 76 |
| 77 // Size of individual buffers. |
| 78 uint32_t buffer_size_; |
| 79 |
| 80 // Shared memory containing all objects. |
| 81 scoped_ptr<base::SharedMemory> shm_; |
| 82 |
| 83 // Queue of bitstream buffers received from the host, waiting to be |
| 84 // handed to the plugin. |
| 85 std::deque<std::pair<int32_t, uint32_t>> enqueued_buffers_; |
| 86 |
| 87 // Memory pointer to buffer id map. |
| 88 typedef std::map<void*, int32_t> BufferMap; |
| 89 BufferMap buffer_map_; |
| 90 |
| 91 DISALLOW_COPY_AND_ASSIGN(BitstreamBufferManager); |
| 92 }; |
| 93 |
| 94 AudioEncoderResource::AudioEncoderResource(Connection connection, |
| 95 PP_Instance instance) |
| 96 : PluginResource(connection, instance), |
| 97 encoder_last_error_(PP_ERROR_FAILED), |
| 98 initialized_(false), |
| 99 closed_(false), |
| 100 buffer_manager_(this) { |
| 101 SendCreate(RENDERER, PpapiHostMsg_AudioEncoder_Create()); |
| 102 } |
| 103 |
| 104 AudioEncoderResource::~AudioEncoderResource() { |
| 105 } |
| 106 |
| 107 thunk::PPB_AudioEncoder_API* AudioEncoderResource::AsPPB_AudioEncoder_API() { |
| 108 return this; |
| 109 } |
| 110 |
| 111 int32_t AudioEncoderResource::GetSupportedProfiles( |
| 112 const PP_ArrayOutput& output, |
| 113 const scoped_refptr<TrackedCallback>& callback) { |
| 114 if (TrackedCallback::IsPending(get_supported_profiles_callback_)) |
| 115 return PP_ERROR_INPROGRESS; |
| 116 |
| 117 get_supported_profiles_callback_ = callback; |
| 118 Call<PpapiPluginMsg_AudioEncoder_GetSupportedProfilesReply>( |
| 119 RENDERER, PpapiHostMsg_AudioEncoder_GetSupportedProfiles(), |
| 120 base::Bind(&AudioEncoderResource::OnPluginMsgGetSupportedProfilesReply, |
| 121 this, output)); |
| 122 return PP_OK_COMPLETIONPENDING; |
| 123 } |
| 124 |
| 125 int32_t AudioEncoderResource::Initialize( |
| 126 uint32_t channels, |
| 127 PP_AudioBuffer_SampleRate input_sample_rate, |
| 128 PP_AudioBuffer_SampleSize input_sample_size, |
| 129 PP_AudioProfile output_profile, |
| 130 uint32_t initial_bitrate, |
| 131 PP_HardwareAcceleration acceleration, |
| 132 const scoped_refptr<TrackedCallback>& callback) { |
| 133 if (initialized_) |
| 134 return PP_ERROR_FAILED; |
| 135 if (TrackedCallback::IsPending(initialize_callback_)) |
| 136 return PP_ERROR_INPROGRESS; |
| 137 |
| 138 initialize_callback_ = callback; |
| 139 |
| 140 PPB_AudioEncodeParameters parameters; |
| 141 parameters.channels = channels; |
| 142 parameters.input_sample_rate = input_sample_rate; |
| 143 parameters.input_sample_size = input_sample_size; |
| 144 parameters.output_profile = output_profile; |
| 145 parameters.initial_bitrate = initial_bitrate; |
| 146 parameters.acceleration = acceleration; |
| 147 |
| 148 Call<PpapiPluginMsg_AudioEncoder_InitializeReply>( |
| 149 RENDERER, PpapiHostMsg_AudioEncoder_Initialize(parameters), |
| 150 base::Bind(&AudioEncoderResource::OnPluginMsgInitializeReply, this)); |
| 151 return PP_OK_COMPLETIONPENDING; |
| 152 } |
| 153 |
| 154 int32_t AudioEncoderResource::GetNumberOfSamples() { |
| 155 if (encoder_last_error_) |
| 156 return encoder_last_error_; |
| 157 return number_of_samples_; |
| 158 } |
| 159 |
| 160 int32_t AudioEncoderResource::GetBuffer( |
| 161 PP_Resource* audio_buffer, |
| 162 const scoped_refptr<TrackedCallback>& callback) { |
| 163 if (encoder_last_error_) |
| 164 return encoder_last_error_; |
| 165 |
| 166 if (TrackedCallback::IsPending(get_buffer_callback_)) |
| 167 return PP_ERROR_INPROGRESS; |
| 168 |
| 169 get_buffer_data_ = audio_buffer; |
| 170 get_buffer_callback_ = callback; |
| 171 |
| 172 // Lazily ask for a shared memory buffer in which audio frames are allocated. |
| 173 if (buffer_manager_.number_of_buffers() == 0) { |
| 174 Call<PpapiPluginMsg_AudioEncoder_GetAudioFramesReply>( |
| 175 RENDERER, PpapiHostMsg_AudioEncoder_GetAudioFrames(), |
| 176 base::Bind(&AudioEncoderResource::OnPluginMsgGetAudioFramesReply, |
| 177 this)); |
| 178 } else { |
| 179 TryWriteAudioBuffer(); |
| 180 } |
| 181 |
| 182 return PP_OK_COMPLETIONPENDING; |
| 183 } |
| 184 |
| 185 int32_t AudioEncoderResource::Encode( |
| 186 PP_Resource audio_buffer, |
| 187 const scoped_refptr<TrackedCallback>& callback) { |
| 188 if (encoder_last_error_) |
| 189 return encoder_last_error_; |
| 190 |
| 191 AudioBufferMap::iterator it = audio_buffers_.find(audio_buffer); |
| 192 if (it == audio_buffers_.end()) |
| 193 // TODO(llandwerlin): accept MediaStreamAudioTrack's audio buffers. |
| 194 return PP_ERROR_BADRESOURCE; |
| 195 |
| 196 scoped_refptr<AudioBufferResource> buffer_resource = it->second; |
| 197 |
| 198 encode_callbacks_.insert( |
| 199 std::make_pair(buffer_resource->GetBufferIndex(), callback)); |
| 200 |
| 201 Post(RENDERER, |
| 202 PpapiHostMsg_AudioEncoder_Encode(buffer_resource->GetBufferIndex())); |
| 203 |
| 204 // Invalidate the frame to prevent the plugin from modifying it. |
| 205 buffer_resource->Invalidate(); |
| 206 audio_buffers_.erase(it); |
| 207 |
| 208 return PP_OK_COMPLETIONPENDING; |
| 209 } |
| 210 |
| 211 int32_t AudioEncoderResource::GetBitstreamBuffer( |
| 212 PP_AudioBitstreamBuffer* bitstream_buffer, |
| 213 const scoped_refptr<TrackedCallback>& callback) { |
| 214 if (encoder_last_error_) |
| 215 return encoder_last_error_; |
| 216 if (TrackedCallback::IsPending(get_bitstream_buffer_callback_)) |
| 217 return PP_ERROR_INPROGRESS; |
| 218 |
| 219 get_bitstream_buffer_callback_ = callback; |
| 220 get_bitstream_buffer_data_ = bitstream_buffer; |
| 221 |
| 222 TryWriteBitstreamBuffer(); |
| 223 |
| 224 return PP_OK_COMPLETIONPENDING; |
| 225 } |
| 226 |
| 227 void AudioEncoderResource::RecycleBitstreamBuffer( |
| 228 const PP_AudioBitstreamBuffer* bitstream_buffer) { |
| 229 if (encoder_last_error_) |
| 230 return; |
| 231 int32_t buffer_id = |
| 232 bitstream_buffer_manager_->GetBufferId(bitstream_buffer->buffer); |
| 233 |
| 234 if (buffer_id >= 0) |
| 235 Post(RENDERER, PpapiHostMsg_AudioEncoder_RecycleBitstreamBuffer(buffer_id)); |
| 236 } |
| 237 |
| 238 void AudioEncoderResource::RequestBitrateChange(uint32_t bitrate) { |
| 239 if (encoder_last_error_) |
| 240 return; |
| 241 Post(RENDERER, PpapiHostMsg_AudioEncoder_RequestBitrateChange(bitrate)); |
| 242 } |
| 243 |
| 244 void AudioEncoderResource::Close() { |
| 245 if (encoder_last_error_) |
| 246 return; |
| 247 Post(RENDERER, PpapiHostMsg_AudioEncoder_Close()); |
| 248 closed_ = true; |
| 249 if (!encoder_last_error_ || !initialized_) |
| 250 NotifyError(PP_ERROR_ABORTED); |
| 251 ReleaseBuffers(); |
| 252 } |
| 253 |
| 254 void AudioEncoderResource::OnReplyReceived( |
| 255 const ResourceMessageReplyParams& params, |
| 256 const IPC::Message& msg) { |
| 257 PPAPI_BEGIN_MESSAGE_MAP(AudioEncoderResource, msg) |
| 258 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL( |
| 259 PpapiPluginMsg_AudioEncoder_BitstreamBufferReady, |
| 260 OnPluginMsgBitstreamBufferReady) |
| 261 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(PpapiPluginMsg_AudioEncoder_EncodeReply, |
| 262 OnPluginMsgEncodeReply) |
| 263 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(PpapiPluginMsg_AudioEncoder_NotifyError, |
| 264 OnPluginMsgNotifyError) |
| 265 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED( |
| 266 PluginResource::OnReplyReceived(params, msg)) |
| 267 PPAPI_END_MESSAGE_MAP() |
| 268 } |
| 269 |
| 270 void AudioEncoderResource::OnPluginMsgGetSupportedProfilesReply( |
| 271 const PP_ArrayOutput& output, |
| 272 const ResourceMessageReplyParams& params, |
| 273 const std::vector<PP_AudioProfileDescription>& profiles) { |
| 274 int32_t error = params.result(); |
| 275 if (error) { |
| 276 NotifyError(error); |
| 277 return; |
| 278 } |
| 279 |
| 280 ArrayWriter writer(output); |
| 281 if (!writer.is_valid()) { |
| 282 RunCallback(&get_supported_profiles_callback_, PP_ERROR_BADARGUMENT); |
| 283 return; |
| 284 } |
| 285 |
| 286 bool write_result = writer.StoreVector(profiles); |
| 287 |
| 288 if (!write_result) { |
| 289 RunCallback(&get_supported_profiles_callback_, PP_ERROR_FAILED); |
| 290 return; |
| 291 } |
| 292 |
| 293 RunCallback(&get_supported_profiles_callback_, |
| 294 base::checked_cast<int32_t>(profiles.size())); |
| 295 } |
| 296 |
| 297 void AudioEncoderResource::OnPluginMsgInitializeReply( |
| 298 const ResourceMessageReplyParams& params, |
| 299 uint32_t number_of_samples, |
| 300 uint32_t buffer_count, |
| 301 uint32_t buffer_size) { |
| 302 DCHECK(!initialized_); |
| 303 |
| 304 int32_t error = params.result(); |
| 305 if (error) { |
| 306 NotifyError(error); |
| 307 return; |
| 308 } |
| 309 |
| 310 base::SharedMemoryHandle buffer_handle; |
| 311 if (!params.TakeSharedMemoryHandleAtIndex(0, &buffer_handle)) { |
| 312 NotifyError(PP_ERROR_FAILED); |
| 313 return; |
| 314 } |
| 315 |
| 316 bitstream_buffer_manager_.reset(new BitstreamBufferManager()); |
| 317 if (!bitstream_buffer_manager_->Initialize( |
| 318 make_scoped_ptr(new base::SharedMemory(buffer_handle, false)), |
| 319 buffer_count, buffer_size)) { |
| 320 NotifyError(PP_ERROR_FAILED); |
| 321 return; |
| 322 } |
| 323 |
| 324 encoder_last_error_ = PP_OK; |
| 325 number_of_samples_ = number_of_samples; |
| 326 initialized_ = true; |
| 327 |
| 328 RunCallback(&initialize_callback_, encoder_last_error_); |
| 329 } |
| 330 |
| 331 void AudioEncoderResource::OnPluginMsgGetAudioFramesReply( |
| 332 const ResourceMessageReplyParams& params, |
| 333 uint32_t frame_count, |
| 334 uint32_t frame_length) { |
| 335 int32_t error = params.result(); |
| 336 if (error) { |
| 337 NotifyError(error); |
| 338 return; |
| 339 } |
| 340 |
| 341 base::SharedMemoryHandle buffer_handle; |
| 342 if (!params.TakeSharedMemoryHandleAtIndex(0, &buffer_handle)) { |
| 343 NotifyError(PP_ERROR_FAILED); |
| 344 return; |
| 345 } |
| 346 |
| 347 if (!buffer_manager_.SetBuffers( |
| 348 frame_count, frame_length + sizeof(ppapi::MediaStreamBuffer::Audio), |
| 349 make_scoped_ptr(new base::SharedMemory(buffer_handle, false)), |
| 350 true)) { |
| 351 NotifyError(PP_ERROR_FAILED); |
| 352 return; |
| 353 } |
| 354 |
| 355 if (TrackedCallback::IsPending(get_buffer_callback_)) |
| 356 TryWriteAudioBuffer(); |
| 357 } |
| 358 |
| 359 void AudioEncoderResource::OnPluginMsgEncodeReply( |
| 360 const ResourceMessageReplyParams& params, |
| 361 uint32_t buffer_id) { |
| 362 // We need to ensure there are still callbacks to be called before |
| 363 // processing this message. We might receive a EncodeReply message |
| 364 // after having sent a Close message to the renderer. In this case, |
| 365 // we don't have any callback left to call. |
| 366 if (encode_callbacks_.empty()) |
| 367 return; |
| 368 encoder_last_error_ = params.result(); |
| 369 |
| 370 EncodeMap::iterator it = encode_callbacks_.find(buffer_id); |
| 371 DCHECK(encode_callbacks_.end() != it); |
| 372 |
| 373 scoped_refptr<TrackedCallback> callback = it->second; |
| 374 encode_callbacks_.erase(it); |
| 375 RunCallback(&callback, encoder_last_error_); |
| 376 |
| 377 buffer_manager_.EnqueueBuffer(buffer_id); |
| 378 // If the plugin is waiting for an audio buffer, we can give the one |
| 379 // that just became available again. |
| 380 if (TrackedCallback::IsPending(get_buffer_callback_)) |
| 381 TryWriteAudioBuffer(); |
| 382 } |
| 383 |
| 384 void AudioEncoderResource::OnPluginMsgBitstreamBufferReady( |
| 385 const ResourceMessageReplyParams& params, |
| 386 uint32_t buffer_id, |
| 387 uint32_t buffer_size) { |
| 388 bitstream_buffer_manager_->EnqueueBuffer( |
| 389 base::checked_cast<int32_t>(buffer_id), buffer_size); |
| 390 |
| 391 if (TrackedCallback::IsPending(get_bitstream_buffer_callback_)) |
| 392 TryWriteBitstreamBuffer(); |
| 393 } |
| 394 |
| 395 void AudioEncoderResource::OnPluginMsgNotifyError( |
| 396 const ResourceMessageReplyParams& params, |
| 397 int32_t error) { |
| 398 NotifyError(error); |
| 399 } |
| 400 |
| 401 void AudioEncoderResource::NotifyError(int32_t error) { |
| 402 encoder_last_error_ = error; |
| 403 RunCallback(&get_supported_profiles_callback_, error); |
| 404 RunCallback(&initialize_callback_, error); |
| 405 RunCallback(&get_buffer_callback_, error); |
| 406 get_buffer_data_ = nullptr; |
| 407 RunCallback(&get_bitstream_buffer_callback_, error); |
| 408 get_bitstream_buffer_data_ = nullptr; |
| 409 for (EncodeMap::iterator it = encode_callbacks_.begin(); |
| 410 it != encode_callbacks_.end(); ++it) { |
| 411 scoped_refptr<TrackedCallback> callback = it->second; |
| 412 RunCallback(&callback, error); |
| 413 } |
| 414 encode_callbacks_.clear(); |
| 415 } |
| 416 |
| 417 void AudioEncoderResource::TryWriteAudioBuffer() { |
| 418 DCHECK(TrackedCallback::IsPending(get_buffer_callback_)); |
| 419 |
| 420 int32_t buffer_id = buffer_manager_.DequeueBuffer(); |
| 421 if (buffer_id < 0) |
| 422 return; |
| 423 |
| 424 scoped_refptr<AudioBufferResource> resource = new AudioBufferResource( |
| 425 pp_instance(), buffer_id, buffer_manager_.GetBufferPointer(buffer_id)); |
| 426 audio_buffers_.insert( |
| 427 AudioBufferMap::value_type(resource->pp_resource(), resource)); |
| 428 |
| 429 *get_buffer_data_ = resource->GetReference(); |
| 430 get_buffer_data_ = nullptr; |
| 431 RunCallback(&get_buffer_callback_, PP_OK); |
| 432 } |
| 433 |
| 434 void AudioEncoderResource::TryWriteBitstreamBuffer() { |
| 435 DCHECK(TrackedCallback::IsPending(get_bitstream_buffer_callback_)); |
| 436 |
| 437 if (!bitstream_buffer_manager_->AvailableBuffers()) |
| 438 return; |
| 439 |
| 440 std::pair<int32_t, uint32_t> data = |
| 441 bitstream_buffer_manager_->DequeueBuffer(); |
| 442 |
| 443 get_bitstream_buffer_data_->buffer = |
| 444 bitstream_buffer_manager_->GetBuffer(data.first); |
| 445 get_bitstream_buffer_data_->size = data.second; |
| 446 get_bitstream_buffer_data_ = nullptr; |
| 447 RunCallback(&get_bitstream_buffer_callback_, PP_OK); |
| 448 } |
| 449 |
| 450 void AudioEncoderResource::ReleaseBuffers() { |
| 451 for (AudioBufferMap::iterator it = audio_buffers_.begin(); |
| 452 it != audio_buffers_.end(); ++it) { |
| 453 it->second->Invalidate(); |
| 454 it->second = nullptr; |
| 455 } |
| 456 audio_buffers_.clear(); |
| 457 } |
| 458 |
| 459 } // namespace proxy |
| 460 } // namespace ppapi |
OLD | NEW |