Files
shaka-player/test/util/stream_utils_unit.js
T
Michelle Zhuo a09ae24b85 Add audio channels count configuration
Added a preferred audio channel count to configuration, default set
to 2.
Choose the codec with the largest number of audio channels less than
or equal to the configured number of output channels. If this is not
possible, choose the smallest number of channels.

Closes #1013.
Issue #1071.

Change-Id: I6c6a956e33637cf34bd4bd79af563dc10a595e94
2018-04-06 20:51:28 +00:00

641 lines
22 KiB
JavaScript

/**
* @license
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
describe('StreamUtils', function() {
const filterVariantsByLanguageAndRole =
shaka.util.StreamUtils.filterVariantsByLanguageAndRole;
const filterStreamsByLanguageAndRole =
shaka.util.StreamUtils.filterStreamsByLanguageAndRole;
const filterVariantsByAudioChannelCount =
shaka.util.StreamUtils.filterVariantsByAudioChannelCount;
let manifest;
describe('filterVariantsByLanguageAndRole', function() {
it('chooses variants in user\'s preferred language', function() {
manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addVariant(0)
.language('es')
.addVariant(1)
.language('en')
.addVariant(2)
.language('en')
.build();
let chosen = filterVariantsByLanguageAndRole(
manifest.periods[0].variants,
'en',
'');
expect(chosen.length).toBe(2);
expect(chosen[0]).toBe(manifest.periods[0].variants[1]);
expect(chosen[1]).toBe(manifest.periods[0].variants[2]);
});
it('prefers primary variants', function() {
manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addVariant(0)
.primary()
.addVariant(1)
.addVariant(2)
.addVariant(3)
.primary()
.build();
let chosen = filterVariantsByLanguageAndRole(
manifest.periods[0].variants,
'en',
'');
expect(chosen.length).toBe(2);
expect(chosen[0]).toBe(manifest.periods[0].variants[0]);
expect(chosen[1]).toBe(manifest.periods[0].variants[3]);
});
it('filters out resctricted variants', function() {
manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addVariant(0)
.addVariant(1)
.addVariant(2)
.build();
manifest.periods[0].variants[0].allowedByKeySystem = false;
manifest.periods[0].variants[1].allowedByApplication = false;
let chosen = filterVariantsByLanguageAndRole(
manifest.periods[0].variants,
'en',
'');
expect(chosen.length).toBe(1);
expect(chosen[0]).toBe(manifest.periods[0].variants[2]);
});
it('chooses variants in preferred language and role', function() {
manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addVariant(0)
.language('en')
.addAudio(0).roles(['main', 'commentary'])
.addVariant(1)
.language('en')
.addAudio(1).roles(['secondary'])
.addVariant(2)
.language('es')
.addAudio(2).roles(['main'])
.build();
let chosen = filterVariantsByLanguageAndRole(
manifest.periods[0].variants,
'en',
'main');
expect(chosen.length).toBe(1);
expect(chosen[0]).toBe(manifest.periods[0].variants[0]);
});
it('chooses only one role, even if none is preferred', function() {
// Regression test for https://github.com/google/shaka-player/issues/949
manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addVariant(0)
.language('en')
.addAudio(0).roles(['commentary'])
.addVariant(1)
.language('en')
.addAudio(1).roles(['commentary'])
.addVariant(2)
.language('en')
.addAudio(2).roles(['secondary'])
.addVariant(3)
.language('en')
.addAudio(3).roles(['secondary'])
.addVariant(4)
.language('en')
.addAudio(4).roles(['main'])
.addVariant(5)
.language('en')
.addAudio(5).roles(['main'])
.build();
let chosen = filterVariantsByLanguageAndRole(
manifest.periods[0].variants,
'en',
'');
// Which role is chosen is an implementation detail.
// Each role is found on two variants, so we should have two.
expect(chosen.length).toBe(2);
expect(chosen[0].audio.roles[0]).toEqual(chosen[1].audio.roles[0]);
});
it('chooses only one role, even if all are primary', function() {
// Regression test for https://github.com/google/shaka-player/issues/949
manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addVariant(0)
.language('en').primary()
.addAudio(0).roles(['commentary'])
.addVariant(1)
.language('en').primary()
.addAudio(1).roles(['commentary'])
.addVariant(2)
.language('en').primary()
.addAudio(2).roles(['secondary'])
.addVariant(3)
.language('en').primary()
.addAudio(3).roles(['secondary'])
.addVariant(4)
.language('en').primary()
.addAudio(4).roles(['main'])
.addVariant(5)
.language('en').primary()
.addAudio(5).roles(['main'])
.build();
let chosen = filterVariantsByLanguageAndRole(
manifest.periods[0].variants,
'zh',
'');
// Which role is chosen is an implementation detail.
// Each role is found on two variants, so we should have two.
expect(chosen.length).toBe(2);
expect(chosen[0].audio.roles[0]).toEqual(chosen[1].audio.roles[0]);
});
it('chooses only one language, even if all are primary', function() {
// Regression test for https://github.com/google/shaka-player/issues/918
manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addVariant(0)
.language('en').primary()
.addAudio(0)
.addVariant(1)
.language('en').primary()
.addAudio(1)
.addVariant(2)
.language('es').primary()
.addAudio(2)
.addVariant(3)
.language('es').primary()
.addAudio(3)
.build();
let chosen = filterVariantsByLanguageAndRole(
manifest.periods[0].variants,
'zh',
'');
// Which language is chosen is an implementation detail.
// Each role is found on two variants, so we should have two.
expect(chosen.length).toBe(2);
expect(chosen[0].language).toEqual(chosen[1].language);
});
it('chooses a role from among primary variants without language match',
function() {
manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addVariant(0)
.language('en').primary()
.addAudio(0).roles(['commentary'])
.addVariant(1)
.language('en').primary()
.addAudio(1).roles(['commentary'])
.addVariant(2)
.language('en')
.addAudio(2).roles(['secondary'])
.addVariant(3)
.language('en')
.addAudio(3).roles(['secondary'])
.addVariant(4)
.language('en').primary()
.addAudio(4).roles(['main'])
.addVariant(5)
.language('en').primary()
.addAudio(5).roles(['main'])
.build();
let chosen = filterVariantsByLanguageAndRole(
manifest.periods[0].variants,
'zh',
'');
// Which role is chosen is an implementation detail.
// Each role is found on two variants, so we should have two.
expect(chosen.length).toBe(2);
expect(chosen[0].audio.roles[0]).toEqual(chosen[1].audio.roles[0]);
// Since nothing matches our language preference, we chose primary
// variants.
expect(chosen[0].primary).toBe(true);
expect(chosen[1].primary).toBe(true);
});
it('chooses a role from best language match, in spite of primary',
function() {
manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addVariant(0)
.language('en').primary()
.addAudio(0).roles(['commentary'])
.addVariant(1)
.language('en').primary()
.addAudio(1).roles(['commentary'])
.addVariant(2)
.language('zh')
.addAudio(2).roles(['secondary'])
.addVariant(3)
.language('zh')
.addAudio(3).roles(['secondary'])
.addVariant(4)
.language('en').primary()
.addAudio(4).roles(['main'])
.addVariant(5)
.language('en').primary()
.addAudio(5).roles(['main'])
.build();
let chosen = filterVariantsByLanguageAndRole(
manifest.periods[0].variants,
'zh',
'');
expect(chosen.length).toBe(2);
expect(chosen[0].language).toBe('zh');
expect(chosen[1].language).toBe('zh');
expect(chosen[0].primary).toBe(false);
expect(chosen[1].primary).toBe(false);
});
});
describe('filterStreamsByLanguageAndRole', function() {
it('chooses text streams in user\'s preferred language', function() {
manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addTextStream(1)
.language('en')
.addTextStream(2)
.language('es')
.addTextStream(3)
.language('en')
.build();
let chosen = filterStreamsByLanguageAndRole(
manifest.periods[0].textStreams,
'en',
'');
expect(chosen.length).toBe(2);
expect(chosen[0]).toBe(manifest.periods[0].textStreams[0]);
expect(chosen[1]).toBe(manifest.periods[0].textStreams[2]);
});
it('chooses primary text streams', function() {
manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addTextStream(1)
.addTextStream(2)
.primary()
.addTextStream(3)
.primary()
.build();
let chosen = filterStreamsByLanguageAndRole(
manifest.periods[0].textStreams,
'en',
'');
expect(chosen.length).toBe(2);
expect(chosen[0]).toBe(manifest.periods[0].textStreams[1]);
expect(chosen[1]).toBe(manifest.periods[0].textStreams[2]);
});
it('chooses text streams in preferred language and role', function() {
manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addTextStream(1)
.language('en')
.roles(['main', 'commentary'])
.addTextStream(2)
.language('es')
.addTextStream(3)
.language('en')
.roles(['caption'])
.build();
let chosen = filterStreamsByLanguageAndRole(
manifest.periods[0].textStreams,
'en',
'main');
expect(chosen.length).toBe(1);
expect(chosen[0]).toBe(manifest.periods[0].textStreams[0]);
});
it('prefers no-role streams if there is no preferred role', function() {
manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addTextStream(0)
.language('en')
.roles(['commentary'])
.addTextStream(1)
.language('en')
.addTextStream(2)
.language('en')
.roles(['secondary'])
.build();
let chosen = filterStreamsByLanguageAndRole(
manifest.periods[0].textStreams,
'en',
'');
expect(chosen.length).toBe(1);
expect(chosen[0].roles.length).toBe(0); // Pick a stream with no role.
});
it('ignores no-role streams if there is a preferred role', function() {
manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addTextStream(0)
.language('en')
.roles(['commentary'])
.addTextStream(1)
.language('en')
.addTextStream(2)
.language('en')
.roles(['secondary'])
.build();
let chosen = filterStreamsByLanguageAndRole(
manifest.periods[0].textStreams,
'en',
'main'); // A role that is not present.
expect(chosen.length).toBe(1);
expect(chosen[0].roles.length).toBe(1); // Pick a stream with a role.
});
it('chooses only one role, even if none is preferred', function() {
// Regression test for https://github.com/google/shaka-player/issues/949
manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addTextStream(0)
.language('en')
.roles(['commentary'])
.addTextStream(1)
.language('en')
.roles(['commentary'])
.addTextStream(2)
.language('en')
.roles(['secondary'])
.addTextStream(3)
.language('en')
.roles(['secondary'])
.addTextStream(4)
.language('en')
.roles(['main'])
.addTextStream(5)
.language('en')
.roles(['main'])
.build();
let chosen = filterStreamsByLanguageAndRole(
manifest.periods[0].textStreams,
'en',
'');
// Which role is chosen is an implementation detail.
// Each role is found on two text streams, so we should have two.
expect(chosen.length).toBe(2);
expect(chosen[0].roles[0]).toEqual(chosen[1].roles[0]);
});
it('chooses only one role, even if all are primary', function() {
// Regression test for https://github.com/google/shaka-player/issues/949
manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addTextStream(0)
.language('en').primary()
.roles(['commentary'])
.addTextStream(1)
.language('en').primary()
.roles(['commentary'])
.addTextStream(2)
.language('en').primary()
.roles(['secondary'])
.addTextStream(3)
.language('en').primary()
.roles(['secondary'])
.addTextStream(4)
.language('en').primary()
.roles(['main'])
.addTextStream(5)
.language('en').primary()
.roles(['main'])
.build();
let chosen = filterStreamsByLanguageAndRole(
manifest.periods[0].textStreams,
'zh',
'');
// Which role is chosen is an implementation detail.
// Each role is found on two text streams, so we should have two.
expect(chosen.length).toBe(2);
expect(chosen[0].roles[0]).toEqual(chosen[1].roles[0]);
});
it('chooses only one language, even if all are primary', function() {
// Regression test for https://github.com/google/shaka-player/issues/918
manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addTextStream(0)
.language('en').primary()
.addTextStream(1)
.language('en').primary()
.addTextStream(2)
.language('es').primary()
.addTextStream(3)
.language('es').primary()
.build();
let chosen = filterStreamsByLanguageAndRole(
manifest.periods[0].textStreams,
'zh',
'');
// Which language is chosen is an implementation detail.
// Each role is found on two variants, so we should have two.
expect(chosen.length).toBe(2);
expect(chosen[0].language).toEqual(chosen[1].language);
});
it('chooses a role from among primary streams without language match',
function() {
manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addTextStream(0)
.language('en').primary()
.roles(['commentary'])
.addTextStream(1)
.language('en').primary()
.roles(['commentary'])
.addTextStream(2)
.language('en')
.roles(['secondary'])
.addTextStream(3)
.language('en')
.roles(['secondary'])
.addTextStream(4)
.language('en').primary()
.roles(['main'])
.addTextStream(5)
.language('en').primary()
.roles(['main'])
.build();
let chosen = filterStreamsByLanguageAndRole(
manifest.periods[0].textStreams,
'zh',
'');
// Which role is chosen is an implementation detail.
// Each role is found on two text streams, so we should have two.
expect(chosen.length).toBe(2);
expect(chosen[0].roles[0]).toEqual(chosen[1].roles[0]);
// Since nothing matches our language preference, we chose primary
// text streams.
expect(chosen[0].primary).toBe(true);
expect(chosen[1].primary).toBe(true);
});
it('chooses a role from best language match, in spite of primary',
function() {
manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addTextStream(0)
.language('en').primary()
.roles(['commentary'])
.addTextStream(1)
.language('en').primary()
.roles(['commentary'])
.addTextStream(2)
.language('zh')
.roles(['secondary'])
.addTextStream(3)
.language('zh')
.roles(['secondary'])
.addTextStream(4)
.language('en').primary()
.roles(['main'])
.addTextStream(5)
.language('en').primary()
.roles(['main'])
.build();
let chosen = filterStreamsByLanguageAndRole(
manifest.periods[0].textStreams,
'zh',
'');
expect(chosen.length).toBe(2);
expect(chosen[0].language).toBe('zh');
expect(chosen[1].language).toBe('zh');
expect(chosen[0].primary).toBe(false);
expect(chosen[1].primary).toBe(false);
});
});
describe('filterVariantsByAudioChannelCount', function() {
it('chooses variants with preferred audio channels count', function() {
manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addVariant(0)
.addAudio(0).channelCount(2)
.addVariant(1)
.addAudio(1).channelCount(6)
.addVariant(2)
.addAudio(2).channelCount(2)
.build();
let chosen = filterVariantsByAudioChannelCount(
manifest.periods[0].variants, 2);
expect(chosen.length).toBe(2);
expect(chosen[0]).toBe(manifest.periods[0].variants[0]);
expect(chosen[1]).toBe(manifest.periods[0].variants[2]);
});
it('chooses variants with largest audio channel count less than config' +
' when no exact audio channel count match is possible', function() {
manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addVariant(0)
.addAudio(0).channelCount(2)
.addVariant(1)
.addAudio(1).channelCount(8)
.addVariant(2)
.addAudio(2).channelCount(2)
.build();
let chosen = filterVariantsByAudioChannelCount(
manifest.periods[0].variants, 6);
expect(chosen.length).toBe(2);
expect(chosen[0]).toBe(manifest.periods[0].variants[0]);
expect(chosen[1]).toBe(manifest.periods[0].variants[2]);
});
it('chooses variants with fewest audio channels when none fit in the ' +
'config', function() {
manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addVariant(0)
.addAudio(0).channelCount(6)
.addVariant(1)
.addAudio(1).channelCount(8)
.addVariant(2)
.addAudio(2).channelCount(6)
.build();
let chosen = filterVariantsByAudioChannelCount(
manifest.periods[0].variants, 2);
expect(chosen.length).toBe(2);
expect(chosen[0]).toBe(manifest.periods[0].variants[0]);
expect(chosen[1]).toBe(manifest.periods[0].variants[2]);
});
});
describe('filterNewPeriod', function() {
let fakeDrmEngine;
beforeAll(function() {
fakeDrmEngine = new shaka.test.FakeDrmEngine();
});
it('filters text streams with the full MIME type', function() {
manifest = new shaka.test.ManifestGenerator()
.addPeriod(0)
.addTextStream(1).mime('text/vtt')
.addTextStream(2).mime('application/mp4', 'wvtt')
.addTextStream(3).mime('text/bogus')
.addTextStream(4).mime('application/mp4', 'bogus')
.build();
let noAudio = null;
let noVideo = null;
shaka.util.StreamUtils.filterNewPeriod(
fakeDrmEngine, noAudio, noVideo, manifest.periods[0]);
// Covers a regression in which we would remove streams with codecs.
// The last two streams should be removed because their full MIME types
// are bogus.
expect(manifest.periods[0].textStreams.length).toBe(2);
let textStreams = manifest.periods[0].textStreams;
expect(textStreams[0].id).toBe(1);
expect(textStreams[1].id).toBe(2);
});
});
});