1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
use crate::common::ring::{ContentType, EncodedFrame};
use crate::linux::core::{
DEFAULT_APPSINK_DROP, DEFAULT_APPSINK_MAX_BUFFERS, DEFAULT_APPSINK_SYNC,
DEFAULT_AUDIO_CHANNELS, DaemonCore,
};
use gstreamer::ClockTime;
use gstreamer::glib::object::Cast;
use gstreamer::prelude::{ElementExtManual, GstBinExt, ObjectExt};
use log::error;
use std::sync::atomic::Ordering;
use std::time::{Instant, SystemTime, UNIX_EPOCH};
use wayclip_core::models::error::WayclipError;
use wayclip_core::settings::recording::AudioSettings;
impl DaemonCore {
pub fn set_appsink_callbacks(
&self,
appsink: &gstreamer_app::AppSink,
content_type: ContentType,
) -> Result<(), WayclipError> {
let ring_buffer_clone = self.ring_buffer.clone();
let last_video_frame_time = self.last_video_frame_time.clone();
let last_audio_frame_time = self.last_audio_frame_time.clone();
appsink.set_callbacks(
gstreamer_app::AppSinkCallbacks::builder()
.new_sample(move |sink| {
match ring_buffer_clone.lock() {
Ok(mut ring_buffer) => {
let sample =
sink.pull_sample().map_err(|_| gstreamer::FlowError::Eos)?;
let buffer_ref = sample.buffer().ok_or(gstreamer::FlowError::Error)?;
let caps = sample.caps().map(|caps| caps.to_owned());
let mut pts = buffer_ref.pts().ok_or_else(|| {
error!("{} buffer missing PTS", content_type);
gstreamer::FlowError::Error
})?;
match content_type {
ContentType::Video => {
let now_ms = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as u64;
last_video_frame_time.store(now_ms, Ordering::Release);
if ring_buffer.awaiting_video_resync {
if let Some(reference) = ring_buffer.video_resync_reference {
let gap = ClockTime::from_mseconds(33);
let target = reference + gap;
ring_buffer.video_pts_offset_ns =
target.nseconds() as i64 - pts.nseconds() as i64;
log::info!(
"video resynced, raw_pts={}ms -> {}ms (offset {}ns)",
pts.mseconds(), target.mseconds(), ring_buffer.video_pts_offset_ns
);
}
ring_buffer.awaiting_video_resync = false;
}
let shifted = pts.nseconds() as i64 + ring_buffer.video_pts_offset_ns;
pts = ClockTime::from_nseconds(shifted.max(0) as u64);
}
ContentType::Audio => {
let now_ms = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as u64;
last_audio_frame_time.store(now_ms, Ordering::Release);
if ring_buffer.awaiting_audio_resync {
if let Some(reference) = ring_buffer.audio_resync_reference {
let gap = ClockTime::from_mseconds(20);
let target = reference + gap;
ring_buffer.audio_pts_offset_ns =
target.nseconds() as i64 - pts.nseconds() as i64;
}
ring_buffer.awaiting_audio_resync = false;
}
let shifted = pts.nseconds() as i64 + ring_buffer.audio_pts_offset_ns;
pts = ClockTime::from_nseconds(shifted.max(0) as u64);
}
}
// Basically, for SOME reason, the video PTS starts off waaay late,
// which de-syncs the audio frame PTS's, meaning we gotta record first
// value, to then subtract it from all the others, but ONLY inside the
// calculations, cant modify original pts value
if ring_buffer.video_first_pts.is_none()
&& let ContentType::Video = content_type
{
ring_buffer.video_first_pts = Some(pts);
ring_buffer.video_start_instant = Some(Instant::now());
log::debug!("First Video PTS Recieved: {}ms", pts.mseconds());
}
if ring_buffer.audio_first_pts.is_none()
&& let ContentType::Audio = content_type
{
ring_buffer.audio_first_pts = Some(pts);
ring_buffer.audio_start_instant = Some(Instant::now());
log::debug!("First Audio PTS Recieved: {}ms", pts.mseconds());
}
#[cfg(debug_assertions)]
{
let limiter = match content_type {
ContentType::Video => &mut ring_buffer.arriving_video_limiter,
ContentType::Audio => &mut ring_buffer.arriving_audio_limiter,
};
if limiter.allow() {
log::trace!(
"{} PTS Arriving: {}ms ({} log/s)",
content_type,
pts.mseconds(),
limiter.interval.as_secs()
);
}
}
let dts = buffer_ref.dts();
let duration = buffer_ref.duration();
let is_keyframe = match content_type {
ContentType::Video => !buffer_ref
.flags()
.contains(gstreamer::BufferFlags::DELTA_UNIT),
ContentType::Audio => true,
};
//let data = buffer_ref
// .map_readable()
// .map_err(|_| gstreamer::FlowError::Error)?
// .as_slice()
// .to_vec();
let frame = EncodedFrame::new(buffer_ref.to_owned(), pts, dts, duration, is_keyframe);
if let Some(caps) = caps {
match content_type {
ContentType::Video => {
if ring_buffer.video_caps.is_none() {
log::debug!("Stored downstream video caps: {:?}", caps);
ring_buffer.video_caps = Some(caps);
}
}
ContentType::Audio => {
if ring_buffer.audio_caps.is_none() {
log::debug!("Stored downstream audio caps: {}", caps);
ring_buffer.audio_caps = Some(caps)
}
}
}
}
let res = match content_type {
ContentType::Video => {
ring_buffer.push_video_frame(frame)
}
ContentType::Audio => {
ring_buffer.push_audio_frame(frame)
}
};
match res {
Ok(_) => (),
Err(e) => {
error!("Pushing {} frames error: {}", content_type, e);
return Err(gstreamer::FlowError::Error);
}
}
}
Err(e) => {
error!("Ring buffer lock poisoned ({}): {}", content_type, e);
return Err(gstreamer::FlowError::Error);
}
}
Ok(gstreamer::FlowSuccess::Ok)
})
.build(),
);
Ok(())
}
pub fn build_mix_pipeline(
&self,
pipeline: &gstreamer::Pipeline,
audio_settings: &AudioSettings,
) -> Result<gstreamer::Element, WayclipError> {
let mix = gstreamer::ElementFactory::make("audiomixer")
.name("audio_mixer")
.property_from_str("start-time-selection", "zero")
.property("ignore-inactive-pads", true)
.build()?;
let audioconvert = gstreamer::ElementFactory::make("audioconvert")
.name("audio_mix_convert")
.build()?;
let caps_filter_audioconvert = gstreamer::ElementFactory::make("capsfilter")
.name("audio_mix_caps_filter")
.build()?;
let caps_audioconvert = gstreamer::Caps::builder("audio/x-raw")
.field("channels", DEFAULT_AUDIO_CHANNELS)
.build();
caps_filter_audioconvert.set_property("caps", &caps_audioconvert);
let encoder = gstreamer::ElementFactory::make(audio_settings.codec.get_encoder())
.name("audio_mixer_encoder")
.build()?;
let parser = gstreamer::ElementFactory::make(audio_settings.codec.get_parser())
.name("audio_mixer_parser")
.build()?;
let audio_queue = gstreamer::ElementFactory::make("queue")
.name("audio_mixer_post_queue")
.build()?;
let mix_pipeline = [
mix.clone(),
audioconvert,
caps_filter_audioconvert,
encoder,
parser,
audio_queue.clone(),
];
for element in mix_pipeline.iter() {
pipeline.add(element)?
}
gstreamer::Element::link_many(
mix_pipeline
.iter()
.collect::<Vec<&gstreamer::Element>>()
.as_slice(),
)?;
let audio_appsink = gstreamer_app::AppSink::builder()
.name("audio_appsink")
.property("drop", DEFAULT_APPSINK_DROP)
.property("max-buffers", DEFAULT_APPSINK_MAX_BUFFERS)
.property("sync", DEFAULT_APPSINK_SYNC)
.build();
pipeline.add(audio_appsink.upcast_ref::<gstreamer::Element>())?;
audio_queue.link(audio_appsink.upcast_ref::<gstreamer::Element>())?;
self.set_appsink_callbacks(&audio_appsink, ContentType::Audio)?;
Ok(mix)
}
}