use std::sync::{Arc, Mutex};
use std::{fmt, time::Duration};
use crate::Result;
use super::{
Device, RetirementTicket, TimestampPair,
device::logical::video::{DecodeStatus, EncodeReceipt},
};
#[derive(Clone)]
#[must_use]
pub struct Event {
device: Arc<Device>,
epoch: u64,
retirement: RetirementTicket,
timing: Option<TimestampPair>,
timing_start: Option<TimestampPair>,
decode_statuses: Option<Arc<Mutex<Vec<Arc<DecodeStatus>>>>>,
encode_receipts: Option<Arc<Mutex<Vec<Arc<EncodeReceipt>>>>>,
}
impl Event {
pub(crate) fn same_as(&self, other: &Self) -> bool {
self.epoch == other.epoch && self.device.same_as(&other.device)
}
pub(crate) fn follows_or_equals(&self, other: &Self) -> bool {
self.epoch >= other.epoch && self.device.same_as(&other.device)
}
pub(super) const fn new(
device: Arc<Device>,
epoch: u64,
retirement: RetirementTicket,
timing: Option<TimestampPair>,
) -> Self {
Self {
device,
epoch,
retirement,
timing,
timing_start: None,
decode_statuses: None,
encode_receipts: None,
}
}
pub(super) fn set_timing_start(&mut self, first: &Self) {
self.timing_start = first.timing.clone();
}
pub(super) fn set_decode_statuses(&mut self, statuses: Vec<Arc<DecodeStatus>>) {
if !statuses.is_empty() {
self.decode_statuses = Some(Arc::new(Mutex::new(statuses)));
}
}
pub(super) fn set_encode_receipts(&mut self, receipts: Vec<Arc<EncodeReceipt>>) {
if !receipts.is_empty() {
self.encode_receipts = Some(Arc::new(Mutex::new(receipts)));
}
}
pub(super) fn encode_receipts(&self) -> Vec<Arc<EncodeReceipt>> {
self
.encode_receipts
.as_ref()
.map(|receipts| {
receipts
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.clone()
})
.unwrap_or_default()
}
fn verify_encode_receipts(&self) -> Result<()> {
if let Some(receipts) = &self.encode_receipts {
let receipts = receipts
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
for receipt in receipts.iter() {
receipt.verify()?;
}
}
Ok(())
}
fn verify_decode_statuses(&self) -> Result<()> {
if let Some(statuses) = &self.decode_statuses {
let mut statuses = statuses
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
for status in statuses.iter() {
status.verify()?;
}
statuses.clear();
}
Ok(())
}
pub(crate) const fn epoch(&self) -> u64 {
self.epoch
}
pub(in crate::runtime) fn comes_from(&self, device: &Arc<Device>) -> bool {
self.device.same_as(device)
}
pub(in crate::runtime) fn retain_until_complete<T>(&self, value: T)
where
T: Send + 'static,
{
self.retirement.retain_until_complete(value);
}
pub fn is_complete(&self) -> Result<bool> {
self.retirement.check_failure()?;
let complete = self.device.is_complete(self.epoch)?;
self.retirement.check_failure()?;
if complete {
self.verify_decode_statuses()?;
self.verify_encode_receipts()?;
}
Ok(complete)
}
pub fn wait(&self) -> Result<()> {
self.retirement.wait()?;
self.verify_decode_statuses()?;
self.verify_encode_receipts()
}
pub fn encoded_packets(&self) -> Result<Vec<crate::video::EncodedVideoPacket>> {
self.wait()?;
self
.encode_receipts()
.iter()
.map(|receipt| receipt.packet())
.collect()
}
pub fn try_device_duration(&self) -> Result<Option<Duration>> {
let Some(timing) = &self.timing else {
return Err(crate::Error::failed_precondition(
"event was not produced by a timed submission",
));
};
if !self.is_complete()? {
return Ok(None);
}
Ok(Some(self.timing_start.as_ref().map_or_else(
|| timing.duration(),
|start| start.duration_until(timing),
)?))
}
pub fn device_duration(&self) -> Result<Duration> {
let Some(timing) = &self.timing else {
return Err(crate::Error::failed_precondition(
"event was not produced by a timed submission",
));
};
self.wait()?;
self
.timing_start
.as_ref()
.map_or_else(|| timing.duration(), |start| start.duration_until(timing))
}
}
impl fmt::Debug for Event {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("Event")
.field("epoch", &self.epoch)
.finish_non_exhaustive()
}
}
#[cfg(test)]
#[path = "../../../../test/rs/runtime/event_unit.rs"]
mod tests;