File: GstVideoSplitter.cs

package info (click to toggle)
longomatch 0.16.8%2Bgit20110626-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,272 kB
  • sloc: cs: 20,332; sh: 10,410; ansic: 7,628; makefile: 318; xml: 302
file content (433 lines) | stat: -rw-r--r-- 12,172 bytes parent folder | download
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
// GstVideoSplitter.cs
//
//  Copyright (C) 2007-2009 Andoni Morales Alastruey
//
// This program 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.
//
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
//
//

namespace LongoMatch.Video.Editor {

	using System;
	using System.Collections;
	using System.Runtime.InteropServices;
	using LongoMatch.Video.Common;


	public class GstVideoSplitter : GLib.Object, IVideoEditor, IVideoSplitter {

		[DllImport("libcesarplayer.dll")]
		static extern unsafe IntPtr gst_video_editor_new(out IntPtr err);

		public event ProgressHandler Progress;
		
		public unsafe GstVideoSplitter () : base (IntPtr.Zero)
		{
			if (GetType () != typeof (GstVideoSplitter)) {
				throw new InvalidOperationException ("Can't override this constructor.");
			}
			IntPtr error = IntPtr.Zero;
			Raw = gst_video_editor_new(out error);
			if (error != IntPtr.Zero) throw new GLib.GException (error);
			PercentCompleted += delegate(object o, PercentCompletedArgs args) {
				if (Progress!= null)
					Progress (args.Percent);
			};
		}

		#region Properties
		
		[GLib.Property ("enable-audio")]
		public bool EnableAudio {
			get {
				GLib.Value val = GetProperty ("enable-audio");
				bool ret = (bool) val;
				val.Dispose ();
				return ret;
			}
			set {
				GLib.Value val = new GLib.Value(value);
				SetProperty("enable-audio", val);
				val.Dispose ();
			}
		}
		
		[GLib.Property ("enable-title")]
		public bool EnableTitle {
			get {
				GLib.Value val = GetProperty ("enable-title");
				bool ret = (bool) val;
				val.Dispose ();
				return ret;
			}
			set {
				GLib.Value val = new GLib.Value(value);
				SetProperty("enable-title", val);
				val.Dispose ();
			}
		}
		
		[GLib.Property ("video_bitrate")]
		public int VideoBitrate {
			get {
				GLib.Value val = GetProperty ("video_bitrate");
				int ret = (int) val;
				val.Dispose ();
				return ret;
			}
			set {
				GLib.Value val = new GLib.Value(value);
				SetProperty("video_bitrate", val);
				val.Dispose ();
			}
		}

		[GLib.Property ("audio_bitrate")]
		public int AudioBitrate {
			get {
				GLib.Value val = GetProperty ("audio_bitrate");
				int ret = (int) val;
				val.Dispose ();
				return ret;
			}
			set {
				GLib.Value val = new GLib.Value(value);
				SetProperty("audio_bitrate", val);
				val.Dispose ();
			}
		}
		
		[GLib.Property ("width")]
		public int Width {
			get {
				GLib.Value val = GetProperty ("width");
				int ret = (int) val;
				val.Dispose ();
				return ret;
			}
			set {
				GLib.Value val = new GLib.Value(value);
				SetProperty("width", val);
				val.Dispose ();
			}
		}
		
		[GLib.Property ("height")]
		public int Height {
			get {
				GLib.Value val = GetProperty ("height");
				int ret = (int) val;
				val.Dispose ();
				return ret;
			}
			set {
				GLib.Value val = new GLib.Value(value);
				SetProperty("height", val);
				val.Dispose ();
			}
		}
		
		[GLib.Property ("output_file")]
		public string OutputFile {
			get {
				GLib.Value val = GetProperty ("output_file");
				string ret = (string) val;
				val.Dispose ();
				return ret;
			}
			set {
				GLib.Value val = new GLib.Value(value);				
				SetProperty("output_file", val);
				val.Dispose ();
			}
		}
		
		#endregion

		
		
		#region GSignals
#pragma warning disable 0169
		[GLib.CDeclCallback]
		delegate void ErrorVMDelegate (IntPtr gvc, IntPtr message);

		static ErrorVMDelegate ErrorVMCallback;

		static void error_cb (IntPtr gvc, IntPtr message)
		{
			try {
				GstVideoSplitter gvc_managed = GLib.Object.GetObject (gvc, false) as GstVideoSplitter;
				gvc_managed.OnError (GLib.Marshaller.Utf8PtrToString (message));
			} catch (Exception e) {
				GLib.ExceptionManager.RaiseUnhandledException (e, false);
			}
		}

