File: gst-play-video.cs

package info (click to toggle)
clutter-sharp 1.0.0~alpha3~git20090817.r1.349dba6-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 4,104 kB
  • ctags: 2,193
  • sloc: xml: 23,456; cs: 9,946; sh: 3,393; perl: 1,213; makefile: 270; awk: 50; sed: 13
file content (304 lines) | stat: -rw-r--r-- 6,614 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
using System;
using Clutter;

public class VideoApp 
{
	const int SEEK_H = 20;
	const int SEEK_W = 690;

	#region Fields
	GstVideoTexture vtexture;

	Group control;
	Texture control_bg;
	Texture control_play;
	Texture control_pause;
	Rectangle control_seek1;
	Rectangle control_seek2;
	Actor control_seekbar;
	Label control_label;

	bool controls_showing;
	bool paused;

	uint controls_timeout;

	Timeline controls_tl;
	Timeline effect1_tl;
	#endregion

	void HandleControlTlNewFrame (object o, NewFrameArgs args)
	{
		control.ShowAll ();
		
		if (paused) {
		 	control_pause.Hide ();
			control_play.Show ();
		}
		else {
			control_play.Hide ();
		 	control_pause.Show ();
		}

		uint opacity = (uint)((args.FrameNum * 0xde) / controls_tl.NumFrames);

		if (!controls_showing)
		 	opacity = 0xde - opacity;

		control.Opacity = (byte)opacity;
	}

	void HandleControlTimelineCompleted (object o, System.EventArgs args)
	{
		if (!controls_showing)
		 	control.HideAll (); 

		controls_timeout = 0;
	}

	void HandleEffect1TlNewFrame (object o, NewFrameArgs args)
	{
		vtexture.SetRotation (RotateAxis.YAxis,
				      (float)args.FrameNum * 12,
				      (int)Stage.Default.Width / 2,
				      0,
				      0); 
	}

	bool HandleControlsTimeout ()
	{
		ShowControls (false);
		return false; 
	}

	void ShowControls (bool vis)
	{
		if (controls_tl.IsPlaying)
		 	return;
			
		if (vis && !controls_showing) {
			controls_showing = true;
			
			controls_tl.Start ();
			
			controls_timeout = GLib.Timeout.Add (5000, new GLib.TimeoutHandler (HandleControlsTimeout));
			return;
		}

		if (vis && controls_showing) {
		 	if (controls_timeout != 0) {
				GLib.Source.Remove (controls_timeout); 
				controls_timeout = GLib.Timeout.Add (5000, new GLib.TimeoutHandler (HandleControlsTimeout));
			}
			return;
		}

		if (!vis && controls_showing) {
			controls_showing = false;
			controls_tl.Start (); 
		}
	}

	void TogglePauseState ()
	{
		if (paused) {
			vtexture.Playing = true;
			paused = false;
			
			control_play.Hide ();
			control_pause.Show (); 
		} 
		else {
			vtexture.Playing = false;
			paused = true;

			control_pause.Hide ();
			control_play.Show ();
		}
	}

	void HandleSizeChange (object o, SizeChangeArgs args)
	{
		Stage stage = Stage.Default;
		int new_x = 0;
		int new_y = 0;

		int new_width = 0;
		int new_height = (args.Height * (int)stage.Width) / args.Width;

		if (new_height <= stage.Height) {
			new_width = (int)stage.Width;

			new_x = 0;
			new_y = ((int)stage.Height - new_height) / 2;		 
		}
		else {
			new_width = (args.Width * (int)stage.Height) / args.Height;
			
			new_x = ((int)stage.Width - new_width) / 2;
			new_y = 0;
		}

		vtexture.SetPosition (new_x, new_y);
		vtexture.SetSize (new_width, new_height);
	}

	bool Tick ()
	{
		int position = vtexture.Position;
		int duration = vtexture.Duration;

		if (position == 0 || duration == 0)
		 	return true;
			
		control_seekbar.SetSize ((position * SEEK_W) / duration, SEEK_H);

		return true;
	}

