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