Thanks to visit codestin.com
Credit goes to webrtc.googlesource.com

blob: cc902443387adccd061c77eab2d684a1163b7f2c [file] [log] [blame]
Marina Cioceae3e07bf2020-02-27 15:28:511/*
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 Alvestrand6431a642024-06-04 21:29:1414#include <cstdint>
Marina Cioceae3e07bf2020-02-27 15:28:5115#include <memory>
Florent Castelli8037fc62024-08-29 13:00:4016#include <optional>
Philipp Hancke0bace222023-10-23 09:57:4317#include <string>
Marina Cioceae3e07bf2020-02-27 15:28:5118
Harald Alvestrand6431a642024-06-04 21:29:1419#include "api/array_view.h"
Harald Alvestrande8a2b3c2023-10-31 13:30:3020#include "api/ref_count.h"
Marina Cioceae3e07bf2020-02-27 15:28:5121#include "api/scoped_refptr.h"
Guido Urdanetae47e4672025-02-13 20:21:3022#include "api/units/time_delta.h"
Harald Alvestrand6431a642024-06-04 21:29:1423#include "api/units/timestamp.h"
Marina Cioceacdc89b42020-05-14 18:01:0224#include "api/video/video_frame_metadata.h"
Philipp Hancke8ac28632025-07-09 17:11:2025#include "rtc_base/checks.h"
Harald Alvestrand6431a642024-06-04 21:29:1426#include "rtc_base/system/rtc_export.h"
Marina Cioceae3e07bf2020-02-27 15:28:5127
28namespace webrtc {
29
Marina Cioceac24b6b72020-03-30 12:51:1030// Owns the frame payload data.
31class TransformableFrameInterface {
32 public:
Tony Herrea45c7052024-05-16 11:38:2533 // 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 Cioceac24b6b72020-03-30 12:51:1045 virtual ~TransformableFrameInterface() = default;
46
47 // Returns the frame payload data. The data is valid until the next non-const
48 // method call.
Evan Shrubsoleee5ab342025-04-15 14:49:4649 virtual ArrayView<const uint8_t> GetData() const = 0;
Marina Cioceac24b6b72020-03-30 12:51:1050
Artem Titov0e61fdd2021-07-25 19:50:1451 // Copies `data` into the owned frame payload data.
Evan Shrubsoleee5ab342025-04-15 14:49:4652 virtual void SetData(ArrayView<const uint8_t> data) = 0;
Marina Cioceac24b6b72020-03-30 12:51:1053
Olga Sharonova92e9ff62021-09-02 07:58:2154 virtual uint8_t GetPayloadType() const = 0;
Guido Urdaneta73e837b2025-05-20 13:25:1055 virtual bool CanSetPayloadType() const { return false; }
56 virtual void SetPayloadType(uint8_t payload_type) { RTC_DCHECK_NOTREACHED(); }
Marina Cioceac24b6b72020-03-30 12:51:1057 virtual uint32_t GetSsrc() const = 0;
Philipp Hancke2ace42f2021-08-24 08:40:1258 virtual uint32_t GetTimestamp() const = 0;
Tony Herreb4062e52023-06-29 09:02:2259 virtual void SetRTPTimestamp(uint32_t timestamp) = 0;
60
Palak Agarwalc4f61fb2024-10-15 11:42:4661 // TODO(https://bugs.webrtc.org/373365537): Remove this once its usage is
62 // removed from blink.
Palak Agarwalc21eb4e2025-03-19 11:06:2563 [[deprecated(
64 "Use GetPresentationTimestamp instead")]] virtual std::optional<Timestamp>
65 GetCaptureTimeIdentifier() const {
Palak Agarwalc4f61fb2024-10-15 11:42:4666 return std::nullopt;
67 }
68
Palak Agarwala09f21b2023-02-22 13:46:2369 // TODO(https://bugs.webrtc.org/14878): Change this to pure virtual after it
70 // is implemented everywhere.
Palak Agarwalc4f61fb2024-10-15 11:42:4671 virtual std::optional<Timestamp> GetPresentationTimestamp() const {
Florent Castelli8037fc62024-08-29 13:00:4072 return std::nullopt;
Palak Agarwala09f21b2023-02-22 13:46:2373 }
Tony Herre8fb41a32021-09-24 12:05:2074
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 Hancke3e3881a2023-11-06 10:28:3084 virtual std::string GetMimeType() const = 0;
Guido Urdanetae18a2e72025-02-25 15:59:0285
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 Urdaneta73e837b2025-05-20 13:25:1091 // 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 Urdanetae18a2e72025-02-25 15:59:0294 // enabled.
Guido Urdaneta73e837b2025-05-20 13:25:1095 // For sender frames, the timestamp is expressed relative to the local
96 // system clock's default epoch.
Guido Urdanetae18a2e72025-02-25 15:59:0297 virtual std::optional<Timestamp> CaptureTime() const = 0;
Guido Urdaneta73e837b2025-05-20 13:25:1098 virtual bool CanSetCaptureTime() const { return false; }
99 virtual void SetCaptureTime(std::optional<Timestamp> capture_time) {
100 RTC_DCHECK_NOTREACHED();
101 }
Guido Urdanetae18a2e72025-02-25 15:59:02102
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 Cioceac24b6b72020-03-30 12:51:10110};
111
112class TransformableVideoFrameInterface : public TransformableFrameInterface {
113 public:
Tony Herrea45c7052024-05-16 11:38:25114 RTC_EXPORT explicit TransformableVideoFrameInterface(Passkey passkey);
Marina Cioceac24b6b72020-03-30 12:51:10115 virtual ~TransformableVideoFrameInterface() = default;
116 virtual bool IsKeyFrame() const = 0;
Guido Urdanetae51c1782025-10-01 08:24:23117 virtual std::optional<std::string> Rid() const { return std::nullopt; }
Tony Herre6d262c52023-02-24 11:20:28118 virtual VideoFrameMetadata Metadata() const = 0;
Tony Herrec381c332023-04-13 07:37:55119 virtual void SetMetadata(const VideoFrameMetadata&) = 0;
Marina Cioceac24b6b72020-03-30 12:51:10120};
121
Marina Ciocea48623202020-04-01 08:19:44122// Extends the TransformableFrameInterface to expose audio-specific information.
123class TransformableAudioFrameInterface : public TransformableFrameInterface {
124 public:
Tony Herrea45c7052024-05-16 11:38:25125 RTC_EXPORT explicit TransformableAudioFrameInterface(Passkey passkey);
Marina Ciocea48623202020-04-01 08:19:44126 virtual ~TransformableAudioFrameInterface() = default;
127
Evan Shrubsoleee5ab342025-04-15 14:49:46128 virtual ArrayView<const uint32_t> GetContributingSources() const = 0;
Tony Herre097a4de2023-06-19 15:13:08129
Florent Castelli8037fc62024-08-29 13:00:40130 virtual const std::optional<uint16_t> SequenceNumber() const = 0;
Tony Herrefc68f1f2023-06-22 13:32:24131
Guido Urdanetae47e4672025-02-13 20:21:30132 // TODO(crbug.com/391114797): Delete this function.
Florent Castelli8037fc62024-08-29 13:00:40133 virtual std::optional<uint64_t> AbsoluteCaptureTimestamp() const = 0;
Tony Herrefc68f1f2023-06-22 13:32:24134
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 Herre64437e82024-04-29 13:13:48140
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 Castelli8037fc62024-08-29 13:00:40144 virtual std::optional<uint8_t> AudioLevel() const = 0;
Guido Urdaneta73e837b2025-05-20 13:25:10145 virtual bool CanSetAudioLevel() const { return false; }
146 virtual void SetAudioLevel(std::optional<uint8_t> audio_level_dbov) {
147 RTC_DCHECK_NOTREACHED();
148 }
Marina Ciocea48623202020-04-01 08:19:44149};
150
Marina Cioceae3e07bf2020-02-27 15:28:51151// Objects implement this interface to be notified with the transformed frame.
Harald Alvestrand6431a642024-06-04 21:29:14152class TransformedFrameCallback : public RefCountInterface {
Marina Cioceae3e07bf2020-02-27 15:28:51153 public:
154 virtual void OnTransformedFrame(
Marina Ciocea81be4212020-05-05 14:03:54155 std::unique_ptr<TransformableFrameInterface> frame) = 0;
Marina Cioceae3e07bf2020-02-27 15:28:51156
Tony Herre6e956052023-11-16 13:59:54157 // 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 Cioceae3e07bf2020-02-27 15:28:51163 protected:
164 ~TransformedFrameCallback() override = default;
165};
166
Marina Cioceac24b6b72020-03-30 12:51:10167// Transforms encoded frames. The transformed frame is sent in a callback using
168// the TransformedFrameCallback interface (see above).
Harald Alvestrand6431a642024-06-04 21:29:14169class FrameTransformerInterface : public RefCountInterface {
Marina Cioceae3e07bf2020-02-27 15:28:51170 public:
Artem Titov0e61fdd2021-07-25 19:50:14171 // Transforms `frame` using the implementing class' processing logic.
Marina Cioceac24b6b72020-03-30 12:51:10172 virtual void Transform(
Marina Ciocea81be4212020-05-05 14:03:54173 std::unique_ptr<TransformableFrameInterface> transformable_frame) = 0;
Marina Cioceae3e07bf2020-02-27 15:28:51174
175 virtual void RegisterTransformedFrameCallback(
Evan Shrubsoleee5ab342025-04-15 14:49:46176 scoped_refptr<TransformedFrameCallback>) {}
Marina Cioceafdabfbc2020-04-10 16:40:11177 virtual void RegisterTransformedFrameSinkCallback(
Evan Shrubsoleee5ab342025-04-15 14:49:46178 scoped_refptr<TransformedFrameCallback>,
Dor Hen049b43b2024-10-15 07:51:54179 uint32_t /* ssrc */) {}
Marina Cioceafdabfbc2020-04-10 16:40:11180 virtual void UnregisterTransformedFrameCallback() {}
Dor Hen049b43b2024-10-15 07:51:54181 virtual void UnregisterTransformedFrameSinkCallback(uint32_t /* ssrc */) {}
Marina Cioceae3e07bf2020-02-27 15:28:51182
183 protected:
184 ~FrameTransformerInterface() override = default;
185};
186
Harald Alvestrandb0e70572024-04-23 14:04:18187// An interface implemented by classes that can host a transform.
188// Currently this is implemented by the RTCRtpSender and RTCRtpReceiver.
189class FrameTransformerHost {
190 public:
191 virtual ~FrameTransformerHost() {}
192 virtual void SetFrameTransformer(
Evan Shrubsoleee5ab342025-04-15 14:49:46193 scoped_refptr<FrameTransformerInterface> frame_transformer) = 0;
Harald Alvestrandb0e70572024-04-23 14:04:18194 // TODO: bugs.webrtc.org/15929 - To be added:
195 // virtual AddIncomingMediaType(RtpCodec codec) = 0;
196 // virtual AddOutgoingMediaType(RtpCodec codec) = 0;
197};
198
Tony Herrea45c7052024-05-16 11:38:25199//------------------------------------------------------------------------------
200// Implementation details follow
201//------------------------------------------------------------------------------
202class 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 Cioceae3e07bf2020-02-27 15:28:51220} // namespace webrtc
221
222#endif // API_FRAME_TRANSFORMER_INTERFACE_H_