Пример #1
0
        /// Constructs a GstVideoFrameContext from Gstreamer Sample object
        public GstVideoFrameContext(Sample sample)
        {
            if (sample == null)
            {
                throw new ArgumentNullException(nameof(sample));
            }

            _buffer = sample.Buffer;

            if (_buffer != null && _buffer.Map(out _mapInfo, MapFlags.Read))
            {
                using (var caps = sample.Caps)
                {
                    using (var cap = caps[0])
                    {
                        Format = cap.GetString("format");

                        int width, height;
                        cap.GetInt("width", out width);
                        cap.GetInt("height", out height);
                        Width  = width;
                        Height = height;

                        Size   = (long)_mapInfo.Size;
                        Stride = Size / Height;
                    }
                }
            }
        }
Пример #2
0
  public void TestBufferData() {
    byte[] data = new byte[] {0, 1, 2, 3, 4, 5};

    Gst.Buffer buffer = new Gst.Buffer (data);

    ArrayIsEqual (data, buffer.ToByteArray ());

    Gst.Base.ByteReader reader = new Gst.Base.ByteReader (buffer);
    byte b;
    uint u;
    Assert.IsTrue (reader.PeekUInt32Be (out u));
    Assert.IsTrue (u == 0x00010203);
    Assert.IsTrue (reader.GetUInt8 (out b));
    Assert.IsTrue (b == 0 && 0 == data[reader.Pos-1]);
    Assert.IsTrue (reader.GetUInt8 (out b));
    Assert.IsTrue (b == 1 && 1 == data[reader.Pos-1]);
    Assert.IsTrue (reader.GetUInt8 (out b));
    Assert.IsTrue (b == 2 && 2 == data[reader.Pos-1]);
    Assert.IsTrue (reader.GetUInt8 (out b));
    Assert.IsTrue (b == 3 && 3 == data[reader.Pos-1]);
    Assert.IsTrue (reader.GetUInt8 (out b));
    Assert.IsTrue (b == 4 && 4 == data[reader.Pos-1]);
    Assert.IsTrue (reader.GetUInt8 (out b));
    Assert.IsTrue (b == 5 && 5 == data[reader.Pos-1]);
    Assert.IsFalse (reader.GetUInt8 (out b));
    Assert.IsTrue (reader.Pos == buffer.Size);
  }
Пример #3
0
 public void TestSubbuffer()
 {
     Gst.Buffer buffer = new Gst.Buffer(4);
     Gst.Buffer sub    = buffer.CreateSub(1, 2);
     Assert.IsNotNull(sub);
     Assert.AreEqual(sub.Size, 2, "subbuffer has wrong size");
 }
    protected override FlowReturn OnTransformIp(Gst.Buffer buf)
    {
        if (!buf.IsWritable)
        {
            return(FlowReturn.Error);
        }

        Cairo.ImageSurface img = new Cairo.ImageSurface(buf.Data, Cairo.Format.Rgb24, width, height, width * 4);

        using (Cairo.Context context = new Cairo.Context(img)) {
            double dx = (double)((buf.Timestamp / Clock.MSecond) % 2180) / 5;
            context.Save();
            context.Scale(width / 640.0, height / 480.0);
            context.MoveTo(300, 10 + dx);
            context.LineTo(500 - dx, 400);
            context.LineWidth = 4.0;
            context.Color     = new Color(0, 0, 1.0);
            context.Stroke();
            context.Restore();

            if (lastX != -1 && lastY != -1)
            {
                context.Color = new Color(1.0, 0, 0);
                context.Translate(lastX, lastY);
                context.Scale(Math.Min(width / 640.0, height / 480.0), Math.Min(width / 640.0, height / 480.0));
                context.Arc(0, 0, 10.0, 0.0, 2 * Math.PI);
                context.Fill();
            }
        }

        img.Destroy();
        return(base.OnTransformIp(buf));
    }