	void HandleButtonPressEvent (object o, ButtonPressEventArgs args) 
	{
		if (!controls_showing)
		 	return;
			
		Actor actor = Stage.Default.GetActorAtPos (args.Event.X, args.Event.Y);

		if (actor == control_pause || actor == control_play) {
			TogglePauseState ();
			return; 
		}

		if (actor == control_seek1 
		 	|| actor == control_seek2
			|| actor == control_seekbar) {
		 	int x;
			int y;

		 	control_seekbar.GetTransformedPosition (out x, out y);

			int dist = args.Event.X - x;

			dist = Math.Max (dist, 0);
			dist = Math.Min (dist, SEEK_W);

			int pos = (dist * vtexture.Duration) / SEEK_W;

			vtexture.Position = pos;
		}
	}

	void HandleKeyReleaseEvent (object o, KeyReleaseEventArgs args)
	{
		uint symbol = args.Event.Symbol;

		Gdk.Key key = (Gdk.Key) Enum.Parse (typeof(Gdk.Key), symbol.ToString ());

		switch (key) {
			case Gdk.Key.q:
			case Gdk.Key.Escape:
				Clutter.Main.Quit ();
				break;
			case Gdk.Key.e:
				if (!effect1_tl.IsPlaying)
				 	effect1_tl.Start ();
				break;
			default:
				TogglePauseState ();
				break;
		}
	}

	public VideoApp (string filename)
	{
		Stage stage = Stage.Default;
		stage.Color = new Color (0x00, 0x00, 0x00, 0x00);
		stage.SetSize (1200, 1024);

		vtexture = new GstVideoTexture ();
		vtexture.SyncSize = false;

		// add size changed call back here
		vtexture.SizeChange += HandleSizeChange;

		vtexture.Filename = filename;
		control = new Group ();

		// panel
		control_bg = new Texture ("vid-panel.png");

		// play button
		control_play = new Texture ("media-actions-start.png");

		// pause button
		control_pause = new Texture ("media-actions-pause.png");

		control_seek1 = new Rectangle (new Color (73, 74, 77, 0xee));
		control_seek2 = new Rectangle (new Color (0xcc, 0xcc, 0xcc, 0xff));
		control_seekbar = new Rectangle (new Color (73, 74, 77, 0xee));
		control_seekbar.Opacity = 0x99;

		// label with name
		string text = System.IO.Path.GetFileName (filename);
		control_label = new Label ("Sans Bold 24", text);
		control_label.Color = new Color (73, 74, 77, 0xee);

		control.AddActor (control_bg);
		control.AddActor (control_play);
 
		control.AddActor (control_pause);
		control.AddActor (control_seek1);
		control.AddActor (control_seek2);
		control.AddActor (control_seekbar);
		control.AddActor (control_label);

		control.Opacity = 0xee;
		control_play.SetPosition (30, 30);
		control_pause.SetPosition (30, 30);

		control_seek1.SetSize (SEEK_W + 10, SEEK_H + 10);
		control_seek1.SetPosition (200, 100);

		control_seek2.SetSize (SEEK_W, SEEK_H);
		control_seek2.SetPosition (205, 105);

		control_seekbar.SetSize (0, SEEK_H);
		control_seekbar.SetPosition (205, 105);

		control_label.SetPosition (200, 40);

		stage.AddActor (vtexture);
		stage.AddActor (control);

		int x = (int)((stage.Width - control.Width) / 2);
		int y = (int)(stage.Height - (stage.Height / 3));

		control.SetPosition (x, y);

		controls_tl = new Timeline (10, 30);
		controls_tl.NewFrame += HandleControlTlNewFrame;
		controls_tl.Completed += HandleControlTimelineCompleted;

		effect1_tl = new Timeline (30, 90);
		effect1_tl.NewFrame += HandleEffect1TlNewFrame;

		GLib.Timeout.Add (1000, new GLib.TimeoutHandler (Tick));
		stage.MotionEvent += delegate { ShowControls (true); };
		stage.ButtonPressEvent += HandleButtonPressEvent;
		stage.KeyReleaseEvent += HandleKeyReleaseEvent;

		vtexture.Playing = true;
		stage.ShowAll ();
		control_play.Hide ();
	}

	public static void Main (string[] args)
	{
		int arg;
			
		Clutter.GstGlobal.GstInit (out arg, string.Empty);

		VideoApp app = new VideoApp (args[0]);
	
		ClutterRun.Main ();
	}
}