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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
use ffi;
use ::Instance;
use ::Media;
use ::EventManager;
use ::libc::{c_void, c_uint};
use ::enums::{State, Position};
use std::mem::transmute;
pub struct MediaPlayer {
pub ptr: *mut ffi::libvlc_media_player_t,
}
impl MediaPlayer {
pub fn new(instance: &Instance) -> Option<MediaPlayer> {
unsafe{
let p = ffi::libvlc_media_player_new(instance.ptr);
if p.is_null() {
return None;
}
Some(MediaPlayer{ptr: p})
}
}
pub fn set_media(&self, md: &Media) {
unsafe{ ffi::libvlc_media_player_set_media(self.ptr, md.ptr) };
}
pub fn get_media(&self) -> Option<Media> {
let p = unsafe{ ffi::libvlc_media_player_get_media(self.ptr) };
if p.is_null() {
None
}else{
Some(Media{ptr: p})
}
}
pub fn event_manager<'a>(&'a self) -> EventManager<'a> {
unsafe{
let p = ffi::libvlc_media_player_event_manager(self.ptr);
assert!(!p.is_null());
EventManager{ptr: p, _phantomdata: ::std::marker::PhantomData}
}
}
pub fn is_playing(&self) -> bool {
if unsafe{ ffi::libvlc_media_player_is_playing(self.ptr) } == 0 {
false
}else{
true
}
}
pub fn play(&self) -> Result<(), ()> {
if unsafe{ ffi::libvlc_media_player_play(self.ptr) } == 0 {
Ok(())
}else{
Err(())
}
}
pub fn set_pause(&self, do_pause: bool) {
unsafe{ ffi::libvlc_media_player_set_pause(self.ptr, if do_pause {1} else {0}) };
}
pub fn pause(&self) {
unsafe{ ffi::libvlc_media_player_pause(self.ptr) };
}
pub fn stop(&self) {
unsafe{ ffi::libvlc_media_player_stop(self.ptr) };
}
pub fn set_callbacks<F>(
&self,
play: F,
pause: Option<Box<Fn(i64) + Send + 'static>>,
resume: Option<Box<Fn(i64) + Send + 'static>>,
flush: Option<Box<Fn(i64) + Send + 'static>>,
drain: Option<Box<Fn() + Send + 'static>>)
where F: Fn(*const c_void, u32, i64) + Send + 'static,
{
let flag_pause = pause.is_some();
let flag_resume = resume.is_some();
let flag_flush = flush.is_some();
let flag_drain = drain.is_some();
let data = AudioCallbacksData {
play: Box::new(play), pause: pause, resume: resume,
flush: flush, drain: drain,
};
let data = Box::into_raw(Box::new(data));
unsafe{
ffi::libvlc_audio_set_callbacks(
self.ptr,
Some(audio_cb_play),
if flag_pause {Some(audio_cb_pause)} else {None},
if flag_resume {Some(audio_cb_resume)} else {None},
if flag_flush {Some(audio_cb_flush)} else {None},
if flag_drain {Some(audio_cb_drain)} else {None},
data as *mut c_void);
}
}
pub fn set_nsobject(&self, drawable: *mut c_void) {
unsafe{ ffi::libvlc_media_player_set_nsobject(self.ptr, drawable) };
}
pub fn get_nsobject(&self) -> Option<*mut c_void> {
let nso = unsafe{ ffi::libvlc_media_player_get_nsobject(self.ptr) };
if nso.is_null() { None }else{ Some(nso) }
}
pub fn set_xwindow(&self, drawable: u32) {
unsafe{ ffi::libvlc_media_player_set_xwindow(self.ptr, drawable) };
}
pub fn get_xwindow(&self) -> Option<u32> {
let id = unsafe{ ffi::libvlc_media_player_get_xwindow(self.ptr) };
if id == 0 { None }else{ Some(id) }
}
pub fn set_hwnd(&self, drawable: *mut c_void) {
unsafe{ ffi::libvlc_media_player_set_hwnd(self.ptr, drawable) };
}
pub fn get_hwnd(&self) -> Option<*mut c_void> {
let hwnd = unsafe{ ffi::libvlc_media_player_get_hwnd(self.ptr) };
if hwnd.is_null() { None }else{ Some(hwnd) }
}
pub fn get_time(&self) -> Option<i64> {
unsafe{
let t = ffi::libvlc_media_player_get_time(self.ptr);
if t == -1 { None }else{ Some(t) }
}
}
pub fn set_time(&self, time: i64) {
unsafe{ ffi::libvlc_media_player_set_time(self.ptr, time); }
}
pub fn get_position(&self) -> Option<f32> {
unsafe{
let pos = ffi::libvlc_media_player_get_position(self.ptr);
if pos == -1f32 { None }else{ Some(pos) }
}
}
pub fn set_position(&self, pos: f32) {
unsafe{ ffi::libvlc_media_player_set_position(self.ptr, pos); }
}
pub fn set_chapter(&self, chapter: i32) {
unsafe{ ffi::libvlc_media_player_set_chapter(self.ptr, chapter); }
}
pub fn get_chapter(&self) -> Option<i32> {
unsafe{
let c = ffi::libvlc_media_player_get_chapter(self.ptr);
if c == -1 { None }else{ Some(c) }
}
}
pub fn chapter_count(&self) -> Option<i32> {
unsafe{
let c = ffi::libvlc_media_player_get_chapter_count(self.ptr);
if c == -1 { None }else{ Some(c) }
}
}
pub fn will_play(&self) -> bool {
unsafe{
let b = ffi::libvlc_media_player_will_play(self.ptr);
if b == 0 { false }else{ true }
}
}
pub fn chapter_count_for_title(&self, title: i32) -> Option<i32> {
unsafe{
let c = ffi::libvlc_media_player_get_chapter_count_for_title(self.ptr, title);
if c == -1 { None }else{ Some(c) }
}
}
pub fn set_title(&self, title: i32) {
unsafe{ ffi::libvlc_media_player_set_title(self.ptr, title); }
}
pub fn get_title(&self) -> Option<i32> {
unsafe{
let t = ffi::libvlc_media_player_get_title(self.ptr);
if t == -1 { None }else{ Some(t) }
}
}
pub fn title_count(&self) -> Option<i32> {
unsafe{
let t = ffi::libvlc_media_player_get_title_count(self.ptr);
if t == -1 { Some(t) } else { None }
}
}
pub fn previous_chapter(&self) {
unsafe{ ffi::libvlc_media_player_previous_chapter(self.ptr); }
}
pub fn next_chapter(&self) {
unsafe{ ffi::libvlc_media_player_next_chapter(self.ptr); }
}
pub fn get_rate(&self) -> f32 {
unsafe{ ffi::libvlc_media_player_get_rate(self.ptr) }
}
pub fn set_rate(&self, rate: f32) -> Result<(),()> {
unsafe{
if ffi::libvlc_media_player_set_rate(self.ptr, rate) == -1 {
Err(())
}else{
Ok(())
}
}
}
pub fn state(&self) -> State {
unsafe{ ffi::libvlc_media_player_get_state(self.ptr) }
}
pub fn has_vout(&self) -> u32 {
unsafe{ ffi::libvlc_media_player_has_vout(self.ptr) }
}
pub fn is_seekable(&self) -> bool {
unsafe{
let b = ffi::libvlc_media_player_is_seekable(self.ptr);
if b == 0 { false }else{ true }
}
}
pub fn can_pause(&self) -> bool {
unsafe{
let b = ffi::libvlc_media_player_can_pause(self.ptr);
if b == 0 { false }else{ true }
}
}
pub fn program_scrambled(&self) -> bool {
unsafe{
let b = ffi::libvlc_media_player_program_scrambled(self.ptr);
if b == 0 { false }else{ true }
}
}
pub fn next_frame(&self) {
unsafe{ ffi::libvlc_media_player_next_frame(self.ptr); }
}
pub fn navigate(&self, navigate: u32) {
unsafe{ ffi::libvlc_media_player_navigate(self.ptr, navigate); }
}
pub fn set_video_title_display(&self, position: Position, timeout: u32) {
unsafe{ ffi::libvlc_media_player_set_video_title_display(self.ptr, position, timeout); }
}
}
impl Drop for MediaPlayer {
fn drop(&mut self) {
unsafe{ ffi::libvlc_media_player_release(self.ptr) };
}
}
struct AudioCallbacksData {
play: Box<Fn(*const c_void, u32, i64) + Send + 'static>,
pause: Option<Box<Fn(i64) + Send + 'static>>,
resume: Option<Box<Fn(i64) + Send + 'static>>,
flush: Option<Box<Fn(i64) + Send + 'static>>,
drain: Option<Box<Fn() + Send + 'static>>,
}
unsafe extern "C" fn audio_cb_play(
data: *mut c_void, samples: *const c_void, count: c_uint, pts: i64) {
let data: &AudioCallbacksData = transmute(data as *mut AudioCallbacksData);
(data.play)(samples, count, pts);
}
unsafe extern "C" fn audio_cb_pause(data: *mut c_void, pts: i64) {
let data: &AudioCallbacksData = transmute(data as *mut AudioCallbacksData);
(data.pause.as_ref().unwrap())(pts);
}
unsafe extern "C" fn audio_cb_resume(data: *mut c_void, pts: i64) {
let data: &AudioCallbacksData = transmute(data as *mut AudioCallbacksData);
(data.resume.as_ref().unwrap())(pts);
}
unsafe extern "C" fn audio_cb_flush(data: *mut c_void, pts: i64) {
let data: &AudioCallbacksData = transmute(data as *mut AudioCallbacksData);
(data.flush.as_ref().unwrap())(pts);
}
unsafe extern "C" fn audio_cb_drain(data: *mut c_void) {
let data: &AudioCallbacksData = transmute(data as *mut AudioCallbacksData);
(data.drain.as_ref().unwrap())();
}
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct TrackDescription {
pub id: i32,
pub name: Option<String>,
}