xref: /aosp_15_r20/external/webrtc/api/crypto/frame_decryptor_interface.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 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 API_CRYPTO_FRAME_DECRYPTOR_INTERFACE_H_
12 #define API_CRYPTO_FRAME_DECRYPTOR_INTERFACE_H_
13 
14 #include <vector>
15 
16 #include "api/array_view.h"
17 #include "api/media_types.h"
18 #include "rtc_base/ref_count.h"
19 
20 namespace webrtc {
21 
22 // FrameDecryptorInterface allows users to provide a custom decryption
23 // implementation for all incoming audio and video frames. The user must also
24 // provide a FrameEncryptorInterface to be able to encrypt the frames being
25 // sent out of the device. Note this is an additional layer of encyrption in
26 // addition to the standard SRTP mechanism and is not intended to be used
27 // without it. You may assume that this interface will have the same lifetime
28 // as the RTPReceiver it is attached to. It must only be attached to one
29 // RTPReceiver. Additional data may be null.
30 class FrameDecryptorInterface : public rtc::RefCountInterface {
31  public:
32   // The Status enum represents all possible states that can be
33   // returned when attempting to decrypt a frame. kRecoverable indicates that
34   // there was an error with the given frame and so it should not be passed to
35   // the decoder, however it hints that the receive stream is still decryptable
36   // which is important for determining when to send key frame requests
37   // kUnknown should never be returned by the implementor.
38   enum class Status { kOk, kRecoverable, kFailedToDecrypt, kUnknown };
39 
40   struct Result {
ResultResult41     Result(Status status, size_t bytes_written)
42         : status(status), bytes_written(bytes_written) {}
43 
IsOkResult44     bool IsOk() const { return status == Status::kOk; }
45 
46     const Status status;
47     const size_t bytes_written;
48   };
49 
~FrameDecryptorInterface()50   ~FrameDecryptorInterface() override {}
51 
52   // Attempts to decrypt the encrypted frame. You may assume the frame size will
53   // be allocated to the size returned from GetMaxPlaintextSize. You may assume
54   // that the frames are in order if SRTP is enabled. The stream is not provided
55   // here and it is up to the implementor to transport this information to the
56   // receiver if they care about it. You must set bytes_written to how many
57   // bytes you wrote to in the frame buffer. kOk must be returned if successful,
58   // kRecoverable should be returned if the failure was due to something other
59   // than a decryption failure. kFailedToDecrypt should be returned in all other
60   // cases.
61   virtual Result Decrypt(cricket::MediaType media_type,
62                          const std::vector<uint32_t>& csrcs,
63                          rtc::ArrayView<const uint8_t> additional_data,
64                          rtc::ArrayView<const uint8_t> encrypted_frame,
65                          rtc::ArrayView<uint8_t> frame) = 0;
66 
67   // Returns the total required length in bytes for the output of the
68   // decryption. This can be larger than the actual number of bytes you need but
69   // must never be smaller as it informs the size of the frame buffer.
70   virtual size_t GetMaxPlaintextByteSize(cricket::MediaType media_type,
71                                          size_t encrypted_frame_size) = 0;
72 };
73 
74 }  // namespace webrtc
75 
76 #endif  // API_CRYPTO_FRAME_DECRYPTOR_INTERFACE_H_
77