Android NTP: Most visited tile clicks should have PageTransition.AUTO_BOOKMARK
[chromium-blink-merge.git] / ppapi / proxy / video_encoder_resource_unittest.cc
blobc1d39084d5863b9914d0e146f322afe3e139efcc
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.
5 #include "base/memory/shared_memory.h"
6 #include "base/process/process.h"
7 #include "base/synchronization/waitable_event.h"
8 #include "ppapi/c/pp_codecs.h"
9 #include "ppapi/c/pp_errors.h"
10 #include "ppapi/c/ppb_video_encoder.h"
11 #include "ppapi/c/ppb_video_frame.h"
12 #include "ppapi/proxy/locking_resource_releaser.h"
13 #include "ppapi/proxy/plugin_message_filter.h"
14 #include "ppapi/proxy/ppapi_message_utils.h"
15 #include "ppapi/proxy/ppapi_messages.h"
16 #include "ppapi/proxy/ppapi_proxy_test.h"
17 #include "ppapi/proxy/video_encoder_resource.h"
18 #include "ppapi/shared_impl/media_stream_buffer.h"
19 #include "ppapi/shared_impl/proxy_lock.h"
20 #include "ppapi/thunk/thunk.h"
22 using ppapi::proxy::ResourceMessageTestSink;
24 namespace ppapi {
25 namespace proxy {
27 namespace {
29 class MockCompletionCallback {
30 public:
31 MockCompletionCallback() : called_(false), call_event_(false, false) {}
33 bool called() { return called_; }
34 int32_t result() { return result_; }
36 void WaitUntilCalled() { call_event_.Wait(); }
38 void Reset() {
39 called_ = false;
40 call_event_.Reset();
43 static void Callback(void* user_data, int32_t result) {
44 MockCompletionCallback* that =
45 reinterpret_cast<MockCompletionCallback*>(user_data);
46 that->call_event_.Signal();
47 that->called_ = true;
48 that->result_ = result;
51 private:
52 bool called_;
53 int32_t result_;
54 base::WaitableEvent call_event_;
57 class VideoEncoderResourceTest : public PluginProxyTest,
58 public MediaStreamBufferManager::Delegate {
59 public:
60 VideoEncoderResourceTest()
61 : encoder_iface_(thunk::GetPPB_VideoEncoder_0_2_Thunk()),
62 encoder_iface_0_1_(thunk::GetPPB_VideoEncoder_0_1_Thunk()),
63 video_frames_manager_(this) {}
64 ~VideoEncoderResourceTest() override {}
66 const PPB_VideoEncoder_0_2* encoder_iface() const { return encoder_iface_; }
67 const PPB_VideoEncoder_0_1* encoder_iface_0_1() const {
68 return encoder_iface_0_1_;
71 const uint32_t kBitstreamBufferSize = 4000;
72 const uint32_t kBitstreamBufferCount = 5;
73 const uint32_t kVideoFrameCount = 3;
74 const uint32_t kBitrate = 200000;
76 const PP_Size kFrameSize = PP_MakeSize(640, 480);
78 void SendReply(const ResourceMessageCallParams& params,
79 int32_t result,
80 const IPC::Message& nested_message) {
81 ResourceMessageReplyParams reply_params(params.pp_resource(),
82 params.sequence());
83 reply_params.set_result(result);
84 PluginMessageFilter::DispatchResourceReplyForTest(reply_params,
85 nested_message);
88 void SendReplyWithHandle(const ResourceMessageCallParams& params,
89 int32_t result,
90 const IPC::Message& nested_message,
91 const SerializedHandle& handle) {
92 ResourceMessageReplyParams reply_params(params.pp_resource(),
93 params.sequence());
94 reply_params.set_result(result);
95 reply_params.AppendHandle(handle);
96 PluginMessageFilter::DispatchResourceReplyForTest(reply_params,
97 nested_message);
100 void SendReplyWithHandles(const ResourceMessageCallParams& params,
101 int32_t result,
102 const IPC::Message& nested_message,
103 const std::vector<SerializedHandle>& handles) {
104 ResourceMessageReplyParams reply_params(params.pp_resource(),
105 params.sequence());
106 reply_params.set_result(result);
107 for (SerializedHandle handle : handles)
108 reply_params.AppendHandle(handle);
109 PluginMessageFilter::DispatchResourceReplyForTest(reply_params,
110 nested_message);
113 PP_Resource CreateEncoder() {
114 PP_Resource result = encoder_iface()->Create(pp_instance());
115 return result;
118 void CreateBitstreamSharedMemory(uint32_t buffer_size, uint32_t nb_buffers) {
119 shared_memory_bitstreams_.clear();
120 for (uint32_t i = 0; i < nb_buffers; ++i) {
121 scoped_ptr<base::SharedMemory> mem(new base::SharedMemory());
122 ASSERT_TRUE(mem->CreateAnonymous(buffer_size));
123 shared_memory_bitstreams_.push_back(mem.Pass());
127 void CreateVideoFramesSharedMemory(uint32_t frame_length,
128 uint32_t frame_count) {
129 scoped_ptr<base::SharedMemory> shared_memory_frames(
130 new base::SharedMemory());
131 uint32_t buffer_length =
132 frame_length + sizeof(ppapi::MediaStreamBuffer::Video);
133 ASSERT_TRUE(shared_memory_frames->CreateAnonymous(buffer_length *
134 frame_count));
135 ASSERT_TRUE(video_frames_manager_.SetBuffers(frame_count,
136 buffer_length,
137 shared_memory_frames.Pass(),
138 true));
139 for (int32_t i = 0; i < video_frames_manager_.number_of_buffers(); ++i) {
140 ppapi::MediaStreamBuffer::Video* buffer =
141 &(video_frames_manager_.GetBufferPointer(i)->video);
142 buffer->header.size = buffer_length;
143 buffer->header.type = ppapi::MediaStreamBuffer::TYPE_VIDEO;
144 buffer->format = PP_VIDEOFRAME_FORMAT_I420;
145 buffer->size = kFrameSize;
146 buffer->data_size = frame_length;
150 PP_Resource CreateAndInitializeEncoder() {
151 PP_Resource encoder = CreateEncoder();
152 PP_Size size = kFrameSize;
153 MockCompletionCallback cb;
154 int32_t result = encoder_iface()->Initialize(
155 encoder, PP_VIDEOFRAME_FORMAT_I420, &size, PP_VIDEOPROFILE_H264MAIN,
156 kBitrate, PP_HARDWAREACCELERATION_WITHFALLBACK,
157 PP_MakeOptionalCompletionCallback(&MockCompletionCallback::Callback,
158 &cb));
159 if (result != PP_OK_COMPLETIONPENDING)
160 return 0;
161 ResourceMessageCallParams params;
162 IPC::Message msg;
163 if (!sink().GetFirstResourceCallMatching(
164 PpapiHostMsg_VideoEncoder_Initialize::ID, &params, &msg))
165 return 0;
166 sink().ClearMessages();
168 SendInitializeReply(params, PP_OK, kVideoFrameCount, kFrameSize);
169 CreateBitstreamSharedMemory(kBitstreamBufferSize, kBitstreamBufferCount);
170 SendBitstreamBuffers(params, kBitstreamBufferSize);
172 if (!cb.called() || cb.result() != PP_OK)
173 return 0;
175 return encoder;
178 int32_t CallGetFramesRequired(PP_Resource pp_encoder) {
179 return encoder_iface()->GetFramesRequired(pp_encoder);
182 int32_t CallGetFrameCodedSize(PP_Resource pp_encoder, PP_Size* coded_size) {
183 return encoder_iface()->GetFrameCodedSize(pp_encoder, coded_size);
186 int32_t CallGetVideoFrame(PP_Resource pp_encoder,
187 PP_Resource* video_frame,
188 MockCompletionCallback* cb) {
189 return encoder_iface()->GetVideoFrame(
190 pp_encoder, video_frame, PP_MakeOptionalCompletionCallback(
191 &MockCompletionCallback::Callback, cb));
194 int32_t CallFirstGetVideoFrame(PP_Resource pp_encoder,
195 PP_Resource* video_frame,
196 MockCompletionCallback* cb) {
197 int32_t result = encoder_iface()->GetVideoFrame(
198 pp_encoder, video_frame, PP_MakeOptionalCompletionCallback(
199 &MockCompletionCallback::Callback, cb));
200 if (result != PP_OK_COMPLETIONPENDING)
201 return result;
203 ResourceMessageCallParams params;
204 CheckGetVideoFramesMsg(&params);
206 uint32_t frame_length = kFrameSize.width * kFrameSize.height * 2;
207 CreateVideoFramesSharedMemory(frame_length, kVideoFrameCount);
208 SendGetVideoFramesReply(params, kVideoFrameCount, frame_length, kFrameSize);
210 return result;
213 int32_t CallEncode(PP_Resource pp_encoder,
214 PP_Resource video_frame,
215 PP_Bool force_keyframe,
216 MockCompletionCallback* cb) {
217 return encoder_iface()->Encode(pp_encoder, video_frame, force_keyframe,
218 PP_MakeOptionalCompletionCallback(
219 &MockCompletionCallback::Callback, cb));
222 int32_t CallCompleteEncode(PP_Resource pp_encoder,
223 PP_Resource video_frame,
224 PP_Bool force_keyframe,
225 MockCompletionCallback* cb) {
226 int32_t result =
227 encoder_iface()->Encode(pp_encoder, video_frame, force_keyframe,
228 PP_MakeOptionalCompletionCallback(
229 &MockCompletionCallback::Callback, cb));
230 if (result != PP_OK_COMPLETIONPENDING)
231 return result;
233 ResourceMessageCallParams params;
234 uint32_t frame_id;
235 bool forced_keyframe;
236 if (!CheckEncodeMsg(&params, &frame_id, &forced_keyframe))
237 return PP_ERROR_FAILED;
239 SendEncodeReply(params, frame_id);
241 return result;
244 int32_t CallGetBitstreamBuffer(PP_Resource pp_encoder,
245 PP_BitstreamBuffer* bitstream_buffer,
246 MockCompletionCallback* cb) {
247 return encoder_iface()->GetBitstreamBuffer(
248 pp_encoder, bitstream_buffer,
249 PP_MakeOptionalCompletionCallback(&MockCompletionCallback::Callback,
250 cb));
253 void CallRecycleBitstreamBuffer(PP_Resource pp_encoder,
254 const PP_BitstreamBuffer& buffer) {
255 encoder_iface()->RecycleBitstreamBuffer(pp_encoder, &buffer);
258 void CallRequestEncodingParametersChange(PP_Resource pp_encoder,
259 uint32_t bitrate,
260 uint32_t framerate) {
261 encoder_iface()->RequestEncodingParametersChange(pp_encoder, bitrate,
262 framerate);
265 void CallClose(PP_Resource pp_encoder) {
266 encoder_iface()->Close(pp_encoder);
269 void SendGetSupportedProfilesReply(
270 const ResourceMessageCallParams& params,
271 const std::vector<PP_VideoProfileDescription>& profiles) {
272 SendReply(params, PP_OK,
273 PpapiPluginMsg_VideoEncoder_GetSupportedProfilesReply(profiles));
276 void SendInitializeReply(const ResourceMessageCallParams& params,
277 int32_t success,
278 uint32_t input_frame_count,
279 const PP_Size& input_coded_size) {
280 SendReply(params, success, PpapiPluginMsg_VideoEncoder_InitializeReply(
281 input_frame_count, input_coded_size));
284 void SendBitstreamBuffers(const ResourceMessageCallParams& params,
285 uint32_t buffer_length) {
286 std::vector<SerializedHandle> handles;
287 for (base::SharedMemory* mem : shared_memory_bitstreams_) {
288 ASSERT_EQ(mem->requested_size(), buffer_length);
289 base::SharedMemoryHandle handle;
291 ASSERT_TRUE(
292 mem->ShareToProcess(base::Process::Current().Handle(), &handle));
293 handles.push_back(SerializedHandle(handle, buffer_length));
295 SendReplyWithHandles(
296 params, PP_OK,
297 PpapiPluginMsg_VideoEncoder_BitstreamBuffers(buffer_length), handles);
300 void SendGetVideoFramesReply(const ResourceMessageCallParams& params,
301 uint32_t frame_count,
302 uint32_t frame_length,
303 const PP_Size& size) {
304 base::SharedMemoryHandle handle;
305 ASSERT_TRUE(video_frames_manager_.shm()->ShareToProcess(
306 base::Process::Current().Handle(), &handle));
307 SendReplyWithHandle(
308 params, PP_OK, PpapiPluginMsg_VideoEncoder_GetVideoFramesReply(
309 frame_count,
310 frame_length + sizeof(MediaStreamBuffer::Video),
311 size),
312 SerializedHandle(
313 handle,
314 static_cast<uint32_t>(
315 video_frames_manager_.shm()->requested_size())));
318 void SendEncodeReply(const ResourceMessageCallParams& params,
319 uint32_t frame_id) {
320 SendReply(params, PP_OK, PpapiPluginMsg_VideoEncoder_EncodeReply(frame_id));
323 void SendBitstreamBufferReady(const ResourceMessageCallParams& params,
324 uint32_t buffer_id,
325 uint32_t buffer_size,
326 bool keyframe) {
327 SendReply(params, PP_OK,
328 PpapiPluginMsg_VideoEncoder_BitstreamBufferReady(
329 buffer_id, buffer_size, PP_FromBool(keyframe)));
332 void SendNotifyError(const ResourceMessageCallParams& params, int32_t error) {
333 SendReply(params, PP_OK, PpapiPluginMsg_VideoEncoder_NotifyError(error));
336 bool CheckGetSupportedProfilesMsg(ResourceMessageCallParams* params) {
337 IPC::Message msg;
338 return sink().GetFirstResourceCallMatching(
339 PpapiHostMsg_VideoEncoder_GetSupportedProfiles::ID, params, &msg);
342 bool CheckInitializeMsg(ResourceMessageCallParams* params,
343 PP_VideoFrame_Format* input_format,
344 struct PP_Size* input_visible_size,
345 PP_VideoProfile* output_profile,
346 uint32_t* bitrate,
347 PP_HardwareAcceleration* acceleration) {
348 IPC::Message msg;
349 if (!sink().GetFirstResourceCallMatching(
350 PpapiHostMsg_VideoEncoder_Initialize::ID, params, &msg))
351 return false;
352 sink().ClearMessages();
353 return UnpackMessage<PpapiHostMsg_VideoEncoder_Initialize>(
354 msg, input_format, input_visible_size, output_profile, bitrate,
355 acceleration);
358 bool CheckGetVideoFramesMsg(ResourceMessageCallParams* params) {
359 IPC::Message msg;
360 if (!sink().GetFirstResourceCallMatching(
361 PpapiHostMsg_VideoEncoder_GetVideoFrames::ID, params, &msg))
362 return false;
363 sink().ClearMessages();
364 return true;
367 bool CheckEncodeMsg(ResourceMessageCallParams* params,
368 uint32_t* frame_id,
369 bool* keyframe) {
370 IPC::Message msg;
371 if (!sink().GetFirstResourceCallMatching(
372 PpapiHostMsg_VideoEncoder_Encode::ID, params, &msg))
373 return false;
374 sink().ClearMessages();
375 return UnpackMessage<PpapiHostMsg_VideoEncoder_Encode>(msg, frame_id,
376 keyframe);
379 bool CheckRecycleBitstreamBufferMsg(ResourceMessageCallParams* params,
380 uint32_t* buffer_id) {
381 IPC::Message msg;
382 if (!sink().GetFirstResourceCallMatching(
383 PpapiHostMsg_VideoEncoder_RecycleBitstreamBuffer::ID, params, &msg))
384 return false;
385 sink().ClearMessages();
386 return UnpackMessage<PpapiHostMsg_VideoEncoder_RecycleBitstreamBuffer>(
387 msg, buffer_id);
390 bool CheckRequestEncodingParametersChangeMsg(
391 ResourceMessageCallParams* params,
392 uint32_t* bitrate,
393 uint32_t* framerate) {
394 IPC::Message msg;
395 if (!sink().GetFirstResourceCallMatching(
396 PpapiHostMsg_VideoEncoder_RequestEncodingParametersChange::ID,
397 params, &msg))
398 return false;
399 sink().ClearMessages();
400 return UnpackMessage<
401 PpapiHostMsg_VideoEncoder_RequestEncodingParametersChange>(msg, bitrate,
402 framerate);
405 bool CheckIsVideoFrame(PP_Resource video_frame) {
406 return thunk::GetPPB_VideoFrame_0_1_Thunk()->IsVideoFrame(video_frame);
409 bool CheckIsVideoFrameValid(PP_Resource video_frame) {
410 PP_Size frame_size;
411 return thunk::GetPPB_VideoFrame_0_1_Thunk()->GetSize(
412 video_frame, &frame_size) == PP_TRUE;
415 private:
416 // MediaStreamBufferManager::Delegate:
417 void OnNewBufferEnqueued() override {}
419 const PPB_VideoEncoder_0_2* encoder_iface_;
420 const PPB_VideoEncoder_0_1* encoder_iface_0_1_;
422 ScopedVector<base::SharedMemory> shared_memory_bitstreams_;
424 MediaStreamBufferManager video_frames_manager_;
427 void* ForwardUserData(void* user_data,
428 uint32_t element_count,
429 uint32_t element_size) {
430 return user_data;
433 } // namespace
435 TEST_F(VideoEncoderResourceTest, GetSupportedProfiles) {
436 // Verifies that GetSupportedProfiles calls into the renderer and
437 // the we get the right results back.
439 LockingResourceReleaser encoder(CreateEncoder());
440 PP_VideoProfileDescription profiles[2];
441 PP_ArrayOutput output;
442 output.user_data = &profiles[0];
443 output.GetDataBuffer = ForwardUserData;
444 ResourceMessageCallParams params;
445 MockCompletionCallback cb;
446 int32_t result = encoder_iface()->GetSupportedProfiles(
447 encoder.get(), output, PP_MakeOptionalCompletionCallback(
448 &MockCompletionCallback::Callback, &cb));
449 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
450 ASSERT_TRUE(CheckGetSupportedProfilesMsg(&params));
452 std::vector<PP_VideoProfileDescription> profiles_response;
453 PP_VideoProfileDescription profile;
454 profile.profile = PP_VIDEOPROFILE_H264MAIN;
455 profile.max_resolution.width = 1920;
456 profile.max_resolution.height = 1080;
457 profile.max_framerate_numerator = 30;
458 profile.max_framerate_denominator = 1;
459 profile.hardware_accelerated = PP_TRUE;
460 profiles_response.push_back(profile);
461 profile.profile = PP_VIDEOPROFILE_VP8_ANY;
462 profile.max_resolution.width = 1920;
463 profile.max_resolution.height = 1080;
464 profile.max_framerate_numerator = 30;
465 profile.max_framerate_denominator = 1;
466 profile.hardware_accelerated = PP_FALSE;
467 profiles_response.push_back(profile);
469 SendGetSupportedProfilesReply(params, profiles_response);
470 ASSERT_EQ(profiles_response.size(), static_cast<uint32_t>(cb.result()));
471 ASSERT_EQ(0, memcmp(&profiles[0], &profiles_response[0], sizeof(profiles)));
475 TEST_F(VideoEncoderResourceTest, GetSupportedProfiles0_1) {
476 // Verifies that GetSupportedProfiles calls into the renderer and
477 // the we get the right results back.
479 LockingResourceReleaser encoder(CreateEncoder());
480 PP_VideoProfileDescription_0_1 profiles[2];
481 PP_ArrayOutput output;
482 output.user_data = &profiles[0];
483 output.GetDataBuffer = ForwardUserData;
484 ResourceMessageCallParams params;
485 MockCompletionCallback cb;
486 int32_t result = encoder_iface_0_1()->GetSupportedProfiles(
487 encoder.get(), output, PP_MakeOptionalCompletionCallback(
488 &MockCompletionCallback::Callback, &cb));
489 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
490 ASSERT_TRUE(CheckGetSupportedProfilesMsg(&params));
492 std::vector<PP_VideoProfileDescription> profiles_response;
493 PP_VideoProfileDescription profile;
494 profile.profile = PP_VIDEOPROFILE_H264MAIN;
495 profile.max_resolution.width = 1920;
496 profile.max_resolution.height = 1080;
497 profile.max_framerate_numerator = 30;
498 profile.max_framerate_denominator = 1;
499 profile.hardware_accelerated = PP_TRUE;
500 profiles_response.push_back(profile);
501 profile.profile = PP_VIDEOPROFILE_VP8_ANY;
502 profile.max_resolution.width = 1920;
503 profile.max_resolution.height = 1080;
504 profile.max_framerate_numerator = 30;
505 profile.max_framerate_denominator = 1;
506 profile.hardware_accelerated = PP_FALSE;
507 profiles_response.push_back(profile);
509 SendGetSupportedProfilesReply(params, profiles_response);
511 ASSERT_EQ(profiles_response.size(), static_cast<uint32_t>(cb.result()));
513 for (uint32 i = 0; i < profiles_response.size(); i++) {
514 ASSERT_EQ(profiles_response[i].profile, profiles[i].profile);
515 ASSERT_EQ(profiles_response[i].max_resolution.width,
516 profiles[i].max_resolution.width);
517 ASSERT_EQ(profiles_response[i].max_resolution.height,
518 profiles[i].max_resolution.height);
519 ASSERT_EQ(profiles_response[i].max_framerate_numerator,
520 profiles[i].max_framerate_numerator);
521 ASSERT_EQ(profiles_response[i].max_framerate_denominator,
522 profiles[i].max_framerate_denominator);
523 if (profiles_response[i].hardware_accelerated)
524 ASSERT_EQ(PP_HARDWAREACCELERATION_ONLY, profiles[i].acceleration);
525 else
526 ASSERT_EQ(PP_HARDWAREACCELERATION_NONE, profiles[i].acceleration);
531 TEST_F(VideoEncoderResourceTest, InitializeFailure) {
533 // Verify the initialize callback is called in case of failure.
534 LockingResourceReleaser encoder(CreateEncoder());
535 ResourceMessageCallParams params;
536 PP_Size size = kFrameSize;
537 MockCompletionCallback cb;
538 int32_t result = encoder_iface()->Initialize(
539 encoder.get(), PP_VIDEOFRAME_FORMAT_BGRA, &size,
540 PP_VIDEOPROFILE_H264MAIN, kBitrate,
541 PP_HARDWAREACCELERATION_WITHFALLBACK,
542 PP_MakeOptionalCompletionCallback(&MockCompletionCallback::Callback,
543 &cb));
544 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
546 PP_VideoFrame_Format input_format;
547 PP_Size input_visible_size;
548 PP_VideoProfile output_profile;
549 uint32_t bitrate;
550 PP_HardwareAcceleration acceleration;
551 ASSERT_TRUE(CheckInitializeMsg(&params, &input_format, &input_visible_size,
552 &output_profile, &bitrate, &acceleration));
553 ASSERT_EQ(PP_VIDEOFRAME_FORMAT_BGRA, input_format);
554 ASSERT_EQ(size.width, input_visible_size.width);
555 ASSERT_EQ(size.height, input_visible_size.height);
556 ASSERT_EQ(kBitrate, bitrate);
557 ASSERT_EQ(PP_VIDEOPROFILE_H264MAIN, output_profile);
558 ASSERT_EQ(PP_HARDWAREACCELERATION_WITHFALLBACK, acceleration);
560 SendInitializeReply(params, PP_ERROR_NOTSUPPORTED, kVideoFrameCount,
561 kFrameSize);
562 ASSERT_TRUE(cb.called());
563 ASSERT_EQ(PP_ERROR_NOTSUPPORTED, cb.result());
566 // Verify the initialize callback is called in case of error
567 // notification.
568 LockingResourceReleaser encoder(CreateEncoder());
569 ResourceMessageCallParams params;
570 PP_Size size = kFrameSize;
571 MockCompletionCallback cb;
572 int32_t result = encoder_iface()->Initialize(
573 encoder.get(), PP_VIDEOFRAME_FORMAT_BGRA, &size,
574 PP_VIDEOPROFILE_H264MAIN, kBitrate,
575 PP_HARDWAREACCELERATION_WITHFALLBACK,
576 PP_MakeOptionalCompletionCallback(&MockCompletionCallback::Callback,
577 &cb));
578 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
580 PP_VideoFrame_Format input_format;
581 PP_Size input_visible_size;
582 PP_VideoProfile output_profile;
583 uint32_t bitrate;
584 PP_HardwareAcceleration acceleration;
585 ASSERT_TRUE(CheckInitializeMsg(&params, &input_format, &input_visible_size,
586 &output_profile, &bitrate, &acceleration));
587 ASSERT_EQ(PP_VIDEOFRAME_FORMAT_BGRA, input_format);
588 ASSERT_EQ(kFrameSize.width, input_visible_size.width);
589 ASSERT_EQ(kFrameSize.height, input_visible_size.height);
590 ASSERT_EQ(kBitrate, bitrate);
591 ASSERT_EQ(PP_VIDEOPROFILE_H264MAIN, output_profile);
592 ASSERT_EQ(PP_HARDWAREACCELERATION_WITHFALLBACK, acceleration);
594 ResourceMessageCallParams error_params(encoder.get(), 0);
595 SendNotifyError(error_params, PP_ERROR_FAILED);
596 ASSERT_TRUE(cb.called());
597 ASSERT_EQ(PP_ERROR_FAILED, cb.result());
600 // Verify that calling initialize twice fails the second time if
601 // we haven't received a response yet.
602 LockingResourceReleaser encoder(CreateEncoder());
603 ResourceMessageCallParams params;
604 PP_Size size = kFrameSize;
605 MockCompletionCallback cb;
606 int32_t result = encoder_iface()->Initialize(
607 encoder.get(), PP_VIDEOFRAME_FORMAT_BGRA, &size,
608 PP_VIDEOPROFILE_H264MAIN, kBitrate,
609 PP_HARDWAREACCELERATION_WITHFALLBACK,
610 PP_MakeOptionalCompletionCallback(&MockCompletionCallback::Callback,
611 &cb));
612 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
614 PP_VideoFrame_Format input_format;
615 PP_Size input_visible_size;
616 PP_VideoProfile output_profile;
617 uint32_t bitrate;
618 PP_HardwareAcceleration acceleration;
619 ASSERT_TRUE(CheckInitializeMsg(&params, &input_format, &input_visible_size,
620 &output_profile, &bitrate, &acceleration));
621 ASSERT_EQ(PP_VIDEOFRAME_FORMAT_BGRA, input_format);
622 ASSERT_EQ(size.width, input_visible_size.width);
623 ASSERT_EQ(size.height, input_visible_size.height);
624 ASSERT_EQ(kBitrate, bitrate);
625 ASSERT_EQ(PP_VIDEOPROFILE_H264MAIN, output_profile);
626 ASSERT_EQ(PP_HARDWAREACCELERATION_WITHFALLBACK, acceleration);
628 result = encoder_iface()->Initialize(
629 encoder.get(), PP_VIDEOFRAME_FORMAT_BGRA, &size,
630 PP_VIDEOPROFILE_H264MAIN, kBitrate,
631 PP_HARDWAREACCELERATION_WITHFALLBACK,
632 PP_MakeOptionalCompletionCallback(&MockCompletionCallback::Callback,
633 &cb));
634 ASSERT_EQ(PP_ERROR_INPROGRESS, result);
636 ResourceMessageCallParams error_params(encoder.get(), 0);
637 SendNotifyError(error_params, PP_ERROR_FAILED);
638 ASSERT_TRUE(cb.called());
639 ASSERT_EQ(PP_ERROR_FAILED, cb.result());
643 TEST_F(VideoEncoderResourceTest, InitializeSuccess) {
645 // Verify the initialize callback is called when initialization is
646 // successfull.
647 LockingResourceReleaser encoder(CreateEncoder());
648 ResourceMessageCallParams params;
649 PP_Size size = kFrameSize;
650 const uint32_t kBitrate = 420000;
651 MockCompletionCallback cb;
652 int32_t result = encoder_iface()->Initialize(
653 encoder.get(), PP_VIDEOFRAME_FORMAT_I420, &size,
654 PP_VIDEOPROFILE_H264MAIN, kBitrate,
655 PP_HARDWAREACCELERATION_WITHFALLBACK,
656 PP_MakeOptionalCompletionCallback(&MockCompletionCallback::Callback,
657 &cb));
658 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
660 PP_VideoFrame_Format input_format;
661 PP_Size input_visible_size;
662 PP_VideoProfile output_profile;
663 uint32_t bitrate;
664 PP_HardwareAcceleration acceleration;
665 ASSERT_TRUE(CheckInitializeMsg(&params, &input_format, &input_visible_size,
666 &output_profile, &bitrate, &acceleration));
667 ASSERT_EQ(PP_VIDEOFRAME_FORMAT_I420, input_format);
668 ASSERT_EQ(kFrameSize.width, input_visible_size.width);
669 ASSERT_EQ(kFrameSize.height, input_visible_size.height);
670 ASSERT_EQ(kBitrate, bitrate);
671 ASSERT_EQ(PP_VIDEOPROFILE_H264MAIN, output_profile);
672 ASSERT_EQ(PP_HARDWAREACCELERATION_WITHFALLBACK, acceleration);
674 SendInitializeReply(params, PP_OK, kVideoFrameCount, kFrameSize);
676 ASSERT_TRUE(cb.called());
677 ASSERT_EQ(PP_OK, cb.result());
680 // Verify that calling initialize a second time, after it already
681 // succeeded, fails.
682 LockingResourceReleaser encoder(CreateEncoder());
683 ResourceMessageCallParams params;
684 PP_Size size = kFrameSize;
685 const uint32_t kBitrate = 420000;
686 MockCompletionCallback cb;
687 int32_t result = encoder_iface()->Initialize(
688 encoder.get(), PP_VIDEOFRAME_FORMAT_I420, &size,
689 PP_VIDEOPROFILE_H264MAIN, kBitrate,
690 PP_HARDWAREACCELERATION_WITHFALLBACK,
691 PP_MakeOptionalCompletionCallback(&MockCompletionCallback::Callback,
692 &cb));
693 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
695 PP_VideoFrame_Format input_format;
696 PP_Size input_visible_size;
697 PP_VideoProfile output_profile;
698 uint32_t bitrate;
699 PP_HardwareAcceleration acceleration;
700 ASSERT_TRUE(CheckInitializeMsg(&params, &input_format, &input_visible_size,
701 &output_profile, &bitrate, &acceleration));
702 ASSERT_EQ(PP_VIDEOFRAME_FORMAT_I420, input_format);
703 ASSERT_EQ(kFrameSize.width, input_visible_size.width);
704 ASSERT_EQ(kFrameSize.height, input_visible_size.height);
705 ASSERT_EQ(kBitrate, bitrate);
706 ASSERT_EQ(PP_VIDEOPROFILE_H264MAIN, output_profile);
707 ASSERT_EQ(PP_HARDWAREACCELERATION_WITHFALLBACK, acceleration);
709 SendInitializeReply(params, PP_OK, kVideoFrameCount, kFrameSize);
711 ASSERT_TRUE(cb.called());
712 ASSERT_EQ(PP_OK, cb.result());
714 result = encoder_iface()->Initialize(
715 encoder.get(), PP_VIDEOFRAME_FORMAT_I420, &size,
716 PP_VIDEOPROFILE_H264MAIN, kBitrate,
717 PP_HARDWAREACCELERATION_WITHFALLBACK,
718 PP_MakeOptionalCompletionCallback(&MockCompletionCallback::Callback,
719 &cb));
720 ASSERT_EQ(PP_ERROR_FAILED, result);
723 // Verify the sending the bitstream buffers details makes them
724 // available through the API. the right values.
725 LockingResourceReleaser encoder(CreateEncoder());
726 ResourceMessageCallParams params;
727 PP_Size size = kFrameSize;
728 const uint32_t kBitrate = 420000;
729 MockCompletionCallback cb;
730 int32_t result = encoder_iface()->Initialize(
731 encoder.get(), PP_VIDEOFRAME_FORMAT_I420, &size,
732 PP_VIDEOPROFILE_H264MAIN, kBitrate,
733 PP_HARDWAREACCELERATION_WITHFALLBACK,
734 PP_MakeOptionalCompletionCallback(&MockCompletionCallback::Callback,
735 &cb));
736 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
738 PP_VideoFrame_Format input_format;
739 PP_Size input_visible_size;
740 PP_VideoProfile output_profile;
741 uint32_t bitrate;
742 PP_HardwareAcceleration acceleration;
743 ASSERT_TRUE(CheckInitializeMsg(&params, &input_format, &input_visible_size,
744 &output_profile, &bitrate, &acceleration));
745 ASSERT_EQ(PP_VIDEOFRAME_FORMAT_I420, input_format);
746 ASSERT_EQ(kFrameSize.width, input_visible_size.width);
747 ASSERT_EQ(kFrameSize.height, input_visible_size.height);
748 ASSERT_EQ(kBitrate, bitrate);
749 ASSERT_EQ(PP_VIDEOPROFILE_H264MAIN, output_profile);
750 ASSERT_EQ(PP_HARDWAREACCELERATION_WITHFALLBACK, acceleration);
752 SendInitializeReply(params, PP_OK, kVideoFrameCount, kFrameSize);
754 ASSERT_TRUE(cb.called());
755 ASSERT_EQ(PP_OK, cb.result());
757 PP_Size coded_size;
758 ASSERT_EQ(PP_OK, CallGetFrameCodedSize(encoder.get(), &coded_size));
759 ASSERT_EQ(kFrameSize.width, coded_size.width);
760 ASSERT_EQ(kFrameSize.height, coded_size.height);
761 ASSERT_EQ(static_cast<int32_t>(kVideoFrameCount),
762 CallGetFramesRequired(encoder.get()));
766 TEST_F(VideoEncoderResourceTest, Uninitialized) {
767 // Operations on uninitialized encoders should fail.
768 LockingResourceReleaser encoder(CreateEncoder());
770 ASSERT_EQ(PP_ERROR_FAILED, CallGetFramesRequired(encoder.get()));
772 PP_Size size;
773 ASSERT_EQ(PP_ERROR_FAILED, CallGetFrameCodedSize(encoder.get(), &size));
775 MockCompletionCallback uncalled_cb;
776 PP_Resource video_frame = 0;
777 ASSERT_EQ(PP_ERROR_FAILED,
778 CallGetVideoFrame(encoder.get(), &video_frame, &uncalled_cb));
779 ASSERT_FALSE(uncalled_cb.called());
780 ASSERT_EQ(0, video_frame);
782 ASSERT_EQ(PP_ERROR_FAILED,
783 CallEncode(encoder.get(), video_frame, PP_FALSE, &uncalled_cb));
784 ASSERT_FALSE(uncalled_cb.called());
786 PP_BitstreamBuffer bitstream_buffer;
787 ASSERT_EQ(
788 PP_ERROR_FAILED,
789 CallGetBitstreamBuffer(encoder.get(), &bitstream_buffer, &uncalled_cb));
790 ASSERT_FALSE(uncalled_cb.called());
792 ResourceMessageCallParams params;
793 uint32_t buffer_id;
794 CallRecycleBitstreamBuffer(encoder.get(), bitstream_buffer);
795 ASSERT_FALSE(CheckRecycleBitstreamBufferMsg(&params, &buffer_id));
797 uint32_t bitrate, framerate;
798 CallRequestEncodingParametersChange(encoder.get(), 0, 0);
799 ASSERT_FALSE(
800 CheckRequestEncodingParametersChangeMsg(&params, &bitrate, &framerate));
803 TEST_F(VideoEncoderResourceTest, InitializeAndGetVideoFrame) {
804 // Verify that we can pull the right number of video frames before
805 // the proxy makes us wait.
806 LockingResourceReleaser encoder(CreateAndInitializeEncoder());
807 ResourceMessageCallParams params;
808 std::vector<PP_Resource> video_frames;
809 MockCompletionCallback get_frame_cb;
811 video_frames.resize(kVideoFrameCount + 1);
813 ASSERT_EQ(PP_OK_COMPLETIONPENDING,
814 CallGetVideoFrame(encoder.get(), &video_frames[0], &get_frame_cb));
815 ASSERT_FALSE(get_frame_cb.called());
816 ASSERT_TRUE(CheckGetVideoFramesMsg(&params));
818 uint32_t frame_length = kFrameSize.width * kFrameSize.height * 2;
819 CreateVideoFramesSharedMemory(frame_length, kVideoFrameCount);
820 SendGetVideoFramesReply(params, kVideoFrameCount, frame_length, kFrameSize);
822 for (uint32_t i = 1; i < kVideoFrameCount; ++i) {
823 get_frame_cb.Reset();
824 ASSERT_EQ(
825 PP_OK_COMPLETIONPENDING,
826 CallGetVideoFrame(encoder.get(), &video_frames[i], &get_frame_cb));
827 ASSERT_TRUE(get_frame_cb.called());
828 ASSERT_EQ(PP_OK, get_frame_cb.result());
829 ASSERT_TRUE(CheckIsVideoFrame(video_frames[i]));
832 get_frame_cb.Reset();
833 ASSERT_EQ(PP_OK_COMPLETIONPENDING,
834 CallGetVideoFrame(encoder.get(), &video_frames[kVideoFrameCount],
835 &get_frame_cb));
836 ASSERT_FALSE(get_frame_cb.called());
838 MockCompletionCallback get_frame_fail_cb;
839 ASSERT_EQ(PP_ERROR_INPROGRESS,
840 CallGetVideoFrame(encoder.get(), &video_frames[kVideoFrameCount],
841 &get_frame_fail_cb));
842 ASSERT_FALSE(get_frame_fail_cb.called());
844 // Unblock the GetVideoFrame callback by freeing up a frame.
845 MockCompletionCallback encode_cb;
846 ASSERT_EQ(
847 PP_OK_COMPLETIONPENDING,
848 CallCompleteEncode(encoder.get(), video_frames[0], PP_FALSE, &encode_cb));
849 ASSERT_TRUE(encode_cb.called());
850 ASSERT_EQ(PP_OK, encode_cb.result());
851 ASSERT_TRUE(get_frame_cb.called());
853 CallClose(encoder.get());
856 TEST_F(VideoEncoderResourceTest, Encode) {
857 // Check Encode() calls into the renderer.
858 LockingResourceReleaser encoder(CreateAndInitializeEncoder());
859 PP_Resource video_frame;
860 MockCompletionCallback get_frame_cb;
862 ASSERT_EQ(PP_OK_COMPLETIONPENDING,
863 CallFirstGetVideoFrame(encoder.get(), &video_frame, &get_frame_cb));
864 ASSERT_TRUE(get_frame_cb.called());
865 ASSERT_EQ(PP_OK, get_frame_cb.result());
867 MockCompletionCallback encode_cb;
868 ASSERT_EQ(PP_OK_COMPLETIONPENDING,
869 CallEncode(encoder.get(), video_frame, PP_TRUE, &encode_cb));
870 ASSERT_FALSE(encode_cb.called());
871 ASSERT_FALSE(CheckIsVideoFrameValid(video_frame));
873 ResourceMessageCallParams params;
874 uint32_t frame_id;
875 bool force_frame;
876 ASSERT_TRUE(CheckEncodeMsg(&params, &frame_id, &force_frame));
878 SendEncodeReply(params, frame_id);
880 ASSERT_TRUE(encode_cb.called());
881 ASSERT_EQ(PP_OK, encode_cb.result());
884 TEST_F(VideoEncoderResourceTest, EncodeAndGetVideoFrame) {
885 // Check the encoding loop works well.
886 LockingResourceReleaser encoder(CreateAndInitializeEncoder());
887 ResourceMessageCallParams params;
888 PP_Resource video_frame;
889 MockCompletionCallback get_frame_cb, encode_cb;
891 ASSERT_EQ(PP_OK_COMPLETIONPENDING,
892 CallFirstGetVideoFrame(encoder.get(), &video_frame, &get_frame_cb));
893 ASSERT_TRUE(get_frame_cb.called());
894 ASSERT_EQ(PP_OK, get_frame_cb.result());
896 for (uint32_t i = 1; i < 20 * kVideoFrameCount; ++i) {
897 encode_cb.Reset();
898 ASSERT_EQ(
899 PP_OK_COMPLETIONPENDING,
900 CallCompleteEncode(encoder.get(), video_frame, PP_FALSE, &encode_cb));
901 ASSERT_TRUE(encode_cb.called());
902 ASSERT_EQ(PP_OK, encode_cb.result());
904 get_frame_cb.Reset();
905 ASSERT_EQ(PP_OK_COMPLETIONPENDING,
906 CallGetVideoFrame(encoder.get(), &video_frame, &get_frame_cb));
907 ASSERT_TRUE(get_frame_cb.called());
908 ASSERT_EQ(PP_OK, get_frame_cb.result());
909 ASSERT_TRUE(CheckIsVideoFrame(video_frame));
912 ASSERT_EQ(
913 PP_OK_COMPLETIONPENDING,
914 CallCompleteEncode(encoder.get(), video_frame, PP_FALSE, &encode_cb));
915 ASSERT_TRUE(encode_cb.called());
916 ASSERT_EQ(PP_OK, encode_cb.result());
919 TEST_F(VideoEncoderResourceTest, GetBitstreamBuffer) {
921 // Verify that the GetBitstreamBuffer callback is fired whenever the
922 // renderer signals a buffer is available.
923 LockingResourceReleaser encoder(CreateAndInitializeEncoder());
925 MockCompletionCallback get_bitstream_buffer_cb;
926 PP_BitstreamBuffer bitstream_buffer;
927 ASSERT_EQ(PP_OK_COMPLETIONPENDING,
928 CallGetBitstreamBuffer(encoder.get(), &bitstream_buffer,
929 &get_bitstream_buffer_cb));
930 ASSERT_FALSE(get_bitstream_buffer_cb.called());
932 ResourceMessageCallParams buffer_params(encoder.get(), 0);
933 SendBitstreamBufferReady(buffer_params, 0, 10, true);
935 ASSERT_TRUE(get_bitstream_buffer_cb.called());
936 ASSERT_EQ(PP_OK, get_bitstream_buffer_cb.result());
937 ASSERT_EQ(10U, bitstream_buffer.size);
938 ASSERT_EQ(PP_TRUE, bitstream_buffer.key_frame);
941 // Verify that calling GetBitstreamBuffer a second time, while the
942 // first callback hasn't been fired fails.
943 LockingResourceReleaser encoder(CreateAndInitializeEncoder());
945 MockCompletionCallback get_bitstream_buffer_cb;
946 PP_BitstreamBuffer bitstream_buffer;
947 ASSERT_EQ(PP_OK_COMPLETIONPENDING,
948 CallGetBitstreamBuffer(encoder.get(), &bitstream_buffer,
949 &get_bitstream_buffer_cb));
950 ASSERT_FALSE(get_bitstream_buffer_cb.called());
952 ASSERT_EQ(PP_ERROR_INPROGRESS,
953 CallGetBitstreamBuffer(encoder.get(), &bitstream_buffer,
954 &get_bitstream_buffer_cb));
955 ASSERT_FALSE(get_bitstream_buffer_cb.called());
957 ResourceMessageCallParams buffer_params(encoder.get(), 0);
958 SendBitstreamBufferReady(buffer_params, 0, 10, true);
962 TEST_F(VideoEncoderResourceTest, RecycleBitstreamBuffer) {
963 // Verify that we signal the renderer that a bitstream buffer has been
964 // recycled.
965 LockingResourceReleaser encoder(CreateAndInitializeEncoder());
967 MockCompletionCallback get_bitstream_buffer_cb;
968 PP_BitstreamBuffer bitstream_buffer;
969 ASSERT_EQ(PP_OK_COMPLETIONPENDING,
970 CallGetBitstreamBuffer(encoder.get(), &bitstream_buffer,
971 &get_bitstream_buffer_cb));
972 ASSERT_FALSE(get_bitstream_buffer_cb.called());
974 ResourceMessageCallParams buffer_params(encoder.get(), 0);
975 SendBitstreamBufferReady(buffer_params, kBitstreamBufferCount - 1, 10, true);
977 ASSERT_TRUE(get_bitstream_buffer_cb.called());
978 ASSERT_EQ(PP_OK, get_bitstream_buffer_cb.result());
980 CallRecycleBitstreamBuffer(encoder.get(), bitstream_buffer);
982 ResourceMessageCallParams recycle_params;
983 uint32_t buffer_id;
984 ASSERT_TRUE(CheckRecycleBitstreamBufferMsg(&recycle_params, &buffer_id));
985 ASSERT_EQ(kBitstreamBufferCount - 1, buffer_id);
988 TEST_F(VideoEncoderResourceTest, RequestEncodingParametersChange) {
989 // Check encoding parameter changes are correctly sent to the
990 // renderer.
991 LockingResourceReleaser encoder(CreateAndInitializeEncoder());
993 CallRequestEncodingParametersChange(encoder.get(), 1, 2);
994 ResourceMessageCallParams params;
995 uint32_t bitrate, framerate;
996 ASSERT_TRUE(
997 CheckRequestEncodingParametersChangeMsg(&params, &bitrate, &framerate));
998 ASSERT_EQ(1U, bitrate);
999 ASSERT_EQ(2U, framerate);
1002 TEST_F(VideoEncoderResourceTest, NotifyError) {
1004 // Check an error from the encoder aborts GetVideoFrame and
1005 // GetBitstreamBuffer callbacks.
1006 LockingResourceReleaser encoder(CreateAndInitializeEncoder());
1008 MockCompletionCallback get_frame_cb;
1009 PP_Resource video_frame;
1010 ASSERT_EQ(PP_OK_COMPLETIONPENDING,
1011 CallGetVideoFrame(encoder.get(), &video_frame, &get_frame_cb));
1012 ASSERT_FALSE(get_frame_cb.called());
1014 MockCompletionCallback get_bitstream_buffer_cb;
1015 PP_BitstreamBuffer bitstream_buffer;
1016 ASSERT_EQ(PP_OK_COMPLETIONPENDING,
1017 CallGetBitstreamBuffer(encoder.get(), &bitstream_buffer,
1018 &get_bitstream_buffer_cb));
1020 ResourceMessageCallParams error_params(encoder.get(), 0);
1021 SendNotifyError(error_params, PP_ERROR_FAILED);
1023 ASSERT_TRUE(get_frame_cb.called());
1024 ASSERT_EQ(PP_ERROR_FAILED, get_frame_cb.result());
1025 ASSERT_TRUE(get_bitstream_buffer_cb.called());
1026 ASSERT_EQ(PP_ERROR_FAILED, get_bitstream_buffer_cb.result());
1029 // Check an error from the encoder aborts Encode and GetBitstreamBuffer
1030 // callbacks.
1031 LockingResourceReleaser encoder(CreateAndInitializeEncoder());
1033 MockCompletionCallback get_frame_cb, encode_cb1, encode_cb2;
1034 PP_Resource video_frame1, video_frame2;
1035 ASSERT_EQ(
1036 PP_OK_COMPLETIONPENDING,
1037 CallFirstGetVideoFrame(encoder.get(), &video_frame1, &get_frame_cb));
1038 ASSERT_TRUE(get_frame_cb.called());
1039 ASSERT_EQ(PP_OK, get_frame_cb.result());
1041 get_frame_cb.Reset();
1042 ASSERT_EQ(
1043 PP_OK_COMPLETIONPENDING,
1044 CallFirstGetVideoFrame(encoder.get(), &video_frame2, &get_frame_cb));
1045 ASSERT_TRUE(get_frame_cb.called());
1046 ASSERT_EQ(PP_OK, get_frame_cb.result());
1048 ASSERT_EQ(PP_OK_COMPLETIONPENDING,
1049 CallEncode(encoder.get(), video_frame1, PP_FALSE, &encode_cb1));
1050 ASSERT_FALSE(encode_cb1.called());
1051 ASSERT_EQ(PP_OK_COMPLETIONPENDING,
1052 CallEncode(encoder.get(), video_frame2, PP_FALSE, &encode_cb2));
1053 ASSERT_FALSE(encode_cb2.called());
1055 MockCompletionCallback get_bitstream_buffer_cb;
1056 PP_BitstreamBuffer bitstream_buffer;
1057 ASSERT_EQ(PP_OK_COMPLETIONPENDING,
1058 CallGetBitstreamBuffer(encoder.get(), &bitstream_buffer,
1059 &get_bitstream_buffer_cb));
1061 ResourceMessageCallParams error_params(encoder.get(), 0);
1062 SendNotifyError(error_params, PP_ERROR_FAILED);
1064 ASSERT_TRUE(encode_cb1.called());
1065 ASSERT_EQ(PP_ERROR_FAILED, encode_cb1.result());
1066 ASSERT_TRUE(encode_cb2.called());
1067 ASSERT_EQ(PP_ERROR_FAILED, encode_cb2.result());
1068 ASSERT_TRUE(get_bitstream_buffer_cb.called());
1069 ASSERT_EQ(PP_ERROR_FAILED, get_bitstream_buffer_cb.result());
1073 TEST_F(VideoEncoderResourceTest, Close) {
1075 // Check closing the encoder aborts GetVideoFrame and
1076 // GetBitstreamBuffer callbacks.
1077 LockingResourceReleaser encoder(CreateAndInitializeEncoder());
1079 MockCompletionCallback get_frame_cb;
1080 PP_Resource video_frame;
1081 ASSERT_EQ(PP_OK_COMPLETIONPENDING,
1082 CallGetVideoFrame(encoder.get(), &video_frame, &get_frame_cb));
1083 ASSERT_FALSE(get_frame_cb.called());
1085 MockCompletionCallback get_bitstream_buffer_cb;
1086 PP_BitstreamBuffer bitstream_buffer;
1087 ASSERT_EQ(PP_OK_COMPLETIONPENDING,
1088 CallGetBitstreamBuffer(encoder.get(), &bitstream_buffer,
1089 &get_bitstream_buffer_cb));
1091 CallClose(encoder.get());
1093 ASSERT_TRUE(get_frame_cb.called());
1094 ASSERT_EQ(PP_ERROR_ABORTED, get_frame_cb.result());
1095 ASSERT_TRUE(get_bitstream_buffer_cb.called());
1096 ASSERT_EQ(PP_ERROR_ABORTED, get_bitstream_buffer_cb.result());
1099 // Check closing the encoder aborts Encode and GetBitstreamBuffer
1100 // callbacks.
1101 LockingResourceReleaser encoder(CreateAndInitializeEncoder());
1103 MockCompletionCallback get_frame_cb, encode_cb1, encode_cb2;
1104 PP_Resource video_frame1, video_frame2;
1105 ASSERT_EQ(
1106 PP_OK_COMPLETIONPENDING,
1107 CallFirstGetVideoFrame(encoder.get(), &video_frame1, &get_frame_cb));
1108 ASSERT_TRUE(get_frame_cb.called());
1109 ASSERT_EQ(PP_OK, get_frame_cb.result());
1111 get_frame_cb.Reset();
1112 ASSERT_EQ(
1113 PP_OK_COMPLETIONPENDING,
1114 CallFirstGetVideoFrame(encoder.get(), &video_frame2, &get_frame_cb));
1115 ASSERT_TRUE(get_frame_cb.called());
1116 ASSERT_EQ(PP_OK, get_frame_cb.result());
1118 ASSERT_EQ(PP_OK_COMPLETIONPENDING,
1119 CallEncode(encoder.get(), video_frame1, PP_FALSE, &encode_cb1));
1120 ASSERT_FALSE(encode_cb1.called());
1121 ASSERT_EQ(PP_OK_COMPLETIONPENDING,
1122 CallEncode(encoder.get(), video_frame2, PP_FALSE, &encode_cb2));
1123 ASSERT_FALSE(encode_cb2.called());
1125 MockCompletionCallback get_bitstream_buffer_cb;
1126 PP_BitstreamBuffer bitstream_buffer;
1127 ASSERT_EQ(PP_OK_COMPLETIONPENDING,
1128 CallGetBitstreamBuffer(encoder.get(), &bitstream_buffer,
1129 &get_bitstream_buffer_cb));
1131 CallClose(encoder.get());
1133 ASSERT_TRUE(encode_cb1.called());
1134 ASSERT_EQ(PP_ERROR_ABORTED, encode_cb1.result());
1135 ASSERT_TRUE(encode_cb2.called());
1136 ASSERT_EQ(PP_ERROR_ABORTED, encode_cb2.result());
1137 ASSERT_TRUE(get_bitstream_buffer_cb.called());
1138 ASSERT_EQ(PP_ERROR_ABORTED, get_bitstream_buffer_cb.result());
1140 // Verify that a remaining encode response from the renderer is
1141 // discarded.
1142 ResourceMessageCallParams params;
1143 uint32_t frame_id;
1144 bool force_frame;
1145 ASSERT_TRUE(CheckEncodeMsg(&params, &frame_id, &force_frame));
1146 SendEncodeReply(params, frame_id);
1150 } // namespace proxy
1151 } // namespace ppapi