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