vlc/
media_list.rs

1// Copyright (c) 2015 T. Okubo
2// This file is part of vlc-rs.
3// Licensed under the MIT license, see the LICENSE file.
4
5use sys;
6use ::{Instance, Media, EventManager};
7
8pub struct MediaList {
9    pub(crate) ptr: *mut sys::libvlc_media_list_t,
10}
11
12impl MediaList {
13    /// Create an empty media list.
14    pub fn new(instance: &Instance) -> Option<MediaList> {
15        unsafe{
16            let p = sys::libvlc_media_list_new(instance.ptr);
17            if p.is_null() { None }else{ Some(MediaList{ptr: p}) }
18        }
19    }
20
21    /// Associate media instance with this media list instance.
22    /// If another media instance was present it will be released. The libvlc_media_list_lock should NOT be held upon entering this function.
23    pub fn set_media(&self, md: &Media) {
24        unsafe{ sys::libvlc_media_list_set_media(self.ptr, md.ptr); }
25    }
26
27    /// Get media instance from this media list instance.
28    /// The MediaList::lock should NOT be held upon entering this function.
29    pub fn media(&self) -> Option<Media> {
30        unsafe{
31            let p = sys::libvlc_media_list_media(self.ptr);
32            if p.is_null() { None }else{ Some(Media{ptr: p}) }
33        }
34    }
35
36    /// Add media instance to media list.
37    /// The MediaList::lock should be held upon entering this function.
38    pub fn add_media(&self, md: &Media) -> Result<(), ()> {
39        unsafe{
40            if sys::libvlc_media_list_add_media(self.ptr, md.ptr) == 0 { Ok(()) }else{ Err(()) }
41        }
42    }
43
44    /// Insert media instance in media list on a position.
45    /// The MediaList::lock should be held upon entering this function.
46    pub fn insert_media(&self, md: &Media, pos: i32) -> Result<(), ()> {
47        unsafe{
48            if sys::libvlc_media_list_insert_media(self.ptr, md.ptr, pos) == 0 { Ok(()) }else{ Err(()) }
49        }
50    }
51
52    /// Remove media instance from media list on a position.
53    /// The MediaList::lock should be held upon entering this function.
54    pub fn remove_index(&self, pos: i32) -> Result<(), ()> {
55        unsafe{
56            if sys::libvlc_media_list_remove_index(self.ptr, pos) == 0 { Ok(()) }else{ Err(()) }
57        }
58    }
59
60    /// Get count on media list items.
61    /// The MediaList::lock should be held upon entering this function.
62    pub fn count(&self) -> i32 {
63        unsafe{ sys::libvlc_media_list_count(self.ptr) }
64    }
65
66    /// List media instance in media list at a position.
67    /// The MediaList::lock should be held upon entering this function.
68    pub fn item_at_index(&self, pos: i32) -> Option<Media> {
69        unsafe{
70            let p = sys::libvlc_media_list_item_at_index(self.ptr, pos);
71            if p.is_null() { None }else{ Some(Media{ptr: p}) }
72        }
73    }
74
75    /// Find index position of List media instance in media list.
76    pub fn index_of_item(&self, md: &Media) -> Option<i32> {
77        unsafe{
78            let i = sys::libvlc_media_list_index_of_item(self.ptr, md.ptr);
79            if i == -1 { None }else{ Some(i) }
80        }
81    }
82
83    /// This indicates if this media list is read-only from a user point of view.
84    pub fn is_readonly(&self) -> bool {
85        unsafe{ if sys::libvlc_media_list_is_readonly(self.ptr) == 0 { false }else{ true } }
86    }
87
88    /// Get lock on media list items
89    pub fn lock(&self) {
90        unsafe{ sys::libvlc_media_list_lock(self.ptr); }
91    }
92
93    /// Release lock on media list items
94    /// The libvlc_media_list_lock should be held upon entering this function.
95    pub fn unlock(&self) {
96        unsafe{ sys::libvlc_media_list_unlock(self.ptr); }
97    }
98
99    /// Get EventManager from this media list instance.
100    pub fn event_manager<'a>(&'a self) -> EventManager<'a> {
101        unsafe{
102            let p = sys::libvlc_media_list_event_manager(self.ptr);
103            assert!(!p.is_null());
104            EventManager{ptr: p, _phantomdata: ::std::marker::PhantomData}
105        }
106    }
107
108    /// Returns raw pointer
109    pub fn raw(&self) -> *mut sys::libvlc_media_list_t {
110        self.ptr
111    }
112}
113
114impl Drop for MediaList {
115    fn drop(&mut self) {
116        unsafe{ sys::libvlc_media_list_release(self.ptr) };
117    }
118}