Пример #5
0
    public void TestBufferData()
    {
        byte[] data = new byte[] { 0, 1, 2, 3, 4, 5 };

        Gst.Buffer buffer = new Gst.Buffer(data);

        ArrayIsEqual(data, buffer.ToByteArray());

        Gst.Base.ByteReader reader = new Gst.Base.ByteReader(buffer);
        byte b;
        uint u;

        Assert.IsTrue(reader.PeekUInt32Be(out u));
        Assert.IsTrue(u == 0x00010203);
        Assert.IsTrue(reader.GetUInt8(out b));
        Assert.IsTrue(b == 0 && 0 == data[reader.Pos - 1]);
        Assert.IsTrue(reader.GetUInt8(out b));
        Assert.IsTrue(b == 1 && 1 == data[reader.Pos - 1]);
        Assert.IsTrue(reader.GetUInt8(out b));
        Assert.IsTrue(b == 2 && 2 == data[reader.Pos - 1]);
        Assert.IsTrue(reader.GetUInt8(out b));
        Assert.IsTrue(b == 3 && 3 == data[reader.Pos - 1]);
        Assert.IsTrue(reader.GetUInt8(out b));
        Assert.IsTrue(b == 4 && 4 == data[reader.Pos - 1]);
        Assert.IsTrue(reader.GetUInt8(out b));
        Assert.IsTrue(b == 5 && 5 == data[reader.Pos - 1]);
        Assert.IsFalse(reader.GetUInt8(out b));
        Assert.IsTrue(reader.Pos == buffer.Size);

        Gst.Base.ByteWriter writer = new Gst.Base.ByteWriter(buffer, true);
        Assert.IsTrue(writer.Remaining == buffer.Size);
        Assert.IsTrue(writer.RemainingReadable == buffer.Size);
        Assert.IsTrue(writer.PutUInt8(5));
        Assert.IsTrue(writer.PutUInt16Be(0x0403));
        Assert.IsTrue(writer.PutUInt8(2));
        Assert.IsTrue(writer.PutUInt16Le(0x0001));
        Assert.IsTrue(writer.Remaining == 0);
        Assert.IsTrue(writer.SetPos(0));
        Assert.IsTrue(writer.PeekUInt32Be(out u));
        Assert.IsTrue(u == 0x05040302);
        Assert.IsTrue(writer.GetUInt8(out b));
        Assert.IsTrue(b == 5 && 0 == data[writer.Pos - 1]);
        Assert.IsTrue(writer.GetUInt8(out b));
        Assert.IsTrue(b == 4 && 1 == data[writer.Pos - 1]);
        Assert.IsTrue(writer.GetUInt8(out b));
        Assert.IsTrue(b == 3 && 2 == data[writer.Pos - 1]);
        Assert.IsTrue(writer.GetUInt8(out b));
        Assert.IsTrue(b == 2 && 3 == data[writer.Pos - 1]);
        Assert.IsTrue(writer.GetUInt8(out b));
        Assert.IsTrue(b == 1 && 4 == data[writer.Pos - 1]);
        Assert.IsTrue(writer.GetUInt8(out b));
        Assert.IsTrue(b == 0 && 5 == data[writer.Pos - 1]);
        Assert.IsFalse(writer.GetUInt8(out b));
        Assert.IsTrue(writer.Pos == buffer.Size);

        writer = new Gst.Base.ByteWriter(buffer, false);
        Assert.IsTrue(writer.Remaining == buffer.Size);
        Assert.IsTrue(writer.RemainingReadable == 0);
    }
