]> git.sesse.net Git - vlc/blob - bindings/cil/libvlc.cs
libvlc_destroy() -> libvlc_release()
[vlc] / bindings / cil / libvlc.cs
1 /*
2  * libvlc.cs - libvlc-control CIL bindings
3  *
4  * $Id$
5  */
6
7 /**********************************************************************
8  *  Copyright (C) 2007 RĂ©mi Denis-Courmont.                           *
9  *  This program is free software; you can redistribute and/or modify *
10  *  it under the terms of the GNU General Public License as published *
11  *  by the Free Software Foundation; version 2 of the license, or (at *
12  *  your option) any later version.                                   *
13  *                                                                    *
14  *  This program is distributed in the hope that it will be useful,   *
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of    *
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.              *
17  *  See the GNU General Public License for more details.              *
18  *                                                                    *
19  *  You should have received a copy of the GNU General Public License *
20  *  along with this program; if not, you can get it from:             *
21  *  http://www.gnu.org/copyleft/gpl.html                              *
22  **********************************************************************/
23
24 using System;
25 using System.Collections.Generic;
26 using System.Runtime.InteropServices;
27
28 namespace VideoLAN.LibVLC
29 {
30     public sealed class VLC
31     {
32         public static Instance CreateInstance (string[] args)
33         {
34             U8String[] argv = new U8String[args.Length];
35             for (int i = 0; i < args.Length; i++)
36                 argv[i] = new U8String (args[i]);
37
38             NativeException ex = new NativeException ();
39
40             InstanceHandle h = InstanceHandle.Create (argv.Length, argv, ex);
41             ex.Raise ();
42
43             return new Instance (h);
44         }
45     };
46
47     /** Safe handle for unmanaged LibVLC instance pointer */
48     public sealed class InstanceHandle : NonNullHandle
49     {
50         private InstanceHandle ()
51         {
52         }
53
54         [DllImport ("libvlc-control.dll", EntryPoint="libvlc_new")]
55         public static extern
56         InstanceHandle Create (int argc, U8String[] argv, NativeException ex);
57
58         [DllImport ("libvlc-control.dll", EntryPoint="libvlc_release")]
59         static extern void Destroy (IntPtr ptr, NativeException ex);
60
61         protected override bool ReleaseHandle ()
62         {
63             Destroy (handle, null);
64             return true;
65         }
66     };
67
68     /**
69      * Managed class for LibVLC instance (including playlist)
70      */
71     public class Instance : BaseObject<InstanceHandle>
72     {
73         Dictionary<int, PlaylistItem> items;
74
75         internal Instance (InstanceHandle self) : base (self)
76         {
77             items = new Dictionary<int, PlaylistItem> ();
78         }
79
80         public MediaDescriptor CreateDescriptor (string mrl)
81         {
82             U8String umrl = new U8String (mrl);
83             DescriptorHandle dh = DescriptorHandle.Create (self, umrl, ex);
84             ex.Raise ();
85
86             return new MediaDescriptor (dh);
87         }
88
89         [DllImport ("libvlc-control.dll", EntryPoint="libvlc_playlist_loop")]
90         static extern void PlaylistLoop (InstanceHandle self, bool b,
91                                          NativeException ex);
92         /** Sets the playlist loop flag */
93         public bool Loop
94         {
95             set
96             {
97                 PlaylistLoop (self, value, ex);
98                 ex.Raise ();
99             }
100         }
101
102         [DllImport ("libvlc-control.dll", EntryPoint="libvlc_playlist_play")]
103         static extern void PlaylistPlay (InstanceHandle self, int id, int optc,
104                                          U8String[] optv, NativeException ex);
105         /** Plays the next playlist item */
106         public void Play ()
107         {
108             PlaylistPlay (self, -1, 0, new U8String[0], ex);
109             ex.Raise ();
110         }
111
112         [DllImport ("libvlc-control.dll", EntryPoint="libvlc_playlist_pause")]
113         static extern void PlaylistPause (InstanceHandle self,
114                                           NativeException ex);
115         /** Toggles pause */
116         public void TogglePause ()
117         {
118             PlaylistPause (self, ex);
119             ex.Raise ();
120         }
121
122         [DllImport ("libvlc-control.dll",
123                     EntryPoint="libvlc_playlist_isplaying")]
124         static extern int PlaylistIsPlaying (InstanceHandle self,
125                                              NativeException ex);
126         /** Whether the playlist is running, or in pause/stop */
127         public bool IsPlaying
128         {
129             get
130             {
131                 int ret = PlaylistIsPlaying (self, ex);
132                 ex.Raise ();
133                 return ret != 0;
134             }
135         }
136
137         [DllImport ("libvlc-control.dll", EntryPoint="libvlc_playlist_stop")]
138         static extern void PlaylistStop (InstanceHandle self,
139                                          NativeException ex);
140         /** Stops playing */
141         public void Stop ()
142         {
143             PlaylistStop (self, ex);
144             ex.Raise ();
145         }
146
147         [DllImport ("libvlc-control.dll", EntryPoint="libvlc_playlist_next")]
148         static extern void PlaylistNext (InstanceHandle self,
149                                          NativeException ex);
150         /** Goes to next playlist item (and start playing it) */
151         public void Next ()
152         {
153             PlaylistNext (self, ex);
154             ex.Raise ();
155         }
156
157         [DllImport ("libvlc-control.dll", EntryPoint="libvlc_playlist_prev")]
158         static extern void PlaylistPrev (InstanceHandle self,
159                                          NativeException ex);
160         /** Goes to previous playlist item (and start playing it) */
161         public void Prev ()
162         {
163             PlaylistPrev (self, ex);
164             ex.Raise ();
165         }
166
167         [DllImport ("libvlc-control.dll", EntryPoint="libvlc_playlist_clear")]
168         static extern void PlaylistClear (InstanceHandle self,
169                                           NativeException ex);
170         /** Clears the whole playlist */
171         public void Clear ()
172         {
173             PlaylistClear (self, ex);
174             ex.Raise ();
175
176             foreach (PlaylistItem item in items.Values)
177                 item.Close ();
178             items.Clear ();
179         }
180
181         [DllImport ("libvlc-control.dll",
182                     EntryPoint="libvlc_playlist_add_extended")]
183         static extern int PlaylistAdd (InstanceHandle self, U8String uri,
184                                        U8String name, int optc,
185                                        U8String[] optv, NativeException e);
186         /** Appends an item to the playlist with options */
187         public PlaylistItem Add (string mrl, string name, string[] opts)
188         {
189             U8String umrl = new U8String (mrl);
190             U8String uname = new U8String (name);
191             U8String[] optv = new U8String[opts.Length];
192             for (int i = 0; i < opts.Length; i++)
193                 optv[i] = new U8String (opts[i]);
194
195             int id = PlaylistAdd (self, umrl, uname, optv.Length, optv, ex);
196             ex.Raise ();
197
198             PlaylistItem item = new PlaylistItem (id);
199             items.Add (id, item);
200             return item;
201         }
202         public PlaylistItem Add (string mrl, string[] opts)
203         {
204             return Add (mrl, null, opts);
205         }
206         public PlaylistItem Add (string mrl, string name)
207         {
208             return Add (mrl, name, new string[0]);
209         }
210         public PlaylistItem Add (string mrl)
211         {
212             return Add (mrl, null, new string[0]);
213         }
214
215         [DllImport ("libvlc-control.dll",
216                     EntryPoint="libvlc_playlist_delete_item")]
217         static extern int PlaylistDelete (InstanceHandle self, int id,
218                                           NativeException e);
219         public void Delete (PlaylistItem item)
220         {
221             int id = item.Id;
222             PlaylistDelete (self, id, ex);
223             ex.Raise ();
224
225             item.Close ();
226             items.Remove (id);
227         }
228     };
229
230     public class PlaylistItem
231     {
232         int id;
233         bool deleted;
234
235         internal PlaylistItem (int id)
236         {
237             this.id = id;
238             this.deleted = false;
239         }
240
241         internal void Close ()
242         {
243             deleted = true;
244         }
245
246         internal int Id
247         {
248             get
249             {
250                 if (deleted)
251                     throw new ObjectDisposedException ("Playlist item deleted");
252                 return id;
253             }
254         }
255     };
256
257     /** Safe handle for unmanaged LibVLC media descriptor */
258     public sealed class DescriptorHandle : NonNullHandle
259     {
260         private DescriptorHandle ()
261         {
262         }
263
264         [DllImport ("libvlc-control.dll",
265                     EntryPoint="libvlc_media_descriptor_new")]
266         public static extern
267         DescriptorHandle Create (InstanceHandle inst, U8String mrl,
268                                  NativeException ex);
269
270         [DllImport ("libvlc-control.dll",
271                     EntryPoint="libvlc_media_descriptor_release")]
272         public static extern void Release (IntPtr ptr);
273
274         protected override bool ReleaseHandle ()
275         {
276             Release (handle);
277             return true;
278         }
279     };
280
281     public class MediaDescriptor : BaseObject<DescriptorHandle>
282     {
283         internal MediaDescriptor (DescriptorHandle self) : base (self)
284         {
285         }
286     };
287 };