From 880a394d74b779f321618984d66953fa9bfd5ab5 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Wed, 6 Feb 2019 15:12:55 +0100 Subject: [PATCH] Synchronize with the streaming process to make sure that we start playing only when the streaming service is ready --- platypush/plugins/media/__init__.py | 19 +++++++++++++++++-- platypush/plugins/media/bin/localstream.js | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/platypush/plugins/media/__init__.py b/platypush/plugins/media/__init__.py index 6c3b88a0..cd951353 100644 --- a/platypush/plugins/media/__init__.py +++ b/platypush/plugins/media/__init__.py @@ -141,6 +141,8 @@ class MediaPlugin(Plugin): self._videos_queue = [] self._streaming_port = streaming_port self._streaming_proc = None + self._streaming_started = Event() + self._streaming_ended = Event() def _get_resource(self, resource): """ @@ -352,22 +354,33 @@ class MediaPlugin(Plugin): @action - def start_streaming(self, media): + def start_streaming(self, media, port=None): + """ + Starts streaming local media over the specified HTTP port. + The stream will be available to HTTP clients on + `http://{this-ip}:{port}/media + + :param media: Media to stream + """ if self._streaming_proc: self.logger.info('A streaming process is already running, ' + 'terminating it first') self.stop_streaming() + self._streaming_started.clear() + self._streaming_ended.clear() self._streaming_proc = subprocess.Popen( [self._local_stream_bin, media, str(self._streaming_port)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT ) threading.Thread(target=self._streaming_process_monitor(media)).start() - url = 'http://{}:{}/video'.format(get_ip_or_hostname(), + url = 'http://{}:{}/media'.format(get_ip_or_hostname(), self._streaming_port) self.logger.info('Starting streaming {} on {}'.format(media, url)) + self._streaming_started.wait() + self.logger.info('Started streaming {} on {}'.format(media, url)) return { 'url': url } @action @@ -398,6 +411,7 @@ class MediaPlugin(Plugin): continue if line.startswith('Listening on'): + self._streaming_started.set() break self.logger.info('Message from streaming service: {}'.format(line)) @@ -405,6 +419,7 @@ class MediaPlugin(Plugin): self._streaming_proc.wait() try: self.stop_streaming() except: pass + self._streaming_ended.set() self.logger.info('Streaming service terminated') return _thread diff --git a/platypush/plugins/media/bin/localstream.js b/platypush/plugins/media/bin/localstream.js index 39dd79e1..595c6860 100755 --- a/platypush/plugins/media/bin/localstream.js +++ b/platypush/plugins/media/bin/localstream.js @@ -30,7 +30,7 @@ function parseArgv() { let args = parseArgv() -app.get('/video', function(req, res) { +app.get('/media', function(req, res) { const path = args.file const ext = args.file.split('.').pop() const stat = fs.statSync(path)