		private static void OverrideError (GLib.GType gtype)
		{
			if (ErrorVMCallback == null)
				ErrorVMCallback = new ErrorVMDelegate (error_cb);
			OverrideVirtualMethod (gtype, "error", ErrorVMCallback);
		}

		[GLib.DefaultSignalHandler(Type=typeof(LongoMatch.Video.Editor.GstVideoSplitter), ConnectionMethod="OverrideError")]
		protected virtual void OnError (string message)
		{
			GLib.Value ret = GLib.Value.Empty;
			GLib.ValueArray inst_and_params = new GLib.ValueArray (2);
			GLib.Value[] vals = new GLib.Value [2];
			vals [0] = new GLib.Value (this);
			inst_and_params.Append (vals [0]);
			vals [1] = new GLib.Value (message);
			inst_and_params.Append (vals [1]);
			g_signal_chain_from_overridden (inst_and_params.ArrayPtr, ref ret);
			foreach (GLib.Value v in vals)
				v.Dispose ();
		}

		[GLib.Signal("error")]
		public event ErrorHandler Error {
			add {
				GLib.Signal sig = GLib.Signal.Lookup (this, "error", typeof (ErrorArgs));
				sig.AddDelegate (value);
			}
			remove {
				GLib.Signal sig = GLib.Signal.Lookup (this, "error", typeof (ErrorArgs));
				sig.RemoveDelegate (value);
			}
		}
		

