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