Skip to content

Commit

Permalink
Fix video downgrading on load (#33170)
Browse files Browse the repository at this point in the history
  • Loading branch information
mszylkowski authored Mar 9, 2021
1 parent 6fc5bc6 commit c121292
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions extensions/amp-video/0.1/flexible-bitrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ export class BitrateManager {
*/
function onNontrivialWait(video, callback) {
listen(video, 'waiting', () => {
// Do not trigger downgrade if not loaded metadata yet.
if (video.readyState < 1) {
return;
}
let timer = null;
const unlisten = listenOnce(video, 'playing', () => {
clearTimeout(timer);
Expand Down
19 changes: 19 additions & 0 deletions extensions/amp-video/0.1/test/test-flexible-bitrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ describes.fakeWin('amp-video flexible-bitrate', {}, (env) => {

expect(currentBitrates(v0)[0]).to.equal(4000);
});

it('should not lower bitrate on waiting before metadata loaded', () => {
const m = getManager('4g');
const v0 = getVideo([4000, 1000, 3000, 2000]);
v0.id = 'v0';
m.sortSources_(v0);
m.manage(v0);
// Video should not downgrade on wait since it has not started loading (we never called `load`).
expect(currentBitrates(v0)[0]).to.equal(2000);
causeWait(v0);
expect(currentBitrates(v0)[0]).to.equal(2000);
});
});

describe('sorting', () => {
Expand Down Expand Up @@ -251,6 +263,7 @@ describes.fakeWin('amp-video flexible-bitrate', {}, (env) => {
video.appendChild(s);
});
});
video.readyStateOverride = 0;

Object.defineProperty(video, 'currentSrc', {
get: () => {
Expand All @@ -265,9 +278,15 @@ describes.fakeWin('amp-video flexible-bitrate', {}, (env) => {
video.currentTimeOverride = val;
},
});
Object.defineProperty(video, 'readyState', {
get: () => {
return video.readyStateOverride;
},
});
video.load = function () {
video.currentTime = 0;
video.currentSrcOverride = video.firstElementChild.src;
video.readyStateOverride = 1;
};
video.play = env.sandbox.spy();
env.win.document.body.appendChild(video);
Expand Down

0 comments on commit c121292

Please sign in to comment.