Benjamin Wright | ea08691 | 2018-08-29 13:06:15 -0700 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef API_CRYPTO_FRAME_DECRYPTOR_INTERFACE_H_ |
| 12 | #define API_CRYPTO_FRAME_DECRYPTOR_INTERFACE_H_ |
Benjamin Wright | ea08691 | 2018-08-29 13:06:15 -0700 | [diff] [blame] | 13 | |
Benjamin Wright | 1f87ec6 | 2018-09-12 13:29:08 -0700 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
Benjamin Wright | ea08691 | 2018-08-29 13:06:15 -0700 | [diff] [blame] | 16 | #include "api/array_view.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 17 | #include "api/media_types.h" |
| 18 | #include "rtc_base/ref_count.h" |
Benjamin Wright | ea08691 | 2018-08-29 13:06:15 -0700 | [diff] [blame] | 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 |
Benjamin Wright | 1f87ec6 | 2018-09-12 13:29:08 -0700 | [diff] [blame] | 29 | // RTPReceiver. Additional data may be null. |
Benjamin Wright | 88e3e3f | 2018-09-06 13:20:14 -0700 | [diff] [blame] | 30 | // Note: This interface is not ready for production use. |
Benjamin Wright | ea08691 | 2018-08-29 13:06:15 -0700 | [diff] [blame] | 31 | class FrameDecryptorInterface : public rtc::RefCountInterface { |
| 32 | public: |
Benjamin Wright | 72e9771 | 2019-04-05 13:15:26 -0700 | [diff] [blame] | 33 | // 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 |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame^] | 37 | // which is important for determining when to send key frame requests |
| 38 | // kUnknown should never be returned by the implementor. |
| 39 | enum class Status { kOk, kRecoverable, kFailedToDecrypt, kUnknown }; |
Benjamin Wright | 72e9771 | 2019-04-05 13:15:26 -0700 | [diff] [blame] | 40 | |
| 41 | struct Result { |
Benjamin Wright | d1c6085 | 2019-04-05 18:05:09 -0700 | [diff] [blame] | 42 | Result(Status status, size_t bytes_written) |
| 43 | : status(status), bytes_written(bytes_written) {} |
| 44 | |
| 45 | bool IsOk() const { return status == Status::kOk; } |
| 46 | |
| 47 | const Status status; |
| 48 | const size_t bytes_written; |
Benjamin Wright | 72e9771 | 2019-04-05 13:15:26 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Benjamin Wright | d81ac95 | 2018-08-29 17:02:10 -0700 | [diff] [blame] | 51 | ~FrameDecryptorInterface() override {} |
Benjamin Wright | ea08691 | 2018-08-29 13:06:15 -0700 | [diff] [blame] | 52 | |
| 53 | // Attempts to decrypt the encrypted frame. You may assume the frame size will |
Benjamin Wright | 1f87ec6 | 2018-09-12 13:29:08 -0700 | [diff] [blame] | 54 | // be allocated to the size returned from GetMaxPlaintextSize. You may assume |
| 55 | // that the frames are in order if SRTP is enabled. The stream is not provided |
| 56 | // here and it is up to the implementor to transport this information to the |
Benjamin Wright | 88e3e3f | 2018-09-06 13:20:14 -0700 | [diff] [blame] | 57 | // receiver if they care about it. You must set bytes_written to how many |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame^] | 58 | // bytes you wrote to in the frame buffer. kOk must be returned if successful, |
| 59 | // kRecoverable should be returned if the failure was due to something other |
| 60 | // than a decryption failure. kFailedToDecrypt should be returned in all other |
| 61 | // cases. |
Benjamin Wright | 72e9771 | 2019-04-05 13:15:26 -0700 | [diff] [blame] | 62 | virtual Result Decrypt(cricket::MediaType media_type, |
| 63 | const std::vector<uint32_t>& csrcs, |
| 64 | rtc::ArrayView<const uint8_t> additional_data, |
| 65 | rtc::ArrayView<const uint8_t> encrypted_frame, |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame^] | 66 | rtc::ArrayView<uint8_t> frame) = 0; |
Benjamin Wright | ea08691 | 2018-08-29 13:06:15 -0700 | [diff] [blame] | 67 | |
| 68 | // Returns the total required length in bytes for the output of the |
Benjamin Wright | 88e3e3f | 2018-09-06 13:20:14 -0700 | [diff] [blame] | 69 | // decryption. This can be larger than the actual number of bytes you need but |
| 70 | // must never be smaller as it informs the size of the frame buffer. |
| 71 | virtual size_t GetMaxPlaintextByteSize(cricket::MediaType media_type, |
| 72 | size_t encrypted_frame_size) = 0; |
Benjamin Wright | ea08691 | 2018-08-29 13:06:15 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | } // namespace webrtc |
| 76 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 77 | #endif // API_CRYPTO_FRAME_DECRYPTOR_INTERFACE_H_ |