test_video_encoder.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.
  4. #include "ppapi/tests/test_video_encoder.h"
  5. #include "ppapi/c/pp_codecs.h"
  6. #include "ppapi/c/pp_errors.h"
  7. #include "ppapi/cpp/video_encoder.h"
  8. #include "ppapi/tests/testing_instance.h"
  9. REGISTER_TEST_CASE(VideoEncoder);
  10. bool TestVideoEncoder::Init() {
  11. video_encoder_interface_ = static_cast<const PPB_VideoEncoder_0_1*>(
  12. pp::Module::Get()->GetBrowserInterface(PPB_VIDEOENCODER_INTERFACE_0_1));
  13. return video_encoder_interface_ && CheckTestingInterface();
  14. }
  15. void TestVideoEncoder::RunTests(const std::string& filter) {
  16. RUN_CALLBACK_TEST(TestVideoEncoder, AvailableCodecs, filter);
  17. RUN_CALLBACK_TEST(TestVideoEncoder, IncorrectSizeFails, filter);
  18. RUN_CALLBACK_TEST(TestVideoEncoder, InitializeVP8, filter);
  19. RUN_CALLBACK_TEST(TestVideoEncoder, InitializeVP9, filter);
  20. }
  21. std::string TestVideoEncoder::TestAvailableCodecs() {
  22. // Test that we get results for supported formats. We should at
  23. // least get the software supported formats.
  24. pp::VideoEncoder video_encoder(instance_);
  25. ASSERT_FALSE(video_encoder.is_null());
  26. TestCompletionCallbackWithOutput<std::vector<PP_VideoProfileDescription> >
  27. callback(instance_->pp_instance(), false);
  28. callback.WaitForResult(
  29. video_encoder.GetSupportedProfiles(callback.GetCallback()));
  30. ASSERT_GE(callback.result(), 1U);
  31. const std::vector<PP_VideoProfileDescription> video_profiles =
  32. callback.output();
  33. ASSERT_GE(video_profiles.size(), 1U);
  34. bool found_vp8 = false, found_vp9 = false;
  35. for (uint32_t i = 0; i < video_profiles.size(); ++i) {
  36. const PP_VideoProfileDescription& description = video_profiles[i];
  37. if (description.hardware_accelerated == PP_FALSE) {
  38. ASSERT_GE(description.max_framerate_numerator /
  39. description.max_framerate_denominator,
  40. 30U);
  41. if (description.profile == PP_VIDEOPROFILE_VP8_ANY)
  42. found_vp8 = true;
  43. else if (description.profile == PP_VIDEOPROFILE_VP9_ANY)
  44. found_vp9 = true;
  45. }
  46. }
  47. ASSERT_TRUE(found_vp8);
  48. ASSERT_TRUE(found_vp9);
  49. PASS();
  50. }
  51. std::string TestVideoEncoder::TestIncorrectSizeFails() {
  52. // Test that initializing the encoder with incorrect size fails.
  53. pp::VideoEncoder video_encoder(instance_);
  54. ASSERT_FALSE(video_encoder.is_null());
  55. pp::Size video_size(0, 0);
  56. TestCompletionCallback callback(instance_->pp_instance(), false);
  57. callback.WaitForResult(video_encoder.Initialize(
  58. PP_VIDEOFRAME_FORMAT_I420, video_size, PP_VIDEOPROFILE_VP8_ANY, 1000000,
  59. PP_HARDWAREACCELERATION_WITHFALLBACK, callback.GetCallback()));
  60. ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
  61. PASS();
  62. }
  63. std::string TestVideoEncoder::TestInitializeVP8() {
  64. return TestInitializeCodec(PP_VIDEOPROFILE_VP8_ANY);
  65. }
  66. std::string TestVideoEncoder::TestInitializeVP9() {
  67. return TestInitializeCodec(PP_VIDEOPROFILE_VP9_ANY);
  68. }
  69. std::string TestVideoEncoder::TestInitializeCodec(PP_VideoProfile profile) {
  70. pp::VideoEncoder video_encoder(instance_);
  71. ASSERT_FALSE(video_encoder.is_null());
  72. pp::Size video_size(640, 480);
  73. TestCompletionCallback callback(instance_->pp_instance(), false);
  74. callback.WaitForResult(video_encoder.Initialize(
  75. PP_VIDEOFRAME_FORMAT_I420, video_size, profile, 1000000,
  76. PP_HARDWAREACCELERATION_WITHFALLBACK, callback.GetCallback()));
  77. ASSERT_EQ(PP_OK, callback.result());
  78. pp::Size coded_size;
  79. ASSERT_EQ(PP_OK, video_encoder.GetFrameCodedSize(&coded_size));
  80. ASSERT_GE(coded_size.GetArea(), video_size.GetArea());
  81. ASSERT_GE(video_encoder.GetFramesRequired(), 1);
  82. TestCompletionCallbackWithOutput<pp::VideoFrame> get_video_frame(
  83. instance_->pp_instance(), false);
  84. get_video_frame.WaitForResult(
  85. video_encoder.GetVideoFrame(get_video_frame.GetCallback()));
  86. ASSERT_EQ(PP_OK, get_video_frame.result());
  87. pp::Size video_frame_size;
  88. ASSERT_TRUE(get_video_frame.output().GetSize(&video_frame_size));
  89. ASSERT_EQ(coded_size.GetArea(), video_frame_size.GetArea());
  90. PASS();
  91. }