Skip to content

Commit ba5f58d

Browse files
committed
video
1 parent 5c6996c commit ba5f58d

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed

libwebrtc/src/native/audio_source.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
use crate::{audio_frame::AudioFrame, audio_source::AudioSourceOptions, RtcError, RtcErrorType};
1616
use cxx::SharedPtr;
17-
use std::borrow::Cow;
18-
use std::sync::atomic::{AtomicUsize, Ordering};
1917
use std::{sync::Arc, time::Duration};
2018
use tokio::{
2119
sync::{Mutex as AsyncMutex, MutexGuard},
@@ -76,7 +74,7 @@ impl NativeAudioSource {
7674

7775
loop {
7876
// We directly use the sys_handle instead of the capture_frame function
79-
// (We don't want to increase the captured_frames count and no need to buffer)
77+
// (We don't want to increase the captured_frames count and no need to buffer)
8078
interval.tick().await;
8179

8280
let inner = source.inner.lock().await;

libwebrtc/src/native/video_source.rs

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use crate::video_frame::{VideoBuffer, VideoFrame};
15+
use crate::video_frame::{I420Buffer, VideoBuffer, VideoFrame};
1616
use crate::video_source::VideoResolution;
1717
use cxx::SharedPtr;
18-
use std::sync::{
19-
atomic::{AtomicUsize, Ordering},
20-
Arc,
21-
};
22-
use std::time::{SystemTime, UNIX_EPOCH};
18+
use parking_lot::Mutex;
19+
use std::sync::Arc;
20+
use std::time::{Duration, SystemTime, UNIX_EPOCH};
2321
use webrtc_sys::video_frame as vf_sys;
22+
use webrtc_sys::video_frame::ffi::VideoRotation;
2423
use webrtc_sys::video_track as vt_sys;
2524

2625
impl From<vt_sys::ffi::VideoResolution> for VideoResolution {
@@ -44,29 +43,64 @@ impl From<VideoResolution> for vt_sys::ffi::VideoResolution {
4443
#[derive(Clone)]
4544
pub struct NativeVideoSource {
4645
sys_handle: SharedPtr<vt_sys::ffi::VideoTrackSource>,
47-
captured_frames: Arc<AtomicUsize>,
46+
inner: Arc<Mutex<VideoSourceInner>>,
47+
}
48+
49+
struct VideoSourceInner {
50+
captured_frames: usize,
4851
}
4952

5053
impl NativeVideoSource {
5154
pub fn new(resolution: VideoResolution) -> NativeVideoSource {
52-
Self {
55+
let source = Self {
5356
sys_handle: vt_sys::ffi::new_video_track_source(&vt_sys::ffi::VideoResolution::from(
54-
resolution,
57+
resolution.clone(),
5558
)),
56-
captured_frames: Arc::new(AtomicUsize::new(0)),
57-
}
59+
inner: Arc::new(Mutex::new(VideoSourceInner { captured_frames: 0 })),
60+
};
61+
62+
tokio::spawn({
63+
let source = source.clone();
64+
let i420 = I420Buffer::new(resolution.width, resolution.height);
65+
async move {
66+
let mut interval = tokio::time::interval(Duration::from_millis(100)); // 10 fps
67+
68+
loop {
69+
interval.tick().await;
70+
71+
let inner = source.inner.lock();
72+
if inner.captured_frames > 0 {
73+
break;
74+
}
75+
76+
let mut builder = vf_sys::ffi::new_video_frame_builder();
77+
builder
78+
.pin_mut()
79+
.set_rotation(VideoRotation::VideoRotation0);
80+
builder
81+
.pin_mut()
82+
.set_video_frame_buffer(i420.as_ref().sys_handle());
83+
84+
let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
85+
builder.pin_mut().set_timestamp_us(now.as_micros() as i64);
86+
87+
source
88+
.sys_handle
89+
.on_captured_frame(&builder.pin_mut().build());
90+
}
91+
}
92+
});
93+
94+
source
5895
}
5996

6097
pub fn sys_handle(&self) -> SharedPtr<vt_sys::ffi::VideoTrackSource> {
6198
self.sys_handle.clone()
6299
}
63100

64-
pub fn captured_frames(&self) -> usize {
65-
self.captured_frames.load(Ordering::Relaxed)
66-
}
67-
68101
pub fn capture_frame<T: AsRef<dyn VideoBuffer>>(&self, frame: &VideoFrame<T>) {
69-
self.captured_frames.fetch_add(1, Ordering::Relaxed);
102+
let mut inner = self.inner.lock();
103+
inner.captured_frames += 1;
70104

71105
let mut builder = vf_sys::ffi::new_video_frame_builder();
72106
builder.pin_mut().set_rotation(frame.rotation.into());

libwebrtc/src/video_source.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ impl RtcVideoSource {
3535
enum_dispatch!(
3636
[Native];
3737
pub fn video_resolution(self: &Self) -> VideoResolution;
38-
pub fn captured_frames(self: &Self) -> usize;
3938
);
4039
}
4140

@@ -69,10 +68,6 @@ pub mod native {
6968
}
7069
}
7170

72-
pub fn captured_frames(&self) -> usize {
73-
self.handle.captured_frames()
74-
}
75-
7671
pub fn capture_frame<T: AsRef<dyn VideoBuffer>>(&self, frame: &VideoFrame<T>) {
7772
self.handle.capture_frame(frame)
7873
}

0 commit comments

Comments
 (0)