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 | |
| 29 | class FakePacketBuffer : public video_coding::PacketBuffer { |
| 30 | public: |
| 31 | FakePacketBuffer() : PacketBuffer(nullptr, 0, 0, nullptr) {} |
| 32 | ~FakePacketBuffer() override {} |
| 33 | |
| 34 | VCMPacket* GetPacket(uint16_t seq_num) override { |
| 35 | auto packet_it = packets_.find(seq_num); |
| 36 | return packet_it == packets_.end() ? nullptr : &packet_it->second; |
| 37 | } |
| 38 | |
| 39 | bool InsertPacket(VCMPacket* packet) override { |
| 40 | packets_[packet->seqNum] = *packet; |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | bool GetBitstream(const video_coding::RtpFrameObject& frame, |
| 45 | uint8_t* destination) override { |
| 46 | return true; |
| 47 | } |
| 48 | |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 49 | private: |
| 50 | std::map<uint16_t, VCMPacket> packets_; |
| 51 | }; |
| 52 | |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 53 | FrameDecryptorInterface::Result DecryptSuccess() { |
| 54 | return FrameDecryptorInterface::Result(FrameDecryptorInterface::Status::kOk, |
| 55 | 0); |
| 56 | } |
| 57 | |
| 58 | FrameDecryptorInterface::Result DecryptFail() { |
| 59 | return FrameDecryptorInterface::Result( |
| 60 | FrameDecryptorInterface::Status::kFailedToDecrypt, 0); |
| 61 | } |
| 62 | |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 63 | } // namespace |
| 64 | |
| 65 | class BufferedFrameDecryptorTest |
| 66 | : public ::testing::Test, |
| 67 | public OnDecryptedFrameCallback, |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 68 | public OnDecryptionStatusChangeCallback, |
Elad Alon | b4643ad | 2019-02-22 11:19:50 +0100 | [diff] [blame] | 69 | public video_coding::OnAssembledFrameCallback { |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 70 | public: |
| 71 | // Implements the OnDecryptedFrameCallbackInterface |
| 72 | void OnDecryptedFrame( |
| 73 | std::unique_ptr<video_coding::RtpFrameObject> frame) override { |
| 74 | decrypted_frame_call_count_++; |
| 75 | } |
| 76 | |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 77 | void OnDecryptionStatusChange(FrameDecryptorInterface::Status status) { |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 78 | ++decryption_status_change_count_; |
| 79 | } |
| 80 | |
Elad Alon | b4643ad | 2019-02-22 11:19:50 +0100 | [diff] [blame] | 81 | // Implements the OnAssembledFrameCallback interface. |
| 82 | void OnAssembledFrame( |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 83 | std::unique_ptr<video_coding::RtpFrameObject> frame) override {} |
| 84 | |
| 85 | // Returns a new fake RtpFrameObject it abstracts the difficult construction |
| 86 | // of the RtpFrameObject to simplify testing. |
| 87 | std::unique_ptr<video_coding::RtpFrameObject> CreateRtpFrameObject( |
| 88 | bool key_frame) { |
| 89 | seq_num_++; |
| 90 | |
| 91 | VCMPacket packet; |
Niels Möller | d5e02f0 | 2019-02-20 13:12:21 +0100 | [diff] [blame] | 92 | packet.video_header.codec = kVideoCodecGeneric; |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 93 | packet.seqNum = seq_num_; |
Niels Möller | abbc50e | 2019-04-24 09:41:16 +0200 | [diff] [blame] | 94 | packet.video_header.frame_type = key_frame |
| 95 | ? VideoFrameType::kVideoFrameKey |
| 96 | : VideoFrameType::kVideoFrameDelta; |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 97 | packet.generic_descriptor = RtpGenericFrameDescriptor(); |
| 98 | fake_packet_buffer_->InsertPacket(&packet); |
| 99 | packet.seqNum = seq_num_; |
Niels Möller | d5e02f0 | 2019-02-20 13:12:21 +0100 | [diff] [blame] | 100 | packet.video_header.is_last_packet_in_frame = true; |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 101 | fake_packet_buffer_->InsertPacket(&packet); |
| 102 | |
| 103 | return std::unique_ptr<video_coding::RtpFrameObject>( |
| 104 | new video_coding::RtpFrameObject(fake_packet_buffer_.get(), seq_num_, |
Chen Xing | f00bf42 | 2019-06-20 10:05:55 +0200 | [diff] [blame] | 105 | seq_num_, 0, 0, 0, 0, {})); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | protected: |
| 109 | BufferedFrameDecryptorTest() : fake_packet_buffer_(new FakePacketBuffer()) {} |
| 110 | void SetUp() override { |
| 111 | fake_packet_data_ = std::vector<uint8_t>(100); |
| 112 | decrypted_frame_call_count_ = 0; |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 113 | decryption_status_change_count_ = 0; |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 114 | seq_num_ = 0; |
| 115 | mock_frame_decryptor_ = new rtc::RefCountedObject<MockFrameDecryptor>(); |
Benjamin Wright | a556448 | 2019-04-03 10:44:18 -0700 | [diff] [blame] | 116 | buffered_frame_decryptor_ = |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame^] | 117 | std::make_unique<BufferedFrameDecryptor>(this, this); |
Benjamin Wright | a556448 | 2019-04-03 10:44:18 -0700 | [diff] [blame] | 118 | buffered_frame_decryptor_->SetFrameDecryptor(mock_frame_decryptor_.get()); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static const size_t kMaxStashedFrames; |
| 122 | |
| 123 | std::vector<uint8_t> fake_packet_data_; |
| 124 | rtc::scoped_refptr<FakePacketBuffer> fake_packet_buffer_; |
| 125 | rtc::scoped_refptr<MockFrameDecryptor> mock_frame_decryptor_; |
| 126 | std::unique_ptr<BufferedFrameDecryptor> buffered_frame_decryptor_; |
| 127 | size_t decrypted_frame_call_count_; |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 128 | size_t decryption_status_change_count_ = 0; |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 129 | uint16_t seq_num_; |
| 130 | }; |
| 131 | |
| 132 | const size_t BufferedFrameDecryptorTest::kMaxStashedFrames = 24; |
| 133 | |
| 134 | // Callback should always be triggered on a successful decryption. |
| 135 | TEST_F(BufferedFrameDecryptorTest, CallbackCalledOnSuccessfulDecryption) { |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 136 | EXPECT_CALL(*mock_frame_decryptor_, Decrypt) |
| 137 | .Times(1) |
| 138 | .WillOnce(Return(DecryptSuccess())); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 139 | EXPECT_CALL(*mock_frame_decryptor_, GetMaxPlaintextByteSize) |
| 140 | .Times(1) |
| 141 | .WillOnce(Return(0)); |
| 142 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 143 | EXPECT_EQ(decrypted_frame_call_count_, static_cast<size_t>(1)); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 144 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(1)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // An initial fail to decrypt should not trigger the callback. |
| 148 | TEST_F(BufferedFrameDecryptorTest, CallbackNotCalledOnFailedDecryption) { |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 149 | EXPECT_CALL(*mock_frame_decryptor_, Decrypt) |
| 150 | .Times(1) |
| 151 | .WillOnce(Return(DecryptFail())); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 152 | EXPECT_CALL(*mock_frame_decryptor_, GetMaxPlaintextByteSize) |
| 153 | .Times(1) |
| 154 | .WillOnce(Return(0)); |
| 155 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 156 | EXPECT_EQ(decrypted_frame_call_count_, static_cast<size_t>(0)); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 157 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(1)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // Initial failures should be stored and retried after the first successful |
| 161 | // decryption. |
| 162 | TEST_F(BufferedFrameDecryptorTest, DelayedCallbackOnBufferedFrames) { |
| 163 | EXPECT_CALL(*mock_frame_decryptor_, Decrypt) |
| 164 | .Times(3) |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 165 | .WillOnce(Return(DecryptFail())) |
| 166 | .WillOnce(Return(DecryptSuccess())) |
| 167 | .WillOnce(Return(DecryptSuccess())); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 168 | EXPECT_CALL(*mock_frame_decryptor_, GetMaxPlaintextByteSize) |
| 169 | .Times(3) |
| 170 | .WillRepeatedly(Return(0)); |
| 171 | |
| 172 | // The first decrypt will fail stashing the first frame. |
| 173 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 174 | EXPECT_EQ(decrypted_frame_call_count_, static_cast<size_t>(0)); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 175 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(1)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 176 | // The second call will succeed playing back both frames. |
| 177 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(false)); |
| 178 | EXPECT_EQ(decrypted_frame_call_count_, static_cast<size_t>(2)); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 179 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(2)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | // Subsequent failure to decrypts after the first successful decryption should |
| 183 | // fail to decryptk |
| 184 | TEST_F(BufferedFrameDecryptorTest, FTDDiscardedAfterFirstSuccess) { |
| 185 | EXPECT_CALL(*mock_frame_decryptor_, Decrypt) |
| 186 | .Times(4) |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 187 | .WillOnce(Return(DecryptFail())) |
| 188 | .WillOnce(Return(DecryptSuccess())) |
| 189 | .WillOnce(Return(DecryptSuccess())) |
| 190 | .WillOnce(Return(DecryptFail())); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 191 | EXPECT_CALL(*mock_frame_decryptor_, GetMaxPlaintextByteSize) |
| 192 | .Times(4) |
| 193 | .WillRepeatedly(Return(0)); |
| 194 | |
| 195 | // The first decrypt will fail stashing the first frame. |
| 196 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 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 | // The second call will succeed playing back both frames. |
| 200 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(false)); |
| 201 | EXPECT_EQ(decrypted_frame_call_count_, static_cast<size_t>(2)); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 202 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(2)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 203 | // A new failure call will not result in an additional decrypted frame |
| 204 | // callback. |
| 205 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 206 | EXPECT_EQ(decrypted_frame_call_count_, static_cast<size_t>(2)); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 207 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(3)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // Validate that the maximum number of stashed frames cannot be exceeded even if |
| 211 | // more than its maximum arrives before the first successful decryption. |
| 212 | TEST_F(BufferedFrameDecryptorTest, MaximumNumberOfFramesStored) { |
| 213 | const size_t failed_to_decrypt_count = kMaxStashedFrames * 2; |
| 214 | EXPECT_CALL(*mock_frame_decryptor_, Decrypt) |
| 215 | .Times(failed_to_decrypt_count) |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 216 | .WillRepeatedly(Return(DecryptFail())); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 217 | EXPECT_CALL(*mock_frame_decryptor_, GetMaxPlaintextByteSize) |
| 218 | .WillRepeatedly(Return(0)); |
| 219 | |
| 220 | for (size_t i = 0; i < failed_to_decrypt_count; ++i) { |
| 221 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 222 | } |
| 223 | EXPECT_EQ(decrypted_frame_call_count_, static_cast<size_t>(0)); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 224 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(1)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 225 | |
| 226 | EXPECT_CALL(*mock_frame_decryptor_, Decrypt) |
| 227 | .Times(kMaxStashedFrames + 1) |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 228 | .WillRepeatedly(Return(DecryptSuccess())); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 229 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 230 | EXPECT_EQ(decrypted_frame_call_count_, kMaxStashedFrames + 1); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 231 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(2)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Benjamin Wright | a556448 | 2019-04-03 10:44:18 -0700 | [diff] [blame] | 234 | // Verifies if a BufferedFrameDecryptor is attached but has no FrameDecryptor |
| 235 | // attached it will still store frames up to the frame max. |
| 236 | TEST_F(BufferedFrameDecryptorTest, FramesStoredIfDecryptorNull) { |
| 237 | buffered_frame_decryptor_->SetFrameDecryptor(nullptr); |
| 238 | for (size_t i = 0; i < (2 * kMaxStashedFrames); ++i) { |
| 239 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 240 | } |
| 241 | |
| 242 | EXPECT_CALL(*mock_frame_decryptor_, Decrypt) |
| 243 | .Times(kMaxStashedFrames + 1) |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 244 | .WillRepeatedly(Return(DecryptSuccess())); |
Benjamin Wright | a556448 | 2019-04-03 10:44:18 -0700 | [diff] [blame] | 245 | EXPECT_CALL(*mock_frame_decryptor_, GetMaxPlaintextByteSize) |
| 246 | .WillRepeatedly(Return(0)); |
| 247 | |
| 248 | // Attach the frame decryptor at a later point after frames have arrived. |
| 249 | buffered_frame_decryptor_->SetFrameDecryptor(mock_frame_decryptor_.get()); |
| 250 | |
| 251 | // Next frame should trigger kMaxStashedFrame decryptions. |
| 252 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 253 | EXPECT_EQ(decrypted_frame_call_count_, kMaxStashedFrames + 1); |
| 254 | } |
| 255 | |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 256 | } // namespace webrtc |