mirror of
https://github.com/Andre-Bzk/twitch4steamdeck.git
synced 2026-11-15 15:43:08 +01:00
Fix: Seeking optimiert?
This commit is contained in:
@@ -15,6 +15,7 @@ export class MpvController {
|
|||||||
private nextRequestId = 1
|
private nextRequestId = 1
|
||||||
private pendingRequests = new Map<number, (value: unknown) => void>()
|
private pendingRequests = new Map<number, (value: unknown) => void>()
|
||||||
private propertyObservers = new Map<string, Set<(value: unknown) => void>>()
|
private propertyObservers = new Map<string, Set<(value: unknown) => void>>()
|
||||||
|
private eventObservers = new Map<string, Set<() => void>>()
|
||||||
|
|
||||||
constructor(private ipcPath: string) {}
|
constructor(private ipcPath: string) {}
|
||||||
|
|
||||||
@@ -61,6 +62,15 @@ export class MpvController {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onEvent(eventName: string, cb: () => void): void {
|
||||||
|
let observers = this.eventObservers.get(eventName)
|
||||||
|
if (!observers) {
|
||||||
|
observers = new Set()
|
||||||
|
this.eventObservers.set(eventName, observers)
|
||||||
|
}
|
||||||
|
observers.add(cb)
|
||||||
|
}
|
||||||
|
|
||||||
seek(seconds: number): void {
|
seek(seconds: number): void {
|
||||||
if (this.socket?.writable) {
|
if (this.socket?.writable) {
|
||||||
this._send({ command: ['seek', seconds, 'relative'] })
|
this._send({ command: ['seek', seconds, 'relative'] })
|
||||||
@@ -107,6 +117,7 @@ export class MpvController {
|
|||||||
this.buf = ''
|
this.buf = ''
|
||||||
this.pendingRequests.clear()
|
this.pendingRequests.clear()
|
||||||
this.propertyObservers.clear()
|
this.propertyObservers.clear()
|
||||||
|
this.eventObservers.clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
private _send(msg: object): void {
|
private _send(msg: object): void {
|
||||||
@@ -152,6 +163,13 @@ export class MpvController {
|
|||||||
for (const observer of observers) observer(msg.data)
|
for (const observer of observers) observer(msg.data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof msg.event === 'string') {
|
||||||
|
const observers = this.eventObservers.get(msg.event)
|
||||||
|
if (observers) {
|
||||||
|
for (const observer of observers) observer()
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
/* ignore malformed lines */
|
/* ignore malformed lines */
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ export class PlaybackService extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const effectiveStart = startSeconds !== undefined ? startSeconds : resumePos
|
const effectiveStart = startSeconds !== undefined ? startSeconds : resumePos
|
||||||
const proc = spawnMpv(hlsUrl, { ipcPath: this.ipcPath, startSeconds: effectiveStart })
|
const proc = spawnMpv(hlsUrl, { ipcPath: this.ipcPath })
|
||||||
|
|
||||||
let mpvStderrBuf = ''
|
let mpvStderrBuf = ''
|
||||||
proc.stdout?.on('data', (d: Buffer) => console.log('[mpv]', d.toString().trim()))
|
proc.stdout?.on('data', (d: Buffer) => console.log('[mpv]', d.toString().trim()))
|
||||||
@@ -125,8 +125,17 @@ export class PlaybackService extends EventEmitter {
|
|||||||
this.current = { process: proc, mpv }
|
this.current = { process: proc, mpv }
|
||||||
this.emit('playback-event', { kind: 'started' } satisfies PlaybackEvent)
|
this.emit('playback-event', { kind: 'started' } satisfies PlaybackEvent)
|
||||||
|
|
||||||
// IPC verbinden und Position tracken
|
// IPC verbinden, initialen Resume-Seek erst nach `file-loaded` setzen und Position tracken.
|
||||||
|
// HLS-VODs reagieren auf Linux/Steam Deck robuster auf einen Seek nach dem Laden
|
||||||
|
// als auf `mpv --start=<sek>`.
|
||||||
mpv.connect().then(() => {
|
mpv.connect().then(() => {
|
||||||
|
let initialSeekDone = effectiveStart <= 0
|
||||||
|
mpv.onEvent('file-loaded', () => {
|
||||||
|
if (initialSeekDone) return
|
||||||
|
initialSeekDone = true
|
||||||
|
mpv.seekAbsolute(effectiveStart)
|
||||||
|
})
|
||||||
|
|
||||||
let lastWrite = 0
|
let lastWrite = 0
|
||||||
mpv.observeTimePos((seconds) => {
|
mpv.observeTimePos((seconds) => {
|
||||||
const now = Date.now()
|
const now = Date.now()
|
||||||
|
|||||||
@@ -70,11 +70,10 @@ export function getStreamUrl(url: string, quality: string): Promise<string> {
|
|||||||
|
|
||||||
export interface MpvOptions {
|
export interface MpvOptions {
|
||||||
ipcPath: string
|
ipcPath: string
|
||||||
startSeconds?: number
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Startet mpv direkt mit einer URL (HLS oder lokal). IPC-Socket wird gesetzt. */
|
/** Startet mpv direkt mit einer URL (HLS oder lokal). IPC-Socket wird gesetzt. */
|
||||||
export function spawnMpv(url: string, { ipcPath, startSeconds }: MpvOptions): ChildProcess {
|
export function spawnMpv(url: string, { ipcPath }: MpvOptions): ChildProcess {
|
||||||
const args = [
|
const args = [
|
||||||
url,
|
url,
|
||||||
`--input-ipc-server=${ipcPath}`,
|
`--input-ipc-server=${ipcPath}`,
|
||||||
@@ -83,8 +82,5 @@ export function spawnMpv(url: string, { ipcPath, startSeconds }: MpvOptions): Ch
|
|||||||
'--hwdec=auto',
|
'--hwdec=auto',
|
||||||
'--force-window=yes'
|
'--force-window=yes'
|
||||||
]
|
]
|
||||||
if (startSeconds && startSeconds > 0) {
|
|
||||||
args.push(`--start=${startSeconds}`)
|
|
||||||
}
|
|
||||||
return spawn(MPV_BIN, args, { stdio: ['ignore', 'pipe', 'pipe'] })
|
return spawn(MPV_BIN, args, { stdio: ['ignore', 'pipe', 'pipe'] })
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user