Add Media::tracks and the return type

merge-requests/7/merge
T. Okubo 2015-11-29 19:50:28 +09:00
parent a02354278a
commit 9a60821b9d
2 changed files with 84 additions and 28 deletions

View File

@ -198,7 +198,7 @@ impl libvlc_media_track_t {
pub unsafe fn audio(&mut self) -> *mut libvlc_audio_track_t { pub unsafe fn audio(&mut self) -> *mut libvlc_audio_track_t {
transmute(self._union0) transmute(self._union0)
} }
pub unsafe fn video(&mut self) -> *mut libvlc_audio_track_t { pub unsafe fn video(&mut self) -> *mut libvlc_video_track_t {
transmute(self._union0) transmute(self._union0)
} }
pub unsafe fn subtitle(&mut self) -> *mut libvlc_subtitle_track_t { pub unsafe fn subtitle(&mut self) -> *mut libvlc_subtitle_track_t {

View File

@ -119,6 +119,64 @@ impl Media {
pub fn is_parsed(&self) -> bool { pub fn is_parsed(&self) -> bool {
if unsafe{ ffi::libvlc_media_is_parsed(self.ptr) } == 0 { false }else{ true } if unsafe{ ffi::libvlc_media_is_parsed(self.ptr) } == 0 { false }else{ true }
} }
pub fn tracks(&self) -> Option<Vec<MediaTrack>> {
unsafe{
let mut p_track: *mut *mut ffi::libvlc_media_track_t = ::std::ptr::null_mut();
let n = ffi::libvlc_media_tracks_get(self.ptr, &mut p_track);
if n == 0 {
return None;
}
let mut track = Vec::new();
for i in 0..n {
let p = p_track.offset(i as isize);
let type_specific_data = match (**p).i_type {
TrackType::Audio => {
let audio = (**p).audio();
MediaTrackUnion::Audio(AudioTrack{
channels: (*audio).i_channels,
rate: (*audio).i_rate,
})
},
TrackType::Video => {
let video = (**p).video();
MediaTrackUnion::Video(VideoTrack{
height: (*video).i_height,
width: (*video).i_width,
sar_num: (*video).i_sar_num,
sar_den: (*video).i_sar_den,
frame_rate_num: (*video).i_frame_rate_num,
frame_rate_den: (*video).i_frame_rate_den,
})
},
TrackType::Text => {
let subtitle = (**p).subtitle();
MediaTrackUnion::Subtitle(SubtitleTrack{
encoding: from_cstr((*subtitle).psz_encoding)
})
},
TrackType::Unknown => MediaTrackUnion::None,
};
track.push(MediaTrack{
codec: (**p).i_codec,
original_fourcc: (**p).i_original_fourcc,
id: (**p).i_id,
track_type: (**p).i_type,
profile: (**p).i_profile,
level: (**p).i_level,
bitrate: (**p).i_bitrate,
language: from_cstr((**p).psz_language),
description: from_cstr((**p).psz_description),
type_specific_data: type_specific_data,
});
}
ffi::libvlc_media_tracks_release(p_track, n);
Some(track)
}
}
} }
impl Drop for Media { impl Drop for Media {
@ -127,45 +185,43 @@ impl Drop for Media {
} }
} }
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] #[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct MediaTrackInfo { pub struct MediaTrack {
pub codec: u32, pub codec: u32,
pub original_fourcc: u32,
pub id: i32, pub id: i32,
pub track_type: TrackType, pub track_type: TrackType,
pub profile: i32, pub profile: i32,
pub level: i32, pub level: i32,
pub bitrate: u32,
pub language: Option<String>,
pub description: Option<String>,
pub type_specific_data: MediaTrackUnion,
}
audio: MediaTrackInfoAudio, #[derive(Clone, PartialEq, Eq, Hash, Debug)]
video: MediaTrackInfoVideo, pub enum MediaTrackUnion {
Audio(AudioTrack), Video(VideoTrack), Subtitle(SubtitleTrack), None,
} }
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct MediaTrackInfoAudio { pub struct AudioTrack {
pub channels: i32, pub channels: u32,
pub rate: i32, pub rate: u32,
} }
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct MediaTrackInfoVideo { pub struct VideoTrack {
pub height: i32, pub height: u32,
pub width: i32, pub width: u32,
pub sar_num: u32,
pub sar_den: u32,
pub frame_rate_num: u32,
pub frame_rate_den: u32,
} }
impl MediaTrackInfo { #[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub fn audio(&self) -> Option<MediaTrackInfoAudio> { pub struct SubtitleTrack {
if self.track_type == TrackType::Audio { pub encoding: Option<String>,
Some(self.audio)
}else{
None
}
} }
pub fn video(&self) -> Option<MediaTrackInfoVideo> {
if self.track_type == TrackType::Video {
Some(self.video)
}else{
None
}
}
}