Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 "video/buffered_frame_decryptor.h" |
| 12 | |
| 13 | #include <map> |
| 14 | #include <memory> |
| 15 | #include <vector> |
| 16 | |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 17 | #include "api/test/mock_frame_decryptor.h" |
| 18 | #include "modules/video_coding/packet_buffer.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "rtc_base/ref_counted_object.h" |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 20 | #include "system_wrappers/include/clock.h" |
| 21 | #include "test/gmock.h" |
| 22 | #include "test/gtest.h" |
| 23 | |
| 24 | using ::testing::Return; |
| 25 | |
| 26 | namespace webrtc { |
| 27 | namespace { |
| 28 | |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 29 | FrameDecryptorInterface::Result DecryptSuccess() { |
| 30 | return FrameDecryptorInterface::Result(FrameDecryptorInterface::Status::kOk, |
| 31 | 0); |
| 32 | } |
| 33 | |
| 34 | FrameDecryptorInterface::Result DecryptFail() { |
| 35 | return FrameDecryptorInterface::Result( |
| 36 | FrameDecryptorInterface::Status::kFailedToDecrypt, 0); |
| 37 | } |
| 38 | |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 39 | } // namespace |
| 40 | |
Danil Chapovalov | ce1ffcd | 2019-10-22 17:12:42 +0200 | [diff] [blame] | 41 | class BufferedFrameDecryptorTest : public ::testing::Test, |
| 42 | public OnDecryptedFrameCallback, |
| 43 | public OnDecryptionStatusChangeCallback { |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 44 | public: |
| 45 | // Implements the OnDecryptedFrameCallbackInterface |
| 46 | void OnDecryptedFrame( |
| 47 | std::unique_ptr<video_coding::RtpFrameObject> frame) override { |
| 48 | decrypted_frame_call_count_++; |
| 49 | } |
| 50 | |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 51 | void OnDecryptionStatusChange(FrameDecryptorInterface::Status status) { |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 52 | ++decryption_status_change_count_; |
| 53 | } |
| 54 | |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 55 | // Returns a new fake RtpFrameObject it abstracts the difficult construction |
| 56 | // of the RtpFrameObject to simplify testing. |
| 57 | std::unique_ptr<video_coding::RtpFrameObject> CreateRtpFrameObject( |
| 58 | bool key_frame) { |
| 59 | seq_num_++; |
| 60 | |
philipel | fc35874 | 2019-09-26 16:59:45 +0200 | [diff] [blame] | 61 | // clang-format off |
Danil Chapovalov | f7457e5 | 2019-09-20 17:57:15 +0200 | [diff] [blame] | 62 | return std::make_unique<video_coding::RtpFrameObject>( |
philipel | fc35874 | 2019-09-26 16:59:45 +0200 | [diff] [blame] | 63 | seq_num_, |
| 64 | seq_num_, |
| 65 | /*markerBit=*/true, |
| 66 | /*times_nacked=*/0, |
| 67 | /*first_packet_received_time=*/0, |
| 68 | /*last_packet_received_time=*/0, |
| 69 | /*rtp_timestamp=*/0, |
| 70 | /*ntp_time_ms=*/0, |
| 71 | VideoSendTiming(), |
| 72 | /*payload_type=*/0, |
| 73 | kVideoCodecGeneric, |
| 74 | kVideoRotation_0, |
| 75 | VideoContentType::UNSPECIFIED, |
| 76 | RTPVideoHeader(), |
| 77 | /*color_space=*/absl::nullopt, |
| 78 | RtpGenericFrameDescriptor(), |
| 79 | RtpPacketInfos(), |
Danil Chapovalov | f7457e5 | 2019-09-20 17:57:15 +0200 | [diff] [blame] | 80 | EncodedImageBuffer::Create(/*size=*/0)); |
philipel | fc35874 | 2019-09-26 16:59:45 +0200 | [diff] [blame] | 81 | // clang-format on |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | protected: |
Danil Chapovalov | f7457e5 | 2019-09-20 17:57:15 +0200 | [diff] [blame] | 85 | BufferedFrameDecryptorTest() { |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 86 | fake_packet_data_ = std::vector<uint8_t>(100); |
| 87 | decrypted_frame_call_count_ = 0; |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 88 | decryption_status_change_count_ = 0; |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 89 | seq_num_ = 0; |
| 90 | mock_frame_decryptor_ = new rtc::RefCountedObject<MockFrameDecryptor>(); |
Benjamin Wright | a556448 | 2019-04-03 10:44:18 -0700 | [diff] [blame] | 91 | buffered_frame_decryptor_ = |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 92 | std::make_unique<BufferedFrameDecryptor>(this, this); |
Benjamin Wright | a556448 | 2019-04-03 10:44:18 -0700 | [diff] [blame] | 93 | buffered_frame_decryptor_->SetFrameDecryptor(mock_frame_decryptor_.get()); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static const size_t kMaxStashedFrames; |
| 97 | |
| 98 | std::vector<uint8_t> fake_packet_data_; |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 99 | rtc::scoped_refptr<MockFrameDecryptor> mock_frame_decryptor_; |
| 100 | std::unique_ptr<BufferedFrameDecryptor> buffered_frame_decryptor_; |
| 101 | size_t decrypted_frame_call_count_; |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 102 | size_t decryption_status_change_count_ = 0; |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 103 | uint16_t seq_num_; |
| 104 | }; |
| 105 | |
| 106 | const size_t BufferedFrameDecryptorTest::kMaxStashedFrames = 24; |
| 107 | |
| 108 | // Callback should always be triggered on a successful decryption. |
| 109 | TEST_F(BufferedFrameDecryptorTest, CallbackCalledOnSuccessfulDecryption) { |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 110 | EXPECT_CALL(*mock_frame_decryptor_, Decrypt) |
| 111 | .Times(1) |
| 112 | .WillOnce(Return(DecryptSuccess())); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 113 | EXPECT_CALL(*mock_frame_decryptor_, GetMaxPlaintextByteSize) |
| 114 | .Times(1) |
| 115 | .WillOnce(Return(0)); |
| 116 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 117 | EXPECT_EQ(decrypted_frame_call_count_, static_cast<size_t>(1)); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 118 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(1)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // An initial fail to decrypt should not trigger the callback. |
| 122 | TEST_F(BufferedFrameDecryptorTest, CallbackNotCalledOnFailedDecryption) { |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 123 | EXPECT_CALL(*mock_frame_decryptor_, Decrypt) |
| 124 | .Times(1) |
| 125 | .WillOnce(Return(DecryptFail())); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 126 | EXPECT_CALL(*mock_frame_decryptor_, GetMaxPlaintextByteSize) |
| 127 | .Times(1) |
| 128 | .WillOnce(Return(0)); |
| 129 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 130 | EXPECT_EQ(decrypted_frame_call_count_, static_cast<size_t>(0)); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 131 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(1)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // Initial failures should be stored and retried after the first successful |
| 135 | // decryption. |
| 136 | TEST_F(BufferedFrameDecryptorTest, DelayedCallbackOnBufferedFrames) { |
| 137 | EXPECT_CALL(*mock_frame_decryptor_, Decrypt) |
| 138 | .Times(3) |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 139 | .WillOnce(Return(DecryptFail())) |
| 140 | .WillOnce(Return(DecryptSuccess())) |
| 141 | .WillOnce(Return(DecryptSuccess())); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 142 | EXPECT_CALL(*mock_frame_decryptor_, GetMaxPlaintextByteSize) |
| 143 | .Times(3) |
| 144 | .WillRepeatedly(Return(0)); |
| 145 | |
| 146 | // The first decrypt will fail stashing the first frame. |
| 147 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 148 | EXPECT_EQ(decrypted_frame_call_count_, static_cast<size_t>(0)); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 149 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(1)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 150 | // The second call will succeed playing back both frames. |
| 151 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(false)); |
| 152 | EXPECT_EQ(decrypted_frame_call_count_, static_cast<size_t>(2)); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 153 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(2)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // Subsequent failure to decrypts after the first successful decryption should |
| 157 | // fail to decryptk |
| 158 | TEST_F(BufferedFrameDecryptorTest, FTDDiscardedAfterFirstSuccess) { |
| 159 | EXPECT_CALL(*mock_frame_decryptor_, Decrypt) |
| 160 | .Times(4) |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 161 | .WillOnce(Return(DecryptFail())) |
| 162 | .WillOnce(Return(DecryptSuccess())) |
| 163 | .WillOnce(Return(DecryptSuccess())) |
| 164 | .WillOnce(Return(DecryptFail())); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 165 | EXPECT_CALL(*mock_frame_decryptor_, GetMaxPlaintextByteSize) |
| 166 | .Times(4) |
| 167 | .WillRepeatedly(Return(0)); |
| 168 | |
| 169 | // The first decrypt will fail stashing the first frame. |
| 170 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 171 | EXPECT_EQ(decrypted_frame_call_count_, static_cast<size_t>(0)); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 172 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(1)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 173 | // The second call will succeed playing back both frames. |
| 174 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(false)); |
| 175 | EXPECT_EQ(decrypted_frame_call_count_, static_cast<size_t>(2)); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 176 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(2)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 177 | // A new failure call will not result in an additional decrypted frame |
| 178 | // callback. |
| 179 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 180 | EXPECT_EQ(decrypted_frame_call_count_, static_cast<size_t>(2)); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 181 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(3)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // Validate that the maximum number of stashed frames cannot be exceeded even if |
| 185 | // more than its maximum arrives before the first successful decryption. |
| 186 | TEST_F(BufferedFrameDecryptorTest, MaximumNumberOfFramesStored) { |
| 187 | const size_t failed_to_decrypt_count = kMaxStashedFrames * 2; |
| 188 | EXPECT_CALL(*mock_frame_decryptor_, Decrypt) |
| 189 | .Times(failed_to_decrypt_count) |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 190 | .WillRepeatedly(Return(DecryptFail())); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 191 | EXPECT_CALL(*mock_frame_decryptor_, GetMaxPlaintextByteSize) |
| 192 | .WillRepeatedly(Return(0)); |
| 193 | |
| 194 | for (size_t i = 0; i < failed_to_decrypt_count; ++i) { |
| 195 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 196 | } |
| 197 | EXPECT_EQ(decrypted_frame_call_count_, static_cast<size_t>(0)); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 198 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(1)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 199 | |
| 200 | EXPECT_CALL(*mock_frame_decryptor_, Decrypt) |
| 201 | .Times(kMaxStashedFrames + 1) |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 202 | .WillRepeatedly(Return(DecryptSuccess())); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 203 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 204 | EXPECT_EQ(decrypted_frame_call_count_, kMaxStashedFrames + 1); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 205 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(2)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Benjamin Wright | a556448 | 2019-04-03 10:44:18 -0700 | [diff] [blame] | 208 | // Verifies if a BufferedFrameDecryptor is attached but has no FrameDecryptor |
| 209 | // attached it will still store frames up to the frame max. |
| 210 | TEST_F(BufferedFrameDecryptorTest, FramesStoredIfDecryptorNull) { |
| 211 | buffered_frame_decryptor_->SetFrameDecryptor(nullptr); |
| 212 | for (size_t i = 0; i < (2 * kMaxStashedFrames); ++i) { |
| 213 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 214 | } |
| 215 | |
| 216 | EXPECT_CALL(*mock_frame_decryptor_, Decrypt) |
| 217 | .Times(kMaxStashedFrames + 1) |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 218 | .WillRepeatedly(Return(DecryptSuccess())); |
Benjamin Wright | a556448 | 2019-04-03 10:44:18 -0700 | [diff] [blame] | 219 | EXPECT_CALL(*mock_frame_decryptor_, GetMaxPlaintextByteSize) |
| 220 | .WillRepeatedly(Return(0)); |
| 221 | |
| 222 | // Attach the frame decryptor at a later point after frames have arrived. |
| 223 | buffered_frame_decryptor_->SetFrameDecryptor(mock_frame_decryptor_.get()); |
| 224 | |
| 225 | // Next frame should trigger kMaxStashedFrame decryptions. |
| 226 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 227 | EXPECT_EQ(decrypted_frame_call_count_, kMaxStashedFrames + 1); |
| 228 | } |
| 229 | |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 230 | } // namespace webrtc |