blob: 49ab9a7bd9daccaa4eddc5e32a78f30458aa6c63 [file] [log] [blame]
Benjamin Wright00765292018-11-30 16:18:26 -08001/*
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#ifndef VIDEO_BUFFERED_FRAME_DECRYPTOR_H_
12#define VIDEO_BUFFERED_FRAME_DECRYPTOR_H_
13
14#include <deque>
15#include <memory>
16
Steve Anton10542f22019-01-11 09:11:00 -080017#include "api/crypto/crypto_options.h"
18#include "api/crypto/frame_decryptor_interface.h"
Benjamin Wright00765292018-11-30 16:18:26 -080019#include "modules/include/module_common_types.h"
20#include "modules/video_coding/frame_object.h"
21
22namespace webrtc {
23
24// This callback is provided during the construction of the
25// BufferedFrameDecryptor and is called each time a frame is sucessfully
26// decrypted by the buffer.
27class OnDecryptedFrameCallback {
28 public:
29 virtual ~OnDecryptedFrameCallback() = default;
30 // Called each time a decrypted frame is returned.
31 virtual void OnDecryptedFrame(
32 std::unique_ptr<video_coding::RtpFrameObject> frame) = 0;
33};
34
Benjamin Wright52426ed2019-03-01 11:01:59 -080035// This callback is called each time there is a status change in the decryption
36// stream. For example going from a none state to a first decryption or going
37// frome a decryptable state to a non decryptable state.
38class OnDecryptionStatusChangeCallback {
39 public:
40 virtual ~OnDecryptionStatusChangeCallback() = default;
41 // Called each time the decryption stream status changes. This call is
42 // blocking so the caller must relinquish the callback quickly. This status
43 // must match what is specified in the FrameDecryptorInterface file. Notably
44 // 0 must indicate success and any positive integer is a failure.
Benjamin Wright2af5dcb2019-04-09 20:08:41 +000045 virtual void OnDecryptionStatusChange(
46 FrameDecryptorInterface::Status status) = 0;
Benjamin Wright52426ed2019-03-01 11:01:59 -080047};
48
Benjamin Wright00765292018-11-30 16:18:26 -080049// The BufferedFrameDecryptor is responsible for deciding when to pass
50// decrypted received frames onto the OnDecryptedFrameCallback. Frames can be
51// delayed when frame encryption is enabled but the key hasn't arrived yet. In
52// this case we stash about 1 second of encrypted frames instead of dropping
53// them to prevent re-requesting the key frame. This optimization is
54// particularly important on low bandwidth networks. Note stashing is only ever
55// done if we have never sucessfully decrypted a frame before. After the first
56// successful decryption payloads will never be stashed.
57class BufferedFrameDecryptor final {
58 public:
59 // Constructs a new BufferedFrameDecryptor that can hold
60 explicit BufferedFrameDecryptor(
61 OnDecryptedFrameCallback* decrypted_frame_callback,
Benjamin Wrighta5564482019-04-03 10:44:18 -070062 OnDecryptionStatusChangeCallback* decryption_status_change_callback);
Benjamin Wright00765292018-11-30 16:18:26 -080063 ~BufferedFrameDecryptor();
64 // This object cannot be copied.
65 BufferedFrameDecryptor(const BufferedFrameDecryptor&) = delete;
66 BufferedFrameDecryptor& operator=(const BufferedFrameDecryptor&) = delete;
Benjamin Wrighta5564482019-04-03 10:44:18 -070067
68 // Sets a new frame decryptor as the decryptor for the buffered frame
69 // decryptor. This allows the decryptor to be switched out without resetting
70 // the video stream.
71 void SetFrameDecryptor(
72 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor);
73
Benjamin Wright00765292018-11-30 16:18:26 -080074 // Determines whether the frame should be stashed, dropped or handed off to
75 // the OnDecryptedFrameCallback.
76 void ManageEncryptedFrame(
77 std::unique_ptr<video_coding::RtpFrameObject> encrypted_frame);
78
79 private:
80 // Represents what should be done with a given frame.
81 enum class FrameDecision { kStash, kDecrypted, kDrop };
82
83 // Attempts to decrypt the frame, if it fails and no prior frames have been
84 // decrypted it will return kStash. Otherwise fail to decrypts will return
85 // kDrop. Successful decryptions will always return kDecrypted.
86 FrameDecision DecryptFrame(video_coding::RtpFrameObject* frame);
87 // Retries all the stashed frames this is triggered each time a kDecrypted
88 // event occurs.
89 void RetryStashedFrames();
90
91 static const size_t kMaxStashedFrames = 24;
92
Benjamin Wright168456c2018-12-07 11:31:25 -080093 const bool generic_descriptor_auth_experiment_;
Benjamin Wright00765292018-11-30 16:18:26 -080094 bool first_frame_decrypted_ = false;
Benjamin Wright2af5dcb2019-04-09 20:08:41 +000095 FrameDecryptorInterface::Status last_status_ =
96 FrameDecryptorInterface::Status::kUnknown;
Benjamin Wrighta5564482019-04-03 10:44:18 -070097 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor_;
Benjamin Wright00765292018-11-30 16:18:26 -080098 OnDecryptedFrameCallback* const decrypted_frame_callback_;
Benjamin Wright52426ed2019-03-01 11:01:59 -080099 OnDecryptionStatusChangeCallback* const decryption_status_change_callback_;
Benjamin Wright00765292018-11-30 16:18:26 -0800100 std::deque<std::unique_ptr<video_coding::RtpFrameObject>> stashed_frames_;
101};
102
103} // namespace webrtc
104
105#endif // VIDEO_BUFFERED_FRAME_DECRYPTOR_H_