		[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
		delegate void PercentCompletedVMDelegate (IntPtr gvc, float percent);

		static PercentCompletedVMDelegate PercentCompletedVMCallback;

		static void percentcompleted_cb (IntPtr gvc, float percent)
		{
			try {
				GstVideoSplitter gvc_managed = GLib.Object.GetObject (gvc, false) as GstVideoSplitter;
				gvc_managed.OnPercentCompleted (percent);
			} catch (Exception e) {
				GLib.ExceptionManager.RaiseUnhandledException (e, false);
			}
		}

		private static void OverridePercentCompleted (GLib.GType gtype)
		{
			if (PercentCompletedVMCallback == null)
				PercentCompletedVMCallback = new PercentCompletedVMDelegate (percentcompleted_cb);
			OverrideVirtualMethod (gtype, "percent_completed", PercentCompletedVMCallback);
		}

		[GLib.DefaultSignalHandler(Type=typeof(LongoMatch.Video.Editor.GstVideoSplitter), ConnectionMethod="OverridePercentCompleted")]
		protected virtual void OnPercentCompleted (float percent)
		{
			GLib.Value ret = GLib.Value.Empty;
			GLib.ValueArray inst_and_params = new GLib.ValueArray (2);
			GLib.Value[] vals = new GLib.Value [2];
			vals [0] = new GLib.Value (this);
			inst_and_params.Append (vals [0]);
			vals [1] = new GLib.Value (percent);
			inst_and_params.Append (vals [1]);
			g_signal_chain_from_overridden (inst_and_params.ArrayPtr, ref ret);
			foreach (GLib.Value v in vals)
				v.Dispose ();
		}

		[GLib.Signal("percent_completed")]
		public event PercentCompletedHandler PercentCompleted {
			add {
				GLib.Signal sig = GLib.Signal.Lookup (this, "percent_completed", typeof (PercentCompletedArgs));
				sig.AddDelegate (value);
			}
			remove {
				GLib.Signal sig = GLib.Signal.Lookup (this, "percent_completed", typeof (PercentCompletedArgs));
				sig.RemoveDelegate (value);
			}
		}
#pragma warning restore 0169
		#endregion
		
		#region Public Methods

		[DllImport("libcesarplayer.dll")]
		static extern IntPtr gst_video_editor_get_type();

		public static new GLib.GType GType { 
			get {
				IntPtr raw_ret = gst_video_editor_get_type();
				GLib.GType ret = new GLib.GType(raw_ret);
				return ret;
			}
		}
		
		

		[DllImport("libcesarplayer.dll")]
		static extern void gst_video_editor_clear_segments_list(IntPtr raw);

		public void ClearList() {
			gst_video_editor_clear_segments_list(Handle);
		}
		
		[DllImport("libcesarplayer.dll")]
		static extern void gst_video_editor_add_segment(IntPtr raw, string file_path, long start, long duration, double rate, IntPtr title, bool hasAudio);

		public void AddSegment(string filePath, long start, long duration, double rate, string title, bool hasAudio) {
			if (Environment.OSVersion.Platform == PlatformID.Win32NT)
				filePath="file:///"+filePath;
			gst_video_editor_add_segment(Handle, filePath, start, duration, rate, GLib.Marshaller.StringToPtrGStrdup(title), hasAudio);
		}
		
		
		[DllImport("libcesarplayer.dll")]
		static extern void gst_video_editor_start(IntPtr raw);

		public void Start() {
			gst_video_editor_start(Handle);
		}
		
		[DllImport("libcesarplayer.dll")]
		static extern void gst_video_editor_cancel(IntPtr raw);

		public void Cancel() {
			// The handle might have already been dealocated
			try{
				gst_video_editor_cancel(Handle);
			}catch{
			}
		}
		
		[DllImport("libcesarplayer.dll")]
		static extern void gst_video_editor_set_video_encoder(IntPtr raw, out IntPtr error_ptr, int type);

		public void SetVideoEncoder(out string error, VideoEncoderType codec) {
			IntPtr error_ptr = IntPtr.Zero;
			gst_video_editor_set_video_encoder(Handle,out error_ptr,(int)codec);
			if (error_ptr != IntPtr.Zero)
				error = GLib.Marshaller.Utf8PtrToString(error_ptr);
			else
				error = null;
		}
		
		[DllImport("libcesarplayer.dll")]
		static extern void gst_video_editor_set_audio_encoder(IntPtr raw, out IntPtr error_ptr, int type);

		public void SetAudioEncoder(out string error, AudioEncoderType codec) {
			IntPtr error_ptr = IntPtr.Zero;
			gst_video_editor_set_audio_encoder(Handle,out error_ptr,(int)codec);
			if (error_ptr != IntPtr.Zero)
				error = GLib.Marshaller.Utf8PtrToString(error_ptr);
			else
				error = null;
		}
		
		[DllImport("libcesarplayer.dll")]
		static extern void gst_video_editor_set_video_muxer(IntPtr raw, out IntPtr error_ptr, int type);

		public void SetVideoMuxer(out string error, VideoMuxerType muxer) {
			IntPtr error_ptr = IntPtr.Zero;
			gst_video_editor_set_video_muxer(Handle,out error_ptr,(int)muxer);
			if (error_ptr != IntPtr.Zero)
				error = GLib.Marshaller.Utf8PtrToString(error_ptr);
			else
				error = null;
		}

		[DllImport("libcesarplayer.dll")]
		static extern void gst_video_editor_init_backend(out int argc, IntPtr argv);

		public static int InitBackend(string argv) {
			int argc;
			gst_video_editor_init_backend(out argc, GLib.Marshaller.StringToPtrGStrdup(argv));
			return argc;
		}
		
		
		public void SetSegment (string filePath, long start, long duration, double rate, string title, bool hasAudio){
			ClearList();
			AddSegment(filePath, start, duration, rate, title,hasAudio);
		}
		
		public VideoQuality VideoQuality{
			set{VideoBitrate=(int)value;}
		}
		
		public AudioQuality AudioQuality{
			set{AudioBitrate = (int)value;}
		}
		
		public VideoFormat VideoFormat{
			set{
				if (value == VideoFormat.PORTABLE){
					Height = 240;
					Width = 320;
				}
				else if (value == VideoFormat.VGA){
					Height = 480 ;
					Width = 640;
				}
				else if (value == VideoFormat.TV){
					Height = 576;
					Width = 720;
				}
				else if (value == VideoFormat.HD720p){
					Height = 720;
					Width = 1280;
				}
				else if (value == VideoFormat.HD1080p){
					Height = 1080;
					Width = 1920;
				}
			}
		}
		
		public AudioEncoderType AudioEncoder{
			set{
				string error;
				SetAudioEncoder(out error,value);
				if (error != null)
					throw new Exception(error);
			}
		}
		
		public VideoEncoderType VideoEncoder{
			set{
				string error;
				SetVideoEncoder(out error, value);
				if (error != null)
					throw new Exception(error);
			}
		}
		
		public VideoMuxerType VideoMuxer{
			set{
				string error;
				SetVideoMuxer(out error,value);
				if (error != null)
					throw new Exception(error);
			}
		}
		
		public string TempDir{
			set{;}
		}
		
		#endregion

		
	}
}