[Refactoring] Media: Remove Stream format support (#3800)
This commit is contained in:
parent
d2b9a55d40
commit
0d9b4d08bd
@ -33,7 +33,7 @@ export default async function downloadMedia(
|
||||
) {
|
||||
const {
|
||||
data, mimeType, fullSize,
|
||||
} = await download(url, client, onProgress, start, end, mediaFormat, isHtmlAllowed) || {};
|
||||
} = await download(url, client, onProgress, start, end, isHtmlAllowed) || {};
|
||||
|
||||
if (!data) {
|
||||
return undefined;
|
||||
@ -75,7 +75,6 @@ async function download(
|
||||
onProgress?: ApiOnProgress,
|
||||
start?: number,
|
||||
end?: number,
|
||||
mediaFormat?: ApiMediaFormat,
|
||||
isHtmlAllowed?: boolean,
|
||||
) {
|
||||
const parsed = parseMediaUrl(url);
|
||||
@ -146,10 +145,6 @@ async function download(
|
||||
}
|
||||
|
||||
if (MEDIA_ENTITY_TYPES.has(entityType)) {
|
||||
if (mediaFormat === ApiMediaFormat.Stream) {
|
||||
onProgress!.acceptsBuffer = true;
|
||||
}
|
||||
|
||||
const data = await client.downloadMedia(entity, {
|
||||
sizeType, start, end, progressCallback: onProgress, workers: DOWNLOAD_WORKERS,
|
||||
});
|
||||
|
||||
@ -4,7 +4,6 @@
|
||||
export enum ApiMediaFormat {
|
||||
BlobUrl,
|
||||
Progressive,
|
||||
Stream,
|
||||
DownloadUrl,
|
||||
Text,
|
||||
}
|
||||
|
||||
@ -24,7 +24,6 @@ export interface ApiOnProgress {
|
||||
): void;
|
||||
|
||||
isCanceled?: boolean;
|
||||
acceptsBuffer?: boolean;
|
||||
}
|
||||
|
||||
export interface ApiAttachment {
|
||||
|
||||
@ -21,9 +21,7 @@ export default function useMediaWithLoadProgress(
|
||||
isHtmlAllowed = false,
|
||||
) {
|
||||
const mediaData = mediaHash ? mediaLoader.getFromMemory(mediaHash) : undefined;
|
||||
const isStreaming = mediaFormat === ApiMediaFormat.Stream || (
|
||||
IS_PROGRESSIVE_SUPPORTED && mediaFormat === ApiMediaFormat.Progressive
|
||||
);
|
||||
const isStreaming = IS_PROGRESSIVE_SUPPORTED && mediaFormat === ApiMediaFormat.Progressive;
|
||||
const forceUpdate = useForceUpdate();
|
||||
const id = useUniqueId();
|
||||
const [loadProgress, setLoadProgress] = useState(mediaData && !isStreaming ? 1 : 0);
|
||||
|
||||
@ -9,8 +9,6 @@ import { Foreman } from '../../../util/foreman';
|
||||
|
||||
interface OnProgress {
|
||||
isCanceled?: boolean;
|
||||
acceptsBuffer?: boolean;
|
||||
|
||||
(
|
||||
progress: number, // Float between 0 and 1.
|
||||
...args: any[]
|
||||
|
||||
@ -6,7 +6,6 @@ export interface CancellableCallback {
|
||||
): void;
|
||||
|
||||
isCanceled?: boolean;
|
||||
acceptsBuffer?: boolean;
|
||||
}
|
||||
|
||||
type InitData = {
|
||||
|
||||
@ -24,7 +24,6 @@ const asCacheApiType = {
|
||||
[ApiMediaFormat.Text]: cacheApi.Type.Text,
|
||||
[ApiMediaFormat.DownloadUrl]: undefined,
|
||||
[ApiMediaFormat.Progressive]: undefined,
|
||||
[ApiMediaFormat.Stream]: undefined,
|
||||
};
|
||||
|
||||
const PROGRESSIVE_URL_PREFIX = `${IS_ELECTRON ? ELECTRON_HOST_URL : '.'}/progressive/`;
|
||||
@ -157,29 +156,6 @@ async function fetchFromCacheOrRemote(
|
||||
}
|
||||
}
|
||||
|
||||
if (mediaFormat === ApiMediaFormat.Stream) {
|
||||
const mediaSource = new MediaSource();
|
||||
const streamUrl = URL.createObjectURL(mediaSource);
|
||||
let isOpen = false;
|
||||
|
||||
mediaSource.addEventListener('sourceopen', () => {
|
||||
if (isOpen) {
|
||||
return;
|
||||
}
|
||||
isOpen = true;
|
||||
|
||||
const sourceBuffer = mediaSource.addSourceBuffer('audio/mpeg');
|
||||
|
||||
const onProgress = makeOnProgress(url, mediaSource, sourceBuffer);
|
||||
cancellableCallbacks.set(url, onProgress);
|
||||
|
||||
void callApi('downloadMedia', { url, mediaFormat }, onProgress);
|
||||
});
|
||||
|
||||
memoryCache.set(url, streamUrl);
|
||||
return streamUrl;
|
||||
}
|
||||
|
||||
const onProgress = makeOnProgress(url);
|
||||
cancellableCallbacks.set(url, onProgress);
|
||||
|
||||
@ -221,22 +197,12 @@ async function fetchFromCacheOrRemote(
|
||||
return prepared;
|
||||
}
|
||||
|
||||
function makeOnProgress(url: string, mediaSource?: MediaSource, sourceBuffer?: SourceBuffer) {
|
||||
const onProgress: ApiOnProgress = (progress: number, arrayBuffer: ArrayBuffer) => {
|
||||
function makeOnProgress(url: string) {
|
||||
const onProgress: ApiOnProgress = (progress: number) => {
|
||||
progressCallbacks.get(url)?.forEach((callback) => {
|
||||
callback(progress);
|
||||
if (callback.isCanceled) onProgress.isCanceled = true;
|
||||
});
|
||||
|
||||
if (progress === 1) {
|
||||
mediaSource?.endOfStream();
|
||||
}
|
||||
|
||||
if (!arrayBuffer) {
|
||||
return;
|
||||
}
|
||||
|
||||
sourceBuffer?.appendBuffer(arrayBuffer);
|
||||
};
|
||||
|
||||
return onProgress;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user