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