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