11import { isUndefined } from '@guardian/libs' ;
22import type {
33 FEFrontCard ,
4+ FEMediaAsset ,
45 FEMediaAtom ,
56 FESupportingContent ,
67} from '../frontend/feFront' ;
@@ -14,8 +15,14 @@ import type { EditionId } from '../lib/edition';
1415import type { Group } from '../lib/getDataLinkName' ;
1516import { getDataLinkNameCard } from '../lib/getDataLinkName' ;
1617import { getLargestImageSize } from '../lib/image' ;
17- import type { SupportedVideoFileType } from '../lib/video' ;
18- import { supportedVideoFileTypes } from '../lib/video' ;
18+ import type {
19+ SupportedVideoFileType ,
20+ SupportedMP4VideoFileType ,
21+ } from '../lib/video' ;
22+ import {
23+ supportedMP4VideoFileTypes ,
24+ supportedVideoFileTypes ,
25+ } from '../lib/video' ;
1926import type { Image } from '../types/content' ;
2027import type {
2128 DCRFrontCard ,
@@ -196,6 +203,7 @@ const decideMediaAtomImage = (
196203
197204export const getActiveMediaAtom = (
198205 videoReplace : boolean ,
206+ enableHlsSupport : boolean ,
199207 mediaAtom ?: FEMediaAtom ,
200208 cardTrailImage ?: string ,
201209) : MainMedia | undefined => {
@@ -220,10 +228,13 @@ export const getActiveMediaAtom = (
220228 * Therefore, we check the platform of the first asset and assume the rest are the same.
221229 */
222230 if ( assets [ 0 ] ?. platform === 'Url' ) {
231+ const supportedFileTypes = enableHlsSupport
232+ ? supportedVideoFileTypes
233+ : supportedMP4VideoFileTypes ;
223234 /**
224235 * Take one source for each supported video file type.
225236 */
226- const sources = supportedVideoFileTypes . reduce < typeof assets > (
237+ const sources = supportedFileTypes . reduce < typeof assets > (
227238 ( acc , type ) => {
228239 const source = assets . find (
229240 ( { mimeType } ) => mimeType === type ,
@@ -239,13 +250,18 @@ export const getActiveMediaAtom = (
239250 ( { assetType } ) => assetType === 'Subtitles' ,
240251 ) ;
241252
253+ const getMimeType = ( source : FEMediaAsset ) =>
254+ enableHlsSupport
255+ ? ( source . mimeType as SupportedVideoFileType )
256+ : ( source . mimeType as SupportedMP4VideoFileType ) ;
257+
242258 return {
243259 type : 'SelfHostedVideo' ,
244260 videoStyle : 'Loop' ,
245261 atomId : mediaAtom . id ,
246262 sources : sources . map ( ( source ) => ( {
247263 src : source . id ,
248- mimeType : source . mimeType as SupportedVideoFileType ,
264+ mimeType : getMimeType ( source ) ,
249265 } ) ) ,
250266 subtitleSource : subtitleAsset ?. id ,
251267 duration : mediaAtom . duration ?? 0 ,
@@ -281,6 +297,7 @@ export const getActiveMediaAtom = (
281297
282298const decideMedia = (
283299 format : ArticleFormat ,
300+ enableHlsSupport : boolean ,
284301 showMainVideo ?: boolean ,
285302 mediaAtom ?: FEMediaAtom ,
286303 galleryCount : number = 0 ,
@@ -293,7 +310,12 @@ const decideMedia = (
293310 // If the showVideo toggle is enabled in the fronts tool,
294311 // we should return the active mediaAtom regardless of the design
295312 if ( ! ! showMainVideo || ! ! videoReplace ) {
296- return getActiveMediaAtom ( ! ! videoReplace , mediaAtom , cardImage ) ;
313+ return getActiveMediaAtom (
314+ ! ! videoReplace ,
315+ enableHlsSupport ,
316+ mediaAtom ,
317+ cardImage ,
318+ ) ;
297319 }
298320
299321 switch ( format . design ) {
@@ -308,7 +330,12 @@ const decideMedia = (
308330 } ;
309331
310332 case ArticleDesign . Video : {
311- return getActiveMediaAtom ( false , mediaAtom , cardImage ) ;
333+ return getActiveMediaAtom (
334+ false ,
335+ enableHlsSupport ,
336+ mediaAtom ,
337+ cardImage ,
338+ ) ;
312339 }
313340
314341 default :
@@ -325,6 +352,7 @@ export const enhanceCards = (
325352 pageId,
326353 discussionApiUrl,
327354 stripBranding = false ,
355+ enableHlsSupport = false ,
328356 } : {
329357 cardInTagPage : boolean ;
330358 /** Used for the data link name to indicate card position in container */
@@ -334,6 +362,7 @@ export const enhanceCards = (
334362 discussionApiUrl : string ;
335363 /** We strip branding from cards if the branding will appear at the collection level instead */
336364 stripBranding ?: boolean ;
365+ enableHlsSupport : boolean ;
337366 } ,
338367) : DCRFrontCard [ ] =>
339368 collections . map ( ( faciaCard , index ) => {
@@ -378,6 +407,7 @@ export const enhanceCards = (
378407
379408 const mainMedia = decideMedia (
380409 format ,
410+ enableHlsSupport ,
381411 faciaCard . properties . showMainVideo ??
382412 faciaCard . properties . mediaSelect ?. showMainVideo ,
383413 faciaCard . mediaAtom ??
0 commit comments