Skip to content

Commit

Permalink
feat: stats request (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
theomonnom authored Nov 3, 2023
1 parent a5ba08e commit e33c5a9
Show file tree
Hide file tree
Showing 22 changed files with 2,651 additions and 2,465 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 26 additions & 14 deletions examples/wgpu_room/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,13 @@ impl LkApp {
});

ui.horizontal(|ui| {
ui.label("E2EE Key: ");
ui.label("E2ee Key: ");
ui.text_edit_singleline(&mut self.state.key);
});

ui.horizontal(|ui| {
ui.add_enabled_ui(true, |ui| {
ui.checkbox(&mut self.state.enable_e2ee, "Enable E2EE");
ui.checkbox(&mut self.state.enable_e2ee, "Enable E2ee");
});
});

Expand Down Expand Up @@ -342,10 +342,23 @@ impl LkApp {
for ((participant_sid, _), video_renderer) in &self.video_renderers {
ui.video_frame(|ui| {
let room = room.as_ref().unwrap().clone();
let name =
room.participants().get(participant_sid).map(|p| p.name());

draw_video(&name.unwrap_or_default(), video_renderer, ui);
if let Some(participant) = room.participants().get(participant_sid)
{
draw_video(
participant.name().as_str(),
participant.is_speaking(),
video_renderer,
ui,
);
} else {
draw_video(
room.local_participant().name().as_str(),
room.local_participant().is_speaking(),
video_renderer,
ui,
);
}
});
}
} else {
Expand Down Expand Up @@ -409,22 +422,21 @@ impl eframe::App for LkApp {
}

/// Draw a wgpu texture to the VideoGrid
fn draw_video(name: &str, video_renderer: &VideoRenderer, ui: &mut egui::Ui) {
fn draw_video(name: &str, speaking: bool, video_renderer: &VideoRenderer, ui: &mut egui::Ui) {
let rect = ui.available_rect_before_wrap();
let inner_rect = rect.shrink(1.0);

// Always draw a background in case we still didn't receive a frame
ui.painter().rect(
rect,
Rounding::none(),
ui.style().visuals.code_bg_color,
Stroke::NONE,
);
if speaking {
ui.painter()
.rect(rect, Rounding::none(), egui::Color32::GREEN, Stroke::NONE);
}

// Always draw a background in case we still didn't receive a frame
let resolution = video_renderer.resolution();
if let Some(tex) = video_renderer.texture_id() {
ui.painter().image(
tex,
rect,
inner_rect,
egui::Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(1.0, 1.0)),
egui::Color32::WHITE,
);
Expand Down
1 change: 0 additions & 1 deletion examples/wgpu_room/src/logo_track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use livekit::webrtc::video_source::RtcVideoSource;
use livekit::webrtc::video_source::VideoResolution;
use livekit::webrtc::{
native::yuv_helper,
video_frame::native::I420BufferExt,
video_frame::{I420Buffer, VideoFrame, VideoRotation},
video_source::native::NativeVideoSource,
};
Expand Down
2 changes: 1 addition & 1 deletion libwebrtc/src/native/video_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use webrtc_sys::video_frame_buffer as vfb_sys;
pub fn new_video_frame_buffer(
mut sys_handle: UniquePtr<vfb_sys::ffi::VideoFrameBuffer>,
) -> Box<dyn vf::VideoFrameBuffer + Send + Sync> {
) -> Box<dyn vf::VideoBuffer + Send + Sync> {
unsafe {
match sys_handle.buffer_type() {
vfb_sys::ffi::VideoFrameBufferType::Native => Box::new(vf::native::NativeBuffer {
Expand Down
4 changes: 2 additions & 2 deletions libwebrtc/src/native/video_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::video_frame::{VideoFrame, VideoFrameBuffer};
use crate::video_frame::{VideoBuffer, VideoFrame};
use crate::video_source::VideoResolution;
use cxx::SharedPtr;
use std::time::{SystemTime, UNIX_EPOCH};
Expand Down Expand Up @@ -55,7 +55,7 @@ impl NativeVideoSource {
self.sys_handle.clone()
}

pub fn capture_frame<T: AsRef<dyn VideoFrameBuffer>>(&self, frame: &VideoFrame<T>) {
pub fn capture_frame<T: AsRef<dyn VideoBuffer>>(&self, frame: &VideoFrame<T>) {
let mut builder = vf_sys::ffi::new_video_frame_builder();
builder.pin_mut().set_rotation(frame.rotation.into());
builder
Expand Down
5 changes: 2 additions & 3 deletions libwebrtc/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ pub use crate::rtp_sender::RtpSender;
pub use crate::rtp_transceiver::{RtpTransceiver, RtpTransceiverDirection, RtpTransceiverInit};
pub use crate::session_description::{SdpType, SessionDescription};
pub use crate::video_frame::{
BoxVideoFrame, BoxVideoFrameBuffer, I010Buffer, I420ABuffer, I420Buffer, I422Buffer,
I444Buffer, NV12Buffer, VideoFormatType, VideoFrame, VideoFrameBuffer, VideoFrameBufferType,
VideoRotation,
BoxVideoBuffer, BoxVideoFrame, I010Buffer, I420ABuffer, I420Buffer, I422Buffer, I444Buffer,
NV12Buffer, VideoBuffer, VideoBufferType, VideoFormatType, VideoFrame, VideoRotation,
};
pub use crate::video_source::{RtcVideoSource, VideoResolution};
pub use crate::video_track::RtcVideoTrack;
Expand Down
82 changes: 63 additions & 19 deletions libwebrtc/src/video_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub enum VideoFormatType {

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum VideoFrameBufferType {
pub enum VideoBufferType {
Native,
I420,
I420A,
Expand All @@ -53,15 +53,15 @@ pub enum VideoFrameBufferType {
#[derive(Debug)]
pub struct VideoFrame<T>
where
T: AsRef<dyn VideoFrameBuffer>,
T: AsRef<dyn VideoBuffer>,
{
pub rotation: VideoRotation,
pub timestamp_us: i64, // When the frame was captured in microseconds
pub buffer: T,
}

pub type BoxVideoFrameBuffer = Box<dyn VideoFrameBuffer>;
pub type BoxVideoFrame = VideoFrame<BoxVideoFrameBuffer>;
pub type BoxVideoBuffer = Box<dyn VideoBuffer>;
pub type BoxVideoFrame = VideoFrame<BoxVideoBuffer>;

pub(crate) mod internal {
use super::{I420Buffer, VideoFormatType};
Expand All @@ -85,10 +85,10 @@ pub(crate) mod internal {
}
}

pub trait VideoFrameBuffer: internal::BufferSealed + Debug {
pub trait VideoBuffer: internal::BufferSealed + Debug {
fn width(&self) -> u32;
fn height(&self) -> u32;
fn buffer_type(&self) -> VideoFrameBufferType;
fn buffer_type(&self) -> VideoBufferType;

#[cfg(not(target_arch = "wasm32"))]
fn as_native(&self) -> Option<&native::NativeBuffer> {
Expand Down Expand Up @@ -152,7 +152,7 @@ macro_rules! new_buffer_type {
}
}

impl VideoFrameBuffer for $type {
impl VideoBuffer for $type {
fn width(&self) -> u32 {
self.handle.width()
}
Expand All @@ -161,8 +161,8 @@ macro_rules! new_buffer_type {
self.handle.height()
}

fn buffer_type(&self) -> VideoFrameBufferType {
VideoFrameBufferType::$variant
fn buffer_type(&self) -> VideoBufferType {
VideoBufferType::$variant
}

fn $as(&self) -> Option<&$type> {
Expand All @@ -179,8 +179,8 @@ macro_rules! new_buffer_type {
}
}

impl AsRef<dyn VideoFrameBuffer> for $type {
fn as_ref(&self) -> &(dyn VideoFrameBuffer + 'static) {
impl AsRef<dyn VideoBuffer> for $type {
fn as_ref(&self) -> &(dyn VideoBuffer + 'static) {
self
}
}
Expand All @@ -195,10 +195,20 @@ new_buffer_type!(I010Buffer, I010, as_i010);
new_buffer_type!(NV12Buffer, NV12, as_nv12);

impl I420Buffer {
pub fn new(width: u32, height: u32, stride_y: u32, stride_u: u32, stride_v: u32) -> I420Buffer {
pub fn with_strides(
width: u32,
height: u32,
stride_y: u32,
stride_u: u32,
stride_v: u32,
) -> I420Buffer {
vf_imp::I420Buffer::new(width, height, stride_y, stride_u, stride_v)
}

pub fn new(width: u32, height: u32) -> I420Buffer {
Self::with_strides(width, height, width, (width + 1) / 2, (width + 1) / 2)
}

pub fn chroma_width(&self) -> u32 {
self.handle.chroma_width()
}
Expand Down Expand Up @@ -271,10 +281,20 @@ impl I420ABuffer {
}

impl I422Buffer {
pub fn new(width: u32, height: u32, stride_y: u32, stride_u: u32, stride_v: u32) -> I422Buffer {
pub fn with_strides(
width: u32,
height: u32,
stride_y: u32,
stride_u: u32,
stride_v: u32,
) -> I422Buffer {
vf_imp::I422Buffer::new(width, height, stride_y, stride_u, stride_v)
}

pub fn new(width: u32, height: u32) -> I422Buffer {
Self::with_strides(width, height, width, (width + 1) / 2, (width + 1) / 2)
}

pub fn chroma_width(&self) -> u32 {
self.handle.chroma_width()
}
Expand Down Expand Up @@ -308,10 +328,20 @@ impl I422Buffer {
}

impl I444Buffer {
pub fn new(width: u32, height: u32, stride_y: u32, stride_u: u32, stride_v: u32) -> I444Buffer {
pub fn with_strides(
width: u32,
height: u32,
stride_y: u32,
stride_u: u32,
stride_v: u32,
) -> I444Buffer {
vf_imp::I444Buffer::new(width, height, stride_y, stride_u, stride_v)
}

pub fn new(width: u32, height: u32) -> I444Buffer {
Self::with_strides(width, height, width, width, width)
}

pub fn chroma_width(&self) -> u32 {
self.handle.chroma_width()
}
Expand Down Expand Up @@ -345,10 +375,20 @@ impl I444Buffer {
}

impl I010Buffer {
pub fn new(width: u32, height: u32, stride_y: u32, stride_u: u32, stride_v: u32) -> I010Buffer {
pub fn with_strides(
width: u32,
height: u32,
stride_y: u32,
stride_u: u32,
stride_v: u32,
) -> I010Buffer {
vf_imp::I010Buffer::new(width, height, stride_y, stride_u, stride_v)
}

pub fn new(width: u32, height: u32) -> I010Buffer {
Self::with_strides(width, height, width, (width + 1) / 2, (width + 1) / 2)
}

pub fn chroma_width(&self) -> u32 {
self.handle.chroma_width()
}
Expand Down Expand Up @@ -382,10 +422,14 @@ impl I010Buffer {
}

impl NV12Buffer {
pub fn new(width: u32, height: u32, stride_y: u32, stride_uv: u32) -> NV12Buffer {
pub fn with_strides(width: u32, height: u32, stride_y: u32, stride_uv: u32) -> NV12Buffer {
vf_imp::NV12Buffer::new(width, height, stride_y, stride_uv)
}

pub fn new(width: u32, height: u32) -> NV12Buffer {
Self::with_strides(width, height, width, width + width % 2)
}

pub fn chroma_width(&self) -> u32 {
self.handle.chroma_width()
}
Expand Down Expand Up @@ -415,12 +459,12 @@ impl NV12Buffer {

#[cfg(not(target_arch = "wasm32"))]
pub mod native {
use super::{vf_imp, I420Buffer, VideoFormatType, VideoFrameBuffer, VideoFrameBufferType};
use super::{vf_imp, I420Buffer, VideoBuffer, VideoBufferType, VideoFormatType};
use std::fmt::Debug;

new_buffer_type!(NativeBuffer, Native, as_native);

pub trait VideoFrameBufferExt: VideoFrameBuffer {
pub trait VideoFrameBufferExt: VideoBuffer {
fn to_i420(&self) -> I420Buffer;
fn to_argb(
&self,
Expand All @@ -432,7 +476,7 @@ pub mod native {
);
}

impl<T: VideoFrameBuffer> VideoFrameBufferExt for T {
impl<T: VideoBuffer> VideoFrameBufferExt for T {
fn to_i420(&self) -> I420Buffer {
self.to_i420()
}
Expand Down
4 changes: 2 additions & 2 deletions libwebrtc/src/video_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl RtcVideoSource {
#[cfg(not(target_arch = "wasm32"))]
pub mod native {
use super::*;
use crate::video_frame::{VideoFrame, VideoFrameBuffer};
use crate::video_frame::{VideoBuffer, VideoFrame};
use std::fmt::{Debug, Formatter};

#[derive(Clone)]
Expand All @@ -68,7 +68,7 @@ pub mod native {
}
}

pub fn capture_frame<T: AsRef<dyn VideoFrameBuffer>>(&self, frame: &VideoFrame<T>) {
pub fn capture_frame<T: AsRef<dyn VideoBuffer>>(&self, frame: &VideoFrame<T>) {
self.handle.capture_frame(frame)
}

Expand Down
Loading

0 comments on commit e33c5a9

Please sign in to comment.