muse/src/services/player.ts

450 lines
12 KiB
TypeScript
Raw Normal View History

2021-11-12 20:30:18 +01:00
import {VoiceChannel, Snowflake, Client, TextChannel} from 'discord.js';
import {Readable} from 'stream';
2020-03-14 02:36:42 +01:00
import hasha from 'hasha';
import ytdl from 'ytdl-core';
import {WriteStream} from 'fs-capacitor';
2020-03-15 03:48:08 +01:00
import ffmpeg from 'fluent-ffmpeg';
2020-03-21 02:47:04 +01:00
import shuffle from 'array-shuffle';
import errorMsg from '../utils/error-msg.js';
2021-11-12 20:30:18 +01:00
import {AudioPlayer, AudioPlayerStatus, createAudioPlayer, createAudioResource, joinVoiceChannel, StreamType, VoiceConnection, VoiceConnectionStatus} from '@discordjs/voice';
import FileCacheProvider from './file-cache.js';
2020-03-21 02:47:04 +01:00
export interface QueuedPlaylist {
title: string;
source: string;
}
export interface QueuedSong {
title: string;
artist: string;
url: string;
length: number;
playlist: QueuedPlaylist | null;
isLive: boolean;
addedInChannelId: Snowflake;
thumbnailUrl: string | null;
requestedBy: string;
2020-03-21 02:47:04 +01:00
}
2020-03-13 04:41:26 +01:00
export enum STATUS {
PLAYING,
2021-09-20 04:24:46 +02:00
PAUSED,
2020-03-13 04:41:26 +01:00
}
export default class {
2020-03-17 23:59:26 +01:00
public status = STATUS.PAUSED;
2020-03-17 02:14:15 +01:00
public voiceConnection: VoiceConnection | null = null;
2020-03-21 02:47:04 +01:00
private queue: QueuedSong[] = [];
private queuePosition = 0;
2021-11-12 20:30:18 +01:00
private audioPlayer: AudioPlayer | null = null;
2020-03-18 01:42:28 +01:00
private nowPlaying: QueuedSong | null = null;
2020-03-16 01:30:07 +01:00
private playPositionInterval: NodeJS.Timeout | undefined;
2020-03-19 04:29:43 +01:00
private lastSongURL = '';
2020-03-13 04:41:26 +01:00
2020-03-15 21:35:34 +01:00
private positionInSeconds = 0;
private readonly discordClient: Client;
private readonly fileCache: FileCacheProvider;
constructor(client: Client, fileCache: FileCacheProvider) {
this.discordClient = client;
this.fileCache = fileCache;
2020-03-13 04:41:26 +01:00
}
async connect(channel: VoiceChannel): Promise<void> {
2021-11-12 20:30:18 +01:00
const conn = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
2022-01-07 19:38:07 +01:00
// @ts-expect-error (see https://github.com/discordjs/voice/issues/166)
2021-11-12 20:30:18 +01:00
adapterCreator: channel.guild.voiceAdapterCreator,
});
2020-03-13 04:41:26 +01:00
this.voiceConnection = conn;
2020-03-13 04:41:26 +01:00
}
2021-11-12 20:30:18 +01:00
disconnect(): void {
if (this.voiceConnection) {
2020-03-16 01:30:07 +01:00
if (this.status === STATUS.PLAYING) {
this.pause();
}
2021-11-12 20:30:18 +01:00
this.voiceConnection.destroy();
this.audioPlayer?.stop();
2020-03-17 23:59:26 +01:00
this.voiceConnection = null;
2021-11-12 20:30:18 +01:00
this.audioPlayer = null;
2020-03-13 04:41:26 +01:00
}
}
async seek(positionSeconds: number): Promise<void> {
2020-03-17 23:59:26 +01:00
this.status = STATUS.PAUSED;
if (this.voiceConnection === null) {
2020-03-14 02:36:42 +01:00
throw new Error('Not connected to a voice channel.');
}
2020-03-21 02:47:04 +01:00
const currentSong = this.getCurrent();
2020-03-14 02:36:42 +01:00
if (!currentSong) {
throw new Error('No song currently playing');
}
2020-03-18 18:40:31 +01:00
if (positionSeconds > currentSong.length) {
throw new Error('Seek position is outside the range of the song.');
}
2020-03-18 23:15:45 +01:00
const stream = await this.getStream(currentSong.url, {seek: positionSeconds});
2021-11-12 20:30:18 +01:00
this.audioPlayer = createAudioPlayer();
this.voiceConnection.subscribe(this.audioPlayer);
this.audioPlayer.play(createAudioResource(stream, {
inputType: StreamType.WebmOpus,
}));
2020-03-16 01:30:07 +01:00
this.attachListeners();
this.startTrackingPosition(positionSeconds);
2020-03-15 21:35:34 +01:00
2020-03-16 01:30:07 +01:00
this.status = STATUS.PLAYING;
2020-03-15 21:35:34 +01:00
}
async forwardSeek(positionSeconds: number): Promise<void> {
return this.seek(this.positionInSeconds + positionSeconds);
}
getPosition(): number {
return this.positionInSeconds;
2020-03-14 02:36:42 +01:00
}
async play(): Promise<void> {
if (this.voiceConnection === null) {
2020-03-13 04:41:26 +01:00
throw new Error('Not connected to a voice channel.');
}
2020-03-21 02:47:04 +01:00
const currentSong = this.getCurrent();
2020-03-17 23:59:26 +01:00
if (!currentSong) {
throw new Error('Queue empty.');
}
// Resume from paused state
2020-03-21 02:47:04 +01:00
if (this.status === STATUS.PAUSED && currentSong.url === this.nowPlaying?.url) {
2021-11-12 20:30:18 +01:00
if (this.audioPlayer) {
this.audioPlayer.unpause();
2020-03-19 00:29:32 +01:00
this.status = STATUS.PLAYING;
2020-03-19 04:29:43 +01:00
this.startTrackingPosition();
2020-03-19 00:29:32 +01:00
return;
}
// Was disconnected, need to recreate stream
2020-03-24 01:40:54 +01:00
if (!currentSong.isLive) {
return this.seek(this.getPosition());
}
2020-03-13 04:41:26 +01:00
}
2020-03-21 02:47:04 +01:00
try {
const stream = await this.getStream(currentSong.url);
2021-11-12 20:30:18 +01:00
this.audioPlayer = createAudioPlayer();
this.voiceConnection.subscribe(this.audioPlayer);
this.audioPlayer.play(createAudioResource(stream, {
inputType: StreamType.WebmOpus,
}));
2020-03-13 04:41:26 +01:00
2020-03-21 02:47:04 +01:00
this.attachListeners();
2020-03-13 04:41:26 +01:00
2020-03-21 02:47:04 +01:00
this.status = STATUS.PLAYING;
this.nowPlaying = currentSong;
2020-03-16 01:30:07 +01:00
2020-03-21 02:47:04 +01:00
if (currentSong.url === this.lastSongURL) {
this.startTrackingPosition();
} else {
// Reset position counter
this.startTrackingPosition(0);
this.lastSongURL = currentSong.url;
}
2020-10-24 18:32:43 +02:00
} catch (error: unknown) {
const currentSong = this.getCurrent();
await this.forward(1);
if ((error as {statusCode: number}).statusCode === 410 && currentSong) {
const channelId = currentSong.addedInChannelId;
if (channelId) {
await (this.discordClient.channels.cache.get(channelId) as TextChannel).send(errorMsg(`${currentSong.title} is unavailable`));
return;
}
}
2020-03-21 02:47:04 +01:00
throw error;
2020-03-19 04:29:43 +01:00
}
2020-03-13 04:41:26 +01:00
}
pause(): void {
2020-03-16 01:30:07 +01:00
if (this.status !== STATUS.PLAYING) {
throw new Error('Not currently playing.');
}
2020-03-13 04:41:26 +01:00
2020-03-16 01:30:07 +01:00
this.status = STATUS.PAUSED;
2021-11-12 20:30:18 +01:00
if (this.audioPlayer) {
this.audioPlayer.pause();
2020-03-16 01:30:07 +01:00
}
this.stopTrackingPosition();
2020-03-13 04:41:26 +01:00
}
2021-04-23 18:30:31 +02:00
async forward(skip: number): Promise<void> {
this.manualForward(skip);
2020-03-21 02:47:04 +01:00
2020-03-24 01:40:54 +01:00
try {
if (this.getCurrent() && this.status !== STATUS.PAUSED) {
await this.play();
} else {
this.status = STATUS.PAUSED;
this.disconnect();
2020-03-21 02:47:04 +01:00
}
2020-10-24 18:32:43 +02:00
} catch (error: unknown) {
2020-03-24 01:40:54 +01:00
this.queuePosition--;
throw error;
}
}
2021-04-23 18:30:31 +02:00
manualForward(skip: number): void {
if ((this.queuePosition + skip - 1) < this.queue.length) {
this.queuePosition += skip;
this.positionInSeconds = 0;
this.stopTrackingPosition();
2020-03-21 02:47:04 +01:00
} else {
throw new Error('No songs in queue to forward to.');
}
}
async back(): Promise<void> {
if (this.queuePosition - 1 >= 0) {
this.queuePosition--;
this.positionInSeconds = 0;
this.stopTrackingPosition();
2020-03-21 02:47:04 +01:00
if (this.status !== STATUS.PAUSED) {
await this.play();
}
} else {
throw new Error('No songs in queue to go back to.');
}
}
getCurrent(): QueuedSong | null {
if (this.queue[this.queuePosition]) {
return this.queue[this.queuePosition];
}
return null;
}
2021-12-12 19:20:36 +01:00
/**
* Returns queue, not including the current song.
* @returns {QueuedSong[]}
*/
2020-03-21 02:47:04 +01:00
getQueue(): QueuedSong[] {
return this.queue.slice(this.queuePosition + 1);
}
add(song: QueuedSong, {immediate = false} = {}): void {
2021-11-18 00:17:08 +01:00
if (song.playlist || !immediate) {
2020-03-21 02:47:04 +01:00
// Add to end of queue
this.queue.push(song);
} else {
2021-11-18 00:17:08 +01:00
// Add as the next song to be played
const insertAt = this.queuePosition + 1;
2020-03-21 02:47:04 +01:00
this.queue = [...this.queue.slice(0, insertAt), song, ...this.queue.slice(insertAt)];
}
}
shuffle(): void {
2020-03-25 23:59:09 +01:00
const shuffledSongs = shuffle(this.queue.slice(this.queuePosition + 1));
this.queue = [...this.queue.slice(0, this.queuePosition + 1), ...shuffledSongs];
2020-03-21 02:47:04 +01:00
}
clear(): void {
const newQueue = [];
// Don't clear curently playing song
const current = this.getCurrent();
if (current) {
newQueue.push(current);
}
this.queuePosition = 0;
this.queue = newQueue;
}
removeFromQueue(index: number, amount = 1): void {
this.queue.splice(this.queuePosition + index, amount);
}
2020-03-21 02:47:04 +01:00
removeCurrent(): void {
this.queue = [...this.queue.slice(0, this.queuePosition), ...this.queue.slice(this.queuePosition + 1)];
}
queueSize(): number {
return this.getQueue().length;
}
isQueueEmpty(): boolean {
return this.queueSize() === 0;
2020-03-20 01:16:07 +01:00
}
private getHashForCache(url: string): string {
return hasha(url);
2020-03-14 02:36:42 +01:00
}
2020-03-18 23:15:45 +01:00
private async getStream(url: string, options: {seek?: number} = {}): Promise<Readable> {
let ffmpegInput = '';
const ffmpegInputOptions: string[] = [];
2020-03-18 23:15:45 +01:00
let shouldCacheVideo = false;
2020-03-28 00:28:50 +01:00
let format: ytdl.videoFormat | undefined;
try {
ffmpegInput = await this.fileCache.getPathFor(this.getHashForCache(url));
2020-03-28 00:28:50 +01:00
if (options.seek) {
ffmpegInputOptions.push('-ss', options.seek.toString());
}
} catch {
2020-03-18 23:15:45 +01:00
// Not yet cached, must download
const info = await ytdl.getInfo(url);
2020-03-14 02:36:42 +01:00
2020-03-18 23:15:45 +01:00
const {formats} = info;
2020-03-14 02:36:42 +01:00
2020-03-18 23:15:45 +01:00
const filter = (format: ytdl.videoFormat): boolean => format.codecs === 'opus' && format.container === 'webm' && format.audioSampleRate !== undefined && parseInt(format.audioSampleRate, 10) === 48000;
2020-03-14 02:36:42 +01:00
2020-03-28 00:28:50 +01:00
format = formats.find(filter);
2020-03-14 02:36:42 +01:00
2020-03-18 23:15:45 +01:00
const nextBestFormat = (formats: ytdl.videoFormat[]): ytdl.videoFormat | undefined => {
2020-10-24 18:32:43 +02:00
if (formats[0].isLive) {
2020-03-25 23:59:09 +01:00
formats = formats.sort((a, b) => (b as unknown as {audioBitrate: number}).audioBitrate - (a as unknown as {audioBitrate: number}).audioBitrate); // Bad typings
2020-03-14 02:36:42 +01:00
2020-03-18 23:15:45 +01:00
return formats.find(format => [128, 127, 120, 96, 95, 94, 93].includes(parseInt(format.itag as unknown as string, 10))); // Bad typings
}
2020-03-15 03:48:08 +01:00
2020-03-18 23:15:45 +01:00
formats = formats
.filter(format => format.averageBitrate)
2020-10-24 18:32:43 +02:00
.sort((a, b) => {
if (a && b) {
return b.averageBitrate! - a.averageBitrate!;
}
return 0;
});
2020-03-18 23:15:45 +01:00
return formats.find(format => !format.bitrate) ?? formats[0];
};
if (!format) {
format = nextBestFormat(info.formats);
if (!format) {
// If still no format is found, throw
throw new Error('Can\'t find suitable format.');
}
2020-03-15 03:48:08 +01:00
}
2020-03-18 23:15:45 +01:00
ffmpegInput = format.url;
2020-03-14 02:36:42 +01:00
2020-03-18 23:15:45 +01:00
// Don't cache livestreams or long videos
const MAX_CACHE_LENGTH_SECONDS = 30 * 60; // 30 minutes
2020-08-24 21:53:18 +02:00
shouldCacheVideo = !info.player_response.videoDetails.isLiveContent && parseInt(info.videoDetails.lengthSeconds, 10) < MAX_CACHE_LENGTH_SECONDS && !options.seek;
2020-03-15 03:48:08 +01:00
2020-03-18 23:15:45 +01:00
ffmpegInputOptions.push(...[
'-reconnect',
'1',
'-reconnect_streamed',
'1',
'-reconnect_delay_max',
2021-09-20 04:24:46 +02:00
'5',
2020-03-18 23:15:45 +01:00
]);
2020-03-14 02:36:42 +01:00
2020-03-28 00:28:50 +01:00
if (options.seek) {
// Fudge seek position since FFMPEG doesn't do a great job
ffmpegInputOptions.push('-ss', (options.seek + 7).toString());
}
2020-03-14 02:36:42 +01:00
}
2020-03-18 23:15:45 +01:00
// Create stream and pipe to capacitor
return new Promise((resolve, reject) => {
const capacitor = new WriteStream();
// Cache video if necessary
if (shouldCacheVideo) {
const cacheStream = this.fileCache.createWriteStream(this.getHashForCache(url));
capacitor.createReadStream().pipe(cacheStream);
} else {
ffmpegInputOptions.push('-re');
}
const youtubeStream = ffmpeg(ffmpegInput)
.inputOptions(ffmpegInputOptions)
.noVideo()
.audioCodec('libopus')
.outputFormat('webm')
.on('error', error => {
console.error(error);
reject(error);
});
youtubeStream.pipe(capacitor);
resolve(capacitor.createReadStream());
});
2020-03-14 02:36:42 +01:00
}
2020-03-16 01:30:07 +01:00
private startTrackingPosition(initalPosition?: number): void {
2020-03-19 04:29:43 +01:00
if (initalPosition !== undefined) {
2020-03-16 01:30:07 +01:00
this.positionInSeconds = initalPosition;
}
2020-03-15 21:35:34 +01:00
2020-03-16 01:30:07 +01:00
if (this.playPositionInterval) {
clearInterval(this.playPositionInterval);
}
this.playPositionInterval = setInterval(() => {
this.positionInSeconds++;
}, 1000);
}
private stopTrackingPosition(): void {
if (this.playPositionInterval) {
clearInterval(this.playPositionInterval);
}
}
private attachListeners(): void {
if (!this.voiceConnection) {
return;
}
if (this.voiceConnection.listeners(VoiceConnectionStatus.Disconnected).length === 0) {
this.voiceConnection.on(VoiceConnectionStatus.Disconnected, this.onVoiceConnectionDisconnect.bind(this));
2021-10-03 18:59:26 +02:00
}
2020-03-16 01:30:07 +01:00
2021-11-12 20:30:18 +01:00
if (!this.audioPlayer) {
2020-03-16 01:30:07 +01:00
return;
}
if (this.audioPlayer.listeners('stateChange').length === 0) {
this.audioPlayer.on('stateChange', this.onAudioPlayerStateChange.bind(this));
2021-10-03 18:59:26 +02:00
}
2020-03-21 02:47:04 +01:00
}
2020-03-19 04:29:43 +01:00
2020-03-21 02:47:04 +01:00
private onVoiceConnectionDisconnect(): void {
2021-11-12 20:30:18 +01:00
this.disconnect();
2020-03-21 02:47:04 +01:00
}
2020-03-19 04:29:43 +01:00
2021-11-12 20:30:18 +01:00
private async onAudioPlayerStateChange(_oldState: {status: AudioPlayerStatus}, newState: {status: AudioPlayerStatus}): Promise<void> {
2020-03-21 02:47:04 +01:00
// Automatically advance queued song at end
2021-11-12 20:30:18 +01:00
if (newState.status === AudioPlayerStatus.Idle && this.status === STATUS.PLAYING) {
2021-04-23 18:30:31 +02:00
await this.forward(1);
2020-03-21 02:47:04 +01:00
}
}
2020-03-13 04:41:26 +01:00
}