Пример #6
0
    public void TestBufferData()
    {
        byte[] data = new byte[] {0, 1, 2, 3, 4, 5};

        Gst.Buffer buffer = new Gst.Buffer (data);

        ArrayIsEqual (data, buffer.ToByteArray ());

        Gst.Base.ByteReader reader = new Gst.Base.ByteReader (buffer);
        byte b;
        uint u;
        Assert.IsTrue (reader.PeekUInt32Be (out u));
        Assert.IsTrue (u == 0x00010203);
        Assert.IsTrue (reader.GetUInt8 (out b));
        Assert.IsTrue (b == 0 && 0 == data[reader.Pos-1]);
        Assert.IsTrue (reader.GetUInt8 (out b));
        Assert.IsTrue (b == 1 && 1 == data[reader.Pos-1]);
        Assert.IsTrue (reader.GetUInt8 (out b));
        Assert.IsTrue (b == 2 && 2 == data[reader.Pos-1]);
        Assert.IsTrue (reader.GetUInt8 (out b));
        Assert.IsTrue (b == 3 && 3 == data[reader.Pos-1]);
        Assert.IsTrue (reader.GetUInt8 (out b));
        Assert.IsTrue (b == 4 && 4 == data[reader.Pos-1]);
        Assert.IsTrue (reader.GetUInt8 (out b));
        Assert.IsTrue (b == 5 && 5 == data[reader.Pos-1]);
        Assert.IsFalse (reader.GetUInt8 (out b));
        Assert.IsTrue (reader.Pos == buffer.Size);

        Gst.Base.ByteWriter writer = new Gst.Base.ByteWriter (buffer, true);
        Assert.IsTrue (writer.Remaining == buffer.Size);
        Assert.IsTrue (writer.RemainingReadable == buffer.Size);
        Assert.IsTrue (writer.PutUInt8 (5));
        Assert.IsTrue (writer.PutUInt16Be (0x0403));
        Assert.IsTrue (writer.PutUInt8 (2));
        Assert.IsTrue (writer.PutUInt16Le (0x0001));
        Assert.IsTrue (writer.Remaining == 0);
        Assert.IsTrue (writer.SetPos (0));
        Assert.IsTrue (writer.PeekUInt32Be (out u));
        Assert.IsTrue (u == 0x05040302);
        Assert.IsTrue (writer.GetUInt8 (out b));
        Assert.IsTrue (b == 5 && 0 == data[writer.Pos-1]);
        Assert.IsTrue (writer.GetUInt8 (out b));
        Assert.IsTrue (b == 4 && 1 == data[writer.Pos-1]);
        Assert.IsTrue (writer.GetUInt8 (out b));
        Assert.IsTrue (b == 3 && 2 == data[writer.Pos-1]);
        Assert.IsTrue (writer.GetUInt8 (out b));
        Assert.IsTrue (b == 2 && 3 == data[writer.Pos-1]);
        Assert.IsTrue (writer.GetUInt8 (out b));
        Assert.IsTrue (b == 1 && 4 == data[writer.Pos-1]);
        Assert.IsTrue (writer.GetUInt8 (out b));
        Assert.IsTrue (b == 0 && 5 == data[writer.Pos-1]);
        Assert.IsFalse (writer.GetUInt8 (out b));
        Assert.IsTrue (writer.Pos == buffer.Size);

        writer = new Gst.Base.ByteWriter (buffer, false);
        Assert.IsTrue (writer.Remaining == buffer.Size);
        Assert.IsTrue (writer.RemainingReadable == 0);
    }
Пример #7
0
 /// Frees unmanaged resources
 public void Dispose()
 {
     if (_buffer != null && _mapInfo.DataPtr != null)
     {
         _buffer.Unmap(_mapInfo);
     }
     _buffer?.Dispose();
     _buffer = null;
 }
    protected override FlowReturn OnCreate(ulong offset, uint size, out Gst.Buffer buf)
    {
        int intSize = (int)size;

        if (intSize == -1)
        {
            throw new Exception();
        }

        var samples = (int)(size / _info.Bpf);

        this._generateSamplesPerBuffer = samples;
        var numBytes = samples * _info.Bpf;

        byte[] data;
        var    nextSample = _nextSample + (ulong)samples;
        var    nextByte   = _nextByte + (ulong)numBytes;
        var    nextTime   = Util.Uint64Scale(_nextSample, SECOND, (ulong)_info.Rate);

        if (!Mute)
        {
            var fact = 2 * Math.PI * Freq / _info.Rate;

/*
 *          var sin = Enumerable.Range((int)_accumulator, samples)
 *              .Select(r => (float)(Math.Sin(fact * r) * Volume));
 *          var zip = sin.Zip(sin, (a, b) => new[] { a, b })
 *              .Aggregate((a, b) => a.Concat(b).ToArray());
 */
            float[] zip1 = new float[samples * 2];
            for (var i = 0; i < samples; i++)
            {
                var r = _accumulator + i;
                var s = (float)(Math.Sin(fact * r) * Volume);
                zip1[2 * i]     = s;
                zip1[2 * i + 1] = s;
            }
            data = zip1.SelectMany(z => BitConverter.GetBytes(z)).ToArray();
        }
        else
        {
            data = Enumerable.Repeat <byte>(0, (int)numBytes).ToArray();
        }

        buf           = new Gst.Buffer(data);
        buf.Offset    = this._nextSample;
        buf.OffsetEnd = nextSample;
        buf.Pts       = this._nextTime;
        buf.Duration  = nextTime - this._nextTime;
        _nextTime     = nextTime;
        _nextSample   = nextSample;
        _nextByte     = nextByte;
        _accumulator += samples;
        _accumulator %= (int)(_info.Rate / Freq);
        return(FlowReturn.Ok);
    }
