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