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