mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 09:01:17 -08:00
perf(media): stream authenticated audio and video
This commit is contained in:
1 parent
963e73df41
commit
e3f1422c3f
3 files changed
+22
-16
No files matched your search
@@ -23,6 +23,7 @@ import { decryptFile, downloadEncryptedMedia, downloadMedia, mxcUrlToHttp } from
|
||||
import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
|
||||
import { useRevokeObjectURL } from '$hooks/useObjectURL';
|
||||
import { MEDIA_VOLUME_KEY } from '$components/media';
|
||||
import { hasControllingServiceWorker } from '$utils/platform';
|
||||
|
||||
const PLAY_TIME_THROTTLE_OPS = {
|
||||
wait: 500,
|
||||
@@ -56,8 +57,8 @@ export function AudioContent({
|
||||
useCallback(async () => {
|
||||
const mediaUrl = mxcUrlToHttp(mx, url, useAuthentication);
|
||||
if (!mediaUrl) throw new Error('Invalid media URL');
|
||||
// Stream via the sable-media:// protocol (Range) instead of buffering a blob.
|
||||
if (!encInfo && isTauri()) return mediaUrl;
|
||||
// Native media and service-worker-authenticated browser media can stream with Range requests.
|
||||
if (!encInfo && (isTauri() || hasControllingServiceWorker())) return mediaUrl;
|
||||
const fileContent = encInfo
|
||||
? await downloadEncryptedMedia(mediaUrl, (encBuf) => decryptFile(encBuf, mimeType, encInfo))
|
||||
: await downloadMedia(mediaUrl);
|
||||
|
||||
@@ -29,6 +29,7 @@ import { useRevokeObjectURL } from '$hooks/useObjectURL';
|
||||
import { validBlurHash } from '$utils/blurHash';
|
||||
import * as css from './style.css';
|
||||
import { MATRIX_UNSTABLE_BLUR_HASH_PROPERTY_NAME } from '../../../../unstable/prefixes';
|
||||
import { hasControllingServiceWorker } from '$utils/platform';
|
||||
|
||||
type RenderVideoProps = {
|
||||
title: string;
|
||||
@@ -83,8 +84,8 @@ export const VideoContent = as<'div', VideoContentProps>(
|
||||
|
||||
const mediaUrl = mxcUrlToHttp(mx, url, useAuthentication);
|
||||
if (!mediaUrl) throw new Error('Invalid media URL');
|
||||
// Stream via the sable-media:// protocol (Range) instead of buffering a blob.
|
||||
if (!encInfo && isTauri()) return mediaUrl;
|
||||
// Native media and service-worker-authenticated browser media can stream with Range requests.
|
||||
if (!encInfo && (isTauri() || hasControllingServiceWorker())) return mediaUrl;
|
||||
const fileContent = encInfo
|
||||
? await downloadEncryptedMedia(mediaUrl, (encBuf) =>
|
||||
decryptFile(encBuf, mimeType, encInfo)
|
||||
|
||||
@@ -691,11 +691,11 @@ function validMediaRequest(url: string, baseUrl: string): boolean {
|
||||
});
|
||||
}
|
||||
|
||||
function fetchConfig(token: string): RequestInit {
|
||||
function fetchConfig(token: string, request?: Request): RequestInit {
|
||||
const headers = new Headers(request?.headers);
|
||||
headers.set('Authorization', `Bearer ${token}`);
|
||||
return {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
headers,
|
||||
cache: 'default',
|
||||
};
|
||||
}
|
||||
@@ -703,16 +703,20 @@ function fetchConfig(token: string): RequestInit {
|
||||
const inflightMediaFetches = new Map<string, Promise<Response>>();
|
||||
|
||||
function respondWithInflightMedia(
|
||||
url: string,
|
||||
request: Request,
|
||||
token: string,
|
||||
redirect: RequestRedirect
|
||||
): Promise<Response> {
|
||||
const key = `${token}\x00${url}\x00${redirect}`;
|
||||
const range = request.headers.get('Range') ?? '';
|
||||
const key = `${token}\x00${request.url}\x00${redirect}\x00${range}`;
|
||||
const existing = inflightMediaFetches.get(key);
|
||||
if (existing) {
|
||||
return existing.then((res) => res.clone());
|
||||
}
|
||||
const promise = fetch(url, { ...fetchConfig(token), redirect }).finally(() => {
|
||||
// Fetch by URL instead of reusing the subresource Request. Image requests commonly carry
|
||||
// mode: "no-cors", which prevents the Authorization header above from reaching the server.
|
||||
// The copied headers still preserve Range for streaming audio and video.
|
||||
const promise = fetch(request.url, { ...fetchConfig(token, request), redirect }).finally(() => {
|
||||
inflightMediaFetches.delete(key);
|
||||
});
|
||||
inflightMediaFetches.set(key, promise);
|
||||
@@ -768,7 +772,7 @@ self.addEventListener('fetch', (event: FetchEvent) => {
|
||||
|
||||
const session = clientId ? sessions.get(clientId) : undefined;
|
||||
if (session && validMediaRequest(url, session.baseUrl)) {
|
||||
event.respondWith(respondWithInflightMedia(url, session.accessToken, redirect));
|
||||
event.respondWith(respondWithInflightMedia(event.request, session.accessToken, redirect));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -788,7 +792,7 @@ self.addEventListener('fetch', (event: FetchEvent) => {
|
||||
? preloadedSession
|
||||
: undefined);
|
||||
if (byBaseUrl) {
|
||||
event.respondWith(respondWithInflightMedia(url, byBaseUrl.accessToken, redirect));
|
||||
event.respondWith(respondWithInflightMedia(event.request, byBaseUrl.accessToken, redirect));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -798,7 +802,7 @@ self.addEventListener('fetch', (event: FetchEvent) => {
|
||||
event.respondWith(
|
||||
loadPersistedSession().then((persisted) => {
|
||||
if (persisted && validMediaRequest(url, persisted.baseUrl)) {
|
||||
return respondWithInflightMedia(url, persisted.accessToken, redirect);
|
||||
return respondWithInflightMedia(event.request, persisted.accessToken, redirect);
|
||||
}
|
||||
return fetch(event.request);
|
||||
})
|
||||
@@ -810,13 +814,13 @@ self.addEventListener('fetch', (event: FetchEvent) => {
|
||||
requestSessionWithTimeout(clientId).then(async (s) => {
|
||||
// Primary: session received from the live client window.
|
||||
if (s && validMediaRequest(url, s.baseUrl)) {
|
||||
return respondWithInflightMedia(url, s.accessToken, redirect);
|
||||
return respondWithInflightMedia(event.request, s.accessToken, redirect);
|
||||
}
|
||||
// Fallback: try the persisted session (helps when SW restarts on iOS and
|
||||
// the client window hasn't responded to requestSession yet).
|
||||
const persisted = await loadPersistedSession();
|
||||
if (persisted && validMediaRequest(url, persisted.baseUrl)) {
|
||||
return respondWithInflightMedia(url, persisted.accessToken, redirect);
|
||||
return respondWithInflightMedia(event.request, persisted.accessToken, redirect);
|
||||
}
|
||||
console.warn(
|
||||
'[SW fetch] No valid session for media request',
|
||||
|
||||
Reference in new issue
Block a user