-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(controls): Audio tracks menu for Dash viewer (#1391)
- Loading branch information
Conrad Chan
authored
May 27, 2021
1 parent
6954be0
commit 688aa03
Showing
10 changed files
with
251 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,68 @@ | ||
import React from 'react'; | ||
import noop from 'lodash/noop'; | ||
import getLanguageName from '../../../lang'; | ||
import MediaSettingsMenuAudioTracks, { AudioTrack, Props as AudioTracksProps } from './MediaSettingsAudioTracks'; | ||
import MediaSettingsMenuAutoplay, { Props as AutoplayProps } from './MediaSettingsMenuAutoplay'; | ||
import MediaSettingsMenuRate, { Props as RateProps } from './MediaSettingsMenuRate'; | ||
import Settings, { Menu } from '../settings'; | ||
|
||
export type Props = AutoplayProps & RateProps & { className?: string }; | ||
export type Props = Partial<AudioTracksProps> & AutoplayProps & RateProps & { className?: string }; | ||
|
||
const generateAudioTrackLabel = (language: string, index: number): string => { | ||
let label = `${__('track')} ${index + 1}`; | ||
if (language !== 'und') { | ||
label = `${label} (${getLanguageName(language) || language})`; | ||
} | ||
|
||
return label; | ||
}; | ||
|
||
const addLabels = (audioTracks: Array<AudioTrack>): Array<AudioTrack> => | ||
audioTracks.map((track, index) => { | ||
const { language } = track; | ||
const label = generateAudioTrackLabel(language, index); | ||
return { | ||
...track, | ||
label, | ||
}; | ||
}); | ||
|
||
export default function MediaSettings({ | ||
audioTrack, | ||
audioTracks = [], | ||
autoplay, | ||
className, | ||
onAudioTrackChange = noop, | ||
onAutoplayChange, | ||
onRateChange, | ||
rate, | ||
}: Props): JSX.Element { | ||
const autoValue = autoplay ? __('media_autoplay_enabled') : __('media_autoplay_disabled'); | ||
const rateValue = rate === '1.0' || !rate ? __('media_speed_normal') : rate; | ||
const labelledAudioTracks = React.useMemo(() => addLabels(audioTracks), [audioTracks]); | ||
const hydratedSelectedAudioTrack = labelledAudioTracks.find(({ id }) => audioTrack === id); | ||
const audioTrackLabel = hydratedSelectedAudioTrack ? hydratedSelectedAudioTrack.label : ''; | ||
const showAudioTrackItems = audioTracks.length > 1; | ||
|
||
return ( | ||
<Settings className={className}> | ||
<Settings.Menu name={Menu.MAIN}> | ||
<Settings.MenuItem label={__('media_autoplay')} target={Menu.AUTOPLAY} value={autoValue} /> | ||
<Settings.MenuItem label={__('media_speed')} target={Menu.RATE} value={rateValue} /> | ||
{showAudioTrackItems && ( | ||
<Settings.MenuItem label={__('media_audio')} target={Menu.AUDIO} value={audioTrackLabel} /> | ||
)} | ||
</Settings.Menu> | ||
|
||
<MediaSettingsMenuAutoplay autoplay={autoplay} onAutoplayChange={onAutoplayChange} /> | ||
<MediaSettingsMenuRate onRateChange={onRateChange} rate={rate} /> | ||
{showAudioTrackItems && ( | ||
<MediaSettingsMenuAudioTracks | ||
audioTrack={audioTrack} | ||
audioTracks={labelledAudioTracks} | ||
onAudioTrackChange={onAudioTrackChange} | ||
/> | ||
)} | ||
</Settings> | ||
); | ||
} |
43 changes: 43 additions & 0 deletions
43
src/lib/viewers/controls/media/MediaSettingsAudioTracks.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React from 'react'; | ||
import Settings, { Menu } from '../settings'; | ||
|
||
export type AudioTrack = { | ||
id: number; | ||
label: string; | ||
language: string; | ||
role: string; | ||
}; | ||
|
||
export type Props = { | ||
audioTrack?: number; | ||
audioTracks: Array<AudioTrack>; | ||
onAudioTrackChange: (id: number) => void; | ||
}; | ||
|
||
export default function MediaSettingsMenuAudioTracks({ | ||
audioTrack, | ||
audioTracks, | ||
onAudioTrackChange, | ||
}: Props): JSX.Element { | ||
const { setActiveMenu } = React.useContext(Settings.Context); | ||
|
||
const handleChange = (value: number): void => { | ||
setActiveMenu(Menu.MAIN); | ||
onAudioTrackChange(value); | ||
}; | ||
|
||
return ( | ||
<Settings.Menu name={Menu.AUDIO}> | ||
<Settings.MenuBack label={__('media_audio')} /> | ||
{audioTracks.map(({ id, label }) => ( | ||
<Settings.RadioItem | ||
key={id} | ||
isSelected={audioTrack === id} | ||
label={label} | ||
onChange={handleChange} | ||
value={id} | ||
/> | ||
))} | ||
</Settings.Menu> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const audioTracks = [ | ||
{ id: 0, label: '', language: 'und', role: 'audio0' }, | ||
{ id: 1, label: '', language: 'en', role: 'audio1' }, | ||
]; | ||
|
||
export default audioTracks; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/lib/viewers/controls/media/__tests__/MediaSettingsAudioTracks-test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from 'react'; | ||
import { mount, ReactWrapper } from 'enzyme'; | ||
import audioTracks from '../__mocks__/audioTracks'; | ||
import MediaSettingsAudioTracks from '../MediaSettingsAudioTracks'; | ||
import Settings, { Context, Menu } from '../../settings'; | ||
|
||
describe('MediaSettingsAudioTracks', () => { | ||
const getContext = (): Partial<Context> => ({ setActiveMenu: jest.fn() }); | ||
const getWrapper = (props = {}, context = getContext()): ReactWrapper => | ||
mount( | ||
<MediaSettingsAudioTracks | ||
audioTrack={1} | ||
audioTracks={audioTracks} | ||
onAudioTrackChange={jest.fn()} | ||
{...props} | ||
/>, | ||
{ | ||
wrappingComponent: Settings.Context.Provider, | ||
wrappingComponentProps: { value: context }, | ||
}, | ||
); | ||
|
||
describe('event handlers', () => { | ||
test('should surface the selected item on change', () => { | ||
const onAudioTrackChange = jest.fn(); | ||
const wrapper = getWrapper({ onAudioTrackChange }); | ||
|
||
wrapper.find({ value: 0 }).simulate('click'); | ||
|
||
expect(onAudioTrackChange).toBeCalledWith(0); | ||
}); | ||
|
||
test('should reset the active menu on change', () => { | ||
const context = getContext(); | ||
const wrapper = getWrapper({}, context); | ||
|
||
wrapper.find({ value: 0 }).simulate('click'); | ||
|
||
expect(context.setActiveMenu).toBeCalledWith(Menu.MAIN); | ||
}); | ||
}); | ||
|
||
describe('render', () => { | ||
test('should return a valid wrapper', () => { | ||
const wrapper = getWrapper(); | ||
|
||
expect(wrapper.exists(Settings.MenuBack)).toBe(true); | ||
expect(wrapper.exists(Settings.RadioItem)).toBe(true); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.