Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ [Story system-layer] Add CC Icon that toggles captions #37884

Merged
merged 23 commits into from
Apr 18, 2022

Conversation

mszylkowski
Copy link
Contributor

@mszylkowski mszylkowski commented Mar 16, 2022

Adds a CC icon on pages with videos that have closed captions.

image

The PR does 3 main things:

  • When a page becomes active (resume_()) it will activate or deactivate the captions of the videos inside it. It will also toggle when the button is clicked.
  • When a page becomes active it needs to show or hide the toggle in the system layer.
  • The system layer needs to toggle the captions in the videos when the CC icon is clicked.

Note: this PR increases the bundle size by 1kB (mostly due to the large SVGs), so my suggestion is to hold off until we have more captions on videos (currently about 1000 stories have captions, which is not very significant). I also don't know why the auto-ads size changes so much.

dist/v0/amp-story-0.1.mjs: Δ +1.11KB
dist/v0/amp-story-1.0.mjs: Δ +1.11KB
dist/v0/amp-story-0.1.js: Δ +0.99KB
dist/v0/amp-story-1.0.js: Δ +0.99KB

dist/v0/amp-video-0.1.mjs: Δ +0.04KB
dist/v0/amp-video-0.1.js: Δ +0.05KB
dist/v0/amp-story-auto-ads-0.1.mjs: Δ +0.42KB
dist/v0/amp-story-auto-ads-0.1.js: Δ +0.47KB

@amp-owners-bot
Copy link

amp-owners-bot bot commented Mar 16, 2022

Hey @gmajoulet, @newmuis! These files were changed:

extensions/amp-story/1.0/_locales/en.json
extensions/amp-story/1.0/amp-story-page.js
extensions/amp-story/1.0/amp-story-store-service.js
extensions/amp-story/1.0/amp-story-system-layer.css
extensions/amp-story/1.0/amp-story-system-layer.js
extensions/amp-story/1.0/audio.js
extensions/amp-story/1.0/test/test-amp-story-system-layer.js
src/service/localization/strings.js

@mszylkowski mszylkowski removed the request for review from rbeckthomas March 16, 2022 18:44
Copy link
Contributor Author

@mszylkowski mszylkowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the other methods in the files. Proposed changing names to closely match the conventions.

extensions/amp-story/1.0/amp-story-page.js Show resolved Hide resolved
extensions/amp-story/1.0/amp-story-system-layer.js Outdated Show resolved Hide resolved
@mszylkowski
Copy link
Contributor Author

@gmajoulet for amp-video integration / OWNERS

@@ -569,6 +585,8 @@ export class AmpStoryStoreService {
[StateProperty.MUTED_STATE]: true,
[StateProperty.PAGE_ATTACHMENT_STATE]: false,
[StateProperty.PAGE_HAS_AUDIO_STATE]: false,
[StateProperty.PAGE_HAS_CAPTIONS_STATE]: false,
[StateProperty.CAPTIONS_STATE]: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(following up from a conversation with @gmajoulet )
Captions are currently on by default. I think it makes sense to keep this field true so the expected UX does not change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want them on if provided by the publisher (current default), but off when provided by Monti (for now, till we hear back from product). I think this code will be OK for this PR, we can turn them off for Monti later. Eventually we'll unify their defaults

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes some kind of sense but this is a setting that's kept throughout the story, so what would happen if there are videos with publisher captions and videos with cache captions in the same story? We just turn it on by default (as long as there's one track in the story provided by the publisher)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand we are leaving captions on by default - even after Monti captions are launched.
@gmajoulet please confirm

Copy link
Contributor

@gmajoulet gmajoulet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Higher level comments, I'll review the rest once we get the heuristics right :))

