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