Skip to content

Commit

Permalink
Add player.isAudioOnly()
Browse files Browse the repository at this point in the history
This simplifies detection of audio-only content and makes the process
a little more obvious.

Closes #942

Backported to v2.1.x for issue #969

Change-Id: I89b599fb8c8915c9e1b45d2f9b81a3ac7193e5ef
  • Loading branch information
joeyparrish committed Aug 16, 2017
1 parent 288a2a7 commit 104f1a7
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 12 deletions.
7 changes: 1 addition & 6 deletions demo/asset_section.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,8 @@ shakaDemo.load = function() {

shakaDemo.hashShouldChange_();

// Audio-only tracks have no width/height.
var videoTracks = player.getVariantTracks().filter(function(t) {
return t.videoCodec;
});

// Set a different poster for audio-only assets.
if (videoTracks.length == 0) {
if (player.isAudioOnly()) {
shakaDemo.localVideo_.poster = shakaDemo.audioOnlyPoster_;
}

Expand Down
7 changes: 1 addition & 6 deletions demo/receiver_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,8 @@ ShakaReceiver.prototype.checkIdle_ = function() {
this.idle_.style.display = 'none';
this.cancelIdleTimer_();

// Audio-only tracks have no width/height.
var videoTracks = this.player_.getVariantTracks().filter(function(t) {
return t.videoCodec;
});

// Set a special poster for audio-only assets.
if (videoTracks.length == 0) {
if (this.player_.isAudioOnly()) {
this.video_.poster =
'//shaka-player-demo.appspot.com/assets/audioOnly.gif';
} else {
Expand Down
20 changes: 20 additions & 0 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,26 @@ shaka.Player.prototype.isInProgress = function() {
};


/**
* @return {boolean} True for audio-only content. False otherwise.
* @export
*/
shaka.Player.prototype.isAudioOnly = function() {
if (!this.manifest_ || !this.manifest_.periods.length)
return false;

var variants = this.manifest_.periods[0].variants;
if (!variants.length)
return false;

// Note that if there are some audio-only variants and some audio-video
// variants, the audio-only variants are removed during filtering.
// Therefore if the first variant has no video, that's sufficient to say it
// is audio-only content.
return !variants[0].video;
};


/**
* Get the seekable range for the current stream.
* @return {{start: number, end: number}}
Expand Down
49 changes: 49 additions & 0 deletions test/player_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2328,6 +2328,55 @@ describe('Player', function() {
}).then(done);
});

describe('isAudioOnly', function() {
it('detects audio-only content', function(done) {
// This factory recreates the parser each time, so updates to |manifest|
// affect the next load() call.
var parserFactory = function() {
return new shaka.test.FakeManifestParser(manifest);
};

// False before we've loaded anything.
expect(player.isAudioOnly()).toEqual(false);

manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addVariant(0).bandwidth(100)
.addVideo(0).mime('video/mp4', 'good')
.addAudio(1).mime('audio/mp4', 'good')
.build();
player.load('', 0, parserFactory).then(function() {
// We have audio & video tracks, so this is not audio-only.
expect(player.isAudioOnly()).toEqual(false);

manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addVariant(0).bandwidth(100)
.addVideo(0).mime('video/mp4', 'good')
.build();
return player.load('', 0, parserFactory);
}).then(function() {
// We have video-only tracks, so this is not audio-only.
expect(player.isAudioOnly()).toEqual(false);

manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addVariant(0).bandwidth(100)
.addAudio(1).mime('audio/mp4', 'good')
.build();
return player.load('', 0, parserFactory);
}).then(function() {
// We have audio-only tracks, so this is audio-only.
expect(player.isAudioOnly()).toEqual(true);

return player.unload();
}).then(function() {
// When we have nothing loaded, we go back to not audio-only status.
expect(player.isAudioOnly()).toEqual(false);
}).catch(fail).then(done);
});
});

describe('load', function() {
it('tolerates bandwidth of NaN, undefined, or 0', function(done) {
// Regression test for https://github.com/google/shaka-player/issues/938
Expand Down

0 comments on commit 104f1a7

Please sign in to comment.