Skip to content

Commit

Permalink
fix(FEC-12679): ISO Code for Chinese and Spanish in Multi Audio (#673)
Browse files Browse the repository at this point in the history
* fix(FEC-12679): ISO Code for Chinese and Spanish in Multi Audio

* change fields name

* change fields name

* change syntax
  • Loading branch information
Tzipi-kaltura authored Nov 21, 2022
1 parent 86b52ea commit 7ad97e1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
2 changes: 2 additions & 0 deletions flow-typed/types/playback-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
declare type PKPlaybackConfigObject = {
audioLanguage: string,
textLanguage: string,
additionalAudioLanguage: string,
additionalTextLanguage: string,
volume: number,
playsinline: boolean,
crossOrigin: string,
Expand Down
24 changes: 19 additions & 5 deletions src/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -2598,8 +2598,21 @@ export default class Player extends FakeEventTarget {
const currentOrConfiguredAudioLang =
this._playbackAttributesState.audioLanguage ||
this._getLanguage<AudioTrack>(this._getAudioTracks(), playbackConfig.audioLanguage, activeTracks.audio);
this._setDefaultTrack<TextTrack>(this._getTextTracks(), currentOrConfiguredTextLang, offTextTrack);
this._setDefaultTrack<AudioTrack>(this._getAudioTracks(), currentOrConfiguredAudioLang, activeTracks.audio);
if (currentOrConfiguredTextLang === playbackConfig.textLanguage) {
this._setDefaultTrack<TextTrack>(this._getTextTracks(), currentOrConfiguredTextLang, offTextTrack, playbackConfig.additionalTextLanguage);
} else {
this._setDefaultTrack<TextTrack>(this._getTextTracks(), currentOrConfiguredTextLang, offTextTrack);
}
if (currentOrConfiguredAudioLang === playbackConfig.audioLanguage) {
this._setDefaultTrack<AudioTrack>(
this._getAudioTracks(),
currentOrConfiguredAudioLang,
activeTracks.audio,
playbackConfig.additionalAudioLanguage
);
} else {
this._setDefaultTrack<AudioTrack>(this._getAudioTracks(), currentOrConfiguredAudioLang, activeTracks.audio);
}
this._setDefaultVideoTrack();
}

Expand Down Expand Up @@ -2632,19 +2645,20 @@ export default class Player extends FakeEventTarget {
* @param {Array<T>} tracks - the audio or text tracks.
* @param {string} language - The track language.
* @param {Track} defaultTrack - The default track to set in case there is no language configured.
* @param {string} additionalLanguage - additional track language.
* @returns {void}
* @private
*/
_setDefaultTrack<T: TextTrack | AudioTrack>(tracks: Array<T>, language: string, defaultTrack: ?Track): void {
_setDefaultTrack<T: TextTrack | AudioTrack>(tracks: Array<T>, language: string, defaultTrack: ?Track, additionalLanguage: ?string): void {
const updateTrack = track => {
this.selectTrack(track);
this._markActiveTrack(track);
};
const sameTrack: ?T = tracks.find(track => Track.langComparer(language, track.language, true));
const sameTrack: ?T = tracks.find(track => Track.langComparer(language, track.language, additionalLanguage, true));
if (sameTrack) {
updateTrack(sameTrack);
} else {
const track: ?T = tracks.find(track => Track.langComparer(language, track.language, false));
const track: ?T = tracks.find(track => Track.langComparer(language, track.language, additionalLanguage, false));
if (track) {
updateTrack(track);
} else if (defaultTrack && !defaultTrack.active) {
Expand Down
10 changes: 8 additions & 2 deletions src/track/track.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ export default class Track {
* Comparing language strings.
* @param {string} inputLang - The configured language.
* @param {string} trackLang - The default track language.
* @param {string} additionalLanguage - Additional configured language.
* @param {boolean} equal - Optional flag to check for matching languages.
* @returns {boolean} - Whether the strings are equal or starts with the same substring.
*/
static langComparer(inputLang: string, trackLang: string, equal: ?boolean): boolean {
static langComparer(inputLang: string, trackLang: string, additionalLanguage: ?string, equal: ?boolean): boolean {
try {
inputLang = inputLang.toLowerCase();
trackLang = trackLang.toLowerCase();
additionalLanguage ? (additionalLanguage = additionalLanguage.toLowerCase()) : additionalLanguage;
if (equal) {
return inputLang ? inputLang === trackLang : false;
if (inputLang === trackLang) {
return true;
} else if (additionalLanguage === trackLang) {
return true;
} else return false;
} else {
return inputLang ? inputLang.startsWith(trackLang) || trackLang.startsWith(inputLang) : false;
}
Expand Down
12 changes: 9 additions & 3 deletions test/src/track/track.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ describe('Track', () => {
});

it('should compare languages using equality flag', () => {
Track.langComparer('zh', 'zh', true).should.be.true;
Track.langComparer('zh_tw', 'zh', true).should.be.false;
Track.langComparer('zh_tw', 'zh', false).should.be.true;
Track.langComparer('zh', 'zh', undefined, true).should.be.true;
Track.langComparer('zh_tw', 'zh', undefined, true).should.be.false;
Track.langComparer('zh_tw', 'zh', undefined, false).should.be.true;
});

it('should compare languages also with the additionalLanguage from the configuration', () => {
Track.langComparer('zh', 'chi', 'chi', true).should.be.true;
Track.langComparer('es', 'spa', 'spa', true).should.be.true;
Track.langComparer('es', 'zh', 'spa', true).should.be.false;
});
});
});

0 comments on commit 7ad97e1

Please sign in to comment.