Release 1.22.2
[platform/upstream/gstreamer.git] / subprojects / gstreamer-sharp / samples / BasicTutorial12.cs
1 // Authors
2 //   Copyright (C) 2014 Stephan Sundermann <stephansundermann@gmail.com>
3
4 using System;
5 using Gst;
6 using System.Runtime.InteropServices;
7
8 namespace GstreamerSharp
9 {
10         class Playback
11         {
12                 static bool IsLive;
13                 static Element Pipeline;
14                 static GLib.MainLoop MainLoop;
15
16                 public static void Main (string[] args)
17                 {
18                         // Initialize GStreamer
19                         Application.Init (ref args);
20
21                         // Build the pipeline
22                         Pipeline = Parse.Launch ("playbin uri=http://download.blender.org/durian/trailer/sintel_trailer-1080p.mp4");
23                         var bus = Pipeline.Bus;
24
25                         // Start playing
26                         var ret = Pipeline.SetState (State.Playing);
27                         if (ret == StateChangeReturn.Failure) {
28                                 Console.WriteLine ("Unable to set the pipeline to the playing state.");
29                                 return;
30                         } else if (ret == StateChangeReturn.NoPreroll) {
31                                 IsLive = true;
32                         }
33
34                         MainLoop = new GLib.MainLoop ();
35
36                         bus.AddSignalWatch ();
37                         bus.Message += HandleMessage;
38
39                         MainLoop.Run ();
40
41                         // Free resources
42                         Pipeline.SetState (State.Null);
43                 }
44
45                 static void HandleMessage (object o, MessageArgs args)
46                 {
47                         var msg = args.Message;
48                         switch (msg.Type) {
49                         case MessageType.Error: {
50                                         GLib.GException err;
51                                         string debug;
52
53                                         msg.ParseError (out err, out debug);
54                                         Console.WriteLine ("Error: {0}", err.Message);
55
56                                         Pipeline.SetState (State.Ready);
57                                         MainLoop.Quit ();
58                                         break;
59                                 }
60                         case MessageType.Eos:
61                                 // end-of-stream
62                                 Pipeline.SetState (State.Ready);
63                                 MainLoop.Quit ();
64                                 break;
65                         case MessageType.Buffering: {
66                                         int percent = 0;
67
68                                         // If the stream is live, we do not care about buffering.
69                                         if (IsLive) break;
70
71                                         percent = msg.ParseBuffering ();
72                                         Console.WriteLine ("Buffering ({0})", percent);
73                                         // Wait until buffering is complete before start/resume playing
74                                         if (percent < 100)
75                                                 Pipeline.SetState (State.Paused);
76                                         else
77                                                 Pipeline.SetState (State.Playing);
78                                         break;
79                                 }
80                         case MessageType.ClockLost:
81                                 // Get a new clock
82                                 Pipeline.SetState (State.Paused);
83                                 Pipeline.SetState (State.Playing);
84                                 break;
85                         default:
86                                 // Unhandled message
87                                 break;
88                         }
89                 }
90         }
91 }