Skip to content

Commit 322dae4

Browse files
OwenEdwardsgkatsev
authored andcommitted
feat(middleware): allow middleware to handle volume setter and getter (#5906)
Allow middleware to handle volume setter and getter. This supports things like ducking the playback volume programmatically, without affecting the player's UI volume control.
1 parent 1a52b69 commit 322dae4

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

src/js/tech/middleware.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ export const allowedGetters = {
186186
duration: 1,
187187
seekable: 1,
188188
played: 1,
189-
paused: 1
189+
paused: 1,
190+
volume: 1
190191
};
191192

192193
/**
@@ -195,7 +196,8 @@ export const allowedGetters = {
195196
* @type {Object}
196197
*/
197198
export const allowedSetters = {
198-
setCurrentTime: 1
199+
setCurrentTime: 1,
200+
setVolume: 1
199201
};
200202

201203
/**

test/unit/play.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ const mainModule = function(playReturnValue, middlewareTermination, subhooks) {
414414

415415
// without enableSourceset this test will fail.
416416
QUnit.test('Player#play() resolves correctly on tech el src', function(assert) {
417-
this.player = TestHelpers.makePlayer({techOrder: ['html5'], enableSourceset: true}, null, false);
417+
this.player = TestHelpers.makePlayer({techOrder: ['html5'], enableSourceset: true});
418418

419419
this.playTest('player/tech start out ready', {
420420
techReady: true,

test/unit/player.test.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1687,13 +1687,17 @@ QUnit.test('should not allow to register custom player when any player has been
16871687

16881688
QUnit.test('techGet runs through middleware if allowedGetter', function(assert) {
16891689
let cts = 0;
1690+
let vols = 0;
16901691
let durs = 0;
16911692
let lps = 0;
16921693

16931694
videojs.use('video/foo', () => ({
16941695
currentTime() {
16951696
cts++;
16961697
},
1698+
volume() {
1699+
vols++;
1700+
},
16971701
duration() {
16981702
durs++;
16991703
},
@@ -1714,10 +1718,12 @@ QUnit.test('techGet runs through middleware if allowedGetter', function(assert)
17141718
player.middleware_ = [middleware.getMiddleware('video/foo')[0](player)];
17151719

17161720
player.techGet_('currentTime');
1721+
player.techGet_('volume');
17171722
player.techGet_('duration');
17181723
player.techGet_('loop');
17191724

17201725
assert.equal(cts, 1, 'currentTime is allowed');
1726+
assert.equal(vols, 1, 'volume is allowed');
17211727
assert.equal(durs, 1, 'duration is allowed');
17221728
assert.equal(lps, 0, 'loop is not allowed');
17231729

@@ -1728,6 +1734,7 @@ QUnit.test('techGet runs through middleware if allowedGetter', function(assert)
17281734
QUnit.test('techCall runs through middleware if allowedSetter', function(assert) {
17291735
let cts = 0;
17301736
let vols = 0;
1737+
let prs = 0;
17311738

17321739
videojs.use('video/foo', () => ({
17331740
setCurrentTime(ct) {
@@ -1736,6 +1743,11 @@ QUnit.test('techCall runs through middleware if allowedSetter', function(assert)
17361743
},
17371744
setVolume() {
17381745
vols++;
1746+
return vols;
1747+
},
1748+
setPlaybackRate() {
1749+
prs++;
1750+
return prs;
17391751
}
17401752
}));
17411753

@@ -1754,11 +1766,13 @@ QUnit.test('techCall runs through middleware if allowedSetter', function(assert)
17541766

17551767
player.techCall_('setCurrentTime', 10);
17561768
player.techCall_('setVolume', 0.5);
1769+
player.techCall_('setPlaybackRate', 0.75);
17571770

17581771
this.clock.tick(1);
17591772

17601773
assert.equal(cts, 1, 'setCurrentTime is allowed');
1761-
assert.equal(vols, 0, 'setVolume is not allowed');
1774+
assert.equal(vols, 1, 'setVolume is allowed');
1775+
assert.equal(prs, 0, 'setPlaybackRate is not allowed');
17621776

17631777
middleware.getMiddleware('video/foo').pop();
17641778
player.dispose();

test/unit/test-helpers.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const TestHelpers = {
1111
return videoTag;
1212
},
1313

14-
makePlayer(playerOptions, videoTag, addTechAsMiddleware = true) {
14+
makePlayer(playerOptions, videoTag) {
1515
videoTag = videoTag || TestHelpers.makeTag();
1616

1717
const fixture = document.getElementById('qunit-fixture');
@@ -21,13 +21,7 @@ const TestHelpers = {
2121
playerOptions = playerOptions || {};
2222
playerOptions.techOrder = playerOptions.techOrder || ['techFaker'];
2323

24-
const player = new Player(videoTag, playerOptions);
25-
26-
if (addTechAsMiddleware) {
27-
player.middleware_ = [player.tech_];
28-
}
29-
30-
return player;
24+
return new Player(videoTag, playerOptions);
3125
},
3226

3327
getComputedStyle(el, rule) {

0 commit comments

Comments
 (0)