From 6e208c12d7ce418ca2a34b7a3a32f078dad8c8ff Mon Sep 17 00:00:00 2001 From: Thomas Fillon Date: Wed, 27 Nov 2013 16:53:44 +0100 Subject: [PATCH] Encoder: Enable timestamp during encoding, provide timestamp and duration in tools/numpy_array_to_gst_buffer.py --- timeside/encoder/core.py | 9 ++++++++- timeside/tools/gstutils.py | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/timeside/encoder/core.py b/timeside/encoder/core.py index 077b862..b3c9580 100644 --- a/timeside/encoder/core.py +++ b/timeside/encoder/core.py @@ -50,6 +50,7 @@ class GstEncoder(Processor): self.eod = False self.metadata = None + self.num_samples = 0 def release(self): if hasattr(self, 'eod') and hasattr(self, 'mainloopthread'): @@ -79,6 +80,7 @@ class GstEncoder(Processor): self.src.set_property('emit-signals', True) self.src.set_property('num-buffers', -1) self.src.set_property('block', True) + self.src.set_property('do-timestamp', True) self.bus = self.pipeline.get_bus() self.bus.add_signal_watch() @@ -108,6 +110,7 @@ class GstEncoder(Processor): self.end_reached = True self.end_cond.notify() self.end_cond.release() + elif t == gst.MESSAGE_ERROR: self.end_cond.acquire() self.pipeline.set_state(gst.STATE_NULL) @@ -120,7 +123,11 @@ class GstEncoder(Processor): def process(self, frames, eod=False): self.eod = eod - buf = numpy_array_to_gst_buffer(frames) + if eod: + self.num_samples += frames.shape[0] + else: + self.num_samples += self.blocksize() + buf = numpy_array_to_gst_buffer(frames, self.blocksize(),self.num_samples, self.samplerate()) self.src.emit('push-buffer', buf) if self.eod: self.src.emit('end-of-stream') diff --git a/timeside/tools/gstutils.py b/timeside/tools/gstutils.py index 461b9af..c5605f4 100644 --- a/timeside/tools/gstutils.py +++ b/timeside/tools/gstutils.py @@ -2,15 +2,22 @@ from numpy import array, getbuffer, frombuffer import pygst pygst.require('0.10') +import gst import gobject gobject.threads_init() -def numpy_array_to_gst_buffer(frames): + +def numpy_array_to_gst_buffer(frames, CHUNK_SIZE, num_samples, SAMPLE_RATE): from gst import Buffer """ gstreamer buffer to numpy array conversion """ buf = Buffer(getbuffer(frames.astype("float32"))) + #Set its timestamp and duration + buf.timestamp = gst.util_uint64_scale(num_samples, gst.SECOND, SAMPLE_RATE) + buf.duration = gst.util_uint64_scale(CHUNK_SIZE, gst.SECOND, SAMPLE_RATE) + return buf + def gst_buffer_to_numpy_array(buf, chan): """ gstreamer buffer to numpy array conversion """ samples = frombuffer(buf.data, dtype='float32') -- 2.39.5