Peter Boström | 7252a2b | 2015-05-18 19:42:03 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/video_decoder.h" |
| 12 | |
Per | 327d8ba | 2015-11-10 14:00:27 +0100 | [diff] [blame] | 13 | #include "webrtc/base/checks.h" |
magjed | f6acc2a | 2016-11-22 01:43:03 -0800 | [diff] [blame^] | 14 | #include "webrtc/media/engine/videodecodersoftwarefallbackwrapper.h" |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 15 | #include "webrtc/modules/video_coding/include/video_error_codes.h" |
kwiberg | ac9f876 | 2016-09-30 22:29:43 -0700 | [diff] [blame] | 16 | #include "webrtc/test/gtest.h" |
Peter Boström | 7252a2b | 2015-05-18 19:42:03 +0200 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | class VideoDecoderSoftwareFallbackWrapperTest : public ::testing::Test { |
| 21 | protected: |
| 22 | VideoDecoderSoftwareFallbackWrapperTest() |
| 23 | : fallback_wrapper_(kVideoCodecVP8, &fake_decoder_) {} |
| 24 | |
| 25 | class CountingFakeDecoder : public VideoDecoder { |
| 26 | public: |
| 27 | int32_t InitDecode(const VideoCodec* codec_settings, |
| 28 | int32_t number_of_cores) override { |
| 29 | ++init_decode_count_; |
| 30 | return WEBRTC_VIDEO_CODEC_OK; |
| 31 | } |
| 32 | |
| 33 | int32_t Decode(const EncodedImage& input_image, |
| 34 | bool missing_frames, |
| 35 | const RTPFragmentationHeader* fragmentation, |
| 36 | const CodecSpecificInfo* codec_specific_info, |
| 37 | int64_t render_time_ms) override { |
| 38 | ++decode_count_; |
| 39 | return decode_return_code_; |
| 40 | } |
| 41 | |
| 42 | int32_t RegisterDecodeCompleteCallback( |
| 43 | DecodedImageCallback* callback) override { |
| 44 | decode_complete_callback_ = callback; |
| 45 | return WEBRTC_VIDEO_CODEC_OK; |
| 46 | } |
| 47 | |
| 48 | int32_t Release() override { |
| 49 | ++release_count_; |
| 50 | return WEBRTC_VIDEO_CODEC_OK; |
| 51 | } |
| 52 | |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 53 | const char* ImplementationName() const override { |
| 54 | return "fake-decoder"; |
| 55 | } |
| 56 | |
Peter Boström | 7252a2b | 2015-05-18 19:42:03 +0200 | [diff] [blame] | 57 | int init_decode_count_ = 0; |
| 58 | int decode_count_ = 0; |
| 59 | int32_t decode_return_code_ = WEBRTC_VIDEO_CODEC_OK; |
| 60 | DecodedImageCallback* decode_complete_callback_ = nullptr; |
| 61 | int release_count_ = 0; |
| 62 | int reset_count_ = 0; |
| 63 | }; |
| 64 | CountingFakeDecoder fake_decoder_; |
| 65 | VideoDecoderSoftwareFallbackWrapper fallback_wrapper_; |
| 66 | }; |
| 67 | |
| 68 | TEST_F(VideoDecoderSoftwareFallbackWrapperTest, InitializesDecoder) { |
| 69 | VideoCodec codec = {}; |
| 70 | fallback_wrapper_.InitDecode(&codec, 2); |
| 71 | EXPECT_EQ(1, fake_decoder_.init_decode_count_); |
| 72 | } |
| 73 | |
| 74 | TEST_F(VideoDecoderSoftwareFallbackWrapperTest, |
| 75 | CanRecoverFromSoftwareFallback) { |
| 76 | VideoCodec codec = {}; |
| 77 | fallback_wrapper_.InitDecode(&codec, 2); |
| 78 | // Unfortunately faking a VP8 frame is hard. Rely on no Decode -> using SW |
| 79 | // decoder. |
| 80 | fake_decoder_.decode_return_code_ = WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; |
| 81 | EncodedImage encoded_image; |
| 82 | fallback_wrapper_.Decode(encoded_image, false, nullptr, nullptr, -1); |
| 83 | EXPECT_EQ(1, fake_decoder_.decode_count_); |
| 84 | |
| 85 | // Fail -> fake_decoder shouldn't be used anymore. |
| 86 | fallback_wrapper_.Decode(encoded_image, false, nullptr, nullptr, -1); |
| 87 | EXPECT_EQ(1, fake_decoder_.decode_count_) |
| 88 | << "Decoder used even though fallback should be active."; |
| 89 | |
| 90 | // Should be able to recover on a keyframe. |
Peter Boström | 49e196a | 2015-10-23 15:58:18 +0200 | [diff] [blame] | 91 | encoded_image._frameType = kVideoFrameKey; |
Peter Boström | 7252a2b | 2015-05-18 19:42:03 +0200 | [diff] [blame] | 92 | fake_decoder_.decode_return_code_ = WEBRTC_VIDEO_CODEC_OK; |
| 93 | fallback_wrapper_.Decode(encoded_image, false, nullptr, nullptr, -1); |
| 94 | EXPECT_EQ(2, fake_decoder_.decode_count_) |
| 95 | << "Wrapper did not try to decode a keyframe using registered decoder."; |
| 96 | |
Peter Boström | 49e196a | 2015-10-23 15:58:18 +0200 | [diff] [blame] | 97 | encoded_image._frameType = kVideoFrameDelta; |
Peter Boström | 7252a2b | 2015-05-18 19:42:03 +0200 | [diff] [blame] | 98 | fallback_wrapper_.Decode(encoded_image, false, nullptr, nullptr, -1); |
| 99 | EXPECT_EQ(3, fake_decoder_.decode_count_) |
| 100 | << "Decoder not used on future delta frames."; |
| 101 | } |
| 102 | |
| 103 | TEST_F(VideoDecoderSoftwareFallbackWrapperTest, DoesNotFallbackOnEveryError) { |
| 104 | VideoCodec codec = {}; |
| 105 | fallback_wrapper_.InitDecode(&codec, 2); |
| 106 | fake_decoder_.decode_return_code_ = WEBRTC_VIDEO_CODEC_ERROR; |
| 107 | EncodedImage encoded_image; |
| 108 | EXPECT_EQ( |
| 109 | fake_decoder_.decode_return_code_, |
| 110 | fallback_wrapper_.Decode(encoded_image, false, nullptr, nullptr, -1)); |
| 111 | EXPECT_EQ(1, fake_decoder_.decode_count_); |
| 112 | |
| 113 | fallback_wrapper_.Decode(encoded_image, false, nullptr, nullptr, -1); |
| 114 | EXPECT_EQ(2, fake_decoder_.decode_count_) |
| 115 | << "Decoder should be active even though previous decode failed."; |
| 116 | } |
| 117 | |
| 118 | TEST_F(VideoDecoderSoftwareFallbackWrapperTest, ForwardsReleaseCall) { |
| 119 | VideoCodec codec = {}; |
| 120 | fallback_wrapper_.InitDecode(&codec, 2); |
| 121 | fallback_wrapper_.Release(); |
| 122 | EXPECT_EQ(1, fake_decoder_.release_count_); |
| 123 | |
| 124 | fake_decoder_.decode_return_code_ = WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; |
| 125 | EncodedImage encoded_image; |
| 126 | fallback_wrapper_.Decode(encoded_image, false, nullptr, nullptr, -1); |
| 127 | EXPECT_EQ(1, fake_decoder_.release_count_) |
| 128 | << "Decoder should not be released during fallback."; |
| 129 | fallback_wrapper_.Release(); |
| 130 | EXPECT_EQ(2, fake_decoder_.release_count_); |
| 131 | } |
| 132 | |
Peter Boström | 7252a2b | 2015-05-18 19:42:03 +0200 | [diff] [blame] | 133 | // TODO(pbos): Fake a VP8 frame well enough to actually receive a callback from |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 134 | // the software decoder. |
Peter Boström | 7252a2b | 2015-05-18 19:42:03 +0200 | [diff] [blame] | 135 | TEST_F(VideoDecoderSoftwareFallbackWrapperTest, |
| 136 | ForwardsRegisterDecodeCompleteCallback) { |
| 137 | class FakeDecodedImageCallback : public DecodedImageCallback { |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 138 | int32_t Decoded(VideoFrame& decodedImage) override { return 0; } |
Per | 327d8ba | 2015-11-10 14:00:27 +0100 | [diff] [blame] | 139 | int32_t Decoded( |
| 140 | webrtc::VideoFrame& decodedImage, int64_t decode_time_ms) override { |
| 141 | RTC_NOTREACHED(); |
| 142 | return -1; |
| 143 | } |
Peter Boström | 7252a2b | 2015-05-18 19:42:03 +0200 | [diff] [blame] | 144 | } callback, callback2; |
| 145 | |
| 146 | VideoCodec codec = {}; |
| 147 | fallback_wrapper_.InitDecode(&codec, 2); |
| 148 | fallback_wrapper_.RegisterDecodeCompleteCallback(&callback); |
| 149 | EXPECT_EQ(&callback, fake_decoder_.decode_complete_callback_); |
| 150 | |
| 151 | fake_decoder_.decode_return_code_ = WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; |
| 152 | EncodedImage encoded_image; |
| 153 | fallback_wrapper_.Decode(encoded_image, false, nullptr, nullptr, -1); |
| 154 | fallback_wrapper_.RegisterDecodeCompleteCallback(&callback2); |
| 155 | EXPECT_EQ(&callback2, fake_decoder_.decode_complete_callback_); |
| 156 | } |
| 157 | |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 158 | TEST_F(VideoDecoderSoftwareFallbackWrapperTest, |
| 159 | ReportsFallbackImplementationName) { |
| 160 | VideoCodec codec = {}; |
| 161 | fallback_wrapper_.InitDecode(&codec, 2); |
| 162 | |
| 163 | fake_decoder_.decode_return_code_ = WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; |
| 164 | EncodedImage encoded_image; |
| 165 | fallback_wrapper_.Decode(encoded_image, false, nullptr, nullptr, -1); |
| 166 | // Hard coded expected value since libvpx is the software implementation name |
| 167 | // for VP8. Change accordingly if the underlying implementation does. |
| 168 | EXPECT_STREQ("libvpx (fallback from: fake-decoder)", |
| 169 | fallback_wrapper_.ImplementationName()); |
| 170 | fallback_wrapper_.Release(); |
| 171 | } |
| 172 | |
Peter Boström | 7252a2b | 2015-05-18 19:42:03 +0200 | [diff] [blame] | 173 | } // namespace webrtc |