3 * @brief Media descriptor class
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. *
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. *
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 **********************************************************************/
25 //using System.Collections.Generic;
26 using System.Runtime.InteropServices;
28 namespace VideoLAN.LibVLC
31 * @brief MediaHandle: unmanaged LibVLC media pointer
34 internal sealed class MediaHandle : NonNullHandle
37 * NonNullHandle.Destroy
39 protected override void Destroy ()
41 LibVLC.MediaRelease (handle);
46 * @brief MetaType: type of a media meta-information entry
70 * @brief State: media/player state
72 * Media and Player objects are always in one of these state.
73 * @see Media::State and @see Player::State.
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 */
88 [StructLayout (LayoutKind.Sequential)]
89 internal sealed class MediaMetaEvent : GenericEvent
91 public MetaType metaType;
93 internal delegate void MediaMetaCallback (MediaMetaEvent e, IntPtr d);
95 /*[StructLayout (LayoutKind.Sequential)]
96 internal sealed class MediaSubitemEvent : GenericEvent
98 public IntPtr child; -- MediaHandle
101 [StructLayout (LayoutKind.Sequential)]
102 internal sealed class MediaDurationEvent : GenericEvent
104 public long duration;
106 internal delegate void MediaDurationCallback (MediaDurationEvent e,
109 [StructLayout (LayoutKind.Sequential)]
110 internal sealed class MediaPreparseEvent : GenericEvent
114 internal delegate void MediaPreparseCallback (MediaPreparseEvent e,
117 /* media_freed -> bad idea w.r.t. the GC */
119 [StructLayout (LayoutKind.Sequential)]
120 internal sealed class MediaStateEvent : GenericEvent
124 internal delegate void MediaStateCallback (MediaStateEvent e, IntPtr d);
127 * @brief Media: a source media
129 * Each media object represents an input media, such as a file or an URL.
131 public class Media : EventingObject, ICloneable
133 internal MediaHandle Handle
137 return handle as MediaHandle;
142 * Creates a Media object.
144 * @param instance VLC instance
145 * @param mrl Media Resource Locator (file path or URL)
147 public Media (VLC instance, string mrl)
149 U8String umrl = new U8String (mrl);
151 handle = LibVLC.MediaCreate (instance.Handle, umrl, ex);
156 private Media (MediaHandle handle)
158 this.handle = handle;
163 * Duplicates a media object.
165 public object Clone ()
167 return new Media (LibVLC.MediaDuplicate (Handle));
170 private void Attach ()
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));
185 * Add VLC input item options to the media.
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);
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
198 public void AddOptions (string options, bool trusted)
200 U8String uopts = new U8String (options);
203 LibVLC.MediaAddOption (Handle, uopts, ex);
205 LibVLC.MediaAddUntrustedOption (Handle, uopts, ex);
210 * The media location (file path, URL, ...).
213 public string Location
217 StringHandle str = LibVLC.MediaGetMRL (Handle, ex);
219 return str.Transform ();
223 public override string ToString ()
229 * @param type meta data type
230 * @return the meta data value, or @a null if unknown
232 public string GetMeta (MetaType type)
234 StringHandle str = LibVLC.MediaGetMeta (Handle, type, ex);
236 return str.Transform ();
239 public delegate void MetaChange (Media media, MetaType type);
240 public event MetaChange MetaChanged;
241 private void MetaCallback (MediaMetaEvent ev, IntPtr data)
243 if (MetaChanged != null)
244 MetaChanged (this, ev.metaType);
248 * Current state of the media.
254 State ret = LibVLC.MediaGetState (Handle, ex);
260 public delegate void StateChange (Media media, State state);
261 public event StateChange StateChanged;
262 private void StateCallback (MediaStateEvent ev, IntPtr data)
264 if (StateChanged != null)
265 StateChanged (this, ev.state);
268 internal override EventManagerHandle GetManager ()
270 return LibVLC.MediaEventManager (Handle, null);
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).
282 long duration = LibVLC.MediaGetDuration (Handle, ex);
288 public delegate void DurationChange (Media media, long duration);
289 public event DurationChange DurationChanged;
290 private void DurationCallback (MediaDurationEvent ev, IntPtr data)
292 if (DurationChanged != null)
293 DurationChanged (this, ev.duration);
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.
301 public bool IsPreparsed
305 int preparsed = LibVLC.MediaIsPreparsed (Handle, ex);
307 return preparsed != 0;
311 public delegate void PreparseChange (Media media, bool preparsed);
312 public event PreparseChange PreparseChanged;
313 private void PreparseCallback (MediaPreparseEvent ev, IntPtr data)
315 if (PreparseChanged != null)
316 PreparseChanged (this, ev.status != 0);