This change introduces a new class BufferedFrameDecryptor that is responsible for decrypting received encrypted frames and passing them on to the RtpReferenceFinder. This decoupling refactoring was triggered by a new optimization also introduced in this patch to stash a small number of undecryptable frames if no frames have ever been decrypted. The goal of this optimization is to prevent re-fectching of key frames on low bandwidth networks simply because the key to decrypt them had not arrived yet. The optimization will stash 24 frames (about 1 second of video) in a ring buffer and will attempt to re-decrypt previously received frames on the first valid decryption. This allows the decoder to receive the key frame without having to request due to short key delivery latencies. In testing this is actually hit quite often and saves an entire RTT which can be up to 200ms on a bad network. As the scope of frame encryption increases in WebRTC and has more specialized optimizations that do not apply to the general flow it makes sense to move it to a more explicit bump in the stack protocol that is decoupled from the WebRTC main flow, similar to how SRTP is utilized with srtp_protect and srtp_unprotect. One advantage of this approach is the BufferedFrameDecryptor isn't even constructed if FrameEncryption is not in use. I have decided against merging the RtpReferenceFinder and EncryptedFrame stash because it introduced a lot of complexity around the mixed scenario where some of the frames in the stash are encrypted and others are not. In this case we would need to mark certain frames as decrypted which appeared to introduce more complexity than this simple decoupling. Bug: webrtc:10022 Change-Id: Iab74f7b7d25ef1cdd15c4a76b5daae1cfa24932c Reviewed-on: https://webrtc-review.googlesource.com/c/112221 Commit-Queue: Benjamin Wright <benwright@webrtc.org> Reviewed-by: Philip Eliasson <philipel@webrtc.org> Reviewed-by: Stefan Holmer <stefan@webrtc.org> Cr-Commit-Position: refs/heads/master@{#25865}
81 lines
3.2 KiB
C++
81 lines
3.2 KiB
C++
/*
|
|
* Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license
|
|
* that can be found in the LICENSE file in the root of the source
|
|
* tree. An additional intellectual property rights grant can be found
|
|
* in the file PATENTS. All contributing project authors may
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
*/
|
|
|
|
#ifndef VIDEO_BUFFERED_FRAME_DECRYPTOR_H_
|
|
#define VIDEO_BUFFERED_FRAME_DECRYPTOR_H_
|
|
|
|
#include <deque>
|
|
#include <memory>
|
|
|
|
#include "api/crypto/cryptooptions.h"
|
|
#include "api/crypto/framedecryptorinterface.h"
|
|
#include "modules/include/module_common_types.h"
|
|
#include "modules/video_coding/frame_object.h"
|
|
|
|
namespace webrtc {
|
|
|
|
// This callback is provided during the construction of the
|
|
// BufferedFrameDecryptor and is called each time a frame is sucessfully
|
|
// decrypted by the buffer.
|
|
class OnDecryptedFrameCallback {
|
|
public:
|
|
virtual ~OnDecryptedFrameCallback() = default;
|
|
// Called each time a decrypted frame is returned.
|
|
virtual void OnDecryptedFrame(
|
|
std::unique_ptr<video_coding::RtpFrameObject> frame) = 0;
|
|
};
|
|
|
|
// The BufferedFrameDecryptor is responsible for deciding when to pass
|
|
// decrypted received frames onto the OnDecryptedFrameCallback. Frames can be
|
|
// delayed when frame encryption is enabled but the key hasn't arrived yet. In
|
|
// this case we stash about 1 second of encrypted frames instead of dropping
|
|
// them to prevent re-requesting the key frame. This optimization is
|
|
// particularly important on low bandwidth networks. Note stashing is only ever
|
|
// done if we have never sucessfully decrypted a frame before. After the first
|
|
// successful decryption payloads will never be stashed.
|
|
class BufferedFrameDecryptor final {
|
|
public:
|
|
// Constructs a new BufferedFrameDecryptor that can hold
|
|
explicit BufferedFrameDecryptor(
|
|
OnDecryptedFrameCallback* decrypted_frame_callback,
|
|
rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor);
|
|
~BufferedFrameDecryptor();
|
|
// This object cannot be copied.
|
|
BufferedFrameDecryptor(const BufferedFrameDecryptor&) = delete;
|
|
BufferedFrameDecryptor& operator=(const BufferedFrameDecryptor&) = delete;
|
|
// Determines whether the frame should be stashed, dropped or handed off to
|
|
// the OnDecryptedFrameCallback.
|
|
void ManageEncryptedFrame(
|
|
std::unique_ptr<video_coding::RtpFrameObject> encrypted_frame);
|
|
|
|
private:
|
|
// Represents what should be done with a given frame.
|
|
enum class FrameDecision { kStash, kDecrypted, kDrop };
|
|
|
|
// Attempts to decrypt the frame, if it fails and no prior frames have been
|
|
// decrypted it will return kStash. Otherwise fail to decrypts will return
|
|
// kDrop. Successful decryptions will always return kDecrypted.
|
|
FrameDecision DecryptFrame(video_coding::RtpFrameObject* frame);
|
|
// Retries all the stashed frames this is triggered each time a kDecrypted
|
|
// event occurs.
|
|
void RetryStashedFrames();
|
|
|
|
static const size_t kMaxStashedFrames = 24;
|
|
|
|
bool first_frame_decrypted_ = false;
|
|
const rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor_;
|
|
OnDecryptedFrameCallback* const decrypted_frame_callback_;
|
|
std::deque<std::unique_ptr<video_coding::RtpFrameObject>> stashed_frames_;
|
|
};
|
|
|
|
} // namespace webrtc
|
|
|
|
#endif // VIDEO_BUFFERED_FRAME_DECRYPTOR_H_
|