diff options
author | François Laignel <fengalin@free.fr> | 2021-10-09 13:17:05 +0300 |
---|---|---|
committer | François Laignel <fengalin@free.fr> | 2021-10-18 16:09:47 +0300 |
commit | 27b9f0d868f436e9b2bcc3e51f393c40b56fcc02 (patch) | |
tree | 93c0db7b1cf26ea7d0e3a4d70a7d2844c2e00975 /video/closedcaption/src | |
parent | bd8a7e8df7e8ebf751b2d00fe6a096d726683c00 (diff) |
Improve usability thanks to opt-ops
The crate option-operations simplifies usage when dealing with
`Option`s, which is often the case with `ClockTime`.
Diffstat (limited to 'video/closedcaption/src')
-rw-r--r-- | video/closedcaption/src/cea608tojson/imp.rs | 20 | ||||
-rw-r--r-- | video/closedcaption/src/jsontovtt/imp.rs | 15 | ||||
-rw-r--r-- | video/closedcaption/src/mcc_parse/imp.rs | 10 | ||||
-rw-r--r-- | video/closedcaption/src/scc_parse/imp.rs | 31 |
4 files changed, 24 insertions, 52 deletions
diff --git a/video/closedcaption/src/cea608tojson/imp.rs b/video/closedcaption/src/cea608tojson/imp.rs index b60e77381..0c90ddc8c 100644 --- a/video/closedcaption/src/cea608tojson/imp.rs +++ b/video/closedcaption/src/cea608tojson/imp.rs @@ -400,9 +400,7 @@ fn dump( duration: impl Into<Option<gst::ClockTime>>, ) { let pts = pts.into(); - let end = pts - .zip(duration.into()) - .map(|(pts, duration)| pts + duration); + let end = pts.opt_add(duration.into()); if cc_data != 0x8080 { gst_debug!( @@ -478,10 +476,10 @@ impl State { Some(Cea608Mode::PopOn) => gst::ClockTime::NONE, _ => self .current_pts - .zip(self.current_duration) - .map(|(cur_pts, cur_duration)| cur_pts + cur_duration) - .zip(self.first_pts) - .and_then(|(cur_end, first_pts)| cur_end.checked_sub(first_pts)), + .opt_add(self.current_duration) + .opt_checked_sub(self.first_pts) + .ok() + .flatten(), } }; @@ -541,10 +539,10 @@ impl State { gst_log!(CAT, obj: element, "Draining pending"); pending.duration = self .current_pts - .zip(self.current_duration) - .map(|(cur_pts, cur_dur)| cur_pts + cur_dur) - .zip(pending.pts) - .and_then(|(cur_end, pending_pts)| cur_end.checked_sub(pending_pts)); + .opt_add(self.current_duration) + .opt_checked_sub(pending.pts) + .ok() + .flatten(); Some(pending) } else { None diff --git a/video/closedcaption/src/jsontovtt/imp.rs b/video/closedcaption/src/jsontovtt/imp.rs index dc604c68a..436c388eb 100644 --- a/video/closedcaption/src/jsontovtt/imp.rs +++ b/video/closedcaption/src/jsontovtt/imp.rs @@ -68,7 +68,7 @@ fn clamp( mut pts: gst::ClockTime, mut duration: Option<gst::ClockTime>, ) -> Option<(gst::ClockTime, Option<gst::ClockTime>)> { - let end_pts = duration.map(|duration| pts + duration).unwrap_or(pts); + let end_pts = pts.opt_add(duration).unwrap_or(pts); if let Some(segment_start) = segment.start() { if end_pts < segment_start { @@ -76,9 +76,7 @@ fn clamp( } if pts < segment_start { - if let Some(ref mut duration) = duration { - *duration = duration.saturating_sub(segment_start - pts); - } + duration.opt_sub_assign(segment_start - pts); pts = segment_start; } } @@ -89,9 +87,7 @@ fn clamp( } if end_pts > segment_stop { - if let Some(ref mut duration) = duration { - *duration = duration.saturating_sub(end_pts - segment_stop); - } + duration.opt_sub_assign(end_pts - segment_stop); } } @@ -322,10 +318,7 @@ impl State { self.drain(&mut ret, self.segment.to_running_time(pts)); - self.last_pts = Some(pts) - .zip(duration) - .map(|(pts, duration)| pts + duration) - .or(Some(pts)); + self.last_pts = Some(pts).opt_add(duration).or(Some(pts)); ret } diff --git a/video/closedcaption/src/mcc_parse/imp.rs b/video/closedcaption/src/mcc_parse/imp.rs index ff0f18bb9..c38c4f19d 100644 --- a/video/closedcaption/src/mcc_parse/imp.rs +++ b/video/closedcaption/src/mcc_parse/imp.rs @@ -1015,17 +1015,11 @@ impl MccParse { let pull = state.pull.as_ref().unwrap(); if start_type == gst::SeekType::Set { - start = start - .zip(pull.duration) - .map(|(start, duration)| start.min(duration)) - .or(start); + start = start.opt_min(pull.duration).or(start); } if stop_type == gst::SeekType::Set { - stop = stop - .zip(pull.duration) - .map(|(stop, duration)| stop.min(duration)) - .or(stop); + stop = stop.opt_min(pull.duration).or(stop); } state.seeking = true; diff --git a/video/closedcaption/src/scc_parse/imp.rs b/video/closedcaption/src/scc_parse/imp.rs index 75cdaac13..2fa363f84 100644 --- a/video/closedcaption/src/scc_parse/imp.rs +++ b/video/closedcaption/src/scc_parse/imp.rs @@ -429,14 +429,8 @@ impl SccParse { timecode.increment_frame(); if clip_buffers { - let end_time = buffer - .pts() - .zip(buffer.duration()) - .map(|(pts, duration)| pts + duration); - if end_time - .zip(segment_start) - .map_or(false, |(end_time, segment_start)| end_time < segment_start) - { + let end_time = buffer.pts().opt_add(buffer.duration()); + if end_time.opt_lt(segment_start).unwrap_or(false) { gst_trace!( CAT, obj: element, @@ -448,12 +442,11 @@ impl SccParse { } } - send_eos = state.segment.stop().map_or(false, |stop| { - buffer - .pts() - .zip(buffer.duration()) - .map_or(false, |(pts, duration)| pts + duration >= stop) - }); + send_eos = buffer + .pts() + .opt_add(buffer.duration()) + .opt_ge(state.segment.stop()) + .unwrap_or(false); let buffers = buffers.get_mut().unwrap(); buffers.add(buffer); @@ -899,17 +892,11 @@ impl SccParse { let pull = state.pull.as_ref().unwrap(); if start_type == gst::SeekType::Set { - start = start - .zip(pull.duration) - .map(|(start, duration)| start.min(duration)) - .or(start); + start = start.opt_min(pull.duration).or(start); } if stop_type == gst::SeekType::Set { - stop = stop - .zip(pull.duration) - .map(|(stop, duration)| stop.min(duration)) - .or(stop); + stop = stop.opt_min(pull.duration).or(stop); } state.seeking = true; |