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
// Copyright (c) 2015 T. Okubo
// This file is part of vlc-rs.
// Licensed under the MIT license, see the LICENSE file.

use ffi;
use ::MediaPlayer;
use ::TrackDescription;
use ::tools::from_cstr;

pub trait MediaPlayerAudioEx {
    fn get_mute(&self) -> Option<bool>;
    fn set_mute(&self, bool);
    fn get_volume(&self) -> i32;
    fn set_volume(&self, volume: i32) -> Result<(), ()>;
    fn get_audio_track_description(&self) -> Option<Vec<TrackDescription>>;
}

impl MediaPlayerAudioEx for MediaPlayer {
    fn get_mute(&self) -> Option<bool> {
        let r = unsafe{ ffi::libvlc_audio_get_mute(self.ptr) };

        if r == 0 {
            Some(false)
        }else if r == -1 {
            None
        }else{
            Some(true)
        }
    }

    fn set_mute(&self, status: bool) {
        unsafe{ ffi::libvlc_audio_set_mute(self.ptr, if status { 1 }else{ 0 }) };
    }

    fn get_volume(&self) -> i32 {
        unsafe{ ffi::libvlc_audio_get_volume(self.ptr) }
    }
    fn set_volume(&self, volume: i32) -> Result<(), ()> {
        unsafe{
            if ffi::libvlc_audio_set_volume(self.ptr, volume) == 0 { Ok(()) }else{ Err(()) }
        }
    }
    fn get_audio_track_description(&self) -> Option<Vec<TrackDescription>> {
        unsafe{
            let p0 = ffi::libvlc_audio_get_track_description(self.ptr);
            if p0.is_null() { return None; }
            let mut td = Vec::new();
            let mut p = p0;

            while !(*p).p_next.is_null() {
                td.push(TrackDescription{ id: (*p).i_id, name: from_cstr((*p).psz_name) });
                p = (*p).p_next;
            }
            ffi::libvlc_track_description_list_release(p0);
            Some(td)
        }
    }

}