2024-07-04 20:59:00 +07:00
|
|
|
import { EventEmitter, Readable, Stream } from "stream";
|
|
|
|
|
import { AudioInformation, Provider } from "../providers/base";
|
2024-06-21 00:30:21 +07:00
|
|
|
import { SeekableStream } from "../utils/SeekableStream";
|
|
|
|
|
|
|
|
|
|
export class Player {
|
|
|
|
|
private providers: Provider[];
|
|
|
|
|
private currentProvider: Provider | null = null;
|
2024-07-04 20:59:00 +07:00
|
|
|
private queue: AudioInformation[] = [];
|
|
|
|
|
private playerEvent: EventEmitter = new EventEmitter();
|
|
|
|
|
private paused: boolean = false;
|
|
|
|
|
private currentAudioInformation: AudioInformation | null = null;
|
2024-06-21 00:30:21 +07:00
|
|
|
|
|
|
|
|
public _stream: SeekableStream | null = null;
|
|
|
|
|
|
2024-07-04 20:59:00 +07:00
|
|
|
constructor(providers: Provider[]) {
|
|
|
|
|
this.providers = providers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get stream() {
|
2024-06-21 00:30:21 +07:00
|
|
|
return this._stream?.stream;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-04 20:59:00 +07:00
|
|
|
private _createStream(
|
|
|
|
|
information: AudioInformation,
|
|
|
|
|
url: string,
|
|
|
|
|
seekTime: number,
|
|
|
|
|
) {
|
|
|
|
|
// If already playing, destroy the current stream
|
|
|
|
|
if (this._stream) {
|
|
|
|
|
this._stream.destroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._stream = new SeekableStream(information, url, seekTime);
|
|
|
|
|
this.currentAudioInformation = information;
|
|
|
|
|
this._stream.on("destroy", () => {
|
|
|
|
|
console.log("Stream destroyed, total song", this.queue.length);
|
|
|
|
|
if (this.queue.length > 0) {
|
|
|
|
|
const next = this.queue.shift();
|
|
|
|
|
console.log("Playing next in queue");
|
|
|
|
|
if (next) {
|
|
|
|
|
this._createStream(next, next.url, 0);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this._stream = null;
|
|
|
|
|
this.currentAudioInformation = null;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.playerEvent.emit("play", information);
|
2024-06-21 00:30:21 +07:00
|
|
|
}
|
|
|
|
|
|
2024-07-05 10:50:22 +07:00
|
|
|
public startCurrentStream() {
|
|
|
|
|
if (this._stream) {
|
|
|
|
|
this._stream.start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-04 20:59:00 +07:00
|
|
|
public endCurrentStream() {
|
|
|
|
|
if (this._stream) {
|
|
|
|
|
this._stream.destroy();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public on(event: string, listener: (...args: any[]) => void) {
|
|
|
|
|
this.playerEvent.on(event, listener);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async getInformation(url: string) {
|
2024-06-21 00:30:21 +07:00
|
|
|
if (!this.currentProvider) {
|
|
|
|
|
const providers = this.providers.filter((provider) =>
|
|
|
|
|
provider.canPlay(url),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (providers.length === 0) {
|
|
|
|
|
throw new Error("No provider can play this URL");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.currentProvider = providers[0];
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-04 20:59:00 +07:00
|
|
|
return await this.currentProvider.getInformation(url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async play(url: string, seekTime: number = 0) {
|
|
|
|
|
const information = await this.getInformation(url);
|
2024-06-21 00:30:21 +07:00
|
|
|
//console.log(information);
|
|
|
|
|
|
|
|
|
|
if (information.livestream)
|
|
|
|
|
// TODO: Implement livestreams
|
|
|
|
|
throw new Error("Livestreams are not supported yet");
|
2024-06-21 12:17:32 +07:00
|
|
|
|
2024-07-04 20:59:00 +07:00
|
|
|
this._createStream(information, url, seekTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async enqueue(url: string, seekTime: number = 0) {
|
|
|
|
|
const information = await this.getInformation(url);
|
|
|
|
|
|
|
|
|
|
if (information.livestream)
|
|
|
|
|
// TODO: Implement livestreams
|
|
|
|
|
throw new Error("Livestreams are not supported yet");
|
|
|
|
|
|
|
|
|
|
this.playerEvent.emit("enqueue", information);
|
|
|
|
|
|
|
|
|
|
// If queue is empty, no stream is playing and not paused, play the current URL
|
|
|
|
|
if (
|
|
|
|
|
this.queue.length === 0 &&
|
|
|
|
|
!this.currentAudioInformation &&
|
|
|
|
|
!this._stream &&
|
|
|
|
|
!this.paused
|
|
|
|
|
) {
|
|
|
|
|
this._createStream(information, url, seekTime);
|
|
|
|
|
} else {
|
|
|
|
|
this.queue.push(information);
|
2024-06-21 12:17:32 +07:00
|
|
|
}
|
|
|
|
|
|
2024-08-17 16:04:30 +07:00
|
|
|
return information;
|
2024-06-21 12:17:32 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async seek(time: number) {
|
|
|
|
|
if (!this._stream) throw new Error("No stream to seek");
|
|
|
|
|
|
|
|
|
|
await this.play(this._stream.referenceUrl, time);
|
2024-06-21 00:30:21 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getCurrentSampleRate() {
|
|
|
|
|
return this._stream?.information.bitrate || 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createPlayer(providers: Provider[]) {
|
|
|
|
|
return new Player(providers);
|
|
|
|
|
}
|