1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright (c) 2015 T. Okubo
// This file is part of vlc-rs.
// Licensed under the MIT license, see the LICENSE file.

use sys;
use ::{Instance, EventManager};
use ::enums::{State, Meta, TrackType};
use ::tools::{to_cstr, from_cstr, path_to_cstr};
use std::path::Path;

pub struct Media {
    pub(crate) ptr: *mut sys::libvlc_media_t,
}

impl Media {
    /// Create a media with a certain given media resource location, for instance a valid URL. 
    pub fn new_location(instance: &Instance, mrl: &str) -> Option<Media> {
        let cstr = to_cstr(mrl);
        
        unsafe{
            let p = sys::libvlc_media_new_location(instance.ptr, cstr.as_ptr());
            if p.is_null() {
                return None;
            }
            
            Some(Media{ptr: p})
        }
    }

    /// Create a media for a certain file path. 
    pub fn new_path<T: AsRef<Path>>(instance: &Instance, path: T) -> Option<Media> {
        let cstr = match path_to_cstr(path.as_ref()) {
            Ok(s) => s,
            Err(_) => { return None; },
        };
        
        unsafe{
            let p = sys::libvlc_media_new_path(instance.ptr, cstr.as_ptr());
            if p.is_null() {
                return None;
            }
            
            Some(Media{ptr: p})
        }
    }
    
    pub fn new_fd(instance: &Instance, fd: i32) -> Option<Media> {
        unsafe{
            let p = sys::libvlc_media_new_fd(instance.ptr, fd);
            if p.is_null() {
                return None;
            }
            
            Some(Media{ptr: p})
        }
    }

    pub fn mrl(&self) -> Option<String> {
        unsafe{
            let p_str = sys::libvlc_media_get_mrl(self.ptr);
            let s = from_cstr(p_str);
            sys::libvlc_free(p_str as *mut ::libc::c_void);
            s
        }
    }

    pub fn event_manager<'a>(&'a self) -> EventManager<'a> {
        unsafe{
            let p = sys::libvlc_media_event_manager(self.ptr);
            assert!(!p.is_null());
            EventManager{ptr: p, _phantomdata: ::std::marker::PhantomData}
        }
    }

    /// Read the meta of the media.
    /// If the media has not yet been parsed this will return None.
    pub fn get_meta(&self, meta: Meta) -> Option<String> {
        unsafe{
            let p_str = sys::libvlc_media_get_meta(self.ptr, meta);
            let s = from_cstr(p_str);
            sys::libvlc_free(p_str as *mut ::libc::c_void);
            s
        }
    }

    /// Set the meta of the media.
    /// (This function will not save the meta, call save_meta in order to save the meta) 
    pub fn set_meta(&self, meta: Meta, value: &str) {
        unsafe{
            sys::libvlc_media_set_meta(self.ptr, meta, to_cstr(value).as_ptr());
        }
    }

    /// Save the meta previously set.
    pub fn save_meta(&self) -> bool {
        if unsafe{ sys::libvlc_media_save_meta(self.ptr) } == 0 { false }else{ true }
    }

    /// Get current state of media descriptor object.
    pub fn state(&self) -> State {
        unsafe{ sys::libvlc_media_get_state(self.ptr) }
    }

    /// Get duration (in ms) of media descriptor object item.
    pub fn duration(&self) -> Option<i64> {
        let time = unsafe{
            sys::libvlc_media_get_duration(self.ptr)
        };
        if time != -1 { Some(time) }else{ None }
    }

    /// Parse a media. 
    pub fn parse(&self) {
        unsafe{ sys::libvlc_media_parse(self.ptr) };
    }

    /// Parse a media.
    pub fn parse_async(&self) {
        unsafe{ sys::libvlc_media_parse_async(self.ptr) };
    }

    /// Get Parsed status for media descriptor object.
    pub fn is_parsed(&self) -> bool {
        if unsafe{ sys::libvlc_media_is_parsed(self.ptr) } == 0 { false }else{ true }
    }

    pub fn tracks(&self) -> Option<Vec<MediaTrack>> {
        unsafe{
            let mut p_track: *mut *mut sys::libvlc_media_track_t = ::std::ptr::null_mut();
            let n = sys::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,
                });
            }

            sys::libvlc_media_tracks_release(p_track, n);
            Some(track)
        }
    }

    /// Returns raw pointer
    pub fn raw(&self) -> *mut sys::libvlc_media_t {
        self.ptr
    }
}

impl Drop for Media {
    fn drop(&mut self) {
        unsafe{ sys::libvlc_media_release(self.ptr) };
    }
}

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct MediaTrack {
    pub codec: u32,
    pub original_fourcc: u32,
    pub id: i32,
    pub track_type: TrackType,
    pub profile: i32,
    pub level: i32,
    pub bitrate: u32,
    pub language: Option<String>,
    pub description: Option<String>,
    pub type_specific_data: MediaTrackUnion,
}

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum MediaTrackUnion {
    Audio(AudioTrack), Video(VideoTrack), Subtitle(SubtitleTrack), None,
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct AudioTrack {
    pub channels: u32,
    pub rate: u32,
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct VideoTrack {
    pub height: u32,
    pub width: u32,
    pub sar_num: u32,
    pub sar_den: u32,
    pub frame_rate_num: u32,
    pub frame_rate_den: u32,
}

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct SubtitleTrack {
    pub encoding: Option<String>,
}