From ba717d64b67b95a7538d7bd726f485c3d65c542d Mon Sep 17 00:00:00 2001 From: yomguy Date: Tue, 15 Mar 2011 15:12:48 +0000 Subject: [PATCH] trying to get appsink and filesink orking together with no luck... --- timeside/encoder/flac.py | 30 ++++++------ timeside/encoder/mp3.py | 50 +++++++++++++------- timeside/encoder/ogg.py | 10 ++-- timeside/encoder/wav.py | 16 +++---- timeside/tests/api/test_lolevel_streaming.py | 14 +++--- 5 files changed, 67 insertions(+), 53 deletions(-) diff --git a/timeside/encoder/flac.py b/timeside/encoder/flac.py index 632e545..dce1caa 100644 --- a/timeside/encoder/flac.py +++ b/timeside/encoder/flac.py @@ -48,27 +48,25 @@ class FlacEncoder(Processor): super(FlacEncoder, self).setup(channels, samplerate, nframes) # TODO open file for writing # the output data format we want - pipe = ''' appsrc name=src max-bytes=32768 block=true - ! audioconvert - ! flacenc - ''' - if self.filename and self.streaming: - pipe += ''' - ! queue2 name=q0 ! tee name=tee - tee. ! queue name=q1 ! appsink name=app sync=false - tee. ! queue name=q2 ! filesink location=%s - ''' % self.filename + self.pipe = ''' appsrc name=src ! audioconvert + ! flacenc ''' + + if self.filename and self.streaming: + self.pipe += ''' + ! tee name=t + ! queue ! appsink name=sink sync=False + t. ! queue ! filesink location=%s ''' % self.filename elif self.filename : - pipe += '! filesink location=%s ' % self.filename + self.pipe += '! filesink location=%s ' % self.filename else: - pipe += '! appsink name=app sync=false ' + self.pipe += '! appsink name=sink blocksize=16384 sync=False ' - self.pipeline = gst.parse_launch(pipe) + self.pipeline = gst.parse_launch(self.pipe) # store a pointer to appsrc in our encoder object self.src = self.pipeline.get_by_name('src') # store a pointer to appsink in our encoder object - self.app = self.pipeline.get_by_name('app') + self.sink = self.pipeline.get_by_name('sink') srccaps = gst.Caps("""audio/x-raw-float, endianness=(int)1234, @@ -115,8 +113,8 @@ class FlacEncoder(Processor): buf = self.numpy_array_to_gst_buffer(frames) self.src.emit('push-buffer', buf) if self.streaming: - pull = self.app.emit('pull-buffer') - if eod: self.src.emit('end-of-stream') + pull = self.sink.emit('pull-buffer') +# if eod: self.src.emit('end-of-stream') if not self.streaming: return frames, eod else: diff --git a/timeside/encoder/mp3.py b/timeside/encoder/mp3.py index 78e0b6b..bbe97a6 100644 --- a/timeside/encoder/mp3.py +++ b/timeside/encoder/mp3.py @@ -24,7 +24,9 @@ from timeside.core import Processor, implements, interfacedoc from timeside.api import IEncoder from numpy import array, frombuffer, getbuffer, float32 +from timeside.core import FixedSizeInputAdapter +import numpy import pygst pygst.require('0.10') import gst @@ -36,35 +38,39 @@ class Mp3Encoder(Processor): """ gstreamer-based mp3 encoder """ implements(IEncoder) - def __init__(self, output, streaming=False): + def __init__(self, output, streaming=False): if isinstance(output, basestring): self.filename = output else: self.filename = None + self.streaming = streaming if not self.filename and not self.streaming: raise Exception('Must give an output') - + + self.max_bytes = 65536 + self.size_adapter = self.max_bytes + @interfacedoc def setup(self, channels=None, samplerate=None, nframes=None): + self.channels = channels super(Mp3Encoder, self).setup(channels, samplerate, nframes) #TODO: open file for writing # the output data format we want - self.pipe = ''' appsrc name=src max-bytes=32768 block=true + self.pipe = '''appsrc name=src max-bytes=%s ! audioconvert - ! lamemp3enc bitrate=192 quality=2 ! id3v2mux - ''' + ! lamemp3enc bitrate=256 ! id3v2mux + ''' % self.max_bytes if self.filename and self.streaming: - self.pipe += ''' - ! queue ! tee name=t - t. ! queue ! appsink name=app sync=false - t. ! queue ! filesink sync=false location=%s + self.pipe += ''' ! queue ! tee name=t + ! queue ! filesink location=%s + t. ! queue ! appsink name=app sync=False ''' % self.filename elif self.filename : self.pipe += '! filesink location=%s ' % self.filename else: - self.pipe += '! appsink name=app ' + self.pipe += '! queue ! appsink name=app sync=False ' self.pipeline = gst.parse_launch(self.pipe) # store a pointer to appsrc in our encoder object @@ -80,8 +86,11 @@ class Mp3Encoder(Processor): self.src.set_property("caps", srccaps) # start pipeline + if self.streaming: + self.app.set_property('emit-signals', True) self.pipeline.set_state(gst.STATE_PLAYING) - + self.adapter = FixedSizeInputAdapter(self.size_adapter, channels, pad=True) + @staticmethod @interfacedoc def id(): @@ -114,13 +123,20 @@ class Mp3Encoder(Processor): @interfacedoc def process(self, frames, eod=False): - buf = self.numpy_array_to_gst_buffer(frames) - self.src.emit('push-buffer', buf) + print frames.shape + emit = 0 + for samples, end in self.adapter.process(frames, eod): + print 'push: ', samples.shape + buf = self.numpy_array_to_gst_buffer(samples) + self.src.emit('push-buffer', buf) + if self.streaming: + pull = self.app.emit('pull-buffer') + emit = 1 if self.streaming: - pull = self.app.emit('pull-buffer') -# if eod: self.app.emit('end-of-stream') - if self.streaming: - return pull, eod + if emit: + return pull, eod + else: + return None, eod else: return frames, eod diff --git a/timeside/encoder/ogg.py b/timeside/encoder/ogg.py index fe84fe3..981bbcb 100644 --- a/timeside/encoder/ogg.py +++ b/timeside/encoder/ogg.py @@ -49,22 +49,22 @@ class VorbisEncoder(Processor): super(VorbisEncoder, self).setup(channels, samplerate, nframes) # TODO open file for writing # the output data format we want - pipe = ''' appsrc name=src max-bytes=32768 block=true + pipe = ''' appsrc name=src ! audioconvert ! vorbisenc ! oggmux ''' if self.filename and self.streaming: pipe += ''' - ! queue2 name=q0 ! tee name=tee - tee. ! queue name=q1 ! appsink name=app sync=false - tee. ! queue name=q2 ! filesink location=%s + ! tee name=t + ! queue ! appsink name=app sync=False + t. ! queue ! filesink location=%s ''' % self.filename elif self.filename : pipe += '! filesink location=%s ' % self.filename else: - pipe += '! appsink name=app sync=false ' + pipe += '! appsink name=app sync=False ' self.pipeline = gst.parse_launch(pipe) # store a pointer to appsrc in our encoder object diff --git a/timeside/encoder/wav.py b/timeside/encoder/wav.py index c60521b..f40c203 100644 --- a/timeside/encoder/wav.py +++ b/timeside/encoder/wav.py @@ -50,23 +50,23 @@ class WavEncoder(Processor): super(WavEncoder, self).setup(channels, samplerate, nframes) # TODO open file for writing # the output data format we want - pipe = ''' appsrc name=src + self.pipe = ''' appsrc name=src ! audioconvert ! wavenc ''' if self.filename and self.streaming: - pipe += ''' - ! queue2 name=q0 ! tee name=tee - tee. ! queue name=q1 ! appsink name=app sync=false - tee. ! queue name=q2 ! filesink location=%s + self.pipe += ''' + ! tee name=t + t. ! queue ! appsink name=app sync=false + t. ! queue ! filesink sync=false location=%s ''' % self.filename elif self.filename : - pipe += '! filesink location=%s ' % self.filename + self.pipe += '! filesink location=%s ' % self.filename else: - pipe += '! appsink name=app sync=false ' + self.pipe += '! appsink name=app sync=false' - self.pipeline = gst.parse_launch(pipe) + self.pipeline = gst.parse_launch(self.pipe) # store a pointer to appsrc in our encoder object self.src = self.pipeline.get_by_name('src') # store a pointer to appsink in our encoder object diff --git a/timeside/tests/api/test_lolevel_streaming.py b/timeside/tests/api/test_lolevel_streaming.py index cdf2754..8bc444f 100644 --- a/timeside/tests/api/test_lolevel_streaming.py +++ b/timeside/tests/api/test_lolevel_streaming.py @@ -11,13 +11,13 @@ if len(sys.argv) > 1: source = sys.argv[1] else: import os.path - source= os.path.join (os.path.dirname(__file__), "../samples/guitar.wav") + source= os.path.join (os.path.dirname(__file__), "../samples/sweep.wav") decoder = FileDecoder(source) print "Creating decoder with id=%s for: %s" % (decoder.id(), source) decoder.setup() channels = decoder.channels() -print channels +print 'channels :', channels samplerate = decoder.samplerate() nframes = decoder.nframes() @@ -25,16 +25,16 @@ dest1 = "/tmp/test.mp3" dest2 = "/tmp/test2.mp3" f = open(dest2,'w') -encoder = WavEncoder(dest1, streaming=True) +streaming=True +encoder = Mp3Encoder(dest1, streaming=streaming) encoder.setup(channels=channels, samplerate=samplerate) -print encoder.pipe - while True: buf, eod = encoder.process(*decoder.process()) - f.write(buf) + if streaming and buf: + f.write(buf) if eod : break f.close() - +print encoder.pipe -- 2.39.5