Пример #9
0
    static void PushAppData(object o, Gst.App.NeedDataArgs args)
    {
        ulong mseconds = 0;

        if (appsrc.Clock != null)
        {
            mseconds = appsrc.Clock.Time / Clock.MSecond;
        }
        Gst.Buffer buffer = DrawData(mseconds);
        appsrc.PushBuffer(buffer);
    }
Пример #10
0
  public void TestIsSpanFast() {
    Gst.Buffer buffer = new Gst.Buffer (4);

    Gst.Buffer sub1 = buffer.CreateSub (0, 2);
    Assert.IsNotNull (sub1, "CreateSub of buffer returned null");

    Gst.Buffer sub2 = buffer.CreateSub (2, 2);
    Assert.IsNotNull (sub2, "CreateSub of buffer returned null");

    Assert.IsFalse (buffer.IsSpanFast (sub2), "a parent buffer can not be SpanFasted");
    Assert.IsFalse (sub1.IsSpanFast (buffer), "a parent buffer can not be SpanFasted");
    Assert.IsTrue (sub1.IsSpanFast (sub2), "two subbuffers next to each other should be SpanFast");
  }
Пример #11
0
        static GLib.MainLoop MainLoop;    // GLib's Main Loop

        // This method is called by the idle GSource in the mainloop, to feed CHUNK_SIZE bytes into appsrc.
        // The idle handler is added to the mainloop when appsrc requests us to start sending data (need-data signal)
        // and is removed when appsrc has enough data (enough-data signal).

        static bool PushData()
        {
            var     numSamples = ChunkSize / 2;         // Because each sample is 16 bits
            MapInfo map;

            // Create a new empty buffer
            var buffer = new Gst.Buffer(null, ChunkSize, AllocationParams.Zero);

            // Set its timestamp and duration
            buffer.Pts      = Util.Uint64Scale((ulong)NumSamples, (ulong)Constants.SECOND, (ulong)SampleRate);
            buffer.Dts      = Util.Uint64Scale((ulong)NumSamples, (ulong)Constants.SECOND, (ulong)SampleRate);
            buffer.Duration = Util.Uint64Scale((ulong)NumSamples, (ulong)Constants.SECOND, (ulong)SampleRate);

            // Generate some psychodelic waveforms
            buffer.Map(out map, MapFlags.Write);
            c += d;
            d -= c / 1000f;
            var freq = 1100f + 1000f * d;

            short[] data = new short[numSamples];
            for (int i = 0; i < numSamples; i++)
            {
                a      += b;
                b      -= a / freq;
                data[i] = (short)(500f * a);
            }
            // convert the short[] to a byte[] by marshalling
            var native = Marshal.AllocHGlobal(data.Length * sizeof(short));

            Marshal.Copy(data, 0, native, data.Length);
            byte[] bytedata = new byte[2 * data.Length];
            Marshal.Copy(native, bytedata, 0, data.Length * sizeof(short));

            map.Data = bytedata;
            buffer.Unmap(map);
            NumSamples += numSamples;

            // Push the buffer into the appsrc
            var ret = AppSource.PushBuffer(buffer);

            // Free the buffer now that we are done with it
            buffer.Dispose();

            if (ret != FlowReturn.Ok)
            {
                // We got some error, stop sending data
                return(false);
            }
            return(true);
        }
