Skip to content

Instantly share code, notes, and snippets.

@kybu
Created April 17, 2018 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kybu/e510323b62594ec47ee5e4165831b52e to your computer and use it in GitHub Desktop.
Save kybu/e510323b62594ec47ee5e4165831b52e to your computer and use it in GitHub Desktop.
gstreamer and appsrc, not working
using System;
using System.IO;
using System.Linq;
using Gst;
using Gst.App;
using Application = Gst.Application;
using DateTime = System.DateTime;
namespace gstreamer
{
class Playback
{
public static void Main(string[] args)
{
var path = Environment.GetEnvironmentVariable("PATH");
Environment.SetEnvironmentVariable("PATH", @"c:\gstreamer\1.0\x86_64\bin;" + path);
Environment.SetEnvironmentVariable("GST_DEBUG", "3");
Application.Init(ref args);
var pipeline = new Pipeline("pipe1");
var bmpFiles = Directory.GetFiles(@"c:\bmp", "*.bmp").ToList();
var bmpFilesSent = 0;
var appsrc = new Gst.App.AppSrc("app_src");
appsrc.Caps = Caps.FromString("image/bmp,framerate=0/1,width=900,height=374");
appsrc.StreamType = AppStreamType.Stream; // No seeking
appsrc.Format = Format.Time;
appsrc.DoTimestamp = true;
var avdecbmp = ElementFactory.Make("avdec_bmp");
var videoconvert = ElementFactory.Make("videoconvert");
var queue = ElementFactory.Make("queue");
var videosink = ElementFactory.Make("autovideosink");
pipeline.Add(appsrc, avdecbmp, videoconvert, queue, videosink);
if (!Element.Link(appsrc, avdecbmp, videoconvert, queue, videosink))
return;
var bus = pipeline.Bus;
bus.AddSignalWatch();
var mainLoop = new GLib.MainLoop();
bus.Connect("message::error", (o, signalArgs) =>
{
GLib.GException err;
string debug;
var msg = (Message)signalArgs.Args[0];
// Print error details on the screen
msg.ParseError(out err, out debug);
Console.WriteLine("Error received from element {0}: {1}", msg.Src.Name, err.Message);
Console.WriteLine("Debugging information: {0}", debug != null ? debug : "none");
mainLoop.Quit();
});
bus.Connect("message::eos", (o, signalArgs) =>
{
mainLoop.Quit();
});
System.DateTime startedTimer = DateTime.Now;
DateTime lastTimer = DateTime.Now;
uint idleId = 0;
appsrc.NeedData += (o, dataArgs) =>
{
if (idleId == 0)
idleId = GLib.Idle.Add(() =>
{
if (bmpFilesSent == 0 || (System.DateTime.Now - lastTimer) > new TimeSpan(0, 0, 0, 0, 40))
{
var buf = new Gst.Buffer(File.ReadAllBytes(bmpFiles.First()));
bmpFiles.RemoveAt(0);
var ret = appsrc.PushBuffer(buf);
buf.Dispose();
++bmpFilesSent;
lastTimer = System.DateTime.Now;
if (ret != FlowReturn.Ok)
return false;
}
return true;
});
};
appsrc.EnoughData += (sender, eventArgs) =>
{
if (idleId != 0)
{
GLib.Idle.Remove(idleId);
idleId = 0;
}
};
pipeline.SetState(State.Playing);
mainLoop.Run();
// Free resources
pipeline.SetState(State.Null);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment