Skip to content

Commit

Permalink
feat(controls): Add support for autogenerated captions (#1404)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conrad Chan authored Jun 23, 2021
1 parent ff7dbfb commit 7c59cb1
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/lib/viewers/controls/media/SubtitlesToggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { Subtitle } from './MediaSettingsMenuSubtitles';
import './SubtitlesToggle.scss';

export type Props = {
isAutoGeneratedSubtitles?: boolean;
isShowingSubtitles?: boolean;
onSubtitlesToggle?: (isShowingSubtitles: boolean) => void;
subtitles?: Array<Subtitle>;
};

export default function SubtitlesToggle({
isAutoGeneratedSubtitles,
isShowingSubtitles,
onSubtitlesToggle = noop,
subtitles = [],
Expand All @@ -19,13 +21,15 @@ export default function SubtitlesToggle({
return null;
}

const title = isAutoGeneratedSubtitles ? __('media_auto_generated_captions') : __('media_subtitles_cc');

return (
<MediaToggle
aria-pressed={isShowingSubtitles}
className="bp-SubtitlesToggle"
data-testid="bp-media-cc-button"
onClick={(): void => onSubtitlesToggle(!isShowingSubtitles)}
title={__('media_subtitles_cc')}
title={title}
>
<div className="bp-SubtitlesToggle-text">CC</div>
</MediaToggle>
Expand Down
13 changes: 13 additions & 0 deletions src/lib/viewers/controls/media/__tests__/SubtitlesToggle-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,18 @@ describe('SubtitlesToggle', () => {
expect(wrapper.prop('aria-pressed')).toBe(isShowingSubtitles);
},
);

test.each`
isAutoGeneratedSubtitles | title
${true} | ${__('media_auto_generated_captions')}
${false} | ${__('media_subtitles_cc')}
`(
'should set the title accordingly when isAutoGeneratedSubtitles is $isAutoGeneratedSubtitles',
({ isAutoGeneratedSubtitles, title }) => {
const wrapper = getWrapper({ isAutoGeneratedSubtitles, title });

expect(wrapper.prop('title')).toBe(title);
},
);
});
});
2 changes: 2 additions & 0 deletions src/lib/viewers/media/DashControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default function DashControls({
bufferedRange,
currentTime,
durationTime,
isAutoGeneratedSubtitles,
isPlaying,
isPlayingHD,
onAudioTrackChange,
Expand Down Expand Up @@ -62,6 +63,7 @@ export default function DashControls({

<div className="bp-DashControls-group">
<SubtitlesToggle
isAutoGeneratedSubtitles={isAutoGeneratedSubtitles}
isShowingSubtitles={subtitle !== SUBTITLES_OFF}
onSubtitlesToggle={onSubtitlesToggle}
subtitles={subtitles}
Expand Down
19 changes: 13 additions & 6 deletions src/lib/viewers/media/DashViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -683,12 +683,18 @@ class DashViewer extends VideoBaseViewer {
this.autoCaptionDisplayer.setTextVisibility(areAutoCaptionsVisible);
} else {
this.setupAutoCaptionDisplayer(textCues);
// Update the subtitles/caption button to reflect auto-translation
this.mediaControls.setLabel(this.mediaControls.subtitlesButtonEl, __('media_auto_generated_captions'));
this.mediaControls.initSubtitles(
[__('auto_generated')],
getLanguageName(this.options.location.locale.substring(0, 2)),
);

if (this.getViewerOption('useReactControls')) {
this.textTracks = [{ id: 0, language: __('auto_generated') }];
this.initSubtitles();
} else {
// Update the subtitles/caption button to reflect auto-translation
this.mediaControls.setLabel(this.mediaControls.subtitlesButtonEl, __('media_auto_generated_captions'));
this.mediaControls.initSubtitles(
[__('auto_generated')],
getLanguageName(this.options.location.locale.substring(0, 2)),
);
}
}
}

Expand Down Expand Up @@ -1134,6 +1140,7 @@ class DashViewer extends VideoBaseViewer {
bufferedRange={this.mediaEl.buffered}
currentTime={this.mediaEl.currentTime}
durationTime={this.mediaEl.duration}
isAutoGeneratedSubtitles={!!this.autoCaptionDisplayer}
isPlaying={!this.mediaEl.paused}
isPlayingHD={this.isPlayingHD()}
onAudioTrackChange={this.setAudioTrack}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/viewers/media/__tests__/DashControls-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ describe('DashControls', () => {

test('should render SubtitlesToggle if subtitles exist', () => {
const onSubtitlesToggle = jest.fn();
const wrapper = getWrapper({ onSubtitlesToggle, subtitles });
const wrapper = getWrapper({ isAutoGeneratedSubtitles: true, onSubtitlesToggle, subtitles });
expect(wrapper.find(SubtitlesToggle).props()).toMatchObject({
isAutoGeneratedSubtitles: true,
isShowingSubtitles: true,
onSubtitlesToggle,
});
Expand Down
20 changes: 20 additions & 0 deletions src/lib/viewers/media/__tests__/DashViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,26 @@ describe('lib/viewers/media/DashViewer', () => {
dash.loadAutoGeneratedCaptions(transcript);
expect(dash.setupAutoCaptionDisplayer).toBeCalledWith(cues);
});

describe('With React controls', () => {
beforeEach(() => {
jest.spyOn(dash, 'getViewerOption').mockImplementation(() => true);
jest.spyOn(dash, 'initSubtitles').mockImplementation();
jest.spyOn(dash.mediaControls, 'setLabel').mockImplementation();
jest.spyOn(dash.mediaControls, 'initSubtitles').mockImplementation();
});

test('should update textTracks and call initSubtitles', () => {
dash.autoCaptionDisplayer = null;
dash.createTextCues.mockReturnValue(cues);
dash.loadAutoGeneratedCaptions(transcript);

expect(dash.initSubtitles).toBeCalled();
expect(dash.textTracks).toEqual([{ id: 0, language: __('auto_generated') }]);
expect(dash.mediaControls.setLabel).not.toBeCalled();
expect(dash.mediaControls.initSubtitles).not.toBeCalled();
});
});
});

describe('createTextCues()', () => {
Expand Down

0 comments on commit 7c59cb1

Please sign in to comment.