oarust 0.14.0

GPU-first semantic computing engine written in Rust
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! Audio-clocked transport for independently owned audio and video sessions.

use std::{
	path::{Path, PathBuf},
	time::{Duration, Instant},
};

pub use crate::media::types::MediaPlayerConfig;
use crate::{
	AudioPlayer, Engine, Error, LogComponent, LogLevel, Result, VideoFrame, VideoPlayer,
	audio::AudioPlayerConfig,
	video::{
		Mp4TrackEdit, VideoContainerInfo, VideoContainerKind, VideoPlayerConfig, mp4_track_edits,
	},
};

use super::clock::TrackTimeline;

impl MediaPlayerConfig {
	pub fn new(video_path: impl Into<PathBuf>, audio_path: impl Into<PathBuf>) -> Self {
		Self {
			video_path: video_path.into(),
			audio_path: audio_path.into(),
			audio_ring_milliseconds: 500,
		}
	}

	/// Decode the video and default audio tracks of the same local container.
	pub fn embedded(path: impl Into<PathBuf>) -> Self {
		let path = path.into();
		Self::new(path.clone(), path)
	}
}

/// Synchronized playback session over independent audio and video owners.
///
/// The caller ticks video on the Engine-owning thread. During the audio segment,
/// the device output clock paces decoded video. The host clock covers silent
/// lead-in and the tail after audio drains. A presented native frame retains its
/// decoder backing until it is replaced and any external GPU consumers retire.
pub struct MediaPlayer {
	audio: Option<AudioPlayer>,
	video: VideoPlayer,
	video_timeline: TrackTimeline,
	audio_timeline: TrackTimeline,
	duration: Duration,
	position: Duration,
	last_tick: Option<Instant>,
	audio_active: bool,
	audio_needs_seek: bool,
	looping: bool,
	done: bool,
}

impl MediaPlayer {
	/// Open both tracks paused, with the first video frame decoded.
	///
	/// # Errors
	///
	/// Requires a supported video stream, both track sources, and an audio
	/// output configuration that can play the selected PCM.
	pub fn open(engine: &Engine, config: MediaPlayerConfig) -> Result<Self> {
		if config.video_path.as_os_str().is_empty() || config.audio_path.as_os_str().is_empty() {
			return Err(Error::invalid_argument(
				"MediaPlayer requires both track paths",
			));
		}
		let video = open_video_track(engine, &config.video_path)?;
		let audio_config = AudioPlayerConfig {
			uri: config.audio_path,
			loop_playback: false,
			ring_milliseconds: config.audio_ring_milliseconds,
		};
		let embedded = audio_config.uri == config.video_path;
		let audio = if embedded {
			AudioPlayer::open_embedded(engine, audio_config)?
				.ok_or_else(|| Error::not_found("MediaPlayer source has no audio track"))?
		} else {
			AudioPlayer::open(engine, audio_config)?
		};
		Self::compose(&config.video_path, video, Some(audio), embedded)
	}

	pub(crate) fn open_viewer(engine: &Engine, path: &Path) -> Result<Self> {
		let mut video = open_video_track(engine, path)?;
		if crate::EnvFlag::is_set("OA_VIEWER_YUV_CANVAS") {
			video.enable_canvas_yuv()?;
		}
		// MPEG-TS is an unindexed sequential video path. The current Audio
		// probe supports embedded PCM sources through MP4 and Matroska/WebM.
		let info = video
			.info()
			.ok_or_else(|| Error::internal("video track failed to open"))?;
		let audio = if info.kind() == VideoContainerKind::MpegTs {
			None
		} else {
			match AudioPlayer::open_embedded(engine, AudioPlayerConfig::new(path)) {
				Ok(audio) => audio,
				Err(error) => {
					engine.log(
						LogLevel::Warn,
						LogComponent::AUDIO,
						format!("Viewer audio unavailable: {error}"),
					)?;
					None
				}
			}
		};
		Self::compose(path, video, audio, true)
	}

