#![allow(dead_code)]
use super::ffi;
use std::fmt;
pub struct CMFormatDescription(*mut std::ffi::c_void);
impl PartialEq for CMFormatDescription {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl Eq for CMFormatDescription {}
impl std::hash::Hash for CMFormatDescription {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
unsafe {
let hash_value = ffi::cm_format_description_hash(self.0);
hash_value.hash(state);
}
}
}
pub mod media_types {
use crate::utils::four_char_code::FourCharCode;
pub const VIDEO: FourCharCode = FourCharCode::from_bytes(*b"vide");
pub const AUDIO: FourCharCode = FourCharCode::from_bytes(*b"soun");
pub const MUXED: FourCharCode = FourCharCode::from_bytes(*b"mux ");
pub const TEXT: FourCharCode = FourCharCode::from_bytes(*b"text");
pub const CLOSED_CAPTION: FourCharCode = FourCharCode::from_bytes(*b"clcp");
pub const METADATA: FourCharCode = FourCharCode::from_bytes(*b"meta");
pub const TIMECODE: FourCharCode = FourCharCode::from_bytes(*b"tmcd");
}
pub mod codec_types {
use crate::utils::four_char_code::FourCharCode;
pub const H264: FourCharCode = FourCharCode::from_bytes(*b"avc1");
pub const HEVC: FourCharCode = FourCharCode::from_bytes(*b"hvc1");
pub const HEVC_2: FourCharCode = FourCharCode::from_bytes(*b"hev1");
pub const JPEG: FourCharCode = FourCharCode::from_bytes(*b"jpeg");
pub const PRORES_422: FourCharCode = FourCharCode::from_bytes(*b"apcn");
pub const PRORES_4444: FourCharCode = FourCharCode::from_bytes(*b"ap4h");
pub const AAC: FourCharCode = FourCharCode::from_bytes(*b"aac ");
pub const LPCM: FourCharCode = FourCharCode::from_bytes(*b"lpcm");
pub const ALAC: FourCharCode = FourCharCode::from_bytes(*b"alac");
pub const OPUS: FourCharCode = FourCharCode::from_bytes(*b"opus");
pub const FLAC: FourCharCode = FourCharCode::from_bytes(*b"flac");
}
impl CMFormatDescription {
pub fn from_raw(ptr: *mut std::ffi::c_void) -> Option<Self> {
if ptr.is_null() {
None
} else {
Some(Self(ptr))
}
}
pub unsafe fn from_ptr(ptr: *mut std::ffi::c_void) -> Self {
Self(ptr)
}
pub fn as_ptr(&self) -> *mut std::ffi::c_void {
self.0
}
pub fn media_type_raw(&self) -> u32 {
unsafe { ffi::cm_format_description_get_media_type(self.0) }
}
pub fn media_type(&self) -> crate::utils::four_char_code::FourCharCode {
crate::utils::four_char_code::FourCharCode::from(self.media_type_raw())
}
pub fn media_subtype_raw(&self) -> u32 {
unsafe { ffi::cm_format_description_get_media_subtype(self.0) }
}
pub fn media_subtype(&self) -> crate::utils::four_char_code::FourCharCode {
crate::utils::four_char_code::FourCharCode::from(self.media_subtype_raw())
}
pub fn extensions(&self) -> Option<*const std::ffi::c_void> {
unsafe {
let ptr = ffi::cm_format_description_get_extensions(self.0);
if ptr.is_null() {
None
} else {
Some(ptr)
}
}
}
pub fn is_video(&self) -> bool {
self.media_type() == media_types::VIDEO
}
pub fn is_audio(&self) -> bool {
self.media_type() == media_types::AUDIO
}
pub fn is_muxed(&self) -> bool {
self.media_type() == media_types::MUXED
}
pub fn is_text(&self) -> bool {
self.media_type() == media_types::TEXT
}
pub fn is_closed_caption(&self) -> bool {
self.media_type() == media_types::CLOSED_CAPTION
}
pub fn is_metadata(&self) -> bool {
self.media_type() == media_types::METADATA
}
pub fn is_timecode(&self) -> bool {
self.media_type() == media_types::TIMECODE
}
pub fn media_type_string(&self) -> String {
self.media_type().display()
}
pub fn media_subtype_string(&self) -> String {
self.media_subtype().display()
}
pub fn is_h264(&self) -> bool {
self.media_subtype() == codec_types::H264
}
pub fn is_hevc(&self) -> bool {
let subtype = self.media_subtype();
subtype == codec_types::HEVC || subtype == codec_types::HEVC_2
}
pub fn is_aac(&self) -> bool {
self.media_subtype() == codec_types::AAC
}
pub fn is_pcm(&self) -> bool {
self.media_subtype() == codec_types::LPCM
}
pub fn is_prores(&self) -> bool {
let subtype = self.media_subtype();
subtype == codec_types::PRORES_422 || subtype == codec_types::PRORES_4444
}
pub fn is_alac(&self) -> bool {
self.media_subtype() == codec_types::ALAC
}
pub fn audio_sample_rate(&self) -> Option<f64> {
if !self.is_audio() {
return None;
}
let rate = unsafe { ffi::cm_format_description_get_audio_sample_rate(self.0) };
if rate > 0.0 {
Some(rate)
} else {
None
}
}
pub fn audio_channel_count(&self) -> Option<u32> {
if !self.is_audio() {
return None;
}
let count = unsafe { ffi::cm_format_description_get_audio_channel_count(self.0) };
if count > 0 {
Some(count)
} else {
None
}
}
pub fn audio_bits_per_channel(&self) -> Option<u32> {
if !self.is_audio() {
return None;
}
let bits = unsafe { ffi::cm_format_description_get_audio_bits_per_channel(self.0) };
if bits > 0 {
Some(bits)
} else {
None
}
}
pub fn audio_bytes_per_frame(&self) -> Option<u32> {
if !self.is_audio() {
return None;
}
let bytes = unsafe { ffi::cm_format_description_get_audio_bytes_per_frame(self.0) };
if bytes > 0 {
Some(bytes)
} else {
None
}
}
pub fn audio_format_flags(&self) -> Option<u32> {
if !self.is_audio() {
return None;
}
Some(unsafe { ffi::cm_format_description_get_audio_format_flags(self.0) })
}
pub fn audio_is_float(&self) -> bool {
self.audio_format_flags().is_some_and(|f| f & 1 != 0)
}
pub fn audio_is_big_endian(&self) -> bool {
self.audio_format_flags().is_some_and(|f| f & 2 != 0)
}
}
impl Clone for CMFormatDescription {
fn clone(&self) -> Self {
unsafe {
let ptr = ffi::cm_format_description_retain(self.0);
Self(ptr)
}
}
}
impl Drop for CMFormatDescription {
fn drop(&mut self) {
unsafe {
ffi::cm_format_description_release(self.0);
}
}
}
unsafe impl Send for CMFormatDescription {}
unsafe impl Sync for CMFormatDescription {}
impl fmt::Debug for CMFormatDescription {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CMFormatDescription")
.field("media_type", &self.media_type_string())
.field("codec", &self.media_subtype_string())
.finish()
}
}
impl fmt::Display for CMFormatDescription {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"CMFormatDescription(type: 0x{:08X}, subtype: 0x{:08X})",
self.media_type_raw(),
self.media_subtype_raw()
)
}
}