blob: 84fc6154846694bb98cbc6127d1645daf4b54717 [file] [log] [blame]
Benjamin Wrightea086912018-08-29 13:06:15 -07001/*
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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef API_CRYPTO_FRAME_DECRYPTOR_INTERFACE_H_
12#define API_CRYPTO_FRAME_DECRYPTOR_INTERFACE_H_
Benjamin Wrightea086912018-08-29 13:06:15 -070013
Benjamin Wright1f87ec62018-09-12 13:29:08 -070014#include <vector>
15
Benjamin Wrightea086912018-08-29 13:06:15 -070016#include "api/array_view.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "api/media_types.h"
18#include "rtc_base/ref_count.h"
Benjamin Wrightea086912018-08-29 13:06:15 -070019
20namespace 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
Benjamin Wright1f87ec62018-09-12 13:29:08 -070029// RTPReceiver. Additional data may be null.
Benjamin Wright88e3e3f2018-09-06 13:20:14 -070030// Note: This interface is not ready for production use.
Benjamin Wrightea086912018-08-29 13:06:15 -070031class FrameDecryptorInterface : public rtc::RefCountInterface {
32 public:
Benjamin Wright72e97712019-04-05 13:15:26 -070033 // The Status enum represents all possible states that can be
34 // returned when attempting to decrypt a frame. kRecoverable indicates that
35 // there was an error with the given frame and so it should not be passed to
36 // the decoder, however it hints that the receive stream is still decryptable
37 // which is important for determining when to send key frame requests.
38 enum class Status { kOk, kRecoverable, kFailedToDecrypt };
39
40 struct Result {
41 Status status;
42 size_t bytes_written;
43 };
44
Benjamin Wrightd81ac952018-08-29 17:02:10 -070045 ~FrameDecryptorInterface() override {}
Benjamin Wrightea086912018-08-29 13:06:15 -070046
47 // Attempts to decrypt the encrypted frame. You may assume the frame size will
Benjamin Wright1f87ec62018-09-12 13:29:08 -070048 // be allocated to the size returned from GetMaxPlaintextSize. You may assume
49 // that the frames are in order if SRTP is enabled. The stream is not provided
50 // here and it is up to the implementor to transport this information to the
Benjamin Wright88e3e3f2018-09-06 13:20:14 -070051 // receiver if they care about it. You must set bytes_written to how many
52 // bytes you wrote to in the frame buffer. 0 must be returned if successful
53 // all other numbers can be selected by the implementer to represent error
54 // codes.
Benjamin Wright72e97712019-04-05 13:15:26 -070055 // TODO(bugs.webrtc.org/10512) - Remove this after implementation rewrite.
Benjamin Wright88e3e3f2018-09-06 13:20:14 -070056 virtual int Decrypt(cricket::MediaType media_type,
Benjamin Wright1f87ec62018-09-12 13:29:08 -070057 const std::vector<uint32_t>& csrcs,
58 rtc::ArrayView<const uint8_t> additional_data,
Benjamin Wright88e3e3f2018-09-06 13:20:14 -070059 rtc::ArrayView<const uint8_t> encrypted_frame,
60 rtc::ArrayView<uint8_t> frame,
Benjamin Wright72e97712019-04-05 13:15:26 -070061 size_t* bytes_written) {
62 bytes_written = 0;
63 return 1;
64 }
65
66 // TODO(bugs.webrtc.org/10512) - Remove the other decrypt function and turn
67 // this to a pure virtual.
68 virtual Result Decrypt(cricket::MediaType media_type,
69 const std::vector<uint32_t>& csrcs,
70 rtc::ArrayView<const uint8_t> additional_data,
71 rtc::ArrayView<const uint8_t> encrypted_frame,
72 rtc::ArrayView<uint8_t> frame) {
73 size_t bytes_written = 0;
74 const int status = Decrypt(media_type, csrcs, additional_data,
75 encrypted_frame, frame, &bytes_written);
76 Result decryption_result;
77 decryption_result.bytes_written = bytes_written;
78 if (status == 0) {
79 decryption_result.status = Status::kOk;
80 } else {
81 decryption_result.status = Status::kFailedToDecrypt;
82 }
83 return decryption_result;
84 }
Benjamin Wrightea086912018-08-29 13:06:15 -070085
86 // Returns the total required length in bytes for the output of the
Benjamin Wright88e3e3f2018-09-06 13:20:14 -070087 // decryption. This can be larger than the actual number of bytes you need but
88 // must never be smaller as it informs the size of the frame buffer.
89 virtual size_t GetMaxPlaintextByteSize(cricket::MediaType media_type,
90 size_t encrypted_frame_size) = 0;
Benjamin Wrightea086912018-08-29 13:06:15 -070091};
92
93} // namespace webrtc
94
Steve Anton10542f22019-01-11 09:11:00 -080095#endif // API_CRYPTO_FRAME_DECRYPTOR_INTERFACE_H_