gstreamer/samples/AppSrc.cs

100 lines
3 KiB
C#
Raw Normal View History

/*
* AppSrc.cs: example of using the appsrc element
*
* Authors:
* Maarten Bosmans <mkbosmans@gmail.com>
*
* Copyright (C) 2009 Maarten Bosmans
*
*/
using System;
using System.Runtime.InteropServices;
using GLib;
using Gst;
using Cairo;
2009-09-20 08:19:49 +00:00
public class AppSrcDemo {
static MainLoop loop;
static Gst.App.AppSrc appsrc;
static Pipeline pipeline;
2009-09-20 08:19:49 +00:00
public static void Main (string[] args) {
Application.Init();
loop = new MainLoop();
2009-09-20 08:19:49 +00:00
// Construct all the elements
pipeline = new Pipeline();
appsrc = new Gst.App.AppSrc ("AppSrcDemo");
Element color = ElementFactory.Make ("ffmpegcolorspace");
Element sink = ElementFactory.Make ("autovideosink");
2009-09-20 08:19:49 +00:00
// Link the elements
pipeline.Add (appsrc, color, sink);
Element.Link (appsrc, color, sink);
2009-09-20 08:19:49 +00:00
// Set the caps on the AppSrc to RGBA, 640x480, 4 fps, square pixels
Gst.Video.VideoFormat fmt = (BitConverter.IsLittleEndian) ? Gst.Video.VideoFormat.BGRA : Gst.Video.VideoFormat.ARGB;
appsrc.Caps = Gst.Video.VideoUtil.FormatNewCaps (fmt, 640, 480, 4, 1, 1, 1);
2009-09-20 08:19:49 +00:00
// Connect the handlers
appsrc.NeedData += PushAppData;
pipeline.Bus.AddSignalWatch();
pipeline.Bus.Message += MessageHandler;
2009-09-20 08:19:49 +00:00
// Run, loop, run!
pipeline.SetState (State.Playing);
loop.Run();
pipeline.SetState (State.Null);
}
2009-09-20 08:19:49 +00:00
static void PushAppData (object o, Gst.App.NeedDataArgs args) {
ulong mseconds = 0;
if (appsrc.Clock != null)
mseconds = appsrc.Clock.Time / Clock.MSecond;
2009-09-22 05:40:57 +00:00
Gst.Buffer buffer = DrawData (mseconds);
2009-09-20 08:19:49 +00:00
appsrc.PushBuffer (buffer);
}
2009-09-22 05:40:57 +00:00
// Returns a Gst.Buffer presentation of one 640x480 BGRA frame using Cairo
static Gst.Buffer DrawData (ulong seconds) {
Gst.Buffer buffer = new Gst.Buffer (640*480*4);
Cairo.ImageSurface img = new Cairo.ImageSurface (buffer.Data, Cairo.Format.Argb32, 640, 480, 640*4);
2009-09-20 08:19:49 +00:00
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();
}
img.Destroy();
2009-09-22 05:40:57 +00:00
return buffer;
2009-09-20 08:19:49 +00:00
}
2009-09-20 08:19:49 +00:00
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);
}
}