From 55c47c29ec272eca25f310d8f9e3e491c96e5158 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sebastian=20Dr=C3=B6ge?= Date: Sun, 21 Jun 2009 12:21:32 +0200 Subject: [PATCH] Add unit test for basic Gst.Element subclasses This implements a simple source and sink and sends 10 buffers from one to another. --- tests/ElementTest.cs | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) diff --git a/tests/ElementTest.cs b/tests/ElementTest.cs index c4a2350..2fe57be 100644 --- a/tests/ElementTest.cs +++ b/tests/ElementTest.cs @@ -9,6 +9,7 @@ using System; using NUnit.Framework; +using GLib; using Gst; [TestFixture] @@ -74,4 +75,147 @@ public class ElementTest source.Unlink(sink); Assert.IsFalse(source.GetStaticPad("src").IsLinked); } + + public class TestSubclassesApp { + static MainLoop loop; + + public class MySrc : Gst.Element { + public MySrc () : base () { + Init (); + } + + public MySrc (IntPtr raw) : base (raw) { + Init (); + } + + private Pad src; + private uint nbuffers = 0; + + private void Init () { + src = new Pad (templ, "src"); + AddPad (src); + } + + static Caps caps = Caps.FromString ("my/dummy-data"); + + private void loop () { + Gst.Buffer buf = new Gst.Buffer (); + buf.Caps = caps; + Gst.FlowReturn ret = src.Push (buf); + nbuffers++; + + Assert.AreEqual (ret, Gst.FlowReturn.Ok); + if (ret != Gst.FlowReturn.Ok) { + src.StopTask (); + this.PostMessage (Message.NewError (this, CoreError.Failed, "Oh no")); + } + + if (nbuffers == 10) { + Assert.IsTrue (src.PushEvent (Gst.Event.NewEos ())); + src.PauseTask (); + } + } + + protected override StateChangeReturn OnChangeState (StateChange transition) { + if (transition == StateChange.ReadyToPaused) + src.StartTask (loop); + else if (transition == StateChange.PausedToReady) + src.StopTask (); + + return StateChangeReturn.Success; + } + + static PadTemplate templ = new PadTemplate ("src", Gst.PadDirection.Src, Gst.PadPresence.Always, Caps.FromString ("my/dummy-data")); + + public static bool Register () { + SetDetails ( (GType) typeof (MySrc), "long", "klass", "desc", "author"); + AddPadTemplate ( (GType) typeof (MySrc), templ); + return ElementFactory.Register (null, "mysrc", (uint) Gst.Rank.None, (GType) typeof (MySrc)); + } + } + + public class MySink : Gst.Element { + public MySink () : base () { + Init (); + } + + public MySink (IntPtr raw) : base (raw) { + Init (); + } + + Gst.FlowReturn on_chain (Gst.Pad pad, Gst.Buffer buffer) { + Assert.IsNotNull (buffer); + return Gst.FlowReturn.Ok; + } + + bool on_event (Gst.Pad pad, Gst.Event evnt) { + if (evnt.Type == Gst.EventType.Eos) { + this.PostMessage (Message.NewEos (this)); + } + + return true; + } + + private void Init () { + Pad pad = new Pad (templ, "sink"); + pad.ChainFunction = on_chain; + pad.EventFunction = on_event; + AddPad (pad); + } + + static PadTemplate templ = new PadTemplate ("sink", Gst.PadDirection.Sink, Gst.PadPresence.Always, Caps.FromString ("my/dummy-data")); + + public static bool Register () { + SetDetails ( (GType) typeof (MySink), "long", "klass", "desc", "author"); + AddPadTemplate ( (GType) typeof (MySink), templ); + + return ElementFactory.Register (null, "mysink", (uint) Gst.Rank.None, (GType) typeof (MySink)); + } + } + + private static bool BusCb (Bus bus, Message message) { + switch (message.Type) { + case MessageType.Error: + Enum err; + string msg; + + message.ParseError (out err, out msg); + Assert.Fail (String.Format ("Error message: {0}", msg)); + loop.Quit (); + break; + case MessageType.Eos: + loop.Quit (); + break; + } + return true; + } + + public static void Run () { + + MySrc.Register (); + MySink.Register (); + + MySrc mysrc = Gst.ElementFactory.Make ("mysrc") as MySrc; + MySink mysink = Gst.ElementFactory.Make ("mysink") as MySink; + + Gst.Pipeline pipeline = new Pipeline ("pipeline"); + pipeline.Add (mysrc, mysink); + Assert.IsTrue (mysrc.Link (mysink)); + + loop = new MainLoop (); + + pipeline.Bus.AddWatch (new BusFunc (BusCb)); + pipeline.SetState (Gst.State.Playing); + + loop.Run (); + + pipeline.SetState (Gst.State.Null); + } + } + + [Test] + public void TestSubclasses () + { + TestSubclassesApp.Run (); + } } -- 2.7.4