improve overrides

This commit is contained in:
SrGooglo 2025-02-05 20:28:13 +00:00
parent 3b04ef6044
commit dde659ef01
2 changed files with 165 additions and 149 deletions

View File

@ -2,188 +2,197 @@ import TrackManifest from "./TrackManifest"
import { MediaPlayer } from "dashjs"
export default class TrackInstance {
constructor(player, manifest) {
if (!player) {
throw new Error("Player core is required")
}
constructor(player, manifest) {
if (!player) {
throw new Error("Player core is required")
}
if (typeof manifest === "undefined") {
throw new Error("Manifest is required")
}
if (typeof manifest === "undefined") {
throw new Error("Manifest is required")
}
this.player = player
this.manifest = manifest
this.player = player
this.manifest = manifest
this.id = this.manifest.id ?? this.manifest._id
this.id = this.manifest.id ?? this.manifest._id
return this
}
return this
}
_initialized = false
_initialized = false
audio = null
audio = null
contextElement = null
contextElement = null
abortController = new AbortController()
abortController = new AbortController()
attachedProcessors = []
attachedProcessors = []
waitUpdateTimeout = null
waitUpdateTimeout = null
mediaEvents = {
"ended": () => {
this.player.next()
},
"loadeddata": () => {
this.player.state.loading = false
},
"loadedmetadata": () => {
// TODO: Detect a livestream and change mode
// if (instance.media.duration === Infinity) {
// instance.manifest.stream = true
mediaEvents = {
ended: () => {
this.player.next()
},
loadeddata: () => {
this.player.state.loading = false
},
loadedmetadata: () => {
// TODO: Detect a livestream and change mode
// if (instance.media.duration === Infinity) {
// instance.manifest.stream = true
// this.state.livestream_mode = true
// }
},
play: () => {
this.player.state.playback_status = "playing"
},
playing: () => {
this.player.state.loading = false
// this.state.livestream_mode = true
// }
},
"play": () => {
this.player.state.playback_status = "playing"
},
"playing": () => {
this.player.state.loading = false
this.player.state.playback_status = "playing"
this.player.state.playback_status = "playing"
if (typeof this.waitUpdateTimeout !== "undefined") {
clearTimeout(this.waitUpdateTimeout)
this.waitUpdateTimeout = null
}
},
pause: () => {
this.player.state.playback_status = "paused"
},
durationchange: () => {
this.player.eventBus.emit(
`player.durationchange`,
this.audio.duration,
)
},
waiting: () => {
if (this.waitUpdateTimeout) {
clearTimeout(this.waitUpdateTimeout)
this.waitUpdateTimeout = null
}
if (typeof this.waitUpdateTimeout !== "undefined") {
clearTimeout(this.waitUpdateTimeout)
this.waitUpdateTimeout = null
}
},
"pause": () => {
this.player.state.playback_status = "paused"
},
"durationchange": () => {
this.player.eventBus.emit(`player.durationchange`, this.audio.duration)
},
"waiting": () => {
if (this.waitUpdateTimeout) {
clearTimeout(this.waitUpdateTimeout)
this.waitUpdateTimeout = null
}
// if takes more than 150ms to load, update loading state
this.waitUpdateTimeout = setTimeout(() => {
this.player.state.loading = true
}, 150)
},
seeked: () => {
this.player.eventBus.emit(`player.seeked`, this.audio.currentTime)
},
}
// if takes more than 150ms to load, update loading state
this.waitUpdateTimeout = setTimeout(() => {
this.player.state.loading = true
}, 150)
},
"seeked": () => {
this.player.eventBus.emit(`player.seeked`, this.audio.currentTime)
},
}
initialize = async () => {
this.manifest = await this.resolveManifest()
initialize = async () => {
this.manifest = await this.resolveManifest()
this.audio = new Audio()
this.audio = new Audio()
this.audio.signal = this.abortController.signal
this.audio.crossOrigin = "anonymous"
this.audio.preload = "metadata"
this.audio.signal = this.abortController.signal
this.audio.crossOrigin = "anonymous"
this.audio.preload = "metadata"
// support for dash audio streaming
if (this.manifest.source.endsWith(".mpd")) {
this.muxerPlayer = MediaPlayer().create()
this.muxerPlayer.initialize(this.audio, null, false)
// support for dash audio streaming
if (this.manifest.source.endsWith(".mpd")) {
this.muxerPlayer = MediaPlayer().create()
this.muxerPlayer.initialize(this.audio, null, false)
this.muxerPlayer.attachSource(this.manifest.source)
} else {
this.audio.src = this.manifest.source
}
this.muxerPlayer.attachSource(this.manifest.source)
} else {
this.audio.src = this.manifest.source
}
for (const [key, value] of Object.entries(this.mediaEvents)) {
this.audio.addEventListener(key, value)
}
for (const [key, value] of Object.entries(this.mediaEvents)) {
this.audio.addEventListener(key, value)
}
this.contextElement = this.player.audioContext.createMediaElementSource(
this.audio,
)
this.contextElement = this.player.audioContext.createMediaElementSource(this.audio)
this._initialized = true
this._initialized = true
return this
}
return this
}
stop = () => {
if (this.audio) {
this.audio.pause()
}
stop = () => {
if (this.audio) {
this.audio.pause()
}
if (this.muxerPlayer) {
this.muxerPlayer.destroy()
}
if (this.muxerPlayer) {
this.muxerPlayer.destroy()
}
const lastProcessor =
this.attachedProcessors[this.attachedProcessors.length - 1]
const lastProcessor = this.attachedProcessors[this.attachedProcessors.length - 1]
if (lastProcessor) {
this.attachedProcessors[
this.attachedProcessors.length - 1
]._destroy(this)
}
if (lastProcessor) {
this.attachedProcessors[this.attachedProcessors.length - 1]._destroy(this)
}
this.attachedProcessors = []
}
this.attachedProcessors = []
}
resolveManifest = async () => {
if (typeof this.manifest === "string") {
this.manifest = {
src: this.manifest,
}
}
resolveManifest = async () => {
if (typeof this.manifest === "string") {
this.manifest = {
src: this.manifest,
}
}
this.manifest = new TrackManifest(this.manifest, {
serviceProviders: this.player.serviceProviders,
})
this.manifest = new TrackManifest(this.manifest, {
serviceProviders: this.player.serviceProviders,
})
if (this.manifest.service) {
if (!this.player.serviceProviders.has(this.manifest.service)) {
throw new Error(
`Service ${this.manifest.service} is not supported`,
)
}
if (this.manifest.service) {
if (!this.player.serviceProviders.has(this.manifest.service)) {
throw new Error(`Service ${this.manifest.service} is not supported`)
}
// try to resolve source file
if (!this.manifest.source) {
console.log("Resolving manifest cause no source defined")
// try to resolve source file
if (!this.manifest.source) {
console.log("Resolving manifest cause no source defined")
this.manifest = await this.player.serviceProviders.resolve(
this.manifest.service,
this.manifest,
)
this.manifest = await this.player.serviceProviders.resolve(this.manifest.service, this.manifest)
console.log("Manifest resolved", this.manifest)
}
}
console.log("Manifest resolved", this.manifest)
}
}
if (!this.manifest.source) {
throw new Error("Manifest `source` is required")
}
if (!this.manifest.source) {
throw new Error("Manifest `source` is required")
}
// set empty metadata if not provided
if (!this.manifest.metadata) {
this.manifest.metadata = {}
}
// set empty metadata if not provided
if (!this.manifest.metadata) {
this.manifest.metadata = {}
}
// auto name if a title is not provided
if (!this.manifest.metadata.title) {
this.manifest.metadata.title = this.manifest.source.split("/").pop()
}
// auto name if a title is not provided
if (!this.manifest.metadata.title) {
this.manifest.metadata.title = this.manifest.source.split("/").pop()
}
// process overrides
const override = await this.manifest.serviceOperations.fetchOverride()
// check if has overrides
const override = await this.manifest.serviceOperations.fetchOverride()
if (override) {
console.log(
`Override found for track ${this.manifest._id}`,
override,
)
if (override) {
console.log(`Override found for track ${this.manifest._id}`, override)
this.manifest.overrides = override
}
this.manifest.overrides = override
}
// FIXME: idk why this is here, move somewhere else
// try {
// this.manifest = await this.manifest.analyzeCoverColor()
// } catch (error) {
// //x
// }
return this.manifest
}
}
return this.manifest
}
}

View File

@ -73,10 +73,6 @@ export default class TrackManifest {
lyrics_enabled = false
liked = null
// TODO: implement this server feature to fetch some data from the server,
// used for example to fix a incorrect lyrics time
overrides = null
async initialize() {
if (this.params.file) {
this.metadata = await this.analyzeMetadata(
@ -167,11 +163,22 @@ export default class TrackManifest {
return null
}
return await this.ctx.serviceProviders.operation(
const result = await this.ctx.serviceProviders.operation(
"resolveLyrics",
this.service,
this,
)
console.log(this.overrides)
if (this.overrides) {
return {
...result,
...this.overrides,
}
}
return result
},
fetchOverride: async () => {
if (!this._id) {