	fn compose(
		path: &Path,
		mut video: VideoPlayer,
		audio: Option<AudioPlayer>,
		embedded: bool,
	) -> Result<Self> {
		let info = video
			.info()
			.ok_or_else(|| Error::internal("video track failed to open"))?;
		let [video_edit, audio_edit] = if embedded && info.kind() == VideoContainerKind::Mp4 {
			mp4_track_edits(
				path,
				[
					Some(info.track_id()),
					audio.as_ref().map(AudioPlayer::track_id),
				],
			)?
		} else {
			[Mp4TrackEdit::default(); 2]
		};
		let video_edit = if info.kind() == VideoContainerKind::MpegTs {
			Mp4TrackEdit {
				media_start: video.current_frame()?.timing().presentation_timestamp(),
				..video_edit
			}
		} else {
			video_edit
		};
		let video_timeline = TrackTimeline::new(video_edit, video.duration()?)?;
		let audio_duration = audio.as_ref().map_or(Duration::ZERO, |audio| {
			Duration::from_micros(audio.duration_us())
		});
		let audio_timeline = TrackTimeline::new(audio_edit, audio_duration)?;
		if let (Some(audio), Some(length)) = (&audio, audio_edit.length) {
			audio.set_playback_end_us(micros(
				audio_edit
					.media_start
					.checked_add(length)
					.ok_or_else(|| Error::out_of_range("audio edit end exceeds Duration"))?,
			));
		}
		if info.kind() != VideoContainerKind::MpegTs && !video_edit.media_start.is_zero() {
			video.seek_time_at_or_before(video_edit.media_start)?;
		}
		Ok(Self {
			audio,
			video,
			video_timeline,
			audio_timeline,
			duration: video_timeline.end.max(audio_timeline.end),
			position: Duration::ZERO,
			last_tick: None,
			audio_active: false,
			audio_needs_seek: true,
			looping: false,
			done: false,
		})
	}

	pub fn play(&mut self) -> Result<()> {
		self.require_open()?;
		if self.is_playing() {
			return Ok(());
		}
		if self.done {
			self.seek_us(0)?;
		}
		self.start_audio()?;
		self.video.play()?;
		self.last_tick = Some(Instant::now());
		Ok(())
	}

	pub fn pause(&mut self) -> Result<()> {
		self.require_open()?;
		if let Some(audio) = &self.audio {
			audio.pause();
		}
		self.audio_active = false;
		self.last_tick = None;
		self.video.pause()
	}

	/// Advance up to `max_frames` display frames reached by the playback clock.
	///
	/// # Errors
	///
	/// Returns an error after close, for a zero budget, or on decoder or device
	/// failure. The transport pauses on a playback failure.
	pub fn tick(&mut self, max_frames: u32) -> Result<u32> {
		self.require_open()?;
		if max_frames == 0 {
			return Err(Error::invalid_argument(
				"MediaPlayer tick budget must be positive",
			));
		}
		let Some(previous) = self.last_tick else {
			return Ok(0);
		};
		let now = Instant::now();
		self.last_tick = Some(now);
		let elapsed = now.saturating_duration_since(previous);
		let result = self.advance_to_clock(elapsed, max_frames);
		if result.is_err() {
			let _ = self.pause();
		}
		result
	}

	fn advance_to_clock(&mut self, elapsed: Duration, budget: u32) -> Result<u32> {
		if let Some(audio) = &self.audio {
			audio.check_playback()?;
		}
		let previous = self.position;
		if self.audio_active {
			let audio = self
				.audio
				.as_ref()
				.ok_or_else(|| Error::internal("active media clock has no audio source"))?;
			let audible = audio.audible_position_us();
			self.position = self
				.position
				.max(self.audio_timeline.movie_at(Duration::from_micros(audible)));
			if audio.is_eos() && audible >= audio.position_us() {
				self.audio_active = false;
			}
		} else {
			self.position = self.position.saturating_add(elapsed);
			if self.audio_needs_seek
				&& self.audio.is_some()
				&& previous < self.audio_timeline.start
				&& self.position >= self.audio_timeline.start
			{
				self.position = self.audio_timeline.start;
				self.start_audio()?;
			}
		}
		if !self.is_seekable() {
			// TS has no indexed end. Pace from the audible clock while sound
			// plays, then let the host clock carry any remaining video.
			let advanced = self
				.video
				.tick_bounded(self.position.saturating_sub(previous), budget)?;
			if self.video.is_done() && !self.audio_active {
				self.finish()?;
			}
			return Ok(advanced);
		}
		if !self.duration.is_zero() {
			self.position = self.position.min(self.duration);
		}
		let mut advanced = 0;
		let video_position = self.position.min(self.video_timeline.end);
		while advanced < budget && !self.video.is_done() {
			let Some(next) = self.video.next_presentation_boundary()? else {
				break;
			};
			if self.video_timeline.movie_at(next) > video_position
				|| self.video_timeline.is_past_end(next)
				|| !self.video.advance_for_playback()?
			{
				break;
			}
			advanced += 1;
		}
		let audio_drained = !self.audio_active;
		if advanced < budget && self.position >= self.duration && audio_drained {
			self.finish()?;
		}
		Ok(advanced)
	}

