1
0
Fork 0
mirror of https://gitlab.com/futo-org/fcast.git synced 2025-06-24 21:25:23 +00:00

Receivers: Multiple MediaCache fixes

This commit is contained in:
Michael Hollister 2025-06-11 14:51:55 -05:00
parent c95f254b3b
commit 7a0c865bb2
4 changed files with 208 additions and 133 deletions

View file

@ -23,17 +23,28 @@ class CacheObject {
export class MediaCache { export class MediaCache {
private static instance: MediaCache = null; private static instance: MediaCache = null;
private cache = new Map<number, CacheObject>(); private cache: Map<number, CacheObject>;
private cacheUrlMap = new Map<string,number>(); private cacheUrlMap: Map<string,number>;
private playlist: PlaylistContent; private playlist: PlaylistContent;
private playlistIndex: number;
private quota: number; private quota: number;
private cacheSize: number = 0; private cacheSize: number;
private cacheWindowStart: number = 0; private cacheWindowStart: number;
private cacheWindowEnd: number = 0; private cacheWindowEnd: number;
private pendingDownloads: Set<number>;
private isDownloading: boolean;
constructor(playlist: PlaylistContent) { constructor(playlist: PlaylistContent) {
MediaCache.instance = this; MediaCache.instance = this;
this.playlist = playlist; this.playlist = playlist;
this.playlistIndex = playlist.offset ? playlist.offset : 0;
this.cache = new Map<number, CacheObject>();
this.cacheUrlMap = new Map<string,number>();
this.cacheSize = 0;
this.cacheWindowStart = 0;
this.cacheWindowEnd = 0;
this.pendingDownloads = new Set();
this.isDownloading = false;
if (!fs.existsSync('/cache')) { if (!fs.existsSync('/cache')) {
fs.mkdirSync('/cache'); fs.mkdirSync('/cache');
@ -41,7 +52,8 @@ export class MediaCache {
// @ts-ignore // @ts-ignore
if (TARGET === 'electron') { if (TARGET === 'electron') {
this.quota = Math.min(Math.floor(os.freemem() / 4), 4 * 1024 * 1024 * 1024); // 4GB // this.quota = Math.min(Math.floor(os.freemem() / 4), 4 * 1024 * 1024 * 1024); // 4GB
this.quota = Math.min(Math.floor(os.freemem() / 4), 35 * 1024 * 1024); // 4GB
// @ts-ignore // @ts-ignore
} else if (TARGET === 'webOS' || TARGET === 'tizenOS') { } else if (TARGET === 'webOS' || TARGET === 'tizenOS') {
@ -55,6 +67,8 @@ export class MediaCache {
} }
public destroy() { public destroy() {
this.cache.forEach((item) => { fs.unlinkSync(item.path); });
MediaCache.instance = null; MediaCache.instance = null;
this.cache.clear(); this.cache.clear();
this.cache = null; this.cache = null;
@ -65,6 +79,8 @@ export class MediaCache {
this.cacheSize = 0; this.cacheSize = 0;
this.cacheWindowStart = 0; this.cacheWindowStart = 0;
this.cacheWindowEnd = 0; this.cacheWindowEnd = 0;
this.pendingDownloads.clear();
this.isDownloading = false;
} }
public static getInstance() { public static getInstance() {
@ -89,137 +105,207 @@ export class MediaCache {
return this.cache.get(this.cacheUrlMap.get(url)).size; return this.cache.get(this.cacheUrlMap.get(url)).size;
} }
public cacheForwardItems(cacheIndex: number, cacheAmount: number, playlistIndex: number) { public cacheItems(playlistIndex: number) {
if (cacheAmount > 0) { this.playlistIndex = playlistIndex;
for (let i = cacheIndex; i < this.playlist.items.length; i++) {
const item = this.playlist.items[i]; if (this.playlist.forwardCache && this.playlist.forwardCache > 0) {
if (item.cache) { let cacheAmount = this.playlist.forwardCache;
if (this.cache.has(i)) {
this.cacheForwardItems(i + 1, cacheAmount - 1, playlistIndex); for (let i = playlistIndex + 1; i < this.playlist.items.length; i++) {
if (cacheAmount === 0) {
break; break;
} }
const tempCacheObject = new CacheObject();
downloadFile(item.url, tempCacheObject.path, if (this.playlist.items[i].cache) {
cacheAmount--;
if (!this.cache.has(i)) {
this.pendingDownloads.add(i);
}
}
}
}
if (this.playlist.backwardCache && this.playlist.backwardCache > 0) {
let cacheAmount = this.playlist.backwardCache;
for (let i = playlistIndex - 1; i >= 0; i--) {
if (cacheAmount === 0) {
break;
}
if (this.playlist.items[i].cache) {
cacheAmount--;
if (!this.cache.has(i)) {
this.pendingDownloads.add(i);
}
}
}
}
this.updateCacheWindow();
if (!this.isDownloading) {
this.isDownloading = true;
this.downloadItems();
}
}
private downloadItems() {
if (this.pendingDownloads.size > 0) {
let itemIndex = 0;
let minDistance = this.playlist.items.length;
for (let i of this.pendingDownloads.values()) {
if (Math.abs(this.playlistIndex - i) < minDistance) {
minDistance = Math.abs(this.playlistIndex - i);
itemIndex = i;
}
else if (Math.abs(this.playlistIndex - i) === minDistance && i > this.playlistIndex) {
itemIndex = i;
}
}
this.pendingDownloads.delete(itemIndex);
// Due to downloads being async, pending downloads can become out-of-sync with the current playlist index/target cache window
if (!this.shouldDownloadItem(itemIndex)) {
logger.debug(`Discarding download index ${itemIndex} since its outside cache window [${this.cacheWindowStart} - ${this.cacheWindowEnd}]`);
this.downloadItems();
return;
}
const tempCacheObject = new CacheObject();
downloadFile(this.playlist.items[itemIndex].url, tempCacheObject.path, true, this.playlist.items[itemIndex].headers,
(downloadedBytes: number) => { (downloadedBytes: number) => {
let underQuota = true; let underQuota = true;
if (this.cacheSize + downloadedBytes > this.quota) { if (this.cacheSize + downloadedBytes > this.quota) {
underQuota = this.purgeCacheItems(i, downloadedBytes, playlistIndex); underQuota = this.purgeCacheItems(itemIndex, downloadedBytes);
} }
return underQuota; return underQuota;
}, null, }, null)
(downloadedBytes: number) => { .then(() => {
this.finalizeCacheItem(tempCacheObject, i, downloadedBytes, playlistIndex); this.finalizeCacheItem(tempCacheObject, itemIndex);
this.cacheForwardItems(i + 1, cacheAmount - 1, playlistIndex); this.downloadItems();
}, true) }, (error) => {
.catch((error) => { logger.warn(error);
logger.error(error); this.downloadItems();
}); });
}
else {
this.isDownloading = false;
}
}
private shouldDownloadItem(index: number): boolean {
let download = false;
if (index > this.playlistIndex) {
if (this.playlist.forwardCache && this.playlist.forwardCache > 0) {
const indexList = [...this.cache.keys(), index].sort((a, b) => a - b);
let forwardCacheItems = this.playlist.forwardCache;
for (let i of indexList) {
if (i > this.playlistIndex) {
forwardCacheItems--;
if (i === index) {
download = true;
}
else if (forwardCacheItems === 0) {
break; break;
} }
} }
} }
} }
}
else if (index < this.playlistIndex) {
if (this.playlist.backwardCache && this.playlist.backwardCache > 0) {
const indexList = [...this.cache.keys(), index].sort((a, b) => b - a);
let backwardCacheItems = this.playlist.backwardCache;
public cacheBackwardItems(cacheIndex: number, cacheAmount: number, playlistIndex: number) { for (let i of indexList) {
if (cacheAmount > 0) { if (i < this.playlistIndex) {
for (let i = cacheIndex; i >= 0; i--) { backwardCacheItems--;
const item = this.playlist.items[i];
if (item.cache) { if (i === index) {
if (this.cache.has(i)) { download = true;
this.cacheBackwardItems(i - 1, cacheAmount - 1, playlistIndex); }
else if (backwardCacheItems === 0) {
break; break;
} }
const tempCacheObject = new CacheObject(); }
}
}
}
downloadFile(item.url, tempCacheObject.path, return download;
(downloadedBytes: number) => { }
private purgeCacheItems(downloadItem: number, downloadedBytes: number): boolean {
let underQuota = true; let underQuota = true;
if (this.cacheSize + downloadedBytes > this.quota) {
underQuota = this.purgeCacheItems(i, downloadedBytes, playlistIndex);
}
return underQuota; while (this.cacheSize + downloadedBytes > this.quota) {
}, null, let purgeIndex = this.playlistIndex;
(downloadedBytes: number) => {
this.finalizeCacheItem(tempCacheObject, i, downloadedBytes, playlistIndex);
this.cacheBackwardItems(i - 1, cacheAmount - 1, playlistIndex);
}, true)
.catch((error) => {
logger.error(error);
});
break;
}
}
}
}
private purgeCacheItems(downloadItem: number, downloadedBytes: number, playlistIndex: number): boolean {
this.updateCacheWindow(playlistIndex);
let underQuota = true;
let purgeIndex = playlistIndex;
let purgeDistance = 0; let purgeDistance = 0;
logger.debug(`Downloading item ${downloadItem} with playlist index ${playlistIndex} and cache window: [${this.cacheWindowStart} - ${this.cacheWindowEnd}]`); logger.debug(`Downloading item ${downloadItem} with playlist index ${this.playlistIndex} and cache window: [${this.cacheWindowStart} - ${this.cacheWindowEnd}]`);
// Priority: // Priority:
// 1. Purge first encountered item outside cache window // 1. Purge first encountered item outside cache window
// 2. Purge item furthest from view index inside window (except next item from view index) // 2. Purge item furthest from view index inside window (except next item from view index)
for (let index of this.cache.keys()) { for (let index of this.cache.keys()) {
if (index === downloadItem || index === playlistIndex || index === playlistIndex + 1) { if (index === downloadItem || index === this.playlistIndex || index === this.playlistIndex + 1) {
continue; continue;
} }
if (index < this.cacheWindowStart) { if (index < this.cacheWindowStart || index > this.cacheWindowEnd) {
purgeIndex = index; purgeIndex = index;
break; break;
} }
else if (index > this.cacheWindowEnd) { else if (Math.abs(this.playlistIndex - index) > purgeDistance) {
purgeIndex = index; purgeDistance = Math.abs(this.playlistIndex - index);
break;
}
else if (Math.abs(playlistIndex - index) > purgeDistance) {
purgeDistance = Math.abs(playlistIndex - index);
purgeIndex = index; purgeIndex = index;
} }
} }
if (purgeIndex !== playlistIndex) { if (purgeIndex !== this.playlistIndex) {
const deleteItem = this.cache.get(purgeIndex); const deleteItem = this.cache.get(purgeIndex);
fs.unlinkSync(deleteItem.path);
this.cacheSize -= deleteItem.size; this.cacheSize -= deleteItem.size;
this.cacheUrlMap.delete(deleteItem.url); this.cacheUrlMap.delete(deleteItem.url);
this.cache.delete(purgeIndex); this.cache.delete(purgeIndex);
this.updateCacheWindow(playlistIndex); this.updateCacheWindow();
logger.info(`Item ${downloadItem} pending download (${downloadedBytes} bytes) cannot fit in cache, purging ${purgeIndex} from cache. Remaining quota ${this.quota - this.cacheSize} bytes`); logger.info(`Item ${downloadItem} pending download (${downloadedBytes} bytes) cannot fit in cache, purging ${purgeIndex} from cache. Remaining quota ${this.quota - this.cacheSize} bytes`);
if (this.cacheSize + downloadedBytes > this.quota) {
underQuota = this.purgeCacheItems(downloadItem, downloadedBytes, playlistIndex);
}
} }
else { else {
// Cannot purge current item since we may already be streaming it // Cannot purge current item since we may already be streaming it
logger.warn(`Aborting item caching, cannot fit item ${downloadItem} (${downloadedBytes} bytes) within remaining space quota (${this.quota - this.cacheSize} bytes)`); logger.warn(`Aborting item caching, cannot fit item ${downloadItem} (${downloadedBytes} bytes) within remaining space quota (${this.quota - this.cacheSize} bytes)`);
underQuota = false; underQuota = false;
break;
}
} }
return underQuota; return underQuota;
} }
private finalizeCacheItem(cacheObject: CacheObject, index: number, size: number, playlistIndex: number) { private finalizeCacheItem(cacheObject: CacheObject, index: number) {
const size = fs.statSync(cacheObject.path).size;
cacheObject.size = size; cacheObject.size = size;
this.cacheSize += size; this.cacheSize += size;
logger.info(`Cached item ${index} (${cacheObject.size} bytes) with remaining quota ${this.quota - this.cacheSize} bytes: ${cacheObject.url}`); logger.info(`Cached item ${index} (${cacheObject.size} bytes) with remaining quota ${this.quota - this.cacheSize} bytes: ${cacheObject.url}`);
this.cache.set(index, cacheObject); this.cache.set(index, cacheObject);
this.cacheUrlMap.set(cacheObject.url, index); this.cacheUrlMap.set(cacheObject.url, index);
this.updateCacheWindow(playlistIndex); this.updateCacheWindow();
} }
private updateCacheWindow(playlistIndex: number) { private updateCacheWindow() {
const indexList = [...this.cache.keys()].sort((a, b) => a - b);
if (this.playlist.forwardCache && this.playlist.forwardCache > 0) { if (this.playlist.forwardCache && this.playlist.forwardCache > 0) {
let forwardCacheItems = this.playlist.forwardCache; let forwardCacheItems = this.playlist.forwardCache;
for (let index of this.cache.keys()) { for (let index of indexList) {
if (index > playlistIndex) { if (index > this.playlistIndex) {
forwardCacheItems--; forwardCacheItems--;
if (forwardCacheItems === 0) { if (forwardCacheItems === 0) {
@ -230,13 +316,13 @@ export class MediaCache {
} }
} }
else { else {
this.cacheWindowEnd = playlistIndex; this.cacheWindowEnd = this.playlistIndex;
} }
if (this.playlist.backwardCache && this.playlist.backwardCache > 0) { if (this.playlist.backwardCache && this.playlist.backwardCache > 0) {
let backwardCacheItems = this.playlist.backwardCache; let backwardCacheItems = this.playlist.backwardCache;
for (let index of this.cache.keys()) { for (let index of indexList) {
if (index < playlistIndex) { if (index < this.playlistIndex) {
backwardCacheItems--; backwardCacheItems--;
if (backwardCacheItems === 0) { if (backwardCacheItems === 0) {
@ -247,7 +333,7 @@ export class MediaCache {
} }
} }
else { else {
this.cacheWindowStart = playlistIndex this.cacheWindowStart = this.playlistIndex
} }
} }
} }

View file

@ -1,4 +1,5 @@
import * as fs from 'fs'; import * as fs from 'fs';
import * as url from 'url';
import { http, https } from 'modules/follow-redirects'; import { http, https } from 'modules/follow-redirects';
import * as memfs from 'modules/memfs'; import * as memfs from 'modules/memfs';
import { Logger, LoggerType } from 'common/Logger'; import { Logger, LoggerType } from 'common/Logger';
@ -35,14 +36,22 @@ export async function fetchJSON(url: string): Promise<any> {
}); });
} }
export async function downloadFile(url: string, destination: string, startCb: (downloadSize: number) => boolean = null, progressCb: (downloadedBytes: number, downloadSize: number) => void = null, finishCb: (downloadedBytes: number) => void = null, inMemory: boolean = false): Promise<void> { export async function downloadFile(downloadUrl: string, destination: string, inMemory: boolean = false, requestHeaders: { [key: string]: string } = null,
startCb: (downloadSize: number) => boolean = null,
progressCb: (downloadedBytes: number, downloadSize: number) => void = null): Promise<void> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const file = inMemory ? memfs.fs.createWriteStream(destination) : fs.createWriteStream(destination); const file = inMemory ? memfs.fs.createWriteStream(destination) : fs.createWriteStream(destination);
const protocol = url.startsWith('https') ? https : http; const protocol = downloadUrl.startsWith('https') ? https : http;
protocol.get(url, (response) => { const parsedUrl = url.parse(downloadUrl);
const options = protocol.RequestOptions = {
...parsedUrl,
headers: requestHeaders
};
protocol.get(options, (response) => {
const downloadSize = Number(response.headers['content-length']); const downloadSize = Number(response.headers['content-length']);
logger.info(`Downloading file ${url} to ${destination} with size: ${downloadSize} bytes`); logger.info(`Downloading file ${downloadUrl} to ${destination} with size: ${downloadSize} bytes`);
if (startCb) { if (startCb) {
if (!startCb(downloadSize)) { if (!startCb(downloadSize)) {
file.close(); file.close();
@ -61,9 +70,6 @@ export async function downloadFile(url: string, destination: string, startCb: (d
}); });
file.on('finish', () => { file.on('finish', () => {
file.close(); file.close();
if (finishCb) {
finishCb(downloadedBytes);
}
resolve(); resolve();
}); });
}).on('error', (err) => { }).on('error', (err) => {

View file

@ -24,7 +24,6 @@ class AppCache {
public appVersion: string = null; public appVersion: string = null;
public playMessage: PlayMessage = null; public playMessage: PlayMessage = null;
public playerVolume: number = null; public playerVolume: number = null;
public playlist: PlaylistContent = null;
public subscribedKeys = new Set<string>(); public subscribedKeys = new Set<string>();
} }
@ -181,7 +180,6 @@ export class Main {
case ContentType.Playlist: { case ContentType.Playlist: {
rendererMessage = json as PlaylistContent; rendererMessage = json as PlaylistContent;
rendererEvent = 'play-playlist'; rendererEvent = 'play-playlist';
Main.cache.playlist = rendererMessage;
if ((rendererMessage.forwardCache && rendererMessage.forwardCache > 0) || (rendererMessage.backwardCache && rendererMessage.backwardCache > 0)) { if ((rendererMessage.forwardCache && rendererMessage.forwardCache > 0) || (rendererMessage.backwardCache && rendererMessage.backwardCache > 0)) {
Main.mediaCache?.destroy(); Main.mediaCache?.destroy();
@ -322,23 +320,8 @@ export class Main {
ipcMain.on('play-request', (event: IpcMainEvent, value: PlayMessage, playlistIndex: number) => { ipcMain.on('play-request', (event: IpcMainEvent, value: PlayMessage, playlistIndex: number) => {
logger.debug(`Received play request for index ${playlistIndex}:`, value); logger.debug(`Received play request for index ${playlistIndex}:`, value);
value.url = Main.mediaCache.has(playlistIndex) ? Main.mediaCache.getUrl(playlistIndex) : value.url;
if (Main.cache.playlist.forwardCache && Main.cache.playlist.forwardCache > 0) { Main.mediaCache.cacheItems(playlistIndex);
if (Main.mediaCache.has(playlistIndex)) {
value.url = Main.mediaCache.getUrl(playlistIndex);
}
Main.mediaCache.cacheForwardItems(playlistIndex + 1, Main.cache.playlist.forwardCache, playlistIndex);
}
if (Main.cache.playlist.backwardCache && Main.cache.playlist.backwardCache > 0) {
if (Main.mediaCache.has(playlistIndex)) {
value.url = Main.mediaCache.getUrl(playlistIndex);
}
Main.mediaCache.cacheBackwardItems(playlistIndex - 1, Main.cache.playlist.backwardCache, playlistIndex);
}
Main.play(value); Main.play(value);
}); });
ipcMain.on('send-download-request', async () => { ipcMain.on('send-download-request', async () => {

View file

@ -365,7 +365,7 @@ export class Updater {
const destination = path.join(Updater.updateDataPath, file); const destination = path.join(Updater.updateDataPath, file);
logger.info(`Downloading '${fileInfo.url}' to '${destination}'.`); logger.info(`Downloading '${fileInfo.url}' to '${destination}'.`);
Updater.isDownloading = true; Updater.isDownloading = true;
await downloadFile(fileInfo.url.toString(), destination, null, (downloadedBytes: number, downloadSize: number) => { await downloadFile(fileInfo.url.toString(), destination, false, null, null, (downloadedBytes: number, downloadSize: number) => {
Updater.updateProgress = downloadedBytes / downloadSize; Updater.updateProgress = downloadedBytes / downloadSize;
}); });