extensions/amp-story/1.0/amp-story-page.js Outdated Show resolved Hide resolved
extensions/amp-story/1.0/amp-story-page.js Outdated Show resolved Hide resolved
* @return {!Promise<boolean>}
* @private
*/
hasVideoWithAudio_() {
hasLoadedVideoWith_(func) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a first read, I really disagree with this code, can you correct me if I'm wrong?

amp-video.buildCallback() will only resolve when the video cache call resolved and is processed (sources are appended, audio attr is set, and track is appended).

waitForElementsWithUnresolvedAudio retrieves the component impl through getImpl() which sounds unnecessary, it should rely on a post buildCallback signal. Same to wait for tracks to be appended.

If you don't want to write new code you can rely on waitForPlaybackMediaLayout_ but technically you could even do sooner than that.
On top of being too complicated for no good reason, and building on top of it, this code is also wrong and doesn't rely on the media selectors that we built to look within friendly iframes (ads).

I'd really want this code to re-use the existing helpers and not build a custom broken solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getImpl() is the promise that resolves when buildCallback finishes, which is why we can use it to wait for the tracks to be appended. I'll try to refactor this so it's easier to follow and more streamlined

Copy link
Contributor Author

@mszylkowski mszylkowski Apr 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that waiting for waitForPlaybackMediaLayout resolves too late, we could wait for whenUpgradedToCustomElement(mediaEl).then((el) => el.signals().whenSignal(BUILD)) but it's longer than getImpl() and does the same thing. I think renaming the method we use to waitForVideosWithCachedSources makes it more explicit and then we can do the logic to find if any of the videos have tracks

extensions/amp-story/1.0/amp-story-page.js Outdated Show resolved Hide resolved
* @private
*/
initializeCaptionsListener_() {
this.hasLoadedVideoWith_((video) => video.querySelector('track')).then(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(also, his helper doesn't even check if the video has loaded at all)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It waits for the buildCallback to finish due to the getImpl() call for cached videos with audio, which is what Loaded was trying to mean in this context. I'll try to clean this logic up anyways, so it's more clear

@@ -774,6 +777,7 @@ export class AmpVideo extends AMP.BaseElement {
if (!captionsElement) {
return;
}
this.captionsAreDelegated_ = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This architecture is wrong, you're mixing and tying up different concepts / features together, which is very hurtful long term. It becomes really hard to untangle as specific features evolve, or for new engs to understand.

What you mean by captionsAreDelegated is "we're using amp-story-captions.
What we want is amp-story components will toggle their captions on/off, this is not related to what system we use to render the captions.

Please use this.isManagedByPool_() to know if you're in a story context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're mixing and tying up different concepts / features together

All the code in this file is written in the methods setupCaptions and toggleCaptions, can you explain better how that's tying up other concepts?

What we want is amp-story components will toggle their captions on/off, this is not related to what system we use to render the captions.

Currently amp-video toggles it's captions when the method toggleCaptions is called, but as I understand you're suggesting that amp-story-page (or similar) should toggle the captions. However, if we don't know whether the captions are managed by amp-story-captions or not, then we couldn't tell whether to show the captions (if the video uses native captions) or hide them (so amp-story-captions can still receive them to display them) when enabling the captions. We need to have the code in amp-video because we want for this to work without amp-story-captions, and we can't have this in amp-story-page because it would be very weird to wait for buildCallback then get the tracks from the video then update them. What do you suggest then?

Please use this.isManagedByPool_() to know if you're in a story context.

That wouldn't work because inside a story the video could show native captions (if there are no amp-story-captions) or custom captions. We need to know if (captions-id is present and points to a valid element) { enable/disable captions} else {hide/disable captions} which is

extensions/amp-story/1.0/amp-story-page.js Outdated Show resolved Hide resolved
extensions/amp-story/1.0/amp-story-page.js Outdated Show resolved Hide resolved
extensions/amp-story/1.0/amp-story-page.js Outdated Show resolved Hide resolved
extensions/amp-story/1.0/amp-story-page.js Outdated Show resolved Hide resolved
extensions/amp-story/1.0/amp-story-page.js Outdated Show resolved Hide resolved
extensions/amp-video/0.1/amp-video.js Outdated Show resolved Hide resolved
extensions/amp-video/0.1/amp-video.js Outdated Show resolved Hide resolved
extensions/amp-story/1.0/amp-story-page.js Outdated Show resolved Hide resolved
extensions/amp-story/1.0/amp-story-system-layer.js Outdated Show resolved Hide resolved
extensions/amp-story/1.0/amp-story-system-layer.js Outdated Show resolved Hide resolved
extensions/amp-story/1.0/amp-story-system-layer.js Outdated Show resolved Hide resolved
extensions/amp-story/1.0/amp-story-system-layer.js Outdated Show resolved Hide resolved
extensions/amp-video/0.1/amp-video.js Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants