~ubuntu-branches/ubuntu/lucid/gnome-subtitles/lucid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*

	Copyright (c)  Goran Sterjov

    This file is part of the GStreamer Playbin Wrapper.
    Derived from the Dissent Project.

    GStreamer Playbin Wrapper is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    The Dissent Project is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with the Dissent Project; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/



using System;
using System.Runtime.InteropServices;


namespace GStreamer
{
	
	
	// media engine enumerations
	public enum MediaStatus { Playing, Stopped, Paused, Loaded, Unloaded }
	public enum MediaEvents { EndOfStream, Error, Buffering, Volume, Seek }
	
	// media engine event handlers
	public delegate void EngineEventHandler (object o, EngineEventArgs args);
	public delegate void EngineStateHandler (object o, EngineStateArgs args);
	
	
	
	/// <summary>
	/// The GStreamer Playbin.
	/// </summary>
	public class Playbin
	{
		
		// engine callbacks from the C wrapper
		delegate void eosCallback (IntPtr engine);
		delegate void errorCallback (IntPtr engine, string error, string debug);
		delegate void bufferCallback (IntPtr engine, int prog);
		
		eosCallback eos_cb;
		errorCallback error_cb;
		bufferCallback buffer_cb;
		
		
		// declarations
		HandleRef engine;
		MediaStatus status = MediaStatus.Unloaded;
		bool buffer_fin;
		EngineVideoInfo videoInfo = null;
		
		/// <summary>Raised when an event occurs. ie: EndOfStream, Error, etc.</summary>
		public event EngineEventHandler EventChanged;
		
		/// <summary>Raised when the playbin state changes. ie: Playing, Paused, etc.</summary>
		public event EngineStateHandler StateChanged;
		
		/// <summary>
		/// Load the GStreamer library and attach it
		/// to the specified window.
		/// </summary>
		public bool Initiate (ulong x_window_id)
		{
			
			// load the gstreamer library
			IntPtr ptr = gst_binding_init (x_window_id);
			
			if(ptr == IntPtr.Zero)
			{
				throwError ("Failed to load the Gstreamer library");
				return false;
			}
			else engine = new HandleRef (this, ptr);
			
			
			// set up callbacks
			eos_cb = new eosCallback (onEos);
			error_cb = new errorCallback (onError);
			buffer_cb = new bufferCallback (onBuffer);
			
			gst_binding_set_eos_cb (engine, eos_cb);
			gst_binding_set_error_cb (engine, error_cb);
			gst_binding_set_buffer_cb (engine, buffer_cb);
			
			
			status = MediaStatus.Stopped;
			return true;
		}
		
		
		/// <summary>
		/// Load the GStreamer library.
		/// </summary>
		public bool Initiate ()
		{
			return Initiate (0);
		}
		
		
		
		
		
		/// <summary>
		/// Disposes the GStreamer library.
		/// </summary>
		public void Dispose ()
		{
			Stop ();
			gst_binding_deinit (engine);
			ChangeState (MediaStatus.Unloaded);
		}
		
		/// <summary>
		/// Loads the specified path into the GStreamer library.
		/// </summary>
		public bool Load (string uri)
		{
			if (!isStopped) Stop ();
			gst_binding_load (engine, uri);
			//ChangeState (MediaStatus.Loaded);
			ChangeState (MediaStatus.Paused);
			return true;
		}
		
		/// <summary>
		/// Plays the loaded media file.
		/// </summary>
		public void Play ()
		{
			if (!isPlaying && !isUnloaded) {
				gst_binding_play (engine);
				ChangeState (MediaStatus.Playing);
			}
		}
		
		/// <summary>
		/// Pauses the loaded media file.
		/// </summary>
		public void Pause ()
		{
			if (isPlaying) {
				gst_binding_pause (engine);
				ChangeState (MediaStatus.Paused);
			}
		}
		
		/// <summary>
		/// Stops the loaded media file and unloads it.
		/// </summary>
		public void Stop ()
		{
			if (isPlaying || isPaused) {
				gst_binding_unload (engine);
				Seek (0);
				ChangeState (MediaStatus.Stopped);
				videoInfo = null;
			}
		}
		
		/// <summary>
		/// Changes the window to which video playback is attached to.
		/// </summary>
		public void SetWindow (ulong window_id)
		{
			gst_binding_set_xid (engine, window_id);
		}
		
		
		
		/// <summary>
		/// Seeks to the nearest millisecond on the media file.
		/// </summary>
		public void Seek (TimeSpan time)
		{
			if (isUnloaded) return;
			gst_binding_set_position (engine, (ulong) time.TotalMilliseconds);
			ChangeEvent (MediaEvents.Seek, null, 0);
		}
		
		
		/// <summary>
		/// Seeks to the nearest millisecond on the media file.
		/// </summary>
		public void Seek (double milliseconds)
		{
			TimeSpan time = TimeSpan.FromMilliseconds (milliseconds);
			Seek (time);
		}
		
		
		
		
		/// <summary>
		/// Returns the current position that the media file is on.
		/// </summary>
		public TimeSpan CurrentPosition
		{
			get
			{
				if (!isPlaying && !isPaused)
					return TimeSpan.Zero;
				
				double pos = (double) gst_binding_get_position (engine);
				return TimeSpan.FromMilliseconds (pos);
			}
		}
		
		
		/// <summary>
		/// Returns the total duration of the media file.
		/// </summary>
		public TimeSpan Duration
		{
			get{
				if (isUnloaded)
					return TimeSpan.Zero;
				
				double dur = (double) gst_binding_get_duration (engine);
				return TimeSpan.FromMilliseconds (dur);
			}
		}
		
		/// <summary>
		/// Returns information on the video stream, or null if it's not available
		/// </summary>
		public EngineVideoInfo VideoInfo
		{
			get{
				if (isUnloaded)
					return null;
					
				if (videoInfo == null) {
					IntPtr ptr = gst_binding_get_video_info (engine);
					if (ptr == IntPtr.Zero)
						return null;
					else {
						videoInfo = new EngineVideoInfo();
						Marshal.PtrToStructure(ptr, videoInfo);
					}
				}
				return videoInfo;
			}
		}

		/// <summary>
		/// Returns the current volume of the GStreamer library.
		/// </summary>
		public double Volume
		{
			get{ return (double) gst_binding_get_volume (engine); }
			set
			{
				gst_binding_set_volume (engine, (int) value);
				ChangeEvent (MediaEvents.Volume, null, 0);
			}
		}
		
		
		
		/// <summary>
		/// Returns a value determining if the media file is a video file.
		/// </summary>
		public bool HasVideo
		{
			get{ return !isUnloaded ? gst_binding_has_video (engine) : false; }
		}
		
		/// <summary>
		/// Returns the current status of the media engine.
		/// </summary>
		public MediaStatus CurrentStatus
		{
			get { return status; }
		}
		
		
		
		
		// throws an error to the global error handler
		void throwError (string message)
		{
			EngineEventArgs args = new EngineEventArgs (MediaEvents.Error, message, 0);
        	if(EventChanged != null) EventChanged(this, args);
		}
		
		
		// the stream has ended
		void onEos (IntPtr engine)
		{
			ChangeEvent (MediaEvents.EndOfStream, null, 0);
		}
		
		
		// an error in the gstreamer pipeline has occured
		void onError (IntPtr engine, string error, string debug)
		{
			throwError (error + "\n\nDebug Message:\n" + debug);
			ChangeState (MediaStatus.Stopped);
			ChangeEvent (MediaEvents.Error, error, 0);
		}
		
		
		// the gstreamer pipeline is being buffered
		void onBuffer (IntPtr engine, int prog)
		{
			if(!buffer_fin) {
				buffer_fin = prog >= 100;
				ChangeEvent (MediaEvents.Buffering, "Buffering", (double)prog);
			}
		}
		
		
		// throw the media state changed event
		void ChangeState(MediaStatus state)
		{
			status = state;
        	EngineStateArgs args = new EngineStateArgs (state);
        	if(StateChanged != null) StateChanged(this, args);
        }
        
        // throw the media event changed event
        void ChangeEvent(MediaEvents engineEvent, string message, double percent)
        {
        	EngineEventArgs args = new EngineEventArgs (engineEvent, message, percent);
        	if(EventChanged != null) EventChanged(this, args);
        }
		
		
		// private convenience properties
		bool isPlaying { get{ return status == MediaStatus.Playing; } }
		bool isPaused { get{ return status == MediaStatus.Paused; } }
		bool isStopped { get{ return status == MediaStatus.Stopped; } }
		bool isUnloaded { get{ return status == MediaStatus.Unloaded; } }
		
		
		
		// core engine functions
		[DllImport("gstreamer_playbin")]
		static extern IntPtr gst_binding_init (ulong xwin);
		[DllImport("gstreamer_playbin")]
		static extern void gst_binding_deinit (HandleRef play);
		[DllImport("gstreamer_playbin")]
		static extern void gst_binding_load (HandleRef play, string uri);
		[DllImport("gstreamer_playbin")]
		static extern void gst_binding_play (HandleRef play);
		[DllImport("gstreamer_playbin")]
		static extern void gst_binding_pause (HandleRef play);
		[DllImport("gstreamer_playbin")]
		static extern void gst_binding_unload (HandleRef play);
		[DllImport("gstreamer_playbin")]
		static extern void gst_binding_set_xid (HandleRef play, ulong xid);
		
		
		// engine property functions
		[DllImport("gstreamer_playbin")]
		static extern ulong gst_binding_get_duration (HandleRef play);
		[DllImport("gstreamer_playbin")]
		static extern IntPtr gst_binding_get_video_info (HandleRef play);
		[DllImport("gstreamer_playbin")]
		static extern ulong gst_binding_get_position (HandleRef play);
		[DllImport("gstreamer_playbin")]
		static extern int gst_binding_get_volume (HandleRef play);
		[DllImport("gstreamer_playbin")]
		static extern void gst_binding_set_position (HandleRef play, ulong pos);
		[DllImport("gstreamer_playbin")]
		static extern void gst_binding_set_volume (HandleRef play, int vol);
		[DllImport("gstreamer_playbin")]
		static extern bool gst_binding_has_video (HandleRef play);
		
		
		// engine callbacks
		[DllImport("gstreamer_playbin")]
		static extern void gst_binding_set_eos_cb (HandleRef play, eosCallback cb);
		[DllImport("gstreamer_playbin")]
		static extern void gst_binding_set_error_cb (HandleRef play, errorCallback cb);
		[DllImport("gstreamer_playbin")]
		static extern void gst_binding_set_buffer_cb (HandleRef play, bufferCallback cb);
	}
	
	
	
	
	/// <summary>
	/// Arguments for a raised event.
	/// </summary>
	public sealed class EngineEventArgs
	{
		MediaEvents engine_event;
		string message;
		double buffer;
		
		public EngineEventArgs (MediaEvents engine_event, string message, double buffer)
		{
        	this.engine_event = engine_event;
        	this.message = message;
        	this.buffer = buffer;
        }
        
        public MediaEvents Event { get{ return engine_event; } }
        public string Message { get{ return message; } }
        public double BufferingPercent { get{ return buffer; } }
    }
    
    
	
	
	/// <summary>
	/// Arguments for a raised state.
	/// </summary>
    public sealed class EngineStateArgs
    {
    	MediaStatus state;
    	
    	public EngineStateArgs (MediaStatus state)
    	{
    		this.state = state;
    	}
    	
    	public MediaStatus State { get{ return state; } }
    }
    
    [StructLayout(LayoutKind.Sequential)]
    public class EngineVideoInfo
    {
    	int width;
		int height;
    	float frame_rate;
    	
    	public int Width { get{ return width; } }
       	public int Height { get{ return height; } }
       	public float AspectRatio { get { return (float)width/height; } }
    	public float FrameRate { get{ return frame_rate; } }
    	
    	public override string ToString () { return "Width=" + width + ", Height=" + height + ", FrameRate=" + frame_rate; }
    }
	
	
}