diff --git a/src/streaming/MediaPlayer.js b/src/streaming/MediaPlayer.js index 47c6197cc2..a4ca2785e0 100644 --- a/src/streaming/MediaPlayer.js +++ b/src/streaming/MediaPlayer.js @@ -1435,6 +1435,19 @@ function MediaPlayer() { return mediaPlayerModel.getBandwidthSafetyFactor(); } + /** + * Returns the average throughput computed in the ABR logic + * + * @param {string} type + * @return {number} value + * @memberof module:MediaPlayer + * @instance + */ + function getAverageThroughput(type) { + const throughputHistory = abrController.getThroughputHistory(); + return throughputHistory ? throughputHistory.getAverageThroughput(type) : 0; + } + /** * A timeout value in seconds, which during the ABRController will block switch-up events. * This will only take effect after an abandoned fragment event occurs. @@ -2455,6 +2468,7 @@ function MediaPlayer() { removeAllABRCustomRule: removeAllABRCustomRule, setBandwidthSafetyFactor: setBandwidthSafetyFactor, getBandwidthSafetyFactor: getBandwidthSafetyFactor, + getAverageThroughput: getAverageThroughput, setAbandonLoadTimeout: setAbandonLoadTimeout, retrieveManifest: retrieveManifest, addUTCTimingSource: addUTCTimingSource, diff --git a/test/unit/mocks/AbrControllerMock.js b/test/unit/mocks/AbrControllerMock.js index b60104004e..95936a6e37 100644 --- a/test/unit/mocks/AbrControllerMock.js +++ b/test/unit/mocks/AbrControllerMock.js @@ -26,13 +26,16 @@ class AbrControllerMock{ this.limitBitrateByPortal = false; this.usePixelRatioInLimitBitrateByPortal = false; this.autoSwitchBitrate = {video: true, audio: true}; + this.throughputHistory = undefined; } initialize() {} createAbrRulesCollection() {} - reset() {} + reset() { + this.setup(); + } setConfig() {} @@ -162,9 +165,9 @@ class AbrControllerMock{ } - setAverageThroughput() {} - - getAverageThroughput() {} + getThroughputHistory() { + return this.throughputHistory; + } updateTopQualityIndex() {} diff --git a/test/unit/streaming.MediaPlayerSpec.js b/test/unit/streaming.MediaPlayerSpec.js index bc836ccfe5..108d8b1a4e 100644 --- a/test/unit/streaming.MediaPlayerSpec.js +++ b/test/unit/streaming.MediaPlayerSpec.js @@ -103,7 +103,7 @@ describe("MediaPlayer", function () { beforeEach(function () { player.initialize(videoElementMock, dummyUrl, false); }); - it("Method isReady should return false", function () { + it("Method isReady should return true", function () { let isReady = player.isReady(); expect(isReady).to.be.true; }); @@ -327,7 +327,7 @@ describe("MediaPlayer", function () { }); - describe("AutoBitrate Functions", function () { + describe("AbrController Functions", function () { afterEach(function () { abrControllerMock.reset(); }) @@ -457,6 +457,22 @@ describe("MediaPlayer", function () { expect(AutoSwitchBitrateFor).to.be.false; }); + it("Method getAverageThroughput should return 0 when throughputHistory is not set up", function() { + const averageThroughput = player.getAverageThroughput('video'); + expect(averageThroughput).to.equal(0); + }); + + it("Method getAverageThroughput should value computed from ThroughputHistory", function() { + const AVERAGE_THROUGHPUT = 2000; + abrControllerMock.throughputHistory = { + getAverageThroughput: function() { + return AVERAGE_THROUGHPUT; + } + }; + const averageThroughput = player.getAverageThroughput('video'); + expect(averageThroughput).to.equal(AVERAGE_THROUGHPUT); + }); + describe("When it is not initialized", function () { it("Method getQualityFor should throw an exception", function () { expect(player.getQualityFor).to.throw(PLAYBACK_NOT_INITIALIZED_ERROR);