summaryrefslogtreecommitdiff
path: root/samples/BasicTutorial12.cs
blob: 5eb572a2fd11da149971b3bcf7f7cf7b90266f3b (plain)
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
// Authors
//   Copyright (C) 2014 Stephan Sundermann <stephansundermann@gmail.com>

using System;
using Gst;
using System.Runtime.InteropServices;

namespace GstreamerSharp
{
	class Playback
	{
		static bool IsLive;
		static Element Pipeline;
		static GLib.MainLoop MainLoop;

		public static void Main (string[] args)
		{
			// Initialize GStreamer
			Application.Init (ref args);

			// Build the pipeline
			Pipeline = Parse.Launch ("playbin uri=http://download.blender.org/durian/trailer/sintel_trailer-1080p.mp4");
			var bus = Pipeline.Bus;

			// Start playing
			var ret = Pipeline.SetState (State.Playing);
			if (ret == StateChangeReturn.Failure) {
				Console.WriteLine ("Unable to set the pipeline to the playing state.");
				return;
			} else if (ret == StateChangeReturn.NoPreroll) {
				IsLive = true;
			}

			MainLoop = new GLib.MainLoop ();

			bus.AddSignalWatch ();
			bus.Message += HandleMessage;

			MainLoop.Run ();

			// Free resources
			Pipeline.SetState (State.Null);
		}

		static void HandleMessage (object o, MessageArgs args)
		{
			var msg = args.Message;
			switch (msg.Type) {
			case MessageType.Error: {
					GLib.GException err;
					string debug;

					msg.ParseError (out err, out debug);
					Console.WriteLine ("Error: {0}", err.Message);

					Pipeline.SetState (State.Ready);
					MainLoop.Quit ();
					break;
				}
			case MessageType.Eos:
				// end-of-stream
				Pipeline.SetState (State.Ready);
				MainLoop.Quit ();
				break;
			case MessageType.Buffering: {
					int percent = 0;

					// If the stream is live, we do not care about buffering.
					if (IsLive) break;

					percent = msg.ParseBuffering ();
					Console.WriteLine ("Buffering ({0})", percent);
					// Wait until buffering is complete before start/resume playing
					if (percent < 100)
						Pipeline.SetState (State.Paused);
					else
						Pipeline.SetState (State.Playing);
					break;
				}
			case MessageType.ClockLost:
				// Get a new clock
				Pipeline.SetState (State.Paused);
				Pipeline.SetState (State.Playing);
				break;
			default:
				// Unhandled message
				break;
			}
		}
	}
}