	fn start_audio(&mut self) -> Result<()> {
		if let Some(audio) = &mut self.audio
			&& self.audio_timeline.contains(self.position)
		{
			if self.audio_needs_seek {
				let position = micros(self.audio_timeline.media_at(self.position));
				if position != 0 || audio.position_us() != 0 || audio.is_eos() {
					audio.seek(position)?;
				}
				self.audio_needs_seek = false;
			}
			if !audio.is_eos() {
				audio.play()?;
				self.audio_active = true;
			}
		}
		Ok(())
	}

	fn finish(&mut self) -> Result<()> {
		if self.looping {
			self.seek_us(0)?;
		} else {
			self.pause()?;
			self.done = true;
		}
		Ok(())
	}

	/// Seek to a movie timestamp, selecting the video frame at or before it.
	/// While paused, the audio decoder is repositioned once on resume.
	///
	/// # Errors
	///
	/// Returns an error when video seeking fails or active audio cannot resume
	/// at the new position. The transport stays paused on failure.
	pub fn seek_us(&mut self, timestamp_us: u64) -> Result<()> {
		self.require_open()?;
		if timestamp_us != 0 && !self.is_seekable() {
			return Err(Error::missing_capability(
				"seeking requires an indexed media source",
			));
		}
		let resume = self.is_playing();
		self.pause()?;
		let target = Duration::from_micros(timestamp_us).min(self.duration);
		let raw = self.video_timeline.media_at(target);
		if !self.is_seekable() {
			self.video.reset()?;
		} else if let Err(error) = self.video.seek_time_at_or_before(raw) {
			let previous = self.video_timeline.media_at(self.position);
			if let Err(restore) = self.video.seek_time_at_or_before(previous) {
				self.video.close();
				return Err(Error::with_kind(
					error.kind(),
					format!(
						"media seek failed ({error}) and the previous frame could not be restored ({restore})"
					),
				));
			}
			self.audio_needs_seek = true;
			return Err(error);
		}
		self.position = target;
		self.audio_needs_seek = true;
		self.done = false;
		if resume {
			self.play()?;
		}
		Ok(())
	}

	pub(crate) fn step_frames(&mut self, steps: i32) -> Result<()> {
		self.pause()?;
		if steps < 0 && !self.is_seekable() {
			return Ok(());
		}
		self.video.step_frames(steps)?;
		let pts = self.current_frame()?.timing().presentation_timestamp();
		self.position = self.video_timeline.movie_at(pts);
		self.audio_needs_seek = true;
		self.done = self.video.is_done() && !self.is_seekable();
		Ok(())
	}

	pub fn current_frame(&self) -> Result<&VideoFrame> {
		self.video.current_frame()
	}

	pub(crate) fn current_viewer_frame(&mut self) -> Result<Option<VideoFrame>> {
		let pts = self.current_frame()?.timing().presentation_timestamp();
		if self.is_seekable() && self.video_timeline.movie_at(pts) > self.position {
			return Ok(None);
		}
		self.video.prepare_viewer_frame().map(Some)
	}

	pub fn position_us(&self) -> Result<u64> {
		self.require_open()?;
		Ok(micros(self.position))
	}

	pub(crate) fn duration_us(&self) -> u64 {
		if self.is_seekable() {
			micros(self.duration)
		} else {
			0
		}
	}

	pub(crate) fn info(&self) -> Option<VideoContainerInfo> {
		self.video.info()
	}

	pub(crate) fn audio(&self) -> Option<&AudioPlayer> {
		self.audio.as_ref()
	}

	pub(crate) fn set_looping(&mut self, looping: bool) {
		self.looping = looping;
	}

	pub fn is_playing(&self) -> bool {
		self.last_tick.is_some()
	}

	pub fn is_done(&self) -> bool {
		self.done || !self.video.is_open()
	}

	pub fn close(&mut self) -> Result<()> {
		self.last_tick = None;
		self.video.close();
		self.audio.take().map_or(Ok(()), |mut audio| audio.close())
	}

	fn require_open(&self) -> Result<()> {
		if self.video.is_open() {
			Ok(())
		} else {
			Err(Error::failed_precondition("MediaPlayer is closed"))
		}
	}

	fn is_seekable(&self) -> bool {
		self
			.info()
			.is_some_and(|info| info.kind() != VideoContainerKind::MpegTs)
	}
}

fn open_video_track(engine: &Engine, path: &Path) -> Result<VideoPlayer> {
	VideoPlayer::new_native(
		engine,
		path,
		VideoPlayerConfig {
			loop_playback: false,
			start_playing: false,
			..VideoPlayerConfig::default()
		},
	)
}

fn micros(duration: Duration) -> u64 {
	u64::try_from(duration.as_micros()).unwrap_or(u64::MAX)
}