import React from 'react';
import { useMedia } from 'react-use';
import { HMSHLSLayer } from '@100mslive/hls-player';
import { CheckIcon, CrossIcon, SettingsIcon } from '@100mslive/react-icons';
import { Box, Dropdown, Flex, Text, Tooltip } from '../../..';
import { Sheet } from '../../../Sheet';
import { config } from '../../../Theme';
import { useIsLandscape } from '../../common/hooks';
export function HLSQualitySelector({
open,
onOpenChange,
layers,
onQualityChange,
selection,
isAuto,
containerRef,
}: {
open: boolean;
onOpenChange: (value: boolean) => void;
layers: HMSHLSLayer[];
onQualityChange: (quality: { [key: string]: string | number } | HMSHLSLayer) => void;
selection: HMSHLSLayer;
isAuto: boolean;
containerRef?: HTMLDivElement;
}) {
const isMobile = useMedia(config.media.md);
const isLandscape = useIsLandscape();
if (layers.length === 0) {
return null;
}
if (isMobile || isLandscape) {
return (
onOpenChange(false)}
>
Quality
onOpenChange(false)}>
{layers.map(layer => {
return (
onQualityChange(layer)}
>
{getQualityText(layer)}
{getBitrateText(layer)}
{!isAuto && layer.width === selection?.width && layer.height === selection?.height && (
)}
);
})}
onQualityChange({ height: 'auto' })}
>
Auto
{isAuto && }
);
}
return (
onOpenChange(value)} modal={false}>
{isAuto && (
<>
Auto
>
)}
{selection && Math.min(selection.width || 0, selection.height || 0)}p
{layers.map(layer => {
return (
onQualityChange(layer)}
key={layer.width}
css={{
bg:
!isAuto && layer.width === selection?.width && layer.height === selection?.height
? '$surface_default'
: '$surface_bright',
'&:hover': {
bg: '$surface_brighter',
},
p: '$2 $4 $2 $8',
h: '$12',
gap: '$2',
}}
>
{getQualityText(layer)}
{getBitrateText(layer)}
{!isAuto && layer.width === selection?.width && layer.height === selection?.height && (
)}
);
})}
onQualityChange({ height: 'auto' })}
key="auto"
css={{
bg: !isAuto ? '$surface_bright' : '$surface_default',
'&:hover': {
bg: '$surface_brighter',
},
p: '$2 $4 $2 $8',
h: '$12',
gap: '$2',
}}
>
Auto
{isAuto && }
);
}
const getQualityText = (layer: HMSHLSLayer) => `${Math.min(layer.height || 0, layer.width || 0)}p `;
const getBitrateText = (layer: HMSHLSLayer) => `(${(Number(layer.bitrate / 1000) / 1000).toFixed(2)} Mbps)`;