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 <utility> |
| 14 | |
| 15 | #include "rtc_base/logging.h" |
| 16 | #include "rtc_base/system/fallthrough.h" |
Benjamin Wright | 168456c | 2018-12-07 11:31:25 -0800 | [diff] [blame] | 17 | #include "system_wrappers/include/field_trial.h" |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | BufferedFrameDecryptor::BufferedFrameDecryptor( |
| 22 | OnDecryptedFrameCallback* decrypted_frame_callback, |
Benjamin Wright | a556448 | 2019-04-03 10:44:18 -0700 | [diff] [blame] | 23 | OnDecryptionStatusChangeCallback* decryption_status_change_callback) |
Benjamin Wright | 168456c | 2018-12-07 11:31:25 -0800 | [diff] [blame] | 24 | : generic_descriptor_auth_experiment_( |
| 25 | field_trial::IsEnabled("WebRTC-GenericDescriptorAuth")), |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 26 | decrypted_frame_callback_(decrypted_frame_callback), |
| 27 | decryption_status_change_callback_(decryption_status_change_callback) {} |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 28 | |
| 29 | BufferedFrameDecryptor::~BufferedFrameDecryptor() {} |
| 30 | |
Benjamin Wright | a556448 | 2019-04-03 10:44:18 -0700 | [diff] [blame] | 31 | void BufferedFrameDecryptor::SetFrameDecryptor( |
| 32 | rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) { |
| 33 | frame_decryptor_ = std::move(frame_decryptor); |
| 34 | } |
| 35 | |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 36 | void BufferedFrameDecryptor::ManageEncryptedFrame( |
| 37 | std::unique_ptr<video_coding::RtpFrameObject> encrypted_frame) { |
| 38 | switch (DecryptFrame(encrypted_frame.get())) { |
| 39 | case FrameDecision::kStash: |
| 40 | if (stashed_frames_.size() >= kMaxStashedFrames) { |
Benjamin Wright | a556448 | 2019-04-03 10:44:18 -0700 | [diff] [blame] | 41 | RTC_LOG(LS_WARNING) << "Encrypted frame stash full poping oldest item."; |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 42 | stashed_frames_.pop_front(); |
| 43 | } |
| 44 | stashed_frames_.push_back(std::move(encrypted_frame)); |
| 45 | break; |
| 46 | case FrameDecision::kDecrypted: |
| 47 | RetryStashedFrames(); |
| 48 | decrypted_frame_callback_->OnDecryptedFrame(std::move(encrypted_frame)); |
| 49 | break; |
| 50 | case FrameDecision::kDrop: |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | BufferedFrameDecryptor::FrameDecision BufferedFrameDecryptor::DecryptFrame( |
| 56 | video_coding::RtpFrameObject* frame) { |
| 57 | // Optionally attempt to decrypt the raw video frame if it was provided. |
| 58 | if (frame_decryptor_ == nullptr) { |
Benjamin Wright | a556448 | 2019-04-03 10:44:18 -0700 | [diff] [blame] | 59 | RTC_LOG(LS_INFO) << "Frame decryption required but not attached to this " |
| 60 | "stream. Stashing frame."; |
| 61 | return FrameDecision::kStash; |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 62 | } |
| 63 | // When using encryption we expect the frame to have the generic descriptor. |
| 64 | absl::optional<RtpGenericFrameDescriptor> descriptor = |
| 65 | frame->GetGenericFrameDescriptor(); |
| 66 | if (!descriptor) { |
| 67 | RTC_LOG(LS_ERROR) << "No generic frame descriptor found dropping frame."; |
| 68 | return FrameDecision::kDrop; |
| 69 | } |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 70 | // Retrieve the maximum possible size of the decrypted payload. |
| 71 | const size_t max_plaintext_byte_size = |
| 72 | frame_decryptor_->GetMaxPlaintextByteSize(cricket::MEDIA_TYPE_VIDEO, |
| 73 | frame->size()); |
| 74 | RTC_CHECK_LE(max_plaintext_byte_size, frame->size()); |
| 75 | // Place the decrypted frame inline into the existing frame. |
Niels Möller | 9c84390 | 2019-01-11 10:21:35 +0100 | [diff] [blame] | 76 | rtc::ArrayView<uint8_t> inline_decrypted_bitstream(frame->data(), |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 77 | max_plaintext_byte_size); |
Benjamin Wright | 168456c | 2018-12-07 11:31:25 -0800 | [diff] [blame] | 78 | |
| 79 | // Only enable authenticating the header if the field trial is enabled. |
| 80 | rtc::ArrayView<const uint8_t> additional_data; |
| 81 | if (generic_descriptor_auth_experiment_) { |
| 82 | additional_data = descriptor->GetByteRepresentation(); |
| 83 | } |
| 84 | |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 85 | // Attempt to decrypt the video frame. |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 86 | const FrameDecryptorInterface::Result decrypt_result = |
| 87 | frame_decryptor_->Decrypt(cricket::MEDIA_TYPE_VIDEO, /*csrcs=*/{}, |
| 88 | additional_data, *frame, |
| 89 | inline_decrypted_bitstream); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 90 | // Optionally call the callback if there was a change in status |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 91 | if (decrypt_result.status != last_status_) { |
| 92 | last_status_ = decrypt_result.status; |
| 93 | decryption_status_change_callback_->OnDecryptionStatusChange( |
| 94 | decrypt_result.status); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 97 | if (!decrypt_result.IsOk()) { |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 98 | // Only stash frames if we have never decrypted a frame before. |
| 99 | return first_frame_decrypted_ ? FrameDecision::kDrop |
| 100 | : FrameDecision::kStash; |
| 101 | } |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 102 | RTC_CHECK_LE(decrypt_result.bytes_written, max_plaintext_byte_size); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 103 | // Update the frame to contain just the written bytes. |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 104 | frame->set_size(decrypt_result.bytes_written); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 105 | |
| 106 | // Indicate that all future fail to decrypt frames should be dropped. |
| 107 | if (!first_frame_decrypted_) { |
| 108 | first_frame_decrypted_ = true; |
| 109 | } |
| 110 | |
| 111 | return FrameDecision::kDecrypted; |
| 112 | } |
| 113 | |
| 114 | void BufferedFrameDecryptor::RetryStashedFrames() { |
Benjamin Wright | 2108cc6 | 2019-04-08 16:50:04 -0700 | [diff] [blame] | 115 | if (!stashed_frames_.empty()) { |
| 116 | RTC_LOG(LS_INFO) << "Retrying stashed encrypted frames. Count: " |
| 117 | << stashed_frames_.size(); |
| 118 | } |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 119 | for (auto& frame : stashed_frames_) { |
| 120 | if (DecryptFrame(frame.get()) == FrameDecision::kDecrypted) { |
| 121 | decrypted_frame_callback_->OnDecryptedFrame(std::move(frame)); |
| 122 | } |
| 123 | } |
| 124 | stashed_frames_.clear(); |
| 125 | } |
| 126 | |
| 127 | } // namespace webrtc |