Пример #12
0
    public void TestIsSpanFast()
    {
        Gst.Buffer buffer = new Gst.Buffer(4);

        Gst.Buffer sub1 = buffer.CreateSub(0, 2);
        Assert.IsNotNull(sub1, "CreateSub of buffer returned null");

        Gst.Buffer sub2 = buffer.CreateSub(2, 2);
        Assert.IsNotNull(sub2, "CreateSub of buffer returned null");

        Assert.IsFalse(buffer.IsSpanFast(sub2), "a parent buffer can not be SpanFasted");
        Assert.IsFalse(sub1.IsSpanFast(buffer), "a parent buffer can not be SpanFasted");
        Assert.IsTrue(sub1.IsSpanFast(sub2), "two subbuffers next to each other should be SpanFast");
    }
Пример #13
0
  public void TestCaps() {
    Gst.Buffer buffer = new Gst.Buffer (4);
    Caps caps = Caps.FromString ("audio/x-raw-int");

    Assert.IsNull (buffer.Caps, "buffer.Caps should be null");
    buffer.Caps = caps;
    Assert.IsNotNull (buffer.Caps, "buffer.Caps is null");

    Caps caps2 = Caps.FromString ("audio/x-raw-float");
    buffer.Caps = caps2;
    Assert.AreNotEqual (buffer.Caps, caps);
    Assert.AreEqual (buffer.Caps, caps2);

    buffer.Caps = null;
    Assert.IsNull (buffer.Caps, "buffer.Caps should be null");
  }
Пример #14
0
 // 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);
   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();
   return buffer;
 }
Пример #15
0
 // 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);
     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();
     return(buffer);
 }
Пример #16
0
 public Image(Sample sample)
     : base()
 {
     FSample = sample;
     FBuffer = sample.Buffer;
     using (var caps = sample.Caps)
         using (var structure = caps.GetStructure(0))
         {
             int width, height;
             structure.GetInt("width", out width);
             structure.GetInt("height", out height);
             var format      = structure.GetString("format");
             var videoFormat = format.ToVideoFormat();
             var pixelFormat = videoFormat.ToPixelFormat();
             FInfo = new ImageInfo(width, height, pixelFormat, format);
         }
 }
 protected override void OnGetTimes(Gst.Buffer buffer, out ulong start, out ulong end)
 {
     start = CLOCK_TIME_NONE;
     end   = CLOCK_TIME_NONE;
     if (IsLive)
     {
         var ts = buffer.Pts;
         if (ts != CLOCK_TIME_NONE)
         {
             var duration = buffer.Duration;
             if (duration != CLOCK_TIME_NONE)
             {
                 end = ts + duration;
             }
             start = ts;
         }
     }
 }
