From 1c25633a1805c89fcdeef3187d68536ddc8961a6 Mon Sep 17 00:00:00 2001 From: Alexandru Branza Date: Fri, 1 Mar 2024 05:38:15 +0200 Subject: [PATCH] Use `loaded` Prop --- src/TizenVideo/TizenVideo.js | 11 +++++- src/withStreamingServer/fetchVideoParams.js | 37 ++++++++++--------- src/withStreamingServer/isPlayerLoaded.js | 24 ++++++++++++ .../withStreamingServer.js | 7 +++- 4 files changed, 60 insertions(+), 19 deletions(-) create mode 100644 src/withStreamingServer/isPlayerLoaded.js diff --git a/src/TizenVideo/TizenVideo.js b/src/TizenVideo/TizenVideo.js index a503ee7..fec45df 100644 --- a/src/TizenVideo/TizenVideo.js +++ b/src/TizenVideo/TizenVideo.js @@ -130,8 +130,10 @@ function TizenVideo(options) { var stream = null; var retries = 0; var maxRetries = 5; + var isLoaded = null; var observedProps = { stream: false, + loaded: false, paused: false, time: false, duration: false, @@ -154,6 +156,9 @@ function TizenVideo(options) { case 'stream': { return stream; } + case 'loaded': { + return isLoaded; + } case 'paused': { if (stream === null) { return null; @@ -600,6 +605,8 @@ function TizenVideo(options) { onPropChanged('duration'); window.webapis.avplay.play(); + isLoaded = true; + onPropChanged('loaded'); onPropChanged('stream'); onPropChanged('paused'); onPropChanged('time'); @@ -638,6 +645,8 @@ function TizenVideo(options) { case 'unload': { stream = null; window.webapis.avplay.stop(); + isLoaded = false; + onPropChanged('loaded'); onPropChanged('stream'); onPropChanged('paused'); onPropChanged('time'); @@ -707,7 +716,7 @@ TizenVideo.canPlayStream = function() { TizenVideo.manifest = { name: 'TizenVideo', external: false, - props: ['stream', 'paused', 'time', 'duration', 'buffering', 'audioTracks', 'selectedAudioTrackId', 'subtitlesTracks', 'selectedSubtitlesTrackId', 'subtitlesOffset', 'subtitlesSize', 'subtitlesTextColor', 'subtitlesBackgroundColor', 'subtitlesOutlineColor', 'subtitlesOpacity', 'playbackSpeed'], + props: ['stream', 'loaded', 'paused', 'time', 'duration', 'buffering', 'audioTracks', 'selectedAudioTrackId', 'subtitlesTracks', 'selectedSubtitlesTrackId', 'subtitlesOffset', 'subtitlesSize', 'subtitlesTextColor', 'subtitlesBackgroundColor', 'subtitlesOutlineColor', 'subtitlesOpacity', 'playbackSpeed'], commands: ['load', 'unload', 'destroy'], events: ['propValue', 'propChanged', 'ended', 'error', 'subtitlesTrackLoaded', 'audioTrackLoaded'] }; diff --git a/src/withStreamingServer/fetchVideoParams.js b/src/withStreamingServer/fetchVideoParams.js index 6d36d75..f37f8e0 100644 --- a/src/withStreamingServer/fetchVideoParams.js +++ b/src/withStreamingServer/fetchVideoParams.js @@ -67,26 +67,29 @@ function fetchFilename(streamingServerURL, mediaURL, infoHash, fileIdx, behavior } function fetchVideoParams(streamingServerURL, mediaURL, infoHash, fileIdx, behaviorHints) { - var result = { hash: null, size: null, filename: null }; - // these need to be sequential as fetchOpensubtitlesParams - // ensures engine /stats.json availability - return fetchOpensubtitlesParams(streamingServerURL, mediaURL, behaviorHints) - .then(function(res) { - result.hash = res.hash; - result.size = res.size; - return fetchFilename(streamingServerURL, mediaURL, infoHash, fileIdx, behaviorHints); - }) - .then(function(res) { + return Promise.allSettled([ + fetchOpensubtitlesParams(streamingServerURL, mediaURL, behaviorHints), + fetchFilename(streamingServerURL, mediaURL, infoHash, fileIdx, behaviorHints) + ]).then(function(results) { + var result = { hash: null, size: null, filename: null }; - if (res) { - result.filename = res; - } + if (results[0].status === 'fulfilled') { + result.hash = results[0].value.hash; + result.size = results[0].value.size; + } else if (results[0].reason) { + // eslint-disable-next-line no-console + console.error(results[0].reason); + } - return result; - }).catch(function(e) { + if (results[1].status === 'fulfilled') { + result.filename = results[1].value; + } else if (results[1].reason) { // eslint-disable-next-line no-console - console.error(e); - }); + console.error(results[1].reason); + } + + return result; + }); } module.exports = fetchVideoParams; diff --git a/src/withStreamingServer/isPlayerLoaded.js b/src/withStreamingServer/isPlayerLoaded.js new file mode 100644 index 0000000..e0c04e8 --- /dev/null +++ b/src/withStreamingServer/isPlayerLoaded.js @@ -0,0 +1,24 @@ +function isPlayerLoaded(video, props) { + if (!props.includes('loaded')) { + return Promise.resolve(true); + } + return new Promise(function(resolve, reject) { + var isLoaded = null; + video.on('propChanged', function(propName, propValue) { + if (propName === 'loaded' && propValue !== null && isLoaded === null) { + isLoaded = propValue; + if (propValue === true) { + resolve(true); + } else if (propValue === false) { + reject(Error('Player failed to load, will not retrieve video params')); + } + } + }); + video.dispatch({ + type: 'observeProp', + propName: 'loaded' + }); + }); +} + +module.exports = isPlayerLoaded; diff --git a/src/withStreamingServer/withStreamingServer.js b/src/withStreamingServer/withStreamingServer.js index aef81d9..038d079 100644 --- a/src/withStreamingServer/withStreamingServer.js +++ b/src/withStreamingServer/withStreamingServer.js @@ -6,6 +6,7 @@ var deepFreeze = require('deep-freeze'); var mediaCapabilities = require('../mediaCapabilities'); var convertStream = require('./convertStream'); var fetchVideoParams = require('./fetchVideoParams'); +var isPlayerLoaded = require('./isPlayerLoaded'); var supportsTranscoding = require('../supportsTranscoding'); var ERROR = require('../error'); @@ -203,7 +204,11 @@ function withStreamingServer(Video) { }); loaded = true; flushActionsQueue(); - fetchVideoParams(commandArgs.streamingServerURL, result.mediaURL, result.infoHash, result.fileIdx, commandArgs.stream.behaviorHints) + + isPlayerLoaded(video, Video.manifest.props) + .then(function() { + return fetchVideoParams(commandArgs.streamingServerURL, result.mediaURL, result.infoHash, result.fileIdx, commandArgs.stream.behaviorHints); + }) .then(function(result) { if (commandArgs !== loadArgs) { return;