Skip to content

Commit

Permalink
refactor: rearrange getOrCreateMediaElementSourceAndUpdateMap
Browse files Browse the repository at this point in the history
  • Loading branch information
WofWca committed Jun 11, 2023
1 parent 505b559 commit c63821f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 21 deletions.
4 changes: 3 additions & 1 deletion src/entry-points/content/AllMediaElementsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ import type { keydownEventToActions } from '@/hotkeys';
import broadcastStatus from './broadcastStatus';
import once from 'lodash/once';
import debounce from 'lodash/debounce';
import { mediaElementSourcesMap } from '@/entry-points/content/audioContext';
import {
mediaElementSourcesMap
} from '@/entry-points/content/getOrCreateMediaElementSourceAndUpdateMap';
import {
lastPlaybackRateSetByThisExtensionMap, lastDefaultPlaybackRateSetByThisExtensionMap,
setPlaybackRateAndRememberIt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ import VolumeFilterNode from '@/entry-points/content/VolumeFilter/VolumeFilterNo
import lookaheadVolumeFilterSmoothing from './lookaheadVolumeFilterSmoothing.json'
import {
audioContext as commonAudioContext,
getOrCreateMediaElementSourceAndUpdateMap
} from '@/entry-points/content/audioContext';
import {
getOrCreateMediaElementSourceAndUpdateMap
} from '@/entry-points/content/getOrCreateMediaElementSourceAndUpdateMap';
import {
setPlaybackRateAndRememberIt,
setDefaultPlaybackRateAndRememberIt,
Expand Down Expand Up @@ -363,7 +365,10 @@ export default class Controller {
if (BUILD_DEFINITIONS.BROWSER === 'gecko') {
const mozCaptureStreamUsed = !unprefixedCaptureStreamPresent;
if (mozCaptureStreamUsed) {
const mediaElementSource = getOrCreateMediaElementSourceAndUpdateMap(element);
const [, mediaElementSource] = getOrCreateMediaElementSourceAndUpdateMap(
element,
() => commonAudioContext
);
mediaElementSource.connect(commonAudioContext.destination);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
*/

import browser from '@/webextensions-api';
import { audioContext, getOrCreateMediaElementSourceAndUpdateMap } from '@/entry-points/content/audioContext';
import { audioContext } from '@/entry-points/content/audioContext';
import {
getOrCreateMediaElementSourceAndUpdateMap
} from '@/entry-points/content/getOrCreateMediaElementSourceAndUpdateMap';
import {
getRealtimeMargin,
getOptimalLookaheadDelay,
Expand Down Expand Up @@ -291,7 +294,10 @@ export default class Controller {
});
}

const mediaElementSource = getOrCreateMediaElementSourceAndUpdateMap(element);
const [, mediaElementSource] = getOrCreateMediaElementSourceAndUpdateMap(
element,
() => audioContext
);
let toDestinationChainLastConnectedLink: { connect: (destinationNode: AudioNode) => void }
= mediaElementSource;
if (this.isStretcherEnabled()) {
Expand Down
16 changes: 0 additions & 16 deletions src/entry-points/content/audioContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,3 @@ export const audioContext = new AudioContext({
// Would be cool if I was wrong.
latencyHint: 'playback',
});

// Doing it the way it's suggested in https://stackoverflow.com/a/39725071/10406353
export const mediaElementSourcesMap: WeakMap<HTMLMediaElement, MediaElementAudioSourceNode> = new WeakMap();

export function getOrCreateMediaElementSourceAndUpdateMap(element: HTMLMediaElement): MediaElementAudioSourceNode {
const srcFromMap = mediaElementSourcesMap.get(element);
let mediaElementSource: MediaElementAudioSourceNode;
if (srcFromMap) {
mediaElementSource = srcFromMap;
mediaElementSource.disconnect();
} else {
mediaElementSource = audioContext.createMediaElementSource(element);
mediaElementSourcesMap.set(element, mediaElementSource)
}
return mediaElementSource;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @license
* Copyright (C) 2020, 2021, 2022, 2023 WofWca <[email protected]>
*
* This file is part of Jump Cutter Browser Extension.
*
* Jump Cutter Browser Extension is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Jump Cutter Browser Extension is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Jump Cutter Browser Extension. If not, see <https://www.gnu.org/licenses/>.
*/

// Doing it the way it's suggested in https://stackoverflow.com/a/39725071/10406353
export const mediaElementSourcesMap:
WeakMap<HTMLMediaElement, [AudioContext, MediaElementAudioSourceNode]>
= new WeakMap();

/**
* @param getDefaultAudioContext must return an AudioContext that is gonna be used when
* `createMediaElementSource` has not been called for the `element` yet.
*/
export function getOrCreateMediaElementSourceAndUpdateMap(
element: HTMLMediaElement,
getDefaultAudioContext: () => AudioContext,
): [AudioContext, MediaElementAudioSourceNode] {
const fromMap = mediaElementSourcesMap.get(element);
// let mediaElementSource: MediaElementAudioSourceNode;
if (fromMap) {
const mediaElementSource = fromMap[1];
// Act as if it's the first time that `createMediaElementSource` is called for the element
// (i.e. it's not connected to anything, not even `context.destination`).
mediaElementSource.disconnect();
return fromMap;
} else {
const audioContext = getDefaultAudioContext();
const mediaElementSource = audioContext.createMediaElementSource(element);
const tuple: [AudioContext, MediaElementAudioSourceNode]
= [audioContext, mediaElementSource];
mediaElementSourcesMap.set(element, tuple);
return tuple;
}
}

0 comments on commit c63821f

Please sign in to comment.