| Marina Ciocea | e3e07bf | 2020-02-27 15:28:51 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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_FRAME_TRANSFORMER_INTERFACE_H_ |
| 12 | #define API_FRAME_TRANSFORMER_INTERFACE_H_ |
| 13 | |
| Harald Alvestrand | 6431a64 | 2024-06-04 21:29:14 | [diff] [blame] | 14 | #include <cstdint> |
| Marina Ciocea | e3e07bf | 2020-02-27 15:28:51 | [diff] [blame] | 15 | #include <memory> |
| Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 16 | #include <optional> |
| Philipp Hancke | 0bace22 | 2023-10-23 09:57:43 | [diff] [blame] | 17 | #include <string> |
| Marina Ciocea | e3e07bf | 2020-02-27 15:28:51 | [diff] [blame] | 18 | |
| Harald Alvestrand | 6431a64 | 2024-06-04 21:29:14 | [diff] [blame] | 19 | #include "api/array_view.h" |
| Harald Alvestrand | e8a2b3c | 2023-10-31 13:30:30 | [diff] [blame] | 20 | #include "api/ref_count.h" |
| Marina Ciocea | e3e07bf | 2020-02-27 15:28:51 | [diff] [blame] | 21 | #include "api/scoped_refptr.h" |
| Guido Urdaneta | e47e467 | 2025-02-13 20:21:30 | [diff] [blame] | 22 | #include "api/units/time_delta.h" |
| Harald Alvestrand | 6431a64 | 2024-06-04 21:29:14 | [diff] [blame] | 23 | #include "api/units/timestamp.h" |
| Marina Ciocea | cdc89b4 | 2020-05-14 18:01:02 | [diff] [blame] | 24 | #include "api/video/video_frame_metadata.h" |
| Philipp Hancke | 8ac2863 | 2025-07-09 17:11:20 | [diff] [blame] | 25 | #include "rtc_base/checks.h" |
| Harald Alvestrand | 6431a64 | 2024-06-04 21:29:14 | [diff] [blame] | 26 | #include "rtc_base/system/rtc_export.h" |
| Marina Ciocea | e3e07bf | 2020-02-27 15:28:51 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | |
| Marina Ciocea | c24b6b7 | 2020-03-30 12:51:10 | [diff] [blame] | 30 | // Owns the frame payload data. |
| 31 | class TransformableFrameInterface { |
| 32 | public: |
| Tony Herre | a45c705 | 2024-05-16 11:38:25 | [diff] [blame] | 33 | // Only a known list of internal implementations of transformable frames are |
| 34 | // permitted to allow internal downcasting. This is enforced via the |
| 35 | // internally-constructable Passkey. |
| 36 | // TODO: bugs.webrtc.org/339815768 - Remove this passkey once the |
| 37 | // downcasts are removed. |
| 38 | class Passkey; |
| 39 | RTC_EXPORT explicit TransformableFrameInterface(Passkey); |
| 40 | |
| 41 | TransformableFrameInterface(TransformableFrameInterface&&) = default; |
| 42 | TransformableFrameInterface& operator=(TransformableFrameInterface&&) = |
| 43 | default; |
| 44 | |
| Marina Ciocea | c24b6b7 | 2020-03-30 12:51:10 | [diff] [blame] | 45 | virtual ~TransformableFrameInterface() = default; |
| 46 | |
| 47 | // Returns the frame payload data. The data is valid until the next non-const |
| 48 | // method call. |
| Evan Shrubsole | ee5ab34 | 2025-04-15 14:49:46 | [diff] [blame] | 49 | virtual ArrayView<const uint8_t> GetData() const = 0; |
| Marina Ciocea | c24b6b7 | 2020-03-30 12:51:10 | [diff] [blame] | 50 | |
| Artem Titov | 0e61fdd | 2021-07-25 19:50:14 | [diff] [blame] | 51 | // Copies `data` into the owned frame payload data. |
| Evan Shrubsole | ee5ab34 | 2025-04-15 14:49:46 | [diff] [blame] | 52 | virtual void SetData(ArrayView<const uint8_t> data) = 0; |
| Marina Ciocea | c24b6b7 | 2020-03-30 12:51:10 | [diff] [blame] | 53 | |
| Olga Sharonova | 92e9ff6 | 2021-09-02 07:58:21 | [diff] [blame] | 54 | virtual uint8_t GetPayloadType() const = 0; |
| Guido Urdaneta | 73e837b | 2025-05-20 13:25:10 | [diff] [blame] | 55 | virtual bool CanSetPayloadType() const { return false; } |
| 56 | virtual void SetPayloadType(uint8_t payload_type) { RTC_DCHECK_NOTREACHED(); } |
| Marina Ciocea | c24b6b7 | 2020-03-30 12:51:10 | [diff] [blame] | 57 | virtual uint32_t GetSsrc() const = 0; |
| Philipp Hancke | 2ace42f | 2021-08-24 08:40:12 | [diff] [blame] | 58 | virtual uint32_t GetTimestamp() const = 0; |
| Tony Herre | b4062e5 | 2023-06-29 09:02:22 | [diff] [blame] | 59 | virtual void SetRTPTimestamp(uint32_t timestamp) = 0; |
| 60 | |
| Palak Agarwal | c4f61fb | 2024-10-15 11:42:46 | [diff] [blame] | 61 | // TODO(https://bugs.webrtc.org/373365537): Remove this once its usage is |
| 62 | // removed from blink. |
| Palak Agarwal | c21eb4e | 2025-03-19 11:06:25 | [diff] [blame] | 63 | [[deprecated( |
| 64 | "Use GetPresentationTimestamp instead")]] virtual std::optional<Timestamp> |
| 65 | GetCaptureTimeIdentifier() const { |
| Palak Agarwal | c4f61fb | 2024-10-15 11:42:46 | [diff] [blame] | 66 | return std::nullopt; |
| 67 | } |
| 68 | |
| Palak Agarwal | a09f21b | 2023-02-22 13:46:23 | [diff] [blame] | 69 | // TODO(https://bugs.webrtc.org/14878): Change this to pure virtual after it |
| 70 | // is implemented everywhere. |
| Palak Agarwal | c4f61fb | 2024-10-15 11:42:46 | [diff] [blame] | 71 | virtual std::optional<Timestamp> GetPresentationTimestamp() const { |
| Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 72 | return std::nullopt; |
| Palak Agarwal | a09f21b | 2023-02-22 13:46:23 | [diff] [blame] | 73 | } |
| Tony Herre | 8fb41a3 | 2021-09-24 12:05:20 | [diff] [blame] | 74 | |
| 75 | enum class Direction { |
| 76 | kUnknown, |
| 77 | kReceiver, |
| 78 | kSender, |
| 79 | }; |
| 80 | // TODO(crbug.com/1250638): Remove this distinction between receiver and |
| 81 | // sender frames to allow received frames to be directly re-transmitted on |
| 82 | // other PeerConnectionss. |
| 83 | virtual Direction GetDirection() const { return Direction::kUnknown; } |
| Philipp Hancke | 3e3881a | 2023-11-06 10:28:30 | [diff] [blame] | 84 | virtual std::string GetMimeType() const = 0; |
| Guido Urdaneta | e18a2e7 | 2025-02-25 15:59:02 | [diff] [blame] | 85 | |
| 86 | // Timestamp at which the packet has been first seen on the network interface. |
| 87 | // Only defined for received frames. |
| 88 | virtual std::optional<Timestamp> ReceiveTime() const = 0; |
| 89 | |
| 90 | // Timestamp at which the frame was captured in the capturer system. |
| Guido Urdaneta | 73e837b | 2025-05-20 13:25:10 | [diff] [blame] | 91 | // For receiver frames, the timestamp is expressed in the capturer system's |
| 92 | // clock relative to the NTP epoch (January 1st 1970 00:00 UTC) and is |
| 93 | // available only if the absolute capture timestamp header extension is |
| Guido Urdaneta | e18a2e7 | 2025-02-25 15:59:02 | [diff] [blame] | 94 | // enabled. |
| Guido Urdaneta | 73e837b | 2025-05-20 13:25:10 | [diff] [blame] | 95 | // For sender frames, the timestamp is expressed relative to the local |
| 96 | // system clock's default epoch. |
| Guido Urdaneta | e18a2e7 | 2025-02-25 15:59:02 | [diff] [blame] | 97 | virtual std::optional<Timestamp> CaptureTime() const = 0; |
| Guido Urdaneta | 73e837b | 2025-05-20 13:25:10 | [diff] [blame] | 98 | virtual bool CanSetCaptureTime() const { return false; } |
| 99 | virtual void SetCaptureTime(std::optional<Timestamp> capture_time) { |
| 100 | RTC_DCHECK_NOTREACHED(); |
| 101 | } |
| Guido Urdaneta | e18a2e7 | 2025-02-25 15:59:02 | [diff] [blame] | 102 | |
| 103 | // Offset between the sender system's clock and the capturer system's clock. |
| 104 | // Can be used to express the capture time in the local system's clock as |
| 105 | // long as the local system can determine the offset between its local clock |
| 106 | // and the sender system's clock. |
| 107 | // Accessible only if the absolute capture timestamp header extension is |
| 108 | // enabled. |
| 109 | virtual std::optional<TimeDelta> SenderCaptureTimeOffset() const = 0; |
| Marina Ciocea | c24b6b7 | 2020-03-30 12:51:10 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | class TransformableVideoFrameInterface : public TransformableFrameInterface { |
| 113 | public: |
| Tony Herre | a45c705 | 2024-05-16 11:38:25 | [diff] [blame] | 114 | RTC_EXPORT explicit TransformableVideoFrameInterface(Passkey passkey); |
| Marina Ciocea | c24b6b7 | 2020-03-30 12:51:10 | [diff] [blame] | 115 | virtual ~TransformableVideoFrameInterface() = default; |
| 116 | virtual bool IsKeyFrame() const = 0; |
| Guido Urdaneta | e51c178 | 2025-10-01 08:24:23 | [diff] [blame] | 117 | virtual std::optional<std::string> Rid() const { return std::nullopt; } |
| Tony Herre | 6d262c5 | 2023-02-24 11:20:28 | [diff] [blame] | 118 | virtual VideoFrameMetadata Metadata() const = 0; |
| Tony Herre | c381c33 | 2023-04-13 07:37:55 | [diff] [blame] | 119 | virtual void SetMetadata(const VideoFrameMetadata&) = 0; |
| Marina Ciocea | c24b6b7 | 2020-03-30 12:51:10 | [diff] [blame] | 120 | }; |
| 121 | |
| Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [diff] [blame] | 122 | // Extends the TransformableFrameInterface to expose audio-specific information. |
| 123 | class TransformableAudioFrameInterface : public TransformableFrameInterface { |
| 124 | public: |
| Tony Herre | a45c705 | 2024-05-16 11:38:25 | [diff] [blame] | 125 | RTC_EXPORT explicit TransformableAudioFrameInterface(Passkey passkey); |
| Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [diff] [blame] | 126 | virtual ~TransformableAudioFrameInterface() = default; |
| 127 | |
| Evan Shrubsole | ee5ab34 | 2025-04-15 14:49:46 | [diff] [blame] | 128 | virtual ArrayView<const uint32_t> GetContributingSources() const = 0; |
| Tony Herre | 097a4de | 2023-06-19 15:13:08 | [diff] [blame] | 129 | |
| Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 130 | virtual const std::optional<uint16_t> SequenceNumber() const = 0; |
| Tony Herre | fc68f1f | 2023-06-22 13:32:24 | [diff] [blame] | 131 | |
| Guido Urdaneta | e47e467 | 2025-02-13 20:21:30 | [diff] [blame] | 132 | // TODO(crbug.com/391114797): Delete this function. |
| Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 133 | virtual std::optional<uint64_t> AbsoluteCaptureTimestamp() const = 0; |
| Tony Herre | fc68f1f | 2023-06-22 13:32:24 | [diff] [blame] | 134 | |
| 135 | enum class FrameType { kEmptyFrame, kAudioFrameSpeech, kAudioFrameCN }; |
| 136 | |
| 137 | // TODO(crbug.com/1456628): Change this to pure virtual after it |
| 138 | // is implemented everywhere. |
| 139 | virtual FrameType Type() const { return FrameType::kEmptyFrame; } |
| Tony Herre | 64437e8 | 2024-04-29 13:13:48 | [diff] [blame] | 140 | |
| 141 | // Audio level in -dBov. Values range from 0 to 127, representing 0 to -127 |
| 142 | // dBov. 127 represents digital silence. Only present on remote frames if |
| 143 | // the audio level header extension was included. |
| Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 144 | virtual std::optional<uint8_t> AudioLevel() const = 0; |
| Guido Urdaneta | 73e837b | 2025-05-20 13:25:10 | [diff] [blame] | 145 | virtual bool CanSetAudioLevel() const { return false; } |
| 146 | virtual void SetAudioLevel(std::optional<uint8_t> audio_level_dbov) { |
| 147 | RTC_DCHECK_NOTREACHED(); |
| 148 | } |
| Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [diff] [blame] | 149 | }; |
| 150 | |
| Marina Ciocea | e3e07bf | 2020-02-27 15:28:51 | [diff] [blame] | 151 | // Objects implement this interface to be notified with the transformed frame. |
| Harald Alvestrand | 6431a64 | 2024-06-04 21:29:14 | [diff] [blame] | 152 | class TransformedFrameCallback : public RefCountInterface { |
| Marina Ciocea | e3e07bf | 2020-02-27 15:28:51 | [diff] [blame] | 153 | public: |
| 154 | virtual void OnTransformedFrame( |
| Marina Ciocea | 81be421 | 2020-05-05 14:03:54 | [diff] [blame] | 155 | std::unique_ptr<TransformableFrameInterface> frame) = 0; |
| Marina Ciocea | e3e07bf | 2020-02-27 15:28:51 | [diff] [blame] | 156 | |
| Tony Herre | 6e95605 | 2023-11-16 13:59:54 | [diff] [blame] | 157 | // Request to no longer be called on each frame, instead having frames be |
| 158 | // sent directly to OnTransformedFrame without additional work. |
| 159 | // TODO(crbug.com/1502781): Make pure virtual once all mocks have |
| 160 | // implementations. |
| 161 | virtual void StartShortCircuiting() {} |
| 162 | |
| Marina Ciocea | e3e07bf | 2020-02-27 15:28:51 | [diff] [blame] | 163 | protected: |
| 164 | ~TransformedFrameCallback() override = default; |
| 165 | }; |
| 166 | |
| Marina Ciocea | c24b6b7 | 2020-03-30 12:51:10 | [diff] [blame] | 167 | // Transforms encoded frames. The transformed frame is sent in a callback using |
| 168 | // the TransformedFrameCallback interface (see above). |
| Harald Alvestrand | 6431a64 | 2024-06-04 21:29:14 | [diff] [blame] | 169 | class FrameTransformerInterface : public RefCountInterface { |
| Marina Ciocea | e3e07bf | 2020-02-27 15:28:51 | [diff] [blame] | 170 | public: |
| Artem Titov | 0e61fdd | 2021-07-25 19:50:14 | [diff] [blame] | 171 | // Transforms `frame` using the implementing class' processing logic. |
| Marina Ciocea | c24b6b7 | 2020-03-30 12:51:10 | [diff] [blame] | 172 | virtual void Transform( |
| Marina Ciocea | 81be421 | 2020-05-05 14:03:54 | [diff] [blame] | 173 | std::unique_ptr<TransformableFrameInterface> transformable_frame) = 0; |
| Marina Ciocea | e3e07bf | 2020-02-27 15:28:51 | [diff] [blame] | 174 | |
| 175 | virtual void RegisterTransformedFrameCallback( |
| Evan Shrubsole | ee5ab34 | 2025-04-15 14:49:46 | [diff] [blame] | 176 | scoped_refptr<TransformedFrameCallback>) {} |
| Marina Ciocea | fdabfbc | 2020-04-10 16:40:11 | [diff] [blame] | 177 | virtual void RegisterTransformedFrameSinkCallback( |
| Evan Shrubsole | ee5ab34 | 2025-04-15 14:49:46 | [diff] [blame] | 178 | scoped_refptr<TransformedFrameCallback>, |
| Dor Hen | 049b43b | 2024-10-15 07:51:54 | [diff] [blame] | 179 | uint32_t /* ssrc */) {} |
| Marina Ciocea | fdabfbc | 2020-04-10 16:40:11 | [diff] [blame] | 180 | virtual void UnregisterTransformedFrameCallback() {} |
| Dor Hen | 049b43b | 2024-10-15 07:51:54 | [diff] [blame] | 181 | virtual void UnregisterTransformedFrameSinkCallback(uint32_t /* ssrc */) {} |
| Marina Ciocea | e3e07bf | 2020-02-27 15:28:51 | [diff] [blame] | 182 | |
| 183 | protected: |
| 184 | ~FrameTransformerInterface() override = default; |
| 185 | }; |
| 186 | |
| Harald Alvestrand | b0e7057 | 2024-04-23 14:04:18 | [diff] [blame] | 187 | // An interface implemented by classes that can host a transform. |
| 188 | // Currently this is implemented by the RTCRtpSender and RTCRtpReceiver. |
| 189 | class FrameTransformerHost { |
| 190 | public: |
| 191 | virtual ~FrameTransformerHost() {} |
| 192 | virtual void SetFrameTransformer( |
| Evan Shrubsole | ee5ab34 | 2025-04-15 14:49:46 | [diff] [blame] | 193 | scoped_refptr<FrameTransformerInterface> frame_transformer) = 0; |
| Harald Alvestrand | b0e7057 | 2024-04-23 14:04:18 | [diff] [blame] | 194 | // TODO: bugs.webrtc.org/15929 - To be added: |
| 195 | // virtual AddIncomingMediaType(RtpCodec codec) = 0; |
| 196 | // virtual AddOutgoingMediaType(RtpCodec codec) = 0; |
| 197 | }; |
| 198 | |
| Tony Herre | a45c705 | 2024-05-16 11:38:25 | [diff] [blame] | 199 | //------------------------------------------------------------------------------ |
| 200 | // Implementation details follow |
| 201 | //------------------------------------------------------------------------------ |
| 202 | class TransformableFrameInterface::Passkey { |
| 203 | public: |
| 204 | ~Passkey() = default; |
| 205 | |
| 206 | private: |
| 207 | // Explicit list of allowed internal implmentations of |
| 208 | // TransformableFrameInterface. |
| 209 | friend class TransformableOutgoingAudioFrame; |
| 210 | friend class TransformableIncomingAudioFrame; |
| 211 | friend class TransformableVideoSenderFrame; |
| 212 | friend class TransformableVideoReceiverFrame; |
| 213 | |
| 214 | friend class MockTransformableFrame; |
| 215 | friend class MockTransformableAudioFrame; |
| 216 | friend class MockTransformableVideoFrame; |
| 217 | Passkey() = default; |
| 218 | }; |
| 219 | |
| Marina Ciocea | e3e07bf | 2020-02-27 15:28:51 | [diff] [blame] | 220 | } // namespace webrtc |
| 221 | |
| 222 | #endif // API_FRAME_TRANSFORMER_INTERFACE_H_ |