Пример #18
0
    public void TestCaps()
    {
        Gst.Buffer buffer = new Gst.Buffer(4);
        Caps       caps   = Caps.FromString("audio/x-raw-int");

        Assert.IsNull(buffer.Caps, "buffer.Caps should be null");
        buffer.Caps = caps;
        Assert.IsNotNull(buffer.Caps, "buffer.Caps is null");

        Caps caps2 = Caps.FromString("audio/x-raw-float");

        buffer.Caps = caps2;
        Assert.AreNotEqual(buffer.Caps, caps);
        Assert.AreEqual(buffer.Caps, caps2);

        buffer.Caps = null;
        Assert.IsNull(buffer.Caps, "buffer.Caps should be null");
    }
        /// <summary>
        /// This method is called by the idle GSource in the mainloop, to feed CHUNK_SIZE bytes into appsrc.
        /// The ide handler is added to the mainloop when appsrc requests us to start sending data(need-data signal)
        /// and is removed when appsrc has enough data(enough-data signal).
        /// </summary>
        static bool PushData()
        {
            var numSamples = CHUNK_SIZE / 2;

//            var numSamples = CHUNK_SIZE ;

            // Create a new empty buffer
            Gst.Buffer buffer = new Gst.Buffer(null, CHUNK_SIZE, AllocationParams.Zero);

            //Set its timestamp and duration
            buffer.Pts      = Util.Uint64Scale((ulong)Data.NumSamples, (ulong)Gst.Constants.SECOND, (ulong)SAMPLE_RATE);
            buffer.Dts      = Util.Uint64Scale((ulong)Data.NumSamples, (ulong)Gst.Constants.SECOND, (ulong)SAMPLE_RATE);
            buffer.Duration = Util.Uint64Scale((ulong)Data.NumSamples, (ulong)Gst.Constants.SECOND, (ulong)SAMPLE_RATE);

            // Generate some psychodelic waveforms
            buffer.Map(out MapInfo map, MapFlags.Write);
// map.Data is a marshal.copy of IntPtr, that's why I can't just set the array values.
//  See PlaybackTutorial03 for the unsafe version
            var raw = map.Data;

            Data.c += Data.d;
            Data.d -= Data.c / 1000;
            float freq = 1100 + 1000 * Data.d;

            for (int i = 0; i < numSamples; i++)
            {
                Data.a += Data.b;
                Data.b -= Data.a / freq;
                var sh    = (short)(500 * Data.a);
                var bytes = BitConverter.GetBytes(sh);
                raw[2 * i]     = bytes[0];
                raw[2 * i + 1] = bytes[1];
            }
            map.Data = raw;
            buffer.Unmap(map);
            Data.NumSamples += numSamples;

            // push the buffer into the appsrc
            var ret = Data.AppSource.PushBuffer(buffer);

            buffer.Dispose();

            return(ret == FlowReturn.Ok);
        }
Пример #20
0
        FlowReturn Chain(Pad pad, Gst.Object parent, Gst.Buffer buffer)
        {
            if (Volume == 1.0)
            {
                return(_src.Push(buffer));
            }

            buffer.MakeWritable();

            MapInfo mapInfo;

            buffer.Map(out mapInfo, MapFlags.Read | MapFlags.Write);

            ScaleInt16(mapInfo.DataPtr, mapInfo.Size / 2, Volume);

            buffer.Unmap(mapInfo);

            return(_src.Push(buffer));
        }
Пример #21
0
            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();
                }
            }
