]> git.sesse.net Git - vlc/blob - bindings/cil/src/media.cs
Add media and player state 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 State: media/player state
47      *
48      * Media and Player objects are always in one of these state.
49      * @see Media::State and @see Player::State.
50      */
51     public enum State
52     {
53         NothingSpecial, /**< Nothing going on */
54         Opening, /**< Being opened */
55         Buffering, /**< Buffering before play */
56         Playing, /**< Playing */
57         Paused, /**< Paused */
58         Stopped, /**< Stopped */
59         Ended, /**< Played until the end */
60         Error, /**< Failed */
61     };
62
63     /**
64      * @brief Media: a source media
65      * @ingroup API
66      * Each media object represents an input media, such as a file or an URL.
67      */
68     public class Media : BaseObject, ICloneable
69     {
70         internal MediaHandle Handle
71         {
72             get
73             {
74                 return handle as MediaHandle;
75             }
76         }
77
78         /**
79          * Creates a Media object.
80          *
81          * @param instance VLC instance
82          * @param mrl Media Resource Locator (file path or URL)
83          */
84         public Media (VLC instance, string mrl)
85         {
86             U8String umrl = new U8String (mrl);
87
88             handle = LibVLC.MediaCreate (instance.Handle, umrl, ex);
89             Raise ();
90         }
91
92         /**
93          * Add VLC input item options to the media.
94          * @param options VLC options in VLC input item format
95          *                (see example below)
96          * @param trusted whether the options are set by a trusted agent
97          *                (e.g. the local computer configuration) or not
98          *                (e.g. a downloaded file).
99          * @code
100          * Media m = new Media(vlc, "http://www.example.com/music.ogg");
101          * m.AddOptions(":http-user-agent=LibVLC.Net "
102          *            + ":http-proxy=proxy:8080", true);
103          * @endcode
104          */
105         public void AddOptions (string options, bool trusted)
106         {
107             U8String uopts = new U8String (options);
108
109             if (trusted)
110                 LibVLC.MediaAddOption (Handle, uopts, ex);
111             else
112                 LibVLC.MediaAddUntrustedOption (Handle, uopts, ex);
113             Raise ();
114         }
115
116         /**
117          * The media location (file path, URL, ...).
118          */
119         public string Location
120         {
121             get
122             {
123                 StringHandle str = LibVLC.MediaGetMRL (Handle, ex);
124                 Raise ();
125                 return str.Transform ();
126             }
127         }
128
129         private Media (MediaHandle handle)
130         {
131             this.handle = handle;
132         }
133
134         /**
135          * Duplicates a media object.
136          */
137         public object Clone ()
138         {
139             return new Media (LibVLC.MediaDuplicate (Handle));
140         }
141
142         /**
143          * Current state of the media.
144          */
145         public State State
146         {
147             get
148             {
149                 State ret = LibVLC.MediaGetState (Handle, ex);
150                 Raise ();
151                 return ret;
152             }
153         }
154
155         /**
156          * Duration of the media in microseconds. The precision of the result
157          * depends on the input stram protocol and file format. The value
158          * might be incorrect and unknown (VLC usually returns 0 or -1 then).
159          */
160         public long Duration
161         {
162             get
163             {
164                 long duration = LibVLC.MediaGetDuration (Handle, ex);
165                 Raise ();
166                 return duration;
167             }
168         }
169
170         /**
171          * Whether the media was "preparsed". If true, the meta-infos were
172          * extracted, even before the media was played. This is normally only
173          * available if the input files is stored on a local filesystem.
174          */
175         public bool IsPreparsed
176         {
177             get
178             {
179                 int preparsed = LibVLC.MediaIsPreparsed (Handle, ex);
180                 Raise ();
181                 return preparsed != 0;
182             }
183         }
184     };
185 };