OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "base/memory/shared_memory.h" |
| 6 #include "ppapi/c/pp_array_output.h" |
| 7 #include "ppapi/proxy/ppapi_messages.h" |
5 #include "ppapi/proxy/video_encoder_resource.h" | 8 #include "ppapi/proxy/video_encoder_resource.h" |
| 9 #include "ppapi/proxy/video_frame_resource.h" |
| 10 #include "ppapi/shared_impl/media_stream_buffer.h" |
| 11 #include "ppapi/shared_impl/media_stream_buffer_manager.h" |
| 12 #include "ppapi/thunk/enter.h" |
6 | 13 |
| 14 using ppapi::proxy::SerializedHandle; |
| 15 using ppapi::thunk::EnterResourceNoLock; |
7 using ppapi::thunk::PPB_VideoEncoder_API; | 16 using ppapi::thunk::PPB_VideoEncoder_API; |
8 | 17 |
9 namespace ppapi { | 18 namespace ppapi { |
10 namespace proxy { | 19 namespace proxy { |
11 | 20 |
| 21 namespace { |
| 22 |
| 23 void RunCallback(scoped_refptr<TrackedCallback>* callback, int32_t error) { |
| 24 if (!TrackedCallback::IsPending(*callback)) |
| 25 return; |
| 26 |
| 27 scoped_refptr<TrackedCallback> temp; |
| 28 callback->swap(temp); |
| 29 temp->Run(error); |
| 30 } |
| 31 |
| 32 } // namespace |
| 33 |
| 34 VideoEncoderResource::ShmBuffer::ShmBuffer(base::SharedMemoryHandle handle, |
| 35 uint32_t id, |
| 36 uint32_t size) |
| 37 : shm(new base::SharedMemory(handle, true)), id(id), size(size) { |
| 38 shm->Map(size); |
| 39 } |
| 40 |
| 41 VideoEncoderResource::ShmBuffer::~ShmBuffer() { |
| 42 } |
| 43 |
| 44 VideoEncoderResource::BitstreamBuffer::BitstreamBuffer(uint32_t id, |
| 45 uint32_t size, |
| 46 bool key_frame) |
| 47 : id(id), size(size), key_frame(key_frame) { |
| 48 } |
| 49 |
| 50 VideoEncoderResource::BitstreamBuffer::BitstreamBuffer( |
| 51 const BitstreamBuffer& other) |
| 52 : id(other.id), size(other.size), key_frame(other.key_frame) { |
| 53 } |
| 54 |
| 55 VideoEncoderResource::BitstreamBuffer::~BitstreamBuffer() { |
| 56 } |
| 57 |
12 VideoEncoderResource::VideoEncoderResource(Connection connection, | 58 VideoEncoderResource::VideoEncoderResource(Connection connection, |
13 PP_Instance instance) | 59 PP_Instance instance) |
14 : PluginResource(connection, instance) { | 60 : PluginResource(connection, instance), |
| 61 initialized_(false), |
| 62 closed_(false), |
| 63 // Set |encoder_last_error_| to PP_OK after successful initialization. |
| 64 // This makes error checking a little more concise, since we can check |
| 65 // that the encoder has been initialized and hasn't returned an error by |
| 66 // just testing |encoder_last_error_|. |
| 67 encoder_last_error_(PP_ERROR_FAILED), |
| 68 input_frame_count_(0), |
| 69 input_coded_size_(PP_MakeSize(0, 0)), |
| 70 buffer_manager_(this), |
| 71 get_video_frame_data_(nullptr), |
| 72 get_bitstream_buffer_data_(nullptr) { |
| 73 SendCreate(RENDERER, PpapiHostMsg_VideoEncoder_Create()); |
15 } | 74 } |
16 | 75 |
17 VideoEncoderResource::~VideoEncoderResource() { | 76 VideoEncoderResource::~VideoEncoderResource() { |
| 77 Close(); |
18 } | 78 } |
19 | 79 |
20 PPB_VideoEncoder_API* VideoEncoderResource::AsPPB_VideoEncoder_API() { | 80 PPB_VideoEncoder_API* VideoEncoderResource::AsPPB_VideoEncoder_API() { |
21 return this; | 81 return this; |
22 } | 82 } |
23 | 83 |
24 int32_t VideoEncoderResource::GetSupportedProfiles( | 84 int32_t VideoEncoderResource::GetSupportedProfiles( |
25 const PP_ArrayOutput& output, | 85 const PP_ArrayOutput& output, |
26 const scoped_refptr<TrackedCallback>& callback) { | 86 const scoped_refptr<TrackedCallback>& callback) { |
27 return PP_ERROR_FAILED; | 87 if (TrackedCallback::IsPending(get_supported_profiles_callback_)) |
| 88 return PP_ERROR_INPROGRESS; |
| 89 |
| 90 get_supported_profiles_callback_ = callback; |
| 91 Call<PpapiPluginMsg_VideoEncoder_GetSupportedProfilesReply>( |
| 92 RENDERER, PpapiHostMsg_VideoEncoder_GetSupportedProfiles(), |
| 93 base::Bind(&VideoEncoderResource::OnPluginMsgGetSupportedProfilesReply, |
| 94 this, output)); |
| 95 return PP_OK_COMPLETIONPENDING; |
| 96 } |
| 97 |
| 98 int32_t VideoEncoderResource::GetFramesRequired() { |
| 99 if (encoder_last_error_) |
| 100 return encoder_last_error_; |
| 101 return input_frame_count_; |
| 102 } |
| 103 |
| 104 int32_t VideoEncoderResource::GetFrameCodedSize(PP_Size* size) { |
| 105 if (encoder_last_error_) |
| 106 return encoder_last_error_; |
| 107 *size = input_coded_size_; |
| 108 return PP_OK; |
28 } | 109 } |
29 | 110 |
30 int32_t VideoEncoderResource::Initialize( | 111 int32_t VideoEncoderResource::Initialize( |
31 PP_VideoFrame_Format input_format, | 112 PP_VideoFrame_Format input_format, |
32 const PP_Size* input_visible_size, | 113 const PP_Size* input_visible_size, |
33 PP_VideoProfile output_profile, | 114 PP_VideoProfile output_profile, |
34 uint32_t initial_bitrate, | 115 uint32_t initial_bitrate, |
35 PP_HardwareAcceleration acceleration, | 116 PP_HardwareAcceleration acceleration, |
36 const scoped_refptr<TrackedCallback>& callback) { | 117 const scoped_refptr<TrackedCallback>& callback) { |
37 return PP_ERROR_FAILED; | 118 if (initialized_) |
38 } | 119 return PP_ERROR_FAILED; |
39 | 120 if (TrackedCallback::IsPending(initialize_callback_)) |
40 int32_t VideoEncoderResource::GetFramesRequired() { | 121 return PP_ERROR_INPROGRESS; |
41 return PP_ERROR_FAILED; | 122 |
42 } | 123 initialize_callback_ = callback; |
43 | 124 Call<PpapiPluginMsg_VideoEncoder_InitializeReply>( |
44 int32_t VideoEncoderResource::GetFrameCodedSize(PP_Size* size) { | 125 RENDERER, PpapiHostMsg_VideoEncoder_Initialize( |
45 return PP_ERROR_FAILED; | 126 input_format, *input_visible_size, output_profile, |
| 127 initial_bitrate, acceleration), |
| 128 base::Bind(&VideoEncoderResource::OnPluginMsgInitializeReply, this)); |
| 129 return PP_OK_COMPLETIONPENDING; |
46 } | 130 } |
47 | 131 |
48 int32_t VideoEncoderResource::GetVideoFrame( | 132 int32_t VideoEncoderResource::GetVideoFrame( |
49 PP_Resource* video_frame, | 133 PP_Resource* video_frame, |
50 const scoped_refptr<TrackedCallback>& callback) { | 134 const scoped_refptr<TrackedCallback>& callback) { |
51 return PP_ERROR_FAILED; | 135 if (encoder_last_error_) |
| 136 return encoder_last_error_; |
| 137 |
| 138 if (TrackedCallback::IsPending(get_video_frame_callback_)) |
| 139 return PP_ERROR_INPROGRESS; |
| 140 |
| 141 // Lazily ask for a shared memory buffer in which video frames are allocated. |
| 142 if (buffer_manager_.number_of_buffers() == 0) { |
| 143 get_video_frame_data_ = video_frame; |
| 144 get_video_frame_callback_ = callback; |
| 145 Call<PpapiPluginMsg_VideoEncoder_GetVideoFramesReply>( |
| 146 RENDERER, PpapiHostMsg_VideoEncoder_GetVideoFrames(), |
| 147 base::Bind(&VideoEncoderResource::OnPluginMsgGetVideoFramesReply, |
| 148 this)); |
| 149 return PP_OK_COMPLETIONPENDING; |
| 150 } |
| 151 |
| 152 if (!WriteVideoFrame(video_frame)) { |
| 153 get_video_frame_data_ = video_frame; |
| 154 get_video_frame_callback_ = callback; |
| 155 return PP_OK_COMPLETIONPENDING; |
| 156 } |
| 157 |
| 158 return PP_OK; |
52 } | 159 } |
53 | 160 |
54 int32_t VideoEncoderResource::Encode( | 161 int32_t VideoEncoderResource::Encode( |
55 PP_Resource video_frame, | 162 PP_Resource video_frame, |
56 PP_Bool force_keyframe, | 163 PP_Bool force_keyframe, |
57 const scoped_refptr<TrackedCallback>& callback) { | 164 const scoped_refptr<TrackedCallback>& callback) { |
58 return PP_ERROR_FAILED; | 165 if (encoder_last_error_) |
| 166 return encoder_last_error_; |
| 167 |
| 168 VideoFrameMap::iterator it = video_frames_.find(video_frame); |
| 169 if (it == video_frames_.end()) |
| 170 // TODO(llandwerlin): accept MediaStreamVideoTrack's video frames. |
| 171 return PP_ERROR_BADRESOURCE; |
| 172 |
| 173 scoped_refptr<VideoFrameResource> frame_resource = it->second; |
| 174 |
| 175 Call<PpapiPluginMsg_VideoEncoder_EncodeReply>( |
| 176 RENDERER, |
| 177 PpapiHostMsg_VideoEncoder_Encode(frame_resource->GetBufferIndex(), |
| 178 PP_ToBool(force_keyframe)), |
| 179 base::Bind(&VideoEncoderResource::OnPluginMsgEncodeReply, this, |
| 180 callback)); |
| 181 |
| 182 return PP_OK_COMPLETIONPENDING; |
59 } | 183 } |
60 | 184 |
61 int32_t VideoEncoderResource::GetBitstreamBuffer( | 185 int32_t VideoEncoderResource::GetBitstreamBuffer( |
62 PP_BitstreamBuffer* picture, | 186 PP_BitstreamBuffer* bitstream_buffer, |
63 const scoped_refptr<TrackedCallback>& callback) { | 187 const scoped_refptr<TrackedCallback>& callback) { |
64 return PP_ERROR_FAILED; | 188 if (encoder_last_error_) |
| 189 return encoder_last_error_; |
| 190 if (TrackedCallback::IsPending(get_bitstream_buffer_callback_)) |
| 191 return PP_ERROR_INPROGRESS; |
| 192 |
| 193 if (available_bitstream_buffers_.empty()) { |
| 194 get_bitstream_buffer_callback_ = callback; |
| 195 get_bitstream_buffer_data_ = bitstream_buffer; |
| 196 return PP_OK_COMPLETIONPENDING; |
| 197 } |
| 198 |
| 199 BitstreamBuffer buffer(available_bitstream_buffers_.front()); |
| 200 |
| 201 available_bitstream_buffers_.pop_front(); |
| 202 WriteBitstreamBuffer(bitstream_buffer, buffer); |
| 203 |
| 204 return PP_OK; |
65 } | 205 } |
66 | 206 |
67 void VideoEncoderResource::RecycleBitstreamBuffer( | 207 void VideoEncoderResource::RecycleBitstreamBuffer( |
68 const PP_BitstreamBuffer* picture) { | 208 const PP_BitstreamBuffer* bitstream_buffer) { |
| 209 if (encoder_last_error_) |
| 210 return; |
| 211 BitstreamBufferMap::const_iterator iter = |
| 212 bitstream_buffer_map_.find(bitstream_buffer->buffer); |
| 213 if (iter != bitstream_buffer_map_.end()) { |
| 214 Post(RENDERER, |
| 215 PpapiHostMsg_VideoEncoder_RecycleBitstreamBuffer(iter->second)); |
| 216 } |
69 } | 217 } |
70 | 218 |
71 void VideoEncoderResource::RequestEncodingParametersChange(uint32_t bitrate, | 219 void VideoEncoderResource::RequestEncodingParametersChange(uint32_t bitrate, |
72 uint32_t framerate) { | 220 uint32_t framerate) { |
| 221 if (encoder_last_error_) |
| 222 return; |
| 223 Post(RENDERER, PpapiHostMsg_VideoEncoder_RequestEncodingParametersChange( |
| 224 bitrate, framerate)); |
73 } | 225 } |
74 | 226 |
75 void VideoEncoderResource::Close() { | 227 void VideoEncoderResource::Close() { |
| 228 if (encoder_last_error_) |
| 229 return; |
| 230 if (closed_) |
| 231 return; |
| 232 // Synchronously call into the host to make sure all in flight |
| 233 // Encode request are properly cancelled. This will also trigger the |
| 234 // NotifyError callback before SyncCall() returns. |
| 235 SyncCall<PpapiPluginMsg_VideoEncoder_CloseReply>( |
| 236 RENDERER, PpapiHostMsg_VideoEncoder_Close()); |
| 237 closed_ = true; |
| 238 ReleaseFrames(); |
| 239 } |
| 240 |
| 241 void VideoEncoderResource::OnReplyReceived( |
| 242 const ResourceMessageReplyParams& params, |
| 243 const IPC::Message& msg) { |
| 244 PPAPI_BEGIN_MESSAGE_MAP(VideoEncoderResource, msg) |
| 245 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL( |
| 246 PpapiPluginMsg_VideoEncoder_BitstreamBufferReady, |
| 247 OnPluginMsgBitstreamBufferReady) |
| 248 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(PpapiPluginMsg_VideoEncoder_NotifyError, |
| 249 OnPluginMsgNotifyError) |
| 250 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED( |
| 251 PluginResource::OnReplyReceived(params, msg)) |
| 252 PPAPI_END_MESSAGE_MAP() |
| 253 } |
| 254 |
| 255 void VideoEncoderResource::OnPluginMsgGetSupportedProfilesReply( |
| 256 const PP_ArrayOutput& output, |
| 257 const ResourceMessageReplyParams& params, |
| 258 const std::vector<PP_VideoProfileDescription>& profiles) { |
| 259 void* ptr = output.GetDataBuffer(output.user_data, profiles.size(), |
| 260 sizeof(PP_VideoProfileDescription)); |
| 261 |
| 262 if (!ptr) { |
| 263 RunCallback(&get_supported_profiles_callback_, PP_ERROR_FAILED); |
| 264 return; |
| 265 } |
| 266 |
| 267 if (profiles.size() > 0) |
| 268 memcpy(ptr, &profiles[0], |
| 269 profiles.size() * sizeof(PP_VideoProfileDescription)); |
| 270 RunCallback(&get_supported_profiles_callback_, PP_OK); |
| 271 } |
| 272 |
| 273 void VideoEncoderResource::OnPluginMsgInitializeReply( |
| 274 const ResourceMessageReplyParams& params, |
| 275 uint32_t buffer_length, |
| 276 uint32_t input_frame_count, |
| 277 const PP_Size& input_coded_size) { |
| 278 DCHECK(!initialized_); |
| 279 |
| 280 encoder_last_error_ = params.result(); |
| 281 if (encoder_last_error_) { |
| 282 RunCallback(&initialize_callback_, encoder_last_error_); |
| 283 return; |
| 284 } |
| 285 |
| 286 std::vector<base::SharedMemoryHandle> shm_handles; |
| 287 params.TakeAllSharedMemoryHandles(&shm_handles); |
| 288 |
| 289 for (uint32_t i = 0; i < shm_handles.size(); ++i) { |
| 290 ShmBuffer* buffer = new ShmBuffer(shm_handles[i], i, buffer_length); |
| 291 shm_buffers_.push_back(buffer); |
| 292 bitstream_buffer_map_.insert( |
| 293 std::make_pair(buffer->shm->memory(), buffer->id)); |
| 294 } |
| 295 |
| 296 input_frame_count_ = input_frame_count; |
| 297 input_coded_size_ = input_coded_size; |
| 298 initialized_ = true; |
| 299 RunCallback(&initialize_callback_, encoder_last_error_); |
| 300 } |
| 301 |
| 302 void VideoEncoderResource::OnPluginMsgGetVideoFramesReply( |
| 303 const ResourceMessageReplyParams& params, |
| 304 uint32_t frame_count, |
| 305 uint32_t frame_length, |
| 306 const PP_Size& frame_size) { |
| 307 int32_t error = params.result(); |
| 308 if (error) { |
| 309 NotifyError(error); |
| 310 return; |
| 311 } |
| 312 |
| 313 base::SharedMemoryHandle buffer_handle; |
| 314 params.TakeSharedMemoryHandleAtIndex(0, &buffer_handle); |
| 315 |
| 316 if (!buffer_manager_.SetBuffers( |
| 317 frame_count, frame_length, |
| 318 make_scoped_ptr(new base::SharedMemory(buffer_handle, false)), |
| 319 true)) { |
| 320 NotifyError(PP_ERROR_FAILED); |
| 321 return; |
| 322 } |
| 323 |
| 324 WriteVideoFrame(get_video_frame_data_); |
| 325 get_video_frame_data_ = nullptr; |
| 326 RunCallback(&get_video_frame_callback_, PP_OK); |
| 327 } |
| 328 |
| 329 void VideoEncoderResource::OnPluginMsgEncodeReply( |
| 330 const scoped_refptr<TrackedCallback>& callback, |
| 331 const ResourceMessageReplyParams& params, |
| 332 uint32_t frame_id) { |
| 333 encoder_last_error_ = params.result(); |
| 334 callback->Run(encoder_last_error_); |
| 335 buffer_manager_.EnqueueBuffer(frame_id); |
| 336 |
| 337 // If the plugin is waiting for a video frame, we can give the one |
| 338 // that just became available again. |
| 339 if (TrackedCallback::IsPending(get_video_frame_callback_)) { |
| 340 WriteVideoFrame(get_video_frame_data_); |
| 341 RunCallback(&get_video_frame_callback_, PP_OK); |
| 342 } |
| 343 } |
| 344 |
| 345 void VideoEncoderResource::OnPluginMsgBitstreamBufferReady( |
| 346 const ResourceMessageReplyParams& params, |
| 347 uint32_t buffer_id, |
| 348 uint32_t buffer_size, |
| 349 bool key_frame) { |
| 350 available_bitstream_buffers_.push_back( |
| 351 BitstreamBuffer(buffer_id, buffer_size, PP_FromBool(key_frame))); |
| 352 |
| 353 if (TrackedCallback::IsPending(get_bitstream_buffer_callback_)) { |
| 354 BitstreamBuffer buffer(available_bitstream_buffers_.front()); |
| 355 available_bitstream_buffers_.pop_front(); |
| 356 WriteBitstreamBuffer(get_bitstream_buffer_data_, buffer); |
| 357 get_bitstream_buffer_data_ = nullptr; |
| 358 |
| 359 scoped_refptr<TrackedCallback> callback; |
| 360 get_bitstream_buffer_callback_.swap(callback); |
| 361 callback->Run(PP_OK); |
| 362 } |
| 363 } |
| 364 |
| 365 void VideoEncoderResource::OnPluginMsgNotifyError( |
| 366 const ResourceMessageReplyParams& params, |
| 367 int32_t error) { |
| 368 NotifyError(error); |
| 369 } |
| 370 |
| 371 void VideoEncoderResource::NotifyError(int32_t error) { |
| 372 encoder_last_error_ = error; |
| 373 RunCallback(&get_supported_profiles_callback_, error); |
| 374 RunCallback(&initialize_callback_, error); |
| 375 RunCallback(&get_video_frame_callback_, error); |
| 376 RunCallback(&get_bitstream_buffer_callback_, error); |
| 377 } |
| 378 |
| 379 bool VideoEncoderResource::WriteVideoFrame(PP_Resource* output) { |
| 380 int32_t frame_id = buffer_manager_.DequeueBuffer(); |
| 381 if (frame_id < 0) |
| 382 return false; |
| 383 |
| 384 scoped_refptr<VideoFrameResource> resource = new VideoFrameResource( |
| 385 pp_instance(), frame_id, buffer_manager_.GetBufferPointer(frame_id)); |
| 386 video_frames_.insert(VideoFrameMap::value_type(resource->pp_resource(), |
| 387 resource)); |
| 388 |
| 389 *output = resource->GetReference(); |
| 390 |
| 391 return true; |
| 392 } |
| 393 |
| 394 void VideoEncoderResource::WriteBitstreamBuffer( |
| 395 PP_BitstreamBuffer* bitstream_buffer, |
| 396 const BitstreamBuffer& buffer) { |
| 397 bitstream_buffer->size = buffer.size; |
| 398 bitstream_buffer->buffer = shm_buffers_[buffer.id]->shm->memory(); |
| 399 bitstream_buffer->key_frame = PP_FromBool(buffer.key_frame); |
| 400 } |
| 401 |
| 402 void VideoEncoderResource::ReleaseFrames() { |
| 403 for (VideoFrameMap::iterator it = video_frames_.begin(); |
| 404 it != video_frames_.end(); ++it) { |
| 405 it->second->Invalidate(); |
| 406 it->second = nullptr; |
| 407 } |
76 } | 408 } |
77 | 409 |
78 } // namespace proxy | 410 } // namespace proxy |
79 } // namespace ppapi | 411 } // namespace ppapi |
OLD | NEW |