Пример #22
0
    public void TestPushUnlinked()
    {
        Pad src = new Pad("src", PadDirection.Src);

        Assert.IsNotNull(src, "Could not create src");
        Caps caps = src.AllowedCaps;

        Assert.IsNull(caps);

        caps = Caps.FromString("foo/bar");
        src.SetCaps(caps);

        Gst.Buffer buffer = new Gst.Buffer();
        Assert.AreEqual(src.Push(buffer), FlowReturn.NotLinked);

        ulong handler_id = src.AddBufferProbe(new PadBufferProbeCallback(ProbeHandler));

        buffer = new Gst.Buffer(new byte[] { 0 });
        FlowReturn flowreturn = src.Push(buffer);

        Assert.AreEqual(flowreturn, FlowReturn.Ok);
    }
        /// <summary>
        /// This method is called by the idle GSource in the mainloop, to feed CHUNK_SIZE bytes into appsrc.
        /// The idle handler is added to the mainloop when appsrc requests us to start sending data(need-data signal)
        /// and is removed when appsrc has enough data(enough-data signal).
        /// </summary>
        static bool PushData()
        {
            var numSamples = CHUNK_SIZE / 2; //Because each sample is 16 bits

            // Create a new empty buffer
            using (var buffer = new Gst.Buffer(null, CHUNK_SIZE, AllocationParams.Zero))
            {
                //Set its timestamp and duration
                buffer.Pts      = Util.Uint64Scale((ulong)_data.NumSamples, (ulong)Constants.SECOND, (ulong)SAMPLE_RATE);
                buffer.Dts      = Util.Uint64Scale((ulong)_data.NumSamples, (ulong)Constants.SECOND, (ulong)SAMPLE_RATE);
                buffer.Duration = Util.Uint64Scale((ulong)_data.NumSamples, (ulong)Constants.SECOND, (ulong)SAMPLE_RATE);

                // Generate some psychodelic waveforms
                buffer.Map(out MapInfo map, MapFlags.Write);

//  See BasicTutorial08 for the managed version
                unsafe
                {
                    var raw = (short *)map.DataPtr;
                    _data.c += _data.d;
                    _data.d -= _data.c / 1000f;
                    var freq = 1100f + 1000f * _data.d;
                    for (int i = 0; i < numSamples; i++)
                    {
                        _data.a += _data.b;
                        _data.b -= _data.a / freq;
                        raw[i]   = (short)(500 * _data.a);
                    }
                }
                buffer.Unmap(map);
                _data.NumSamples += numSamples;

                // push the buffer into the appsrc
                var ret = _data.AppSource.PushBuffer(buffer);

                return(ret == FlowReturn.Ok);
                // Free the buffer now that we are done with it
            }
        }
    protected override FlowReturn OnAggregate(bool timeout)
    {
        ulong          pts = 0;
        bool           eos = false;
        Image <Rgba32> outImg;

        using (outImg = new Image <Rgba32>(Configuration.Default, WIDTH, HEIGHT))
        {
            ForeachSinkPad(mixBuffers);
            var f       = outImg.Frames[0];
            var span    = f.GetPixelSpan();
            var bytes   = MemoryMarshal.AsBytes(span).ToArray();
            var outBuff = new Gst.Buffer(null, (ulong)bytes.LongLength, AllocationParams.Zero);
            outBuff.Fill(0, bytes);
            outBuff.Pts = pts;
            var ret = FinishBuffer(outBuff);
        }

        return(eos ? FlowReturn.Eos : FlowReturn.Ok);

        bool mixBuffers(Element agg, Pad pad)
        {
            var aggPad = pad as AggregatorPad;
            var buff   = aggPad.PopBuffer();
            var isMap  = buff.Map(out MapInfo info, MapFlags.Read);

            using (var img = Image.LoadPixelData <Rgba32>(info.Data, WIDTH, HEIGHT))
            {
                outImg.Mutate((ctx) => ctx.DrawImage(img, PixelColorBlendingMode.Lighten, 0.5f));
                pts = buff.Pts;
                eos = false;
            }
            buff.Unmap(info);
            return(true);
        }
    }
Пример #25
0
      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 ();
        }
      }
Пример #26
0
 Gst.FlowReturn on_chain(Gst.Pad pad, Gst.Buffer buffer)
 {
     Assert.IsNotNull(buffer);
     return(Gst.FlowReturn.Ok);
 }
Пример #27
0
 protected override FlowReturn OnTransform(Gst.Buffer inbuf, Gst.Buffer outbuf)
 {
     Console.WriteLine($"{inbuf.Dts} : {inbuf.Pts}");
     return(FlowReturn.Ok);
 }
Пример #28
0
  public void TestPushUnlinked() {
    Pad src = new Pad ("src", PadDirection.Src);
    Assert.IsNotNull (src, "Could not create src");
    Caps caps = src.AllowedCaps;
    Assert.IsNull (caps);

    caps = Caps.FromString ("foo/bar");
    src.SetCaps (caps);

    Gst.Buffer buffer = new Gst.Buffer();
    Assert.AreEqual (src.Push (buffer), FlowReturn.NotLinked);

    ulong handler_id = src.AddBufferProbe (new PadBufferProbeCallback (ProbeHandler));
    buffer = new Gst.Buffer (new byte[] {0});
    FlowReturn flowreturn = src.Push (buffer);
    Assert.AreEqual (flowreturn, FlowReturn.Ok);
  }
Пример #29
0
 bool ProbeHandler(Pad pad, Gst.Buffer buffer)
 {
     //Console.WriteLine("event worked");
     return(false);
 }
Пример #30
0
 public Data(Gst.Buffer buffer, int scanSize)
 {
     FBuffer  = buffer;
     ScanSize = scanSize;
     buffer.Map(out FMapInfo, MapFlags.Read);
 }
Пример #31
0
 private void OnHandoff(object o, GLib.SignalArgs args)
 {
     Gst.Buffer buffer = args.Args[0] as Gst.Buffer;
     Console.WriteLine(buffer.Duration + "\t" + buffer.Timestamp);
 }
