@@ -95,14 +95,20 @@ function createAudioTracks(audioTracks: ICompatAudioTrackList): Array<{
9595 const languagesOccurences : Partial < Record < string , number > > = { } ;
9696 for ( let i = 0 ; i < audioTracks . length ; i ++ ) {
9797 const audioTrack = audioTracks [ i ] ;
98- const language = audioTrack . language === "" ? "nolang" : audioTrack . language ;
98+ /**
99+ * "und" is a special value in ISO 639-3 that stands for "undetermined language".
100+ * If a track is announced in the manifest with lang "und", Safari will
101+ * incorrectly set `audioTrack.language` with an empty string instead of "und".
102+ * To patch this, check if the label is "und".
103+ */
104+ const language = audioTrack . language || ( audioTrack . label === "und" ? "und" : "" ) ;
99105 const occurences = languagesOccurences [ language ] ?? 1 ;
100- const id = "gen_audio_" + language + "_" + occurences . toString ( ) ;
106+ const id = "gen_audio_" + ( language || "nolang" ) + "_" + occurences . toString ( ) ;
101107 languagesOccurences [ language ] = occurences + 1 ;
102108 const track = {
103- language : audioTrack . language ,
109+ language,
104110 id,
105- normalized : normalizeLanguage ( audioTrack . language ) ,
111+ normalized : normalizeLanguage ( language ) ,
106112 audioDescription :
107113 audioTrack . kind === "descriptions" ||
108114 // Safari seem to prefer the non-standard singular
@@ -127,9 +133,15 @@ function createTextTracks(
127133 const languagesOccurences : Partial < Record < string , number > > = { } ;
128134 for ( let i = 0 ; i < textTracks . length ; i ++ ) {
129135 const textTrack = textTracks [ i ] ;
130- const language = textTrack . language === "" ? "nolang" : textTrack . language ;
136+ /**
137+ * "und" is a special value in ISO 639-3 that stands for "undetermined language".
138+ * If a track is announced in the manifest with lang "und", Safari will
139+ * incorrectly set `textTrack.language` with an empty string instead of "und".
140+ * To patch this, check if the label is "und".
141+ */
142+ const language = textTrack . language || ( textTrack . label === "und" ? "und" : "" ) ;
131143 const occurences = languagesOccurences [ language ] ?? 1 ;
132- const id = "gen_text_" + language + "_" + occurences . toString ( ) ;
144+ const id = "gen_text_" + ( language || "nolang" ) + "_" + occurences . toString ( ) ;
133145 languagesOccurences [ language ] = occurences + 1 ;
134146
135147 // Safari seems to be indicating that the subtitles track is a forced
@@ -138,11 +150,11 @@ function createTextTracks(
138150 // @see https://github.com/whatwg/html/issues/4472
139151 const forced = ( textTrack . kind as string ) === "forced" ? true : undefined ;
140152 const track = {
141- language : textTrack . language ,
153+ language,
142154 forced,
143155 label : textTrack . label ,
144156 id,
145- normalized : normalizeLanguage ( textTrack . language ) ,
157+ normalized : normalizeLanguage ( language ) ,
146158 closedCaption : textTrack . kind === "captions" ,
147159 } ;
148160 newTextTracks . push ( { track, nativeTrack : textTrack } ) ;
@@ -163,9 +175,15 @@ function createVideoTracks(videoTracks: ICompatVideoTrackList): Array<{
163175 const languagesOccurences : Partial < Record < string , number > > = { } ;
164176 for ( let i = 0 ; i < videoTracks . length ; i ++ ) {
165177 const videoTrack = videoTracks [ i ] ;
166- const language = videoTrack . language === "" ? "nolang" : videoTrack . language ;
178+ /**
179+ * "und" is a special value in ISO 639-3 that stands for "undetermined language".
180+ * If a track is announced in the manifest with lang "und", Safari will
181+ * incorrectly set `videoTrack.language` with an empty string instead of "und".
182+ * To patch this, check if the label is "und".
183+ */
184+ const language = videoTrack . language || ( videoTrack . label === "und" ? "und" : "" ) ;
167185 const occurences = languagesOccurences [ language ] ?? 1 ;
168- const id = "gen_video_" + language + "_" + occurences . toString ( ) ;
186+ const id = "gen_video_" + ( language || "nolang" ) + "_" + occurences . toString ( ) ;
169187 languagesOccurences [ language ] = occurences + 1 ;
170188 newVideoTracks . push ( {
171189 track : { id, representations : [ ] as IRepresentation [ ] } ,
0 commit comments