From 7698ae88012ce8b8693a3e4d22f15cd62b71db5d Mon Sep 17 00:00:00 2001 From: Maarten Bosmans Date: Sat, 20 Jun 2009 22:55:27 +0200 Subject: [PATCH] Add samples/AppSrc.cs This sample started as a test case for a bug with Buffer.Data, but remains useful as a example of how to use the AppSrc element. --- samples/AppSrc.cs | 103 ++++++++++++++++++++++++++++++++++++++++++++ samples/Makefile.am | 5 ++- 2 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 samples/AppSrc.cs diff --git a/samples/AppSrc.cs b/samples/AppSrc.cs new file mode 100644 index 0000000000..eebefd7353 --- /dev/null +++ b/samples/AppSrc.cs @@ -0,0 +1,103 @@ +/* + * AppSrc.cs: example of using the appsrc element + * + * Authors: + * Maarten Bosmans + * + * Copyright (C) 2009 Maarten Bosmans + * + */ + +using System; +using System.Runtime.InteropServices; +using GLib; +using Gst; +using Cairo; + +public class AppSrcDemo +{ + static MainLoop loop; + static Gst.App.AppSrc appsrc; + static Pipeline pipeline; + + public static void Main (string[] args) + { + Application.Init(); + loop = new MainLoop(); + + // Construct all the elements + pipeline = new Pipeline(); + appsrc = new Gst.App.AppSrc("AppSrcDemo"); + Element color = ElementFactory.Make("ffmpegcolorspace"); + Element sink = ElementFactory.Make("autovideosink"); + + // Link the elements + pipeline.Add(appsrc, color, sink); + Element.Link(appsrc, color, sink); + + // Set the caps on the AppSrc to RGBA, 640x480, 4 fps, square pixels + appsrc.Caps = Gst.Video.VideoUtil.FormatNewCaps(Gst.Video.VideoFormat.BGRA, 640, 480, 4, 1, 1, 1); + + // Connect the handlers + GLib.Signal.Lookup(appsrc, "need-data").AddDelegate(new EventHandler(PushAppData)); + pipeline.Bus.AddSignalWatch(); + pipeline.Bus.Message += MessageHandler; + + // Run, loop, run! + pipeline.SetState(State.Playing); + loop.Run(); + } + + static void PushAppData(object o, EventArgs e) + { + ulong mseconds = 0; + if (appsrc.Clock != null) + mseconds = appsrc.Clock.Time / Clock.MSecond; + byte[] data = DrawData(mseconds); + + Gst.Buffer buffer = new Gst.Buffer(data); + appsrc.PushBuffer(buffer); + } + + // Returns a byte[] presentation of one 640x480 BGRA frame using Cairo + static byte[] DrawData(ulong seconds) + { + Cairo.ImageSurface img = new Cairo.ImageSurface(Cairo.Format.Argb32, 640, 480); + using(Cairo.Context context = new Cairo.Context(img)) + { + double dx = (double) (seconds % 2180) / 5; + context.Color = new Color(1.0, 1.0, 0); + context.Paint(); + context.MoveTo(300, 10 + dx); + context.LineTo(500 - dx, 400); + context.LineWidth = 4.0; + context.Color = new Color(0, 0, 1.0); + context.Stroke(); + } + return img.Data; + } + + static void MessageHandler(object sender, MessageArgs args) + { + Message message = args.Message; + string text = String.Format("Message from {0}: \t{1}", message.Src.Name, message.Type); + switch (message.Type) { + case MessageType.Error: + Enum err; + string msg; + message.ParseError(out err, out msg); + text += String.Format("\t({0})", msg); + break; + case MessageType.StateChanged: + State oldstate, newstate, pending; + message.ParseStateChanged(out oldstate, out newstate, out pending); + text += String.Format("\t\t{0} -> {1} ({2})", oldstate, newstate, pending); + break; + case MessageType.Eos: + loop.Quit(); + break; + } + Console.WriteLine(text); + } +} + diff --git a/samples/Makefile.am b/samples/Makefile.am index e24ce21112..6ee67d5e2c 100644 --- a/samples/Makefile.am +++ b/samples/Makefile.am @@ -1,7 +1,7 @@ if USE_MONO_COMPILER MCS_TARGETS = gtk-video-player.exe endif -TARGETS = playbin-player.exe decodebin-transcoder.exe helloworld.exe typefind.exe metadata.exe $(MCS_TARGETS) +TARGETS = playbin-player.exe decodebin-transcoder.exe helloworld.exe typefind.exe metadata.exe appsrc.exe $(MCS_TARGETS) DEBUGS = $(addsuffix .mdb, $(TARGETS)) all: $(TARGETS) link @@ -30,6 +30,9 @@ metadata.exe: $(srcdir)/MetaData.cs $(assemblies) mp3launchparse.exe: $(srcdir)/MP3LaunchParse.cs $(assemblies) $(CSC) -out:$@ $(GLIBSHARP_LIBS) $(references) $(srcdir)/MP3LaunchParse.cs +appsrc.exe: $(srcdir)/AppSrc.cs $(assemblies) + $(CSC) -out:$@ $(GLIBSHARP_LIBS) $(references) -r:Mono.Cairo.dll $(srcdir)/AppSrc.cs + gtk-video-player.exe: $(srcdir)/GtkVideoPlayer.cs $(assemblies) $(CSC) -out:$@ $(GLIBSHARP_LIBS) $(references) -pkg:gtk-sharp-2.0 $(srcdir)/GtkVideoPlayer.cs