]> git.sesse.net Git - vlc/blob - bindings/cil/src/media.cs
Media event handling
[vlc] / bindings / cil / src / media.cs
1 /**
2  * @file media.cs
3  * @brief Media descriptor class
4  * @ingroup API
5  */
6
7 /**********************************************************************
8  *  Copyright (C) 2007-2009 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     /**
31      * @brief MediaHandle: unmanaged LibVLC media pointer
32      * @ingroup Internals
33      */
34     internal sealed class MediaHandle : NonNullHandle
35     {
36         /**
37          * NonNullHandle.Destroy
38          */
39         protected override void Destroy ()
40         {
41             LibVLC.MediaRelease (handle);
42         }
43     };
44
45     /**
46      * @brief MetaType: type of a media meta-information entry
47      */
48     public enum MetaType
49     {
50         Title,
51         Artist,
52         Genre,
53         Copyright,
54         Album,
55         TrackNumber,
56         Description,
57         Rating,
58         Date,
59         Setting,
60         URL,
61         Language,
62         NowPlaying,
63         Publisher,
64         EncodedBy,
65         ArtworkURL,
66         TrackID,
67     };
68
69     /**
70      * @brief State: media/player state
71      *
72      * Media and Player objects are always in one of these state.
73      * @see Media::State and @see Player::State.
74      */
75     public enum State
76     {
77         NothingSpecial, /**< Nothing going on */
78         Opening, /**< Being opened */
79         Buffering, /**< Buffering before play */
80         Playing, /**< Playing */
81         Paused, /**< Paused */
82         Stopped, /**< Stopped */
83         Ended, /**< Played until the end */
84         Error, /**< Failed */
85     };
86
87     /* Media events */
88     [StructLayout (LayoutKind.Sequential)]
89     internal sealed class MediaMetaEvent : GenericEvent
90     {
91         public MetaType metaType;
92     };
93     internal delegate void MediaMetaCallback (MediaMetaEvent e, IntPtr d);
94
95     /*[StructLayout (LayoutKind.Sequential)]
96     internal sealed class MediaSubitemEvent : GenericEvent
97     {
98         public IntPtr child; -- MediaHandle
99     };*/
100
101     [StructLayout (LayoutKind.Sequential)]
102     internal sealed class MediaDurationEvent : GenericEvent
103     {
104         public long duration;
105     };
106     internal delegate void MediaDurationCallback (MediaDurationEvent e,
107                                                   IntPtr d);
108
109     [StructLayout (LayoutKind.Sequential)]
110     internal sealed class MediaPreparseEvent : GenericEvent
111     {
112         public int status;
113     };
114     internal delegate void MediaPreparseCallback (MediaPreparseEvent e,
115                                                   IntPtr d);
116
117     /* media_freed -> bad idea w.r.t. the GC */
118
119     [StructLayout (LayoutKind.Sequential)]
120     internal sealed class MediaStateEvent : GenericEvent
121     {
122         public State state;
123     };
124     internal delegate void MediaStateCallback (MediaStateEvent e, IntPtr d);
125
126     /**
127      * @brief Media: a source media
128      * @ingroup API
129      * Each media object represents an input media, such as a file or an URL.
130      */
131     public class Media : EventingObject, ICloneable
132     {
133         internal MediaHandle Handle
134         {
135             get
136             {
137                 return handle as MediaHandle;
138             }
139         }
140
141         /**
142          * Creates a Media object.
143          *
144          * @param instance VLC instance
145          * @param mrl Media Resource Locator (file path or URL)
146          */
147         public Media (VLC instance, string mrl)
148         {
149             U8String umrl = new U8String (mrl);
150
151             handle = LibVLC.MediaCreate (instance.Handle, umrl, ex);
152             Raise ();
153             Attach ();
154         }
155
156         private Media (MediaHandle handle)
157         {
158             this.handle = handle;
159             Attach ();
160         }
161
162         /**
163          * Duplicates a media object.
164          */
165         public object Clone ()
166         {
167             return new Media (LibVLC.MediaDuplicate (Handle));
168         }
169
170         private void Attach ()
171         {
172             Attach (EventType.MediaMetaChanged,
173                     new MediaMetaCallback (MetaCallback));
174             //Attach (EventType.MediaSubItemAdded, SubItemAdded);
175             Attach (EventType.MediaDurationChanged,
176                     new MediaDurationCallback (DurationCallback));
177             /*Attach (EventType.MediaPreparsedChanged,
178                     new MediaPreparseCallback (PreparseCallback));*/
179             /* MediaFreed: better not... */
180             Attach (EventType.MediaStateChanged,
181                     new MediaStateCallback (StateCallback));
182         }
183
184         /**
185          * Add VLC input item options to the media.
186          * @code
187          * Media m = new Media(vlc, "http://www.example.com/music.ogg");
188          * m.AddOptions(":http-user-agent=LibVLC.Net "
189          *            + ":http-proxy=proxy:8080", true);
190          * @endcode
191          * @param options VLC options in VLC input item format
192          *                (see example below)
193          * @param trusted whether the options are set by a trusted agent
194          *                (e.g. the local computer configuration) or not
195          *                (e.g. a downloaded file).
196          * @version VLC 0.9.9 if trusted is false
197          */
198         public void AddOptions (string options, bool trusted)
199         {
200             U8String uopts = new U8String (options);
201
202             if (trusted)
203                 LibVLC.MediaAddOption (Handle, uopts, ex);
204             else
205                 LibVLC.MediaAddUntrustedOption (Handle, uopts, ex);
206             Raise ();
207         }
208
209         /**
210          * The media location (file path, URL, ...).
211          * @version VLC 1.0
212          */
213         public string Location
214         {
215             get
216             {
217                 StringHandle str = LibVLC.MediaGetMRL (Handle, ex);
218                 Raise ();
219                 return str.Transform ();
220             }
221         }
222
223         public override string ToString ()
224         {
225             return Location;
226         }
227
228         /**
229          * Current state of the media.
230          */
231         public State State
232         {
233             get
234             {
235                 State ret = LibVLC.MediaGetState (Handle, ex);
236                 Raise ();
237                 return ret;
238             }
239         }
240
241         public delegate void StateChange (Media media, State state);
242         public event StateChange StateChanged;
243         private void StateCallback (MediaStateEvent ev, IntPtr data)
244         {
245             if (StateChanged != null)
246                 StateChanged (this, ev.state);
247         }
248
249         internal override EventManagerHandle GetManager ()
250         {
251             return LibVLC.MediaEventManager (Handle, null);
252         }
253
254         /**
255          * Duration of the media in microseconds. The precision of the result
256          * depends on the input stram protocol and file format. The value
257          * might be incorrect and unknown (VLC usually returns 0 or -1 then).
258          */
259         public long Duration
260         {
261             get
262             {
263                 long duration = LibVLC.MediaGetDuration (Handle, ex);
264                 Raise ();
265                 return duration;
266             }
267         }
268
269         public delegate void DurationChange (Media media, long duration);
270         public event DurationChange DurationChanged;
271         private void DurationCallback (MediaDurationEvent ev, IntPtr data)
272         {
273             if (DurationChanged != null)
274                 DurationChanged (this, ev.duration);
275         }
276
277         /**
278          * Whether the media was "preparsed". If true, the meta-infos were
279          * extracted, even before the media was played. This is normally only
280          * available if the input files is stored on a local filesystem.
281          */
282         public bool IsPreparsed
283         {
284             get
285             {
286                 int preparsed = LibVLC.MediaIsPreparsed (Handle, ex);
287                 Raise ();
288                 return preparsed != 0;
289             }
290         }
291
292         public delegate void PreparseChange (Media media, bool preparsed);
293         public event PreparseChange PreparseChanged;
294         private void PreparseCallback (MediaPreparseEvent ev, IntPtr data)
295         {
296             if (PreparseChanged != null)
297                 PreparseChanged (this, ev.status != 0);
298         }
299     };
300 };