From 70bba5fc9609fa02c791abe3aa0a66480a458aad Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Wed, 26 Jun 2019 11:05:16 +0200 Subject: [PATCH] Support for browser webplayer and subtitles in new webplugin --- .../http/static/js/plugins/media/index.js | 20 ++++++++++++++----- .../js/plugins/media/players/browser.js | 9 ++++++++- .../static/js/plugins/media/players/local.js | 4 ++-- platypush/plugins/media/__init__.py | 7 +++++-- platypush/plugins/media/search/local.py | 2 +- 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/platypush/backend/http/static/js/plugins/media/index.js b/platypush/backend/http/static/js/plugins/media/index.js index c1e4b5227..f83bd90e4 100644 --- a/platypush/backend/http/static/js/plugins/media/index.js +++ b/platypush/backend/http/static/js/plugins/media/index.js @@ -105,10 +105,10 @@ Vue.component('media', { play: async function(item) { if (!this.selectedDevice.accepts[item.type]) { - item = await this.startStreaming(item.url); + item = await this.startStreaming(item); } - let status = await this.selectedDevice.play(item.url, item.subtitles); + let status = await this.selectedDevice.play(item, item.subtitles); if (item.title) status.title = item.title; @@ -167,14 +167,24 @@ Vue.component('media', { }, startStreaming: async function(item) { - const resource = item instanceof Object ? item.url : item; + if (typeof item === 'string') + item = {url: item}; + const ret = await request('media.start_streaming', { - media: resource, + media: item.url, + subtitles: item.subtitles, }); + const hostRegex = /^(https?:\/\/[^:/]+(:[0-9]+)?\/?)/; + const baseURL = window.location.href.match(hostRegex)[1]; + + ret.url = ret.url.replace(hostRegex, baseURL); + if (ret.subtitles_url) + ret.subtitles_url = ret.subtitles_url.replace(hostRegex, baseURL); + this.bus.$emit('streaming-started', { url: ret.url, - resource: resource, + resource: item.url, }); return ret; diff --git a/platypush/backend/http/static/js/plugins/media/players/browser.js b/platypush/backend/http/static/js/plugins/media/players/browser.js index 31061c04a..e516adb19 100644 --- a/platypush/backend/http/static/js/plugins/media/players/browser.js +++ b/platypush/backend/http/static/js/plugins/media/players/browser.js @@ -38,7 +38,14 @@ MediaPlayers.browser = Vue.extend({ return {}; }, - play: async function(item) { + play: async function(item, subtitles) { + let url = item.url; + if (item.source && item.source.startsWith('file://')) + url += '?webplayer' + + let playerWindow = window.open(url, '_blank'); + console.log(playerWindow); + return {}; }, stop: async function() { diff --git a/platypush/backend/http/static/js/plugins/media/players/local.js b/platypush/backend/http/static/js/plugins/media/players/local.js index 8d9721841..1260130c5 100644 --- a/platypush/backend/http/static/js/plugins/media/players/local.js +++ b/platypush/backend/http/static/js/plugins/media/players/local.js @@ -53,10 +53,10 @@ MediaPlayers.local = Vue.extend({ return await request(this.pluginPrefix.concat('.status')); }, - play: async function(resource, subtitles=undefined) { + play: async function(item, subtitles=undefined) { return await request( this.pluginPrefix.concat('.play'), - {resource: resource, subtitles: subtitles} + {resource: item.url, subtitles: subtitles} ); }, diff --git a/platypush/plugins/media/__init__.py b/platypush/plugins/media/__init__.py index 11e9b3dc9..b871107f0 100644 --- a/platypush/plugins/media/__init__.py +++ b/platypush/plugins/media/__init__.py @@ -358,7 +358,7 @@ class MediaPlugin(Plugin): return filename.lower().split('.')[-1] in cls.audio_extensions @action - def start_streaming(self, media, download=False): + def start_streaming(self, media, subtitles=None, download=False): """ Starts streaming local media over the specified HTTP port. The stream will be available to HTTP clients on @@ -367,6 +367,9 @@ class MediaPlugin(Plugin): :param media: Media to stream :type media: str + :param subtitles: Path or URL to the subtitles track to be used + :type subtitles: str + :param download: Set to True if you prefer to download the file from the streaming link instead of streaming it :type download: bool @@ -391,7 +394,7 @@ class MediaPlugin(Plugin): self.logger.info('Starting streaming {}'.format(media)) response = requests.put('{url}/media{download}'.format( url=http.local_base_url, download='?download' if download else ''), - json={'source': media}) + json={'source': media, 'subtitles': subtitles}) if not response.ok: self.logger.warning('Unable to start streaming: {}'. diff --git a/platypush/plugins/media/search/local.py b/platypush/plugins/media/search/local.py index d79bbd7cd..204f39daa 100644 --- a/platypush/plugins/media/search/local.py +++ b/platypush/plugins/media/search/local.py @@ -215,7 +215,7 @@ class LocalMediaSearcher(MediaSearcher): filter(MediaToken.token.in_(query_tokens)). \ group_by(MediaFile.path). \ having(func.count(MediaFileToken.token_id) >= len(query_tokens)): - if (os.path.isfile(file_record.path)): + if os.path.isfile(file_record.path): results[file_record.path] = { 'url': 'file://' + file_record.path, 'title': os.path.basename(file_record.path),