]> git.parisson.com Git - timeside.git/commitdiff
Encoder: Enable timestamp during encoding, provide timestamp and duration in tools...
authorThomas Fillon <thomas@parisson.com>
Wed, 27 Nov 2013 15:53:44 +0000 (16:53 +0100)
committerThomas Fillon <thomas@parisson.com>
Wed, 27 Nov 2013 15:53:44 +0000 (16:53 +0100)
timeside/encoder/core.py
timeside/tools/gstutils.py

index 077b8621f842553738106849ccd48934944b8024..b3c9580c30910ebe1c603e4b025ac5c3b82b15ad 100644 (file)
@@ -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')
index 461b9af2b260ae6ca9f8deb0b1a5d6ea279f18c1..c5605f4d177f7c75e688f1b54031f213cbd7062f 100644 (file)
@@ -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')