gosuto_livekit/room/track/
local_video_track.rs1use std::{fmt::Debug, sync::Arc};
16
17use gosuto_libwebrtc::{prelude::*, stats::RtcStats};
18use livekit_protocol as proto;
19
20use super::TrackInner;
21use crate::{prelude::*, rtc_engine::lk_runtime::LkRuntime};
22
23#[derive(Clone)]
24pub struct LocalVideoTrack {
25 inner: Arc<TrackInner>,
26 source: RtcVideoSource,
27}
28
29impl Debug for LocalVideoTrack {
30 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31 f.debug_struct("LocalVideoTrack")
32 .field("sid", &self.sid())
33 .field("name", &self.name())
34 .field("source", &self.source())
35 .finish()
36 }
37}
38
39impl LocalVideoTrack {
40 pub fn new(name: String, rtc_track: RtcVideoTrack, source: RtcVideoSource) -> Self {
41 Self {
42 inner: Arc::new(super::new_inner(
43 "TR_unknown".to_owned().try_into().unwrap(),
44 name,
45 TrackKind::Video,
46 MediaStreamTrack::Video(rtc_track),
47 )),
48 source,
49 }
50 }
51
52 pub fn create_video_track(name: &str, source: RtcVideoSource) -> LocalVideoTrack {
53 let rtc_track = match source.clone() {
54 #[cfg(not(target_arch = "wasm32"))]
55 RtcVideoSource::Native(native_source) => {
56 use gosuto_libwebrtc::peer_connection_factory::native::PeerConnectionFactoryExt;
57 LkRuntime::instance()
58 .pc_factory()
59 .create_video_track(&gosuto_libwebrtc::native::create_random_uuid(), native_source)
60 }
61 _ => panic!("unsupported video source"),
62 };
63
64 Self::new(name.to_string(), rtc_track, source)
65 }
66
67 pub fn sid(&self) -> TrackSid {
68 self.inner.info.read().sid.clone()
69 }
70
71 pub fn name(&self) -> String {
72 self.inner.info.read().name.clone()
73 }
74
75 pub fn kind(&self) -> TrackKind {
76 self.inner.info.read().kind
77 }
78
79 pub fn source(&self) -> TrackSource {
80 self.inner.info.read().source
81 }
82
83 pub fn stream_state(&self) -> StreamState {
84 self.inner.info.read().stream_state
85 }
86
87 pub fn is_enabled(&self) -> bool {
88 self.inner.rtc_track.enabled()
89 }
90
91 pub fn enable(&self) {
92 self.inner.rtc_track.set_enabled(true);
93 }
94
95 pub fn disable(&self) {
96 self.inner.rtc_track.set_enabled(false);
97 }
98
99 pub fn is_muted(&self) -> bool {
100 self.inner.info.read().muted
101 }
102
103 pub fn mute(&self) {
104 super::set_muted(&self.inner, &Track::LocalVideo(self.clone()), true);
105 }
106
107 pub fn unmute(&self) {
108 super::set_muted(&self.inner, &Track::LocalVideo(self.clone()), false);
109 }
110
111 pub fn rtc_track(&self) -> RtcVideoTrack {
112 if let MediaStreamTrack::Video(video) = self.inner.rtc_track.clone() {
113 return video;
114 }
115 unreachable!();
116 }
117
118 pub fn is_remote(&self) -> bool {
119 false
120 }
121
122 pub fn rtc_source(&self) -> RtcVideoSource {
123 self.source.clone()
124 }
125
126 pub async fn get_stats(&self) -> RoomResult<Vec<RtcStats>> {
127 super::local_track::get_stats(&self.inner).await
128 }
129
130 pub(crate) fn on_muted(&self, f: impl Fn(Track) + Send + 'static) {
131 self.inner.events.lock().muted.replace(Box::new(f));
132 }
133
134 pub(crate) fn on_unmuted(&self, f: impl Fn(Track) + Send + 'static) {
135 self.inner.events.lock().unmuted.replace(Box::new(f));
136 }
137
138 pub(crate) fn transceiver(&self) -> Option<RtpTransceiver> {
139 self.inner.info.read().transceiver.clone()
140 }
141
142 pub(crate) fn set_transceiver(&self, transceiver: Option<RtpTransceiver>) {
143 self.inner.info.write().transceiver = transceiver;
144 }
145
146 pub(crate) fn update_info(&self, info: proto::TrackInfo) {
147 super::update_info(&self.inner, &Track::LocalVideo(self.clone()), info);
148 }
149}