Skip to main content

ffmpeg_the_third/format/stream/
stream.rs

1use super::Disposition;
2use crate::codec;
3use crate::ffi::*;
4use crate::format::context::common::Context;
5use crate::{DictionaryRef, Discard, Rational};
6
7#[cfg(not(feature = "ffmpeg_8_0"))]
8use crate::codec::packet;
9#[cfg(not(feature = "ffmpeg_8_0"))]
10use libc::c_int;
11
12#[derive(Debug)]
13pub struct Stream<'a> {
14    context: &'a Context,
15    index: usize,
16}
17
18impl<'a> Stream<'a> {
19    pub unsafe fn wrap(context: &Context, index: usize) -> Stream<'_> {
20        Stream { context, index }
21    }
22
23    pub unsafe fn as_ptr(&self) -> *const AVStream {
24        *(*self.context.as_ptr()).streams.add(self.index)
25    }
26}
27
28impl<'a> Stream<'a> {
29    pub fn id(&self) -> i32 {
30        unsafe { (*self.as_ptr()).id }
31    }
32
33    pub fn parameters(&self) -> codec::ParametersRef<'_> {
34        unsafe {
35            codec::ParametersRef::from_raw((*self.as_ptr()).codecpar).expect("codecpar is non-null")
36        }
37    }
38
39    pub fn index(&self) -> usize {
40        unsafe { (*self.as_ptr()).index as usize }
41    }
42
43    pub fn time_base(&self) -> Rational {
44        unsafe { Rational::from((*self.as_ptr()).time_base) }
45    }
46
47    pub fn start_time(&self) -> i64 {
48        unsafe { (*self.as_ptr()).start_time }
49    }
50
51    pub fn duration(&self) -> i64 {
52        unsafe { (*self.as_ptr()).duration }
53    }
54
55    pub fn frames(&self) -> i64 {
56        unsafe { (*self.as_ptr()).nb_frames }
57    }
58
59    pub fn disposition(&self) -> Disposition {
60        unsafe { Disposition::from_bits_truncate((*self.as_ptr()).disposition) }
61    }
62
63    pub fn discard(&self) -> Discard {
64        unsafe { Discard::from((*self.as_ptr()).discard) }
65    }
66
67    #[cfg(not(feature = "ffmpeg_8_0"))]
68    pub fn side_data(&self) -> SideDataIter<'_> {
69        SideDataIter::new(self)
70    }
71
72    pub fn rate(&self) -> Rational {
73        unsafe { Rational::from((*self.as_ptr()).r_frame_rate) }
74    }
75
76    pub fn avg_frame_rate(&self) -> Rational {
77        unsafe { Rational::from((*self.as_ptr()).avg_frame_rate) }
78    }
79
80    pub fn metadata(&self) -> DictionaryRef<'_> {
81        unsafe { DictionaryRef::from_raw((*self.as_ptr()).metadata) }
82    }
83
84    pub fn sample_aspect_ratio(&self) -> Rational {
85        unsafe { Rational::from((*self.as_ptr()).sample_aspect_ratio) }
86    }
87}
88
89impl<'a> PartialEq for Stream<'a> {
90    fn eq(&self, other: &Self) -> bool {
91        unsafe { self.as_ptr() == other.as_ptr() }
92    }
93}
94
95impl<'a> Eq for Stream<'a> {}
96
97#[cfg(not(feature = "ffmpeg_8_0"))]
98pub struct SideDataIter<'a> {
99    stream: &'a Stream<'a>,
100    current: c_int,
101}
102
103#[cfg(not(feature = "ffmpeg_8_0"))]
104impl<'a> SideDataIter<'a> {
105    pub fn new<'sd, 's: 'sd>(stream: &'s Stream) -> SideDataIter<'sd> {
106        SideDataIter { stream, current: 0 }
107    }
108}
109
110#[cfg(not(feature = "ffmpeg_8_0"))]
111impl<'a> Iterator for SideDataIter<'a> {
112    type Item = packet::SideData<'a>;
113
114    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
115        unsafe {
116            if self.current >= (*self.stream.as_ptr()).nb_side_data {
117                return None;
118            }
119
120            self.current += 1;
121
122            Some(packet::SideData::wrap(
123                (*self.stream.as_ptr())
124                    .side_data
125                    .offset((self.current - 1) as isize),
126            ))
127        }
128    }
129
130    fn size_hint(&self) -> (usize, Option<usize>) {
131        unsafe {
132            let length = (*self.stream.as_ptr()).nb_side_data as usize;
133
134            (
135                length - self.current as usize,
136                Some(length - self.current as usize),
137            )
138        }
139    }
140}
141
142#[cfg(not(feature = "ffmpeg_8_0"))]
143impl<'a> ExactSizeIterator for SideDataIter<'a> {}