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