From da7149ee670f4fcc295dcee86f1a7382d2142e08 Mon Sep 17 00:00:00 2001 From: Huyen Nguyen <25715018+huyenltnguyen@users.noreply.github.com> Date: Sat, 28 Sep 2019 21:22:59 +0700 Subject: [PATCH 1/3] Get stream based on bitrate and listeners --- src/components/App.js | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/components/App.js b/src/components/App.js index e11aa44..7a53880 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -252,26 +252,37 @@ export default class App extends React.Component { } } - // choose either high or low bitrate - bitrateFinder(streames, low = false) { - let arr = streames.sort( - (a, b) => parseFloat(a.bitrate) - parseFloat(b.bitrate) - ); - if (low) return arr[0].url; - return arr[arr.length - 1].url; + streamFinder(streams, lowBitrate = false) { + let arr = streams.sort((a, b) => { + if (lowBitrate) { + // sort by bitrate from low to high + if (parseFloat(a.bitrate) < parseFloat(b.bitrate)) return -1; + if (parseFloat(a.bitrate) > parseFloat(b.bitrate)) return 1; + } else { + // sort by bitrate, from high to low + if (parseFloat(a.bitrate) < parseFloat(b.bitrate)) return 1; + if (parseFloat(a.bitrate) > parseFloat(b.bitrate)) return -1; + } + + // if both items have the same bitrate, sort by listeners from low to high + if (a.listeners.current < b.listeners.current) return -1; + if (a.listeners.current > b.listeners.current) return 1; + return 0; + }); + return arr[0].url; } // choose the stream based on the connection and availablity of relay(remotes) setMountToConnection(mounts = [], remotes = []) { let url = null; if (this.state.fastConnection === false && remotes.length > 0) { - url = this.bitrateFinder(remotes, true); + url = this.streamFinder(remotes, true); } else if (this.state.fastConnection && remotes.length > 0) { - url = this.bitrateFinder(remotes); + url = this.streamFinder(remotes); } else if (this.state.fastConnection === false) { - url = this.bitrateFinder(mounts, true); + url = this.streamFinder(mounts, true); } else { - url = this.bitrateFinder(mounts); + url = this.streamFinder(mounts); } this._player.src = url; this.setState({ From 80847d5002c30fb1de029bff2e94cde56c54f6cb Mon Sep 17 00:00:00 2001 From: Huyen Nguyen <25715018+huyenltnguyen@users.noreply.github.com> Date: Sat, 4 Apr 2020 02:12:44 +0700 Subject: [PATCH 2/3] refactor and handle error --- src/components/App.js | 41 +++++++++++++++++++++++++++++++--------- src/components/Footer.js | 2 +- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/components/App.js b/src/components/App.js index 7a53880..a1fec99 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -252,8 +252,8 @@ export default class App extends React.Component { } } - streamFinder(streams, lowBitrate = false) { - let arr = streams.sort((a, b) => { + sortStreams = (streams, lowBitrate = false) => { + return streams.sort((a, b) => { if (lowBitrate) { // sort by bitrate from low to high if (parseFloat(a.bitrate) < parseFloat(b.bitrate)) return -1; @@ -269,20 +269,24 @@ export default class App extends React.Component { if (a.listeners.current > b.listeners.current) return 1; return 0; }); - return arr[0].url; - } + }; + + getStreamUrl = (streams, lowBitrate) => { + const sorted = this.sortStreams(streams, lowBitrate); + return sorted[0].url; + }; // choose the stream based on the connection and availablity of relay(remotes) setMountToConnection(mounts = [], remotes = []) { let url = null; if (this.state.fastConnection === false && remotes.length > 0) { - url = this.streamFinder(remotes, true); + url = this.getStreamUrl(remotes, true); } else if (this.state.fastConnection && remotes.length > 0) { - url = this.streamFinder(remotes); + url = this.getStreamUrl(remotes); } else if (this.state.fastConnection === false) { - url = this.streamFinder(mounts, true); + url = this.getStreamUrl(mounts, true); } else { - url = this.streamFinder(mounts); + url = this.getStreamUrl(mounts); } this._player.src = url; this.setState({ @@ -339,6 +343,21 @@ export default class App extends React.Component { ) ); + onPlayerError = () => { + const { mounts, remotes, url } = this.state; + + // Get the stream list, sorted by bitrate, from high to low, + // find and move the current url to the beginning of the array. + const sortedStreams = this.sortStreams([...mounts, ...remotes]).filter( + stream => stream.url !== url + ); + const currentStream = sortedStreams.find(stream => stream.url === url); + sortedStreams.unshift(currentStream); + + // Then play the next item in the array + this.setUrl(sortedStreams[1].url); + }; + render() { return ( @@ -349,7 +368,11 @@ export default class App extends React.Component { player={this._player} playing={this.state.playing} /> -