frame_decryptor_interface.h 3.4 KB

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