BlueGit

ict rusty-teams

Files

rusty-teams / vendor / webrtc-sys / include / livekit / video_track.h

video_track.h

video_track.h 5.9 KiB Download Raw
1/*
2 * Copyright 2025 LiveKit, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <atomic>
20#include <memory>
21
22#include "api/media_stream_interface.h"
23#include "api/video/video_frame.h"
24#include "livekit/helper.h"
25#include "livekit/encoded_video_frame_buffer.h"
26#include "livekit/media_stream_track.h"
27#include "livekit/video_frame.h"
28#include "livekit/webrtc.h"
29#include "media/base/adapted_video_track_source.h"
30#include "rtc_base/synchronization/mutex.h"
31#include "rtc_base/timestamp_aligner.h"
32#include "rust/cxx.h"
33
34namespace livekit_ffi {
35class VideoTrack;
36class NativeVideoSink;
37class VideoTrackSource;
38class PacketTrailerHandler; // forward declaration to avoid circular include
39} // namespace livekit_ffi
40#include "webrtc-sys/src/video_track.rs.h"
41
42namespace livekit_ffi {
43
44class VideoTrack : public MediaStreamTrack {
45 private:
46 friend RtcRuntime;
47 VideoTrack(std::shared_ptr<RtcRuntime> rtc_runtime,
48 webrtc::scoped_refptr<webrtc::VideoTrackInterface> track);
49
50 public:
51 ~VideoTrack();
52
53 void add_sink(const std::shared_ptr<NativeVideoSink>& sink) const;
54 void remove_sink(const std::shared_ptr<NativeVideoSink>& sink) const;
55
56 void set_should_receive(bool should_receive) const;
57 bool should_receive() const;
58 ContentHint content_hint() const;
59 void set_content_hint(ContentHint hint) const;
60
61 private:
62 webrtc::VideoTrackInterface* track() const {
63 return static_cast<webrtc::VideoTrackInterface*>(track_.get());
64 }
65
66 mutable webrtc::Mutex mutex_;
67
68 // Same for AudioTrack:
69 // Keep a strong reference to the added sinks, so we don't need to
70 // manage the lifetime safety on the Rust side
71 mutable std::vector<std::shared_ptr<NativeVideoSink>> sinks_;
72};
73
74class NativeVideoSink : public webrtc::VideoSinkInterface<webrtc::VideoFrame> {
75 public:
76 explicit NativeVideoSink(rust::Box<VideoSinkWrapper> observer);
77
78 void OnFrame(const webrtc::VideoFrame& frame) override;
79 void OnDiscardedFrame() override;
80 void OnConstraintsChanged(
81 const webrtc::VideoTrackSourceConstraints& constraints) override;
82
83 private:
84 rust::Box<VideoSinkWrapper> observer_;
85};
86
87std::shared_ptr<NativeVideoSink> new_native_video_sink(
88 rust::Box<VideoSinkWrapper> observer);
89
90class VideoTrackSource {
91 class InternalSource : public webrtc::AdaptedVideoTrackSource {
92 public:
93 InternalSource(const VideoResolution& resolution,
94 bool is_screencast); // (0, 0) means no resolution/optional, the
95 // source will guess the resolution at the
96 // first captured frame
97 ~InternalSource() override;
98
99 bool is_screencast() const override;
100 std::optional<bool> needs_denoising() const override;
101 SourceState state() const override;
102 bool remote() const override;
103 VideoResolution video_resolution() const;
104 bool on_captured_frame(const webrtc::VideoFrame& frame,
105 const FrameMetadata& frame_metadata);
106
107 void set_packet_trailer_handler(
108 std::shared_ptr<PacketTrailerHandler> handler);
109
110 // Shared with every EncodedVideoFrameBuffer this source emits; the
111 // pass-through encoder raises it on unsatisfied keyframe requests.
112 std::shared_ptr<std::atomic<bool>> keyframe_request_flag() const {
113 return keyframe_request_flag_;
114 }
115 std::shared_ptr<livekit::EncodedRateControlState> rate_control_state()
116 const {
117 return rate_control_state_;
118 }
119
120 private:
121 mutable webrtc::Mutex mutex_;
122 webrtc::TimestampAligner timestamp_aligner_;
123 VideoResolution resolution_;
124 std::shared_ptr<PacketTrailerHandler> packet_trailer_handler_;
125 std::shared_ptr<std::atomic<bool>> keyframe_request_flag_ =
126 std::make_shared<std::atomic<bool>>(false);
127 std::shared_ptr<livekit::EncodedRateControlState> rate_control_state_ =
128 std::make_shared<livekit::EncodedRateControlState>();
129 bool is_screencast_;
130 };
131
132 public:
133 VideoTrackSource(const VideoResolution& resolution, bool is_screencast);
134
135 VideoResolution video_resolution() const;
136
137 bool on_captured_frame(const std::unique_ptr<VideoFrame>& frame,
138 const FrameMetadata& frame_metadata)
139 const; // frames pushed from Rust (+interior mutability)
140
141 bool capture_encoded_frame(int width,
142 int height,
143 const EncodedVideoFrameData& frame,
144 rust::Slice<const uint8_t> payload,
145 const FrameMetadata& frame_metadata) const;
146
147 // Returns and clears the pending upstream keyframe request raised by the
148 // pass-through encoder (PLI/FIR or post-reconfigure). Poll from the
149 // capture loop.
150 bool take_keyframe_request() const;
151 EncodedRateControlRequest take_rate_control_request() const;
152
153 void set_packet_trailer_handler(
154 std::shared_ptr<PacketTrailerHandler> handler) const;
155
156 webrtc::scoped_refptr<InternalSource> get() const;
157
158 private:
159 webrtc::scoped_refptr<InternalSource> source_;
160};
161
162std::shared_ptr<VideoTrackSource> new_video_track_source(
163 const VideoResolution& resolution, bool is_screencast);
164
165static std::shared_ptr<MediaStreamTrack> video_to_media(
166 std::shared_ptr<VideoTrack> track) {
167 return track;
168}
169
170static std::shared_ptr<VideoTrack> media_to_video(
171 std::shared_ptr<MediaStreamTrack> track) {
172 return std::static_pointer_cast<VideoTrack>(track);
173}
174
175static std::shared_ptr<VideoTrack> _shared_video_track() {
176 return nullptr; // Ignore
177}
178
179} // namespace livekit_ffi