You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
4.8 KiB

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