From 58d6dadca40827f98976724901715b0362037aa4 Mon Sep 17 00:00:00 2001 From: MaZderMind Date: Tue, 2 Feb 2016 23:48:03 +0100 Subject: add base for a multi-file streaming script --- .../gstreamer/source-nostream-music-from-folder.py | 87 ++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100755 example-scripts/gstreamer/source-nostream-music-from-folder.py (limited to 'example-scripts') diff --git a/example-scripts/gstreamer/source-nostream-music-from-folder.py b/example-scripts/gstreamer/source-nostream-music-from-folder.py new file mode 100755 index 0000000..87ca3c7 --- /dev/null +++ b/example-scripts/gstreamer/source-nostream-music-from-folder.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python3 +import sys, gi, signal + +gi.require_version('Gst', '1.0') +from gi.repository import Gst, GObject + +# init GObject & Co. before importing local classes +GObject.threads_init() +Gst.init([]) + +class LoopSource(object): + def __init__(self): + pipeline = """ + audioresample name=join ! + audioconvert ! + audio/x-raw,format=S16LE,channels=2,layout=interleaved,rate=48000 ! + matroskamux ! + tcpclientsink host=localhost port=18000 + """ + + self.pipeline = Gst.parse_launch(pipeline) + + # https://c3voc.mazdermind.de/testfiles/music-snippets.tar + + self.src = Gst.ElementFactory.make('uridecodebin', None) + self.src.set_property('uri', 'file:///home/peter/Music/pieces/001 - Bruno Mars - Grenade.mp3'); + self.src.connect('pad-added', self.on_pad_added) + self.pipeline.add(self.src) + + self.joinpad = self.pipeline.get_by_name('join').get_static_pad('sink') + + # Binding End-of-Stream-Signal on Source-Pipeline + self.pipeline.bus.add_signal_watch() + self.pipeline.bus.connect("message::eos", self.on_eos) + self.pipeline.bus.connect("message::error", self.on_error) + + print("playing") + self.pipeline.set_state(Gst.State.PLAYING) + + def on_pad_added(self, src, pad): + print('New Pad: '+str(pad)) + pad.add_probe(Gst.PadProbeType.EVENT_DOWNSTREAM | Gst.PadProbeType.BLOCK, self.on_pad_event) + if self.joinpad.is_linked(): + self.joinpad.unlink(self.joinpad.get_peer()) + pad.link(self.joinpad) + + def on_pad_event(self, pad, info): + event = info.get_event() + print('Pad Event: '+str(event.type)+' on Pad '+str(pad)) + if event.type == Gst.EventType.EOS: + print('Is an EOS event, dropping & unlinking') + GObject.idle_add(self.next_track) + return Gst.PadProbeReturn.DROP + + return Gst.PadProbeReturn.PASS + + def next_track(self): + print("next_track") + self.src.set_state(Gst.State.READY) + self.src.set_property('uri', 'file:///home/peter/Music/pieces/003 - Taio Cruz feat. Kylie Minogue - Higher.mp3'); + self.src.set_state(Gst.State.PLAYING) + return False + + def on_eos(self, bus, message): + print('Received EOS-Signal') + sys.exit(1) + + def on_error(self, bus, message): + print('Received Error-Signal') + (error, debug) = message.parse_error() + print('Error-Details: #%u: %s' % (error.code, debug)) + sys.exit(1) + +def main(): + signal.signal(signal.SIGINT, signal.SIG_DFL) + + src = LoopSource() + + mainloop = GObject.MainLoop() + try: + mainloop.run() + except KeyboardInterrupt: + print('Terminated via Ctrl-C') + + +if __name__ == '__main__': + main() -- cgit v1.2.3 From dedd68862d1bd66a2fa40f4524a1704096c5ff15 Mon Sep 17 00:00:00 2001 From: MaZderMind Date: Tue, 2 Feb 2016 23:59:19 +0100 Subject: change complete pipeline state, fixes timing things --- example-scripts/gstreamer/source-nostream-music-from-folder.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'example-scripts') diff --git a/example-scripts/gstreamer/source-nostream-music-from-folder.py b/example-scripts/gstreamer/source-nostream-music-from-folder.py index 87ca3c7..8bd77c2 100755 --- a/example-scripts/gstreamer/source-nostream-music-from-folder.py +++ b/example-scripts/gstreamer/source-nostream-music-from-folder.py @@ -56,9 +56,9 @@ class LoopSource(object): def next_track(self): print("next_track") - self.src.set_state(Gst.State.READY) + self.pipeline.set_state(Gst.State.READY) self.src.set_property('uri', 'file:///home/peter/Music/pieces/003 - Taio Cruz feat. Kylie Minogue - Higher.mp3'); - self.src.set_state(Gst.State.PLAYING) + self.pipeline.set_state(Gst.State.PLAYING) return False def on_eos(self, bus, message): -- cgit v1.2.3 From 766264600f1811068ff6d55c4995c2b9cecc9f9a Mon Sep 17 00:00:00 2001 From: MaZderMind Date: Thu, 4 Feb 2016 21:17:39 +0100 Subject: implement inotify directory listing --- .../gstreamer/source-nostream-music-from-folder.py | 83 ++++++++++++++++++++-- 1 file changed, 79 insertions(+), 4 deletions(-) (limited to 'example-scripts') diff --git a/example-scripts/gstreamer/source-nostream-music-from-folder.py b/example-scripts/gstreamer/source-nostream-music-from-folder.py index 8bd77c2..fb09247 100755 --- a/example-scripts/gstreamer/source-nostream-music-from-folder.py +++ b/example-scripts/gstreamer/source-nostream-music-from-folder.py @@ -1,15 +1,79 @@ #!/usr/bin/env python3 -import sys, gi, signal +import os, sys, gi, signal +import argparse, logging, pyinotify gi.require_version('Gst', '1.0') -from gi.repository import Gst, GObject +from gi.repository import Gst, GObject, GLib # init GObject & Co. before importing local classes GObject.threads_init() Gst.init([]) +class Directory(object): + def __init__(self, path): + self.log = logging.getLogger('Directory') + self.path = path + self.scheduled = False + self.rescan() + + self.log.info('setting up inotify watch for %s', self.path) + wm = pyinotify.WatchManager() + notifier = pyinotify.Notifier(wm, + timeout=10, + default_proc_fun=self.inotify_callback) + + wm.add_watch( + self.path, + #pyinotify.ALL_EVENTS, + pyinotify.IN_DELETE | pyinotify.IN_CREATE | pyinotify.IN_MODIFY, + rec=True) + + GLib.io_add_watch( + notifier._fd, + GLib.IO_IN, + self.io_callback, + notifier) + + def inotify_callback(self, notifier): + self.log.info('inotify callback %s: %s', notifier.maskname, notifier.pathname) + if not self.scheduled: + self.scheduled = True + GLib.timeout_add(100, self.rescan) + return True + + def io_callback(self, source, condition, notifier): + notifier.process_events() + while notifier.check_events(): + notifier.read_events() + notifier.process_events() + + return True + + def is_playable_file(self, filepath): + root, ext = os.path.splitext(filepath) + return ext in ['.mp3', '.ogg', '.oga', '.wav', '.m4a', '.flac', 'self.opus'] + + def rescan(self): + self.log.info('scanning directory %s', self.path) + self.scheduled = False + + all_files = [] + + for root, dirs, files in os.walk(self.path): + files = filter(self.is_playable_file, files) + files = map(lambda f: os.path.join(root, f), files) + files = list(files) + + self.log.debug('found directory %s: %u playable file(s)', root, len(files)) + all_files.extend(files) + + self.log.info('found %u playable files', len(all_files)) + self.files = all_files + + class LoopSource(object): - def __init__(self): + def __init__(self, directory): + self.log = logging.getLogger('LoopSource') pipeline = """ audioresample name=join ! audioconvert ! @@ -72,9 +136,20 @@ class LoopSource(object): sys.exit(1) def main(): + logging.basicConfig( + level=logging.DEBUG, + format='%(levelname)8s %(name)s: %(message)s') + signal.signal(signal.SIGINT, signal.SIG_DFL) - src = LoopSource() + parser = argparse.ArgumentParser(description='Voctocore Music-Source') + parser.add_argument('directory') + + args = parser.parse_args() + print('Playing from Directory '+args.directory) + + directory = Directory(args.directory) + #src = LoopSource(directory) mainloop = GObject.MainLoop() try: -- cgit v1.2.3 From 13b2bde9fb9913b1a3e3b1cbd46fa006ec1e476c Mon Sep 17 00:00:00 2001 From: MaZderMind Date: Tue, 9 Feb 2016 21:17:34 +0100 Subject: implement random music playback --- .../gstreamer/source-nostream-music-from-folder.py | 46 ++++++++++++++++------ 1 file changed, 33 insertions(+), 13 deletions(-) (limited to 'example-scripts') diff --git a/example-scripts/gstreamer/source-nostream-music-from-folder.py b/example-scripts/gstreamer/source-nostream-music-from-folder.py index fb09247..ea99aea 100755 --- a/example-scripts/gstreamer/source-nostream-music-from-folder.py +++ b/example-scripts/gstreamer/source-nostream-music-from-folder.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -import os, sys, gi, signal +import os, sys, gi, signal, random import argparse, logging, pyinotify gi.require_version('Gst', '1.0') @@ -70,10 +70,18 @@ class Directory(object): self.log.info('found %u playable files', len(all_files)) self.files = all_files + def get_random_file(self): + return random.choice(self.files) + + def get_random_uri(self): + return 'file://'+self.get_random_file() + class LoopSource(object): def __init__(self, directory): self.log = logging.getLogger('LoopSource') + self.directory = directory + pipeline = """ audioresample name=join ! audioconvert ! @@ -82,15 +90,21 @@ class LoopSource(object): tcpclientsink host=localhost port=18000 """ + # Parsing Pipeline + self.log.debug('creating pipeline\n%s', pipeline) self.pipeline = Gst.parse_launch(pipeline) - # https://c3voc.mazdermind.de/testfiles/music-snippets.tar + # Selecting inital URI + inital_uri = self.directory.get_random_uri() + self.log.info('starting with track %s', inital_uri) + # Create decoder-element self.src = Gst.ElementFactory.make('uridecodebin', None) - self.src.set_property('uri', 'file:///home/peter/Music/pieces/001 - Bruno Mars - Grenade.mp3'); + self.src.set_property('uri', inital_uri); self.src.connect('pad-added', self.on_pad_added) self.pipeline.add(self.src) + # Save pad on the Join-Element self.joinpad = self.pipeline.get_by_name('join').get_static_pad('sink') # Binding End-of-Stream-Signal on Source-Pipeline @@ -98,41 +112,47 @@ class LoopSource(object): self.pipeline.bus.connect("message::eos", self.on_eos) self.pipeline.bus.connect("message::error", self.on_error) - print("playing") + self.log.info('setting pipeline to playing') self.pipeline.set_state(Gst.State.PLAYING) def on_pad_added(self, src, pad): - print('New Pad: '+str(pad)) + self.log.debug('new pad on decoder, setting pad-probe') pad.add_probe(Gst.PadProbeType.EVENT_DOWNSTREAM | Gst.PadProbeType.BLOCK, self.on_pad_event) if self.joinpad.is_linked(): + self.log.debug('unlinking with joinpad') self.joinpad.unlink(self.joinpad.get_peer()) + + self.log.debug('linking with joinpad') pad.link(self.joinpad) def on_pad_event(self, pad, info): event = info.get_event() - print('Pad Event: '+str(event.type)+' on Pad '+str(pad)) + self.log.debug('event %s on pad %s', event.type, pad) + if event.type == Gst.EventType.EOS: - print('Is an EOS event, dropping & unlinking') + self.log.debug('scheduling next track and dropping EOS-Event') GObject.idle_add(self.next_track) return Gst.PadProbeReturn.DROP return Gst.PadProbeReturn.PASS def next_track(self): - print("next_track") + next_uri = self.directory.get_random_uri() + self.log.info('using next track %s', next_uri) + self.pipeline.set_state(Gst.State.READY) - self.src.set_property('uri', 'file:///home/peter/Music/pieces/003 - Taio Cruz feat. Kylie Minogue - Higher.mp3'); + self.src.set_property('uri', next_uri); self.pipeline.set_state(Gst.State.PLAYING) return False def on_eos(self, bus, message): - print('Received EOS-Signal') + self.log.info('received EOS-Event on bus, exiting') sys.exit(1) def on_error(self, bus, message): - print('Received Error-Signal') + self.log.warning('received Error-Event on bus, exiting') (error, debug) = message.parse_error() - print('Error-Details: #%u: %s' % (error.code, debug)) + self.log.warning('Error-Details: #%u: %s', error.code, debug) sys.exit(1) def main(): @@ -149,7 +169,7 @@ def main(): print('Playing from Directory '+args.directory) directory = Directory(args.directory) - #src = LoopSource(directory) + src = LoopSource(directory) mainloop = GObject.MainLoop() try: -- cgit v1.2.3 From e26af9436a9c84f111b1b8d40d6f565a6d8738cd Mon Sep 17 00:00:00 2001 From: MaZderMind Date: Tue, 9 Feb 2016 21:50:58 +0100 Subject: finish logging --- .../gstreamer/source-nostream-music-from-folder.py | 27 ++++++++++++++-------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'example-scripts') diff --git a/example-scripts/gstreamer/source-nostream-music-from-folder.py b/example-scripts/gstreamer/source-nostream-music-from-folder.py index ea99aea..a25a58a 100755 --- a/example-scripts/gstreamer/source-nostream-music-from-folder.py +++ b/example-scripts/gstreamer/source-nostream-music-from-folder.py @@ -16,7 +16,7 @@ class Directory(object): self.scheduled = False self.rescan() - self.log.info('setting up inotify watch for %s', self.path) + self.log.debug('setting up inotify watch for %s', self.path) wm = pyinotify.WatchManager() notifier = pyinotify.Notifier(wm, timeout=10, @@ -96,7 +96,7 @@ class LoopSource(object): # Selecting inital URI inital_uri = self.directory.get_random_uri() - self.log.info('starting with track %s', inital_uri) + self.log.info('initial track %s', inital_uri) # Create decoder-element self.src = Gst.ElementFactory.make('uridecodebin', None) @@ -112,7 +112,7 @@ class LoopSource(object): self.pipeline.bus.connect("message::eos", self.on_eos) self.pipeline.bus.connect("message::error", self.on_error) - self.log.info('setting pipeline to playing') + self.log.debug('setting pipeline to playing') self.pipeline.set_state(Gst.State.PLAYING) def on_pad_added(self, src, pad): @@ -138,7 +138,7 @@ class LoopSource(object): def next_track(self): next_uri = self.directory.get_random_uri() - self.log.info('using next track %s', next_uri) + self.log.info('next track %s', next_uri) self.pipeline.set_state(Gst.State.READY) self.src.set_property('uri', next_uri); @@ -156,17 +156,26 @@ class LoopSource(object): sys.exit(1) def main(): - logging.basicConfig( - level=logging.DEBUG, - format='%(levelname)8s %(name)s: %(message)s') - signal.signal(signal.SIGINT, signal.SIG_DFL) parser = argparse.ArgumentParser(description='Voctocore Music-Source') parser.add_argument('directory') + parser.add_argument('-v|-vv', '--verbose', action='count', default=0, + help="Also print INFO and DEBUG messages.") + args = parser.parse_args() - print('Playing from Directory '+args.directory) + + if args.verbose >= 2: + level = logging.DEBUG + elif args.verbose == 1: + level = logging.INFO + else: + level = logging.WARNING + + logging.basicConfig( + level=level, + format='%(levelname)8s %(name)s: %(message)s') directory = Directory(args.directory) src = LoopSource(directory) -- cgit v1.2.3 From 5352bfa148eecb02eed944c8b9e72cb16abc3cc7 Mon Sep 17 00:00:00 2001 From: MaZderMind Date: Tue, 9 Feb 2016 21:51:05 +0100 Subject: fix timing --- example-scripts/gstreamer/source-nostream-music-from-folder.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'example-scripts') diff --git a/example-scripts/gstreamer/source-nostream-music-from-folder.py b/example-scripts/gstreamer/source-nostream-music-from-folder.py index a25a58a..a176a7d 100755 --- a/example-scripts/gstreamer/source-nostream-music-from-folder.py +++ b/example-scripts/gstreamer/source-nostream-music-from-folder.py @@ -122,6 +122,12 @@ class LoopSource(object): self.log.debug('unlinking with joinpad') self.joinpad.unlink(self.joinpad.get_peer()) + clock = self.pipeline.get_clock() + if clock: + runtime = clock.get_time() - self.pipeline.get_base_time() + self.log.debug('setting pad offset to pipeline runtime: %sns', runtime) + pad.set_offset(runtime) + self.log.debug('linking with joinpad') pad.link(self.joinpad) @@ -140,9 +146,9 @@ class LoopSource(object): next_uri = self.directory.get_random_uri() self.log.info('next track %s', next_uri) - self.pipeline.set_state(Gst.State.READY) + self.src.set_state(Gst.State.READY) self.src.set_property('uri', next_uri); - self.pipeline.set_state(Gst.State.PLAYING) + self.src.set_state(Gst.State.PLAYING) return False def on_eos(self, bus, message): -- cgit v1.2.3