playa_ffmpeg/codec/
parameters.rs

1use std::{any::Any, rc::Rc};
2
3use super::{Context, Id};
4use crate::{ffi::*, media};
5
6pub struct Parameters {
7    ptr: *mut AVCodecParameters,
8    owner: Option<Rc<dyn Any>>,
9}
10
11unsafe impl Send for Parameters {}
12
13impl Parameters {
14    pub unsafe fn wrap(ptr: *mut AVCodecParameters, owner: Option<Rc<dyn Any>>) -> Self {
15        Parameters { ptr, owner }
16    }
17
18    pub unsafe fn as_ptr(&self) -> *const AVCodecParameters {
19        self.ptr as *const _
20    }
21
22    pub unsafe fn as_mut_ptr(&mut self) -> *mut AVCodecParameters {
23        self.ptr
24    }
25}
26
27impl Parameters {
28    pub fn new() -> Self {
29        unsafe { Parameters { ptr: avcodec_parameters_alloc(), owner: None } }
30    }
31
32    pub fn medium(&self) -> media::Type {
33        unsafe { media::Type::from((*self.as_ptr()).codec_type) }
34    }
35
36    pub fn id(&self) -> Id {
37        unsafe { Id::from((*self.as_ptr()).codec_id) }
38    }
39}
40
41impl Default for Parameters {
42    fn default() -> Self {
43        Self::new()
44    }
45}
46
47impl Drop for Parameters {
48    fn drop(&mut self) {
49        unsafe {
50            if self.owner.is_none() {
51                avcodec_parameters_free(&mut self.as_mut_ptr());
52            }
53        }
54    }
55}
56
57impl Clone for Parameters {
58    fn clone(&self) -> Self {
59        let mut ctx = Parameters::new();
60        ctx.clone_from(self);
61
62        ctx
63    }
64
65    fn clone_from(&mut self, source: &Self) {
66        unsafe {
67            avcodec_parameters_copy(self.as_mut_ptr(), source.as_ptr());
68        }
69    }
70}
71
72impl<C: AsRef<Context>> From<C> for Parameters {
73    fn from(context: C) -> Parameters {
74        let mut parameters = Parameters::new();
75        let context = context.as_ref();
76        unsafe {
77            avcodec_parameters_from_context(parameters.as_mut_ptr(), context.as_ptr());
78        }
79        parameters
80    }
81}