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 | |
| 17 | #include "absl/memory/memory.h" |
| 18 | #include "api/test/mock_frame_decryptor.h" |
| 19 | #include "modules/video_coding/packet_buffer.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "rtc_base/ref_counted_object.h" |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 21 | #include "system_wrappers/include/clock.h" |
| 22 | #include "test/gmock.h" |
| 23 | #include "test/gtest.h" |
| 24 | |
| 25 | using ::testing::Return; |
| 26 | |
| 27 | namespace webrtc { |
| 28 | namespace { |
| 29 | |
| 30 | class FakePacketBuffer : public video_coding::PacketBuffer { |
| 31 | public: |
| 32 | FakePacketBuffer() : PacketBuffer(nullptr, 0, 0, nullptr) {} |
| 33 | ~FakePacketBuffer() override {} |
| 34 | |
| 35 | VCMPacket* GetPacket(uint16_t seq_num) override { |
| 36 | auto packet_it = packets_.find(seq_num); |
| 37 | return packet_it == packets_.end() ? nullptr : &packet_it->second; |
| 38 | } |
| 39 | |
| 40 | bool InsertPacket(VCMPacket* packet) override { |
| 41 | packets_[packet->seqNum] = *packet; |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | bool GetBitstream(const video_coding::RtpFrameObject& frame, |
| 46 | uint8_t* destination) override { |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | void ReturnFrame(video_coding::RtpFrameObject* frame) override { |
| 51 | packets_.erase(frame->first_seq_num()); |
| 52 | } |
| 53 | |
| 54 | private: |
| 55 | std::map<uint16_t, VCMPacket> packets_; |
| 56 | }; |
| 57 | |
| 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 | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 72 | void OnDecryptionStatusChange(int status) { |
| 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 | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame^] | 89 | packet.frameType = key_frame ? VideoFrameType::kVideoFrameKey |
| 90 | : VideoFrameType::kVideoFrameDelta; |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 91 | packet.generic_descriptor = RtpGenericFrameDescriptor(); |
| 92 | fake_packet_buffer_->InsertPacket(&packet); |
| 93 | packet.seqNum = seq_num_; |
Niels Möller | d5e02f0 | 2019-02-20 13:12:21 +0100 | [diff] [blame] | 94 | packet.video_header.is_last_packet_in_frame = true; |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 95 | fake_packet_buffer_->InsertPacket(&packet); |
| 96 | |
| 97 | return std::unique_ptr<video_coding::RtpFrameObject>( |
| 98 | new video_coding::RtpFrameObject(fake_packet_buffer_.get(), seq_num_, |
Ilya Nikolaevskiy | 4348ce2 | 2018-12-07 16:26:56 +0100 | [diff] [blame] | 99 | seq_num_, 0, 0, 0, 0)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | protected: |
| 103 | BufferedFrameDecryptorTest() : fake_packet_buffer_(new FakePacketBuffer()) {} |
| 104 | void SetUp() override { |
| 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>(); |
| 110 | buffered_frame_decryptor_ = absl::make_unique<BufferedFrameDecryptor>( |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 111 | this, this, mock_frame_decryptor_.get()); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static const size_t kMaxStashedFrames; |
| 115 | |
| 116 | std::vector<uint8_t> fake_packet_data_; |
| 117 | rtc::scoped_refptr<FakePacketBuffer> fake_packet_buffer_; |
| 118 | rtc::scoped_refptr<MockFrameDecryptor> mock_frame_decryptor_; |
| 119 | std::unique_ptr<BufferedFrameDecryptor> buffered_frame_decryptor_; |
| 120 | size_t decrypted_frame_call_count_; |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 121 | size_t decryption_status_change_count_ = 0; |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 122 | uint16_t seq_num_; |
| 123 | }; |
| 124 | |
| 125 | const size_t BufferedFrameDecryptorTest::kMaxStashedFrames = 24; |
| 126 | |
| 127 | // Callback should always be triggered on a successful decryption. |
| 128 | TEST_F(BufferedFrameDecryptorTest, CallbackCalledOnSuccessfulDecryption) { |
| 129 | EXPECT_CALL(*mock_frame_decryptor_, Decrypt).Times(1).WillOnce(Return(0)); |
| 130 | EXPECT_CALL(*mock_frame_decryptor_, GetMaxPlaintextByteSize) |
| 131 | .Times(1) |
| 132 | .WillOnce(Return(0)); |
| 133 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 134 | EXPECT_EQ(decrypted_frame_call_count_, static_cast<size_t>(1)); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 135 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(1)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // An initial fail to decrypt should not trigger the callback. |
| 139 | TEST_F(BufferedFrameDecryptorTest, CallbackNotCalledOnFailedDecryption) { |
| 140 | EXPECT_CALL(*mock_frame_decryptor_, Decrypt).Times(1).WillOnce(Return(1)); |
| 141 | EXPECT_CALL(*mock_frame_decryptor_, GetMaxPlaintextByteSize) |
| 142 | .Times(1) |
| 143 | .WillOnce(Return(0)); |
| 144 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 145 | EXPECT_EQ(decrypted_frame_call_count_, static_cast<size_t>(0)); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 146 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(1)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // Initial failures should be stored and retried after the first successful |
| 150 | // decryption. |
| 151 | TEST_F(BufferedFrameDecryptorTest, DelayedCallbackOnBufferedFrames) { |
| 152 | EXPECT_CALL(*mock_frame_decryptor_, Decrypt) |
| 153 | .Times(3) |
| 154 | .WillOnce(Return(1)) |
| 155 | .WillOnce(Return(0)) |
| 156 | .WillOnce(Return(0)); |
| 157 | EXPECT_CALL(*mock_frame_decryptor_, GetMaxPlaintextByteSize) |
| 158 | .Times(3) |
| 159 | .WillRepeatedly(Return(0)); |
| 160 | |
| 161 | // The first decrypt will fail stashing the first frame. |
| 162 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 163 | EXPECT_EQ(decrypted_frame_call_count_, static_cast<size_t>(0)); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 164 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(1)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 165 | // The second call will succeed playing back both frames. |
| 166 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(false)); |
| 167 | EXPECT_EQ(decrypted_frame_call_count_, static_cast<size_t>(2)); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 168 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(2)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | // Subsequent failure to decrypts after the first successful decryption should |
| 172 | // fail to decryptk |
| 173 | TEST_F(BufferedFrameDecryptorTest, FTDDiscardedAfterFirstSuccess) { |
| 174 | EXPECT_CALL(*mock_frame_decryptor_, Decrypt) |
| 175 | .Times(4) |
| 176 | .WillOnce(Return(1)) |
| 177 | .WillOnce(Return(0)) |
| 178 | .WillOnce(Return(0)) |
| 179 | .WillOnce(Return(1)); |
| 180 | EXPECT_CALL(*mock_frame_decryptor_, GetMaxPlaintextByteSize) |
| 181 | .Times(4) |
| 182 | .WillRepeatedly(Return(0)); |
| 183 | |
| 184 | // The first decrypt will fail stashing the first frame. |
| 185 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 186 | EXPECT_EQ(decrypted_frame_call_count_, static_cast<size_t>(0)); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 187 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(1)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 188 | // The second call will succeed playing back both frames. |
| 189 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(false)); |
| 190 | EXPECT_EQ(decrypted_frame_call_count_, static_cast<size_t>(2)); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 191 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(2)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 192 | // A new failure call will not result in an additional decrypted frame |
| 193 | // callback. |
| 194 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 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>(3)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // Validate that the maximum number of stashed frames cannot be exceeded even if |
| 200 | // more than its maximum arrives before the first successful decryption. |
| 201 | TEST_F(BufferedFrameDecryptorTest, MaximumNumberOfFramesStored) { |
| 202 | const size_t failed_to_decrypt_count = kMaxStashedFrames * 2; |
| 203 | EXPECT_CALL(*mock_frame_decryptor_, Decrypt) |
| 204 | .Times(failed_to_decrypt_count) |
| 205 | .WillRepeatedly(Return(1)); |
| 206 | EXPECT_CALL(*mock_frame_decryptor_, GetMaxPlaintextByteSize) |
| 207 | .WillRepeatedly(Return(0)); |
| 208 | |
| 209 | for (size_t i = 0; i < failed_to_decrypt_count; ++i) { |
| 210 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 211 | } |
| 212 | EXPECT_EQ(decrypted_frame_call_count_, static_cast<size_t>(0)); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 213 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(1)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 214 | |
| 215 | EXPECT_CALL(*mock_frame_decryptor_, Decrypt) |
| 216 | .Times(kMaxStashedFrames + 1) |
| 217 | .WillRepeatedly(Return(0)); |
| 218 | buffered_frame_decryptor_->ManageEncryptedFrame(CreateRtpFrameObject(true)); |
| 219 | EXPECT_EQ(decrypted_frame_call_count_, kMaxStashedFrames + 1); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 220 | EXPECT_EQ(decryption_status_change_count_, static_cast<size_t>(2)); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | } // namespace webrtc |