Пример #32
0
 public void TestSubbuffer()
 {
     Gst.Buffer buffer = new Gst.Buffer (4);
     Gst.Buffer sub = buffer.CreateSub (1, 2);
     Assert.IsNotNull (sub);
     Assert.AreEqual (sub.Size, 2, "subbuffer has wrong size");
 }
        // GST polling thread function
        void KeepPolling()
        {
            while (isrunning)
            {
                switch (playerState)
                {
                case VideoPlayerState.STOPPED:
                    break;

                case VideoPlayerState.LOADING:
                    //get video width/height
                    int w = 0, h = 0;
                    //Query video information
                    Gst.Buffer buf = appSink.PullBuffer();
                    if (buf != null)
                    {
                        Console.WriteLine(buf.Caps.ToString());

                        //string format = buf.Caps[0].GetValue("format").Val.ToString();
                        //Console.WriteLine("format: " + format);
                        int.TryParse(buf.Caps[0].GetValue("width").Val.ToString(), out w);
                        int.TryParse(buf.Caps[0].GetValue("height").Val.ToString(), out h);
                        if (w * h != 0)
                        {
                            //Create decoded buffer
                            lock (lockFrameBuf)
                            {
                                width   = w;
                                height  = h;
                                bufferY = new byte[width * height];
                                bufferU = new byte[width * height / 4];
                                bufferV = new byte[width * height / 4];
                                IntPtr src = buf.Data;
                                Marshal.Copy(src, bufferY, 0, width * height);
                                src = new IntPtr(src.ToInt64() + width * height);
                                Marshal.Copy(src, bufferU, 0, width * height / 4);
                                src = new IntPtr(src.ToInt64() + width * height / 4);
                                Marshal.Copy(src, bufferV, 0, width * height / 4);
                                isFrameNew = true;
                                //Dispose handle to avoid memory leak
                                //gst_mini_object_unref(buf.Handle);
                                buf.Dispose();
                            }

                            Console.WriteLine("PLAYING");
                            playerState = VideoPlayerState.PLAYING;

                            continue;
                        }
                    }
                    break;

                case VideoPlayerState.PLAYING:
                    Gst.Buffer buf2 = appSink.PullBuffer();
                    if (buf2 != null)
                    {
                        lock (lockFrameBuf){
                            //Update buffer
                            IntPtr src = buf2.Data;
                            Marshal.Copy(src, bufferY, 0, width * height);
                            src = new IntPtr(src.ToInt64() + width * height);
                            Marshal.Copy(src, bufferU, 0, width * height / 4);
                            src = new IntPtr(src.ToInt64() + width * height / 4);
                            Marshal.Copy(src, bufferV, 0, width * height / 4);
                            isFrameNew = true;
                            //gst_mini_object_unref(buf2.Handle);
                            //buf2.Dispose();
                        }
                        buf2.Dispose();
                    }
                    else
                    {
                        lock (lockFrameBuf)
                        {
                            //Clear buffer
                            bufferY = new byte[width * height];
                            bufferU = new byte[width * height / 4];
                            bufferV = new byte[width * height / 4];
                        }
                        playerState = VideoPlayerState.STOPPED;
                        Console.WriteLine("STOPPED");
                    }

                    break;

                case VideoPlayerState.PAUSED:
                    //Do nothing
                    break;

                default:
                    //Do nothing
                    break;
                }
                Thread.Sleep(10);
            }

            //Clean up
            this.PlayerState = VideoPlayerState.STOPPED;
            playBin.SetState(State.Null);
            playBin.Dispose();
            appSink.SetState(State.Null);
            appSink.Dispose();
            playBin = null;
            appSink = null;
        }
 protected override FlowReturn OnTransformIp(Gst.Buffer buf)
 {
     Assert.IsTrue(buf.IsWritable);
     return(base.OnTransformIp(buf));
 }
 protected override FlowReturn OnTransform(Gst.Buffer inbuf, Gst.Buffer outbuf)
 {
     Assert.IsTrue(outbuf.IsWritable);
     transformed = true;
     return(base.OnTransform(inbuf, outbuf));
 }