scuffle_ffmpeg/io/
input.rs

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
use std::ffi::CStr;

use ffmpeg_sys_next::*;

use super::internal::{read_packet, seek, Inner, InnerOptions};
use crate::consts::{Const, DEFAULT_BUFFER_SIZE};
use crate::dict::Dictionary;
use crate::error::FfmpegError;
use crate::packet::{Packet, Packets};
use crate::smart_object::SmartObject;
use crate::stream::Streams;

pub struct Input<T: Send + Sync> {
	inner: SmartObject<Inner<T>>,
}

/// Safety: `Input` is safe to send between threads.
unsafe impl<T: Send + Sync> Send for Input<T> {}

#[derive(Debug, Clone)]
pub struct InputOptions<I: FnMut() -> bool> {
	pub buffer_size: usize,
	pub dictionary: Dictionary,
	pub interrupt_callback: Option<I>,
}

impl Default for InputOptions<fn() -> bool> {
	fn default() -> Self {
		Self {
			buffer_size: DEFAULT_BUFFER_SIZE,
			dictionary: Dictionary::new(),
			interrupt_callback: None,
		}
	}
}

impl<T: std::io::Read + Send + Sync> Input<T> {
	pub fn new(input: T) -> Result<Self, FfmpegError> {
		Self::with_options(input, &mut InputOptions::default())
	}

	pub fn with_options(input: T, options: &mut InputOptions<impl FnMut() -> bool>) -> Result<Self, FfmpegError> {
		Self::create_input(
			Inner::new(
				input,
				InnerOptions {
					buffer_size: options.buffer_size,
					read_fn: Some(read_packet::<T>),
					..Default::default()
				},
			)?,
			None,
			&mut options.dictionary,
		)
	}

	pub fn seekable(input: T) -> Result<Self, FfmpegError>
	where
		T: std::io::Seek,
	{
		Self::seekable_with_options(input, InputOptions::default())
	}

	pub fn seekable_with_options(input: T, mut options: InputOptions<impl FnMut() -> bool>) -> Result<Self, FfmpegError>
	where
		T: std::io::Seek,
	{
		Self::create_input(
			Inner::new(
				input,
				InnerOptions {
					buffer_size: options.buffer_size,
					read_fn: Some(read_packet::<T>),
					seek_fn: Some(seek::<T>),
					..Default::default()
				},
			)?,
			None,
			&mut options.dictionary,
		)
	}
}

impl<T: Send + Sync> Input<T> {
	pub fn as_ptr(&self) -> *const AVFormatContext {
		self.inner.context.as_ptr()
	}

	pub fn as_mut_ptr(&mut self) -> *mut AVFormatContext {
		self.inner.context.as_mut_ptr()
	}

	pub fn streams(&self) -> Const<'_, Streams<'_>> {
		Const::new(Streams::new(self.inner.context.as_deref_except()))
	}

	pub fn packets(&mut self) -> Packets<'_> {
		Packets::new(self.inner.context.as_deref_mut_except())
	}

	pub fn receive_packet(&mut self) -> Result<Option<Packet>, FfmpegError> {
		self.packets().receive()
	}

	fn create_input(mut inner: Inner<T>, path: Option<&CStr>, dictionary: &mut Dictionary) -> Result<Self, FfmpegError> {
		// Safety: avformat_open_input is safe to call
		let ec = unsafe {
			avformat_open_input(
				inner.context.as_mut(),
				path.map(|p| p.as_ptr()).unwrap_or(std::ptr::null()),
				std::ptr::null(),
				dictionary.as_mut_ptr_ref(),
			)
		};
		if ec != 0 {
			return Err(FfmpegError::Code(ec.into()));
		}

		if inner.context.as_ptr().is_null() {
			return Err(FfmpegError::Alloc);
		}

		let mut inner = SmartObject::new(inner, |inner| unsafe {
			// We own this resource so we need to free it
			avformat_close_input(inner.context.as_mut());
		});

		// We now own the context and this is freed when the object is dropped
		inner.context.set_destructor(|_| {});

		// Safety: avformat_find_stream_info is safe to call
		let ec = unsafe { avformat_find_stream_info(inner.context.as_mut_ptr(), std::ptr::null_mut()) };
		if ec < 0 {
			return Err(FfmpegError::Code(ec.into()));
		}

		Ok(Self { inner })
	}
}

impl Input<()> {
	pub fn open(path: &str) -> Result<Self, FfmpegError> {
		// We immediately create an input and setup the inner, before using it.
		let inner = unsafe { Inner::empty() };

		Self::create_input(inner, Some(&std::ffi::CString::new(path).unwrap()), &mut Dictionary::new())
	}
}