Skip to content

Commit b04977c

Browse files
committed
Set is_screencast on publish_track
Makes InternalSource's is_screencast configurable. Setting it improves the latency when sharing screen content.
1 parent d98893d commit b04977c

File tree

7 files changed

+56
-5
lines changed

7 files changed

+56
-5
lines changed

libwebrtc/src/native/video_source.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,8 @@ impl NativeVideoSource {
113113
pub fn video_resolution(&self) -> VideoResolution {
114114
self.sys_handle.video_resolution().into()
115115
}
116+
117+
pub fn set_is_screencast(&self, is_screencast: bool) {
118+
self.sys_handle.set_is_screencast(is_screencast);
119+
}
116120
}

libwebrtc/src/video_source.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ impl RtcVideoSource {
4343
[Native];
4444
pub fn video_resolution(self: &Self) -> VideoResolution;
4545
);
46+
enum_dispatch!(
47+
[Native];
48+
pub fn set_is_screencast(self: &Self, is_screencast: bool);
49+
);
4650
}
4751

4852
#[cfg(not(target_arch = "wasm32"))]
@@ -81,6 +85,10 @@ pub mod native {
8185
pub fn video_resolution(&self) -> VideoResolution {
8286
self.handle.video_resolution()
8387
}
88+
89+
pub fn set_is_screencast(&self, is_screencast: bool) {
90+
self.handle.set_is_screencast(is_screencast);
91+
}
8492
}
8593
}
8694

livekit-protocol/src/enum_dispatch.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,23 @@ macro_rules! enum_dispatch {
3333
}
3434
};
3535

36+
// Handle functions without a return type
37+
(@fnc [$($variant:ident),+]: $vis:vis fn $fnc:ident($self:ident: $sty:ty $(, $arg:ident: $t:ty)*)) => {
38+
#[inline]
39+
$vis fn $fnc($self: $sty, $($arg: $t),*) {
40+
enum_dispatch!(@match [$($variant),+]: $fnc, $self, ($($arg,)*))
41+
}
42+
};
43+
3644
($variants:tt; $($vis:vis fn $fnc:ident$args:tt -> $ret:ty;)+) => {
3745
$(
3846
enum_dispatch!(@fnc $variants: $vis fn $fnc$args -> $ret);
3947
)+
4048
};
49+
50+
($variants:tt; $($vis:vis fn $fnc:ident$args:tt;)+) => {
51+
$(
52+
enum_dispatch!(@fnc $variants: $vis fn $fnc$args);
53+
)+
54+
};
4155
}

livekit/src/room/participant/local_participant.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,13 @@ impl LocalParticipant {
262262

263263
encodings = compute_video_encodings(req.width, req.height, &options);
264264
req.layers = video_layers_from_encodings(req.width, req.height, &encodings);
265+
266+
match options.source {
267+
TrackSource::Screenshare => {
268+
video_track.rtc_source().set_is_screencast(true);
269+
}
270+
_ => {}
271+
}
265272
}
266273
LocalTrack::Audio(_audio_track) => {
267274
// Setup audio encoding

webrtc-sys/include/livekit/video_track.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,13 @@ class VideoTrackSource {
9999
bool remote() const override;
100100
VideoResolution video_resolution() const;
101101
bool on_captured_frame(const webrtc::VideoFrame& frame);
102+
void set_is_screencast(bool is_screencast);
102103

103104
private:
104105
mutable webrtc::Mutex mutex_;
105106
webrtc::TimestampAligner timestamp_aligner_;
106107
VideoResolution resolution_;
108+
bool is_screencast_;
107109
};
108110

109111
public:
@@ -116,6 +118,8 @@ class VideoTrackSource {
116118

117119
webrtc::scoped_refptr<InternalSource> get() const;
118120

121+
void set_is_screencast(bool is_screencast) const;
122+
119123
private:
120124
webrtc::scoped_refptr<InternalSource> source_;
121125
};

webrtc-sys/src/video_track.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ VideoTrack::~VideoTrack() {
4848

4949
void VideoTrack::add_sink(const std::shared_ptr<NativeVideoSink>& sink) const {
5050
webrtc::MutexLock lock(&mutex_);
51-
track()->AddOrUpdateSink(sink.get(),
52-
webrtc::VideoSinkWants()); // TODO(theomonnom): Expose
53-
// VideoSinkWants to Rust?
51+
track()->AddOrUpdateSink(
52+
sink.get(),
53+
webrtc::VideoSinkWants()); // TODO(theomonnom): Expose
54+
// VideoSinkWants to Rust?
5455
sinks_.push_back(sink);
5556
}
5657

@@ -106,12 +107,20 @@ std::shared_ptr<NativeVideoSink> new_native_video_sink(
106107

107108
VideoTrackSource::InternalSource::InternalSource(
108109
const VideoResolution& resolution)
109-
: webrtc::AdaptedVideoTrackSource(4), resolution_(resolution) {}
110+
: webrtc::AdaptedVideoTrackSource(4),
111+
resolution_(resolution),
112+
is_screencast_(false) {}
110113

111114
VideoTrackSource::InternalSource::~InternalSource() {}
112115

113116
bool VideoTrackSource::InternalSource::is_screencast() const {
114-
return false;
117+
webrtc::MutexLock lock(&mutex_);
118+
return is_screencast_;
119+
}
120+
121+
void VideoTrackSource::InternalSource::set_is_screencast(bool is_screencast) {
122+
webrtc::MutexLock lock(&mutex_);
123+
is_screencast_ = is_screencast;
115124
}
116125

117126
std::optional<bool> VideoTrackSource::InternalSource::needs_denoising() const {
@@ -189,6 +198,10 @@ bool VideoTrackSource::on_captured_frame(
189198
return source_->on_captured_frame(rtc_frame);
190199
}
191200

201+
void VideoTrackSource::set_is_screencast(bool is_screencast) const {
202+
source_->set_is_screencast(is_screencast);
203+
}
204+
192205
webrtc::scoped_refptr<VideoTrackSource::InternalSource> VideoTrackSource::get()
193206
const {
194207
return source_;

webrtc-sys/src/video_track.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ pub mod ffi {
6767

6868
fn video_resolution(self: &VideoTrackSource) -> VideoResolution;
6969
fn on_captured_frame(self: &VideoTrackSource, frame: &UniquePtr<VideoFrame>) -> bool;
70+
fn set_is_screencast(self: &VideoTrackSource, is_screencast: bool);
7071
fn new_video_track_source(resolution: &VideoResolution) -> SharedPtr<VideoTrackSource>;
7172
fn video_to_media(track: SharedPtr<VideoTrack>) -> SharedPtr<MediaStreamTrack>;
7273
unsafe fn media_to_video(track: SharedPtr<MediaStreamTrack>) -> SharedPtr<VideoTrack>;

0 commit comments

Comments
 (0)