]> git.sesse.net Git - vlc/blob - bindings/cil/src/media.cs
Media meta data support
[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          * @param type meta data type
230          * @return the meta data value, or @a null if unknown
231          */
232         public string GetMeta (MetaType type)
233         {
234             StringHandle str = LibVLC.MediaGetMeta (Handle, type, ex);
235             Raise ();
236             return str.Transform ();
237         }
238
239         public delegate void MetaChange (Media media, MetaType type);
240         public event MetaChange MetaChanged;
241         private void MetaCallback (MediaMetaEvent ev, IntPtr data)
242         {
243             if (MetaChanged != null)
244                 MetaChanged (this, ev.metaType);
245         }
246
247         /**
248          * Current state of the media.
249          */
250         public State State
251         {
252             get
253             {
254                 State ret = LibVLC.MediaGetState (Handle, ex);
255                 Raise ();
256                 return ret;
257             }
258         }
259
260         public delegate void StateChange (Media media, State state);
261         public event StateChange StateChanged;
262         private void StateCallback (MediaStateEvent ev, IntPtr data)
263         {
264             if (StateChanged != null)
265                 StateChanged (this, ev.state);
266         }
267
268         internal override EventManagerHandle GetManager ()
269         {
270             return LibVLC.MediaEventManager (Handle, null);
271         }
272
273         /**
274          * Duration of the media in microseconds. The precision of the result
275          * depends on the input stram protocol and file format. The value
276          * might be incorrect and unknown (VLC usually returns 0 or -1 then).
277          */
278         public long Duration
279         {
280             get
281             {
282                 long duration = LibVLC.MediaGetDuration (Handle, ex);
283                 Raise ();
284                 return duration;
285             }
286         }
287
288         public delegate void DurationChange (Media media, long duration);
289         public event DurationChange DurationChanged;
290         private void DurationCallback (MediaDurationEvent ev, IntPtr data)
291         {
292             if (DurationChanged != null)
293                 DurationChanged (this, ev.duration);
294         }
295
296         /**
297          * Whether the media was "preparsed". If true, the meta-infos were
298          * extracted, even before the media was played. This is normally only
299          * available if the input files is stored on a local filesystem.
300          */
301         public bool IsPreparsed
302         {
303             get
304             {
305                 int preparsed = LibVLC.MediaIsPreparsed (Handle, ex);
306                 Raise ();
307                 return preparsed != 0;
308             }
309         }
310
311         public delegate void PreparseChange (Media media, bool preparsed);
312         public event PreparseChange PreparseChanged;
313         private void PreparseCallback (MediaPreparseEvent ev, IntPtr data)
314         {
315             if (PreparseChanged != null)
316                 PreparseChanged (this, ev.status != 0);
317         }
318     };
319 };