From 16dadd5ddc63f9c46745a37540dbd3d14099bd13 Mon Sep 17 00:00:00 2001 From: Alexandru Branza Date: Sun, 18 Feb 2024 03:13:52 +0200 Subject: [PATCH 1/4] Do Not Trust `HTMLVideo` with Supported Types --- .../withStreamingServer.js | 49 ++++++++----------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/src/withStreamingServer/withStreamingServer.js b/src/withStreamingServer/withStreamingServer.js index e8ff622..e170766 100644 --- a/src/withStreamingServer/withStreamingServer.js +++ b/src/withStreamingServer/withStreamingServer.js @@ -341,36 +341,29 @@ function withStreamingServer(Video) { } VideoWithStreamingServer.canPlayStream = function(stream, options) { - return Video.canPlayStream(stream) - .then(function(canPlay) { - if (!canPlay) { - throw new Error('Fallback using /hlsv2/probe'); - } - - return canPlay; + var queryParams = new URLSearchParams([['mediaURL', stream.url]]); + return fetch(url.resolve(options.streamingServerURL, '/hlsv2/probe?' + queryParams.toString())) + .then(function(resp) { + return resp.json(); }) - .catch(function() { - var queryParams = new URLSearchParams([['mediaURL', stream.url]]); - return fetch(url.resolve(options.streamingServerURL, '/hlsv2/probe?' + queryParams.toString())) - .then(function(resp) { - return resp.json(); - }) - .then(function(probe) { - var isFormatSupported = options.formats.some(function(format) { - return probe.format.name.indexOf(format) !== -1; - }); - var areStreamsSupported = probe.streams.every(function(stream) { - if (stream.track === 'audio') { - return stream.channels <= options.maxAudioChannels && - options.audioCodecs.indexOf(stream.codec) !== -1; - } else if (stream.track === 'video') { - return options.videoCodecs.indexOf(stream.codec) !== -1; - } + .then(function(probe) { + var isFormatSupported = options.formats.some(function(format) { + return probe.format.name.indexOf(format) !== -1; + }); + var areStreamsSupported = probe.streams.every(function(stream) { + if (stream.track === 'audio') { + return stream.channels <= options.maxAudioChannels && + options.audioCodecs.indexOf(stream.codec) !== -1; + } else if (stream.track === 'video') { + return options.videoCodecs.indexOf(stream.codec) !== -1; + } - return true; - }); - return isFormatSupported && areStreamsSupported; - }); + return true; + }); + return isFormatSupported && areStreamsSupported; + }) + .catch(function(err) { + return Video.canPlayStream(stream) }); }; From 2518e29da4682a21f3d44dfb9b3c704474f01ca3 Mon Sep 17 00:00:00 2001 From: Alexandru Branza Date: Sun, 18 Feb 2024 03:17:21 +0200 Subject: [PATCH 2/4] Lint --- src/withStreamingServer/withStreamingServer.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/withStreamingServer/withStreamingServer.js b/src/withStreamingServer/withStreamingServer.js index e170766..a57c533 100644 --- a/src/withStreamingServer/withStreamingServer.js +++ b/src/withStreamingServer/withStreamingServer.js @@ -362,8 +362,8 @@ function withStreamingServer(Video) { }); return isFormatSupported && areStreamsSupported; }) - .catch(function(err) { - return Video.canPlayStream(stream) + .catch(function() { + return Video.canPlayStream(stream); }); }; From 0ba97b7b05a63bf057f4b93683fd2f77756b469c Mon Sep 17 00:00:00 2001 From: Alexandru Branza Date: Sun, 18 Feb 2024 04:03:08 +0200 Subject: [PATCH 3/4] Trust Video Player Implementation When Transcoding is Not Possible Maybe this could be checked with a server endpoint in the future.. --- src/supportsTranscoding.js | 8 +++ .../withStreamingServer.js | 53 +++++++++++-------- 2 files changed, 40 insertions(+), 21 deletions(-) create mode 100644 src/supportsTranscoding.js diff --git a/src/supportsTranscoding.js b/src/supportsTranscoding.js new file mode 100644 index 0000000..da0e22a --- /dev/null +++ b/src/supportsTranscoding.js @@ -0,0 +1,8 @@ +function supportsTranscoding() { + if (typeof global.tizen !== 'undefined' || typeof global.webOS !== 'undefined') { + return Promise.resolve(false); + } + return Promise.resolve(true); +} + +module.exports = supportsTranscoding; diff --git a/src/withStreamingServer/withStreamingServer.js b/src/withStreamingServer/withStreamingServer.js index a57c533..75ee656 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 supportsTranscoding = require('../supportsTranscoding'); var ERROR = require('../error'); function withStreamingServer(Video) { @@ -342,28 +343,38 @@ function withStreamingServer(Video) { VideoWithStreamingServer.canPlayStream = function(stream, options) { var queryParams = new URLSearchParams([['mediaURL', stream.url]]); - return fetch(url.resolve(options.streamingServerURL, '/hlsv2/probe?' + queryParams.toString())) - .then(function(resp) { - return resp.json(); - }) - .then(function(probe) { - var isFormatSupported = options.formats.some(function(format) { - return probe.format.name.indexOf(format) !== -1; - }); - var areStreamsSupported = probe.streams.every(function(stream) { - if (stream.track === 'audio') { - return stream.channels <= options.maxAudioChannels && - options.audioCodecs.indexOf(stream.codec) !== -1; - } else if (stream.track === 'video') { - return options.videoCodecs.indexOf(stream.codec) !== -1; - } + return supportsTranscoding() + .then(function(supported) { + if (!supported) { + // we cannot probe the video in this case + return Video.canPlayStream(stream); + } + // probing normally gives more accurate results + return fetch(url.resolve(options.streamingServerURL, '/hlsv2/probe?' + queryParams.toString())) + .then(function(resp) { + return resp.json(); + }) + .then(function(probe) { + var isFormatSupported = options.formats.some(function(format) { + return probe.format.name.indexOf(format) !== -1; + }); + var areStreamsSupported = probe.streams.every(function(stream) { + if (stream.track === 'audio') { + return stream.channels <= options.maxAudioChannels && + options.audioCodecs.indexOf(stream.codec) !== -1; + } else if (stream.track === 'video') { + return options.videoCodecs.indexOf(stream.codec) !== -1; + } - return true; - }); - return isFormatSupported && areStreamsSupported; - }) - .catch(function() { - return Video.canPlayStream(stream); + return true; + }); + return isFormatSupported && areStreamsSupported; + }) + .catch(function() { + // this uses content-type header in HTMLVideo which + // is unreliable, check can also fail due to CORS + return Video.canPlayStream(stream); + }); }); }; From 5810e3ea20df9fc8f203dd23f11de88d8d51cafd Mon Sep 17 00:00:00 2001 From: Alexandru Branza Date: Sun, 18 Feb 2024 04:10:36 +0200 Subject: [PATCH 4/4] Move Line for Readability --- src/withStreamingServer/withStreamingServer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/withStreamingServer/withStreamingServer.js b/src/withStreamingServer/withStreamingServer.js index 75ee656..aef81d9 100644 --- a/src/withStreamingServer/withStreamingServer.js +++ b/src/withStreamingServer/withStreamingServer.js @@ -342,7 +342,6 @@ function withStreamingServer(Video) { } VideoWithStreamingServer.canPlayStream = function(stream, options) { - var queryParams = new URLSearchParams([['mediaURL', stream.url]]); return supportsTranscoding() .then(function(supported) { if (!supported) { @@ -350,6 +349,7 @@ function withStreamingServer(Video) { return Video.canPlayStream(stream); } // probing normally gives more accurate results + var queryParams = new URLSearchParams([['mediaURL', stream.url]]); return fetch(url.resolve(options.streamingServerURL, '/hlsv2/probe?' + queryParams.toString())) .then(function(resp) { return resp.json();