refactor(Queue): rename createStream to onBeforeCreateStream
This commit is contained in:
parent
ddf279e0cb
commit
ef239229d4
4 changed files with 23 additions and 21 deletions
15
README.md
15
README.md
|
@ -202,19 +202,18 @@ Here's an example on how you can use **[play-dl](https://npmjs.com/package/play-
|
|||
const playdl = require("play-dl");
|
||||
|
||||
// other code
|
||||
|
||||
const queue = player.createQueue(...);
|
||||
if (!queue.createStream) {
|
||||
queue.createStream = async (track, source, _queue) => {
|
||||
const queue = player.createQueue(..., {
|
||||
...,
|
||||
async onBeforeCreateStream(track, source, _queue) {
|
||||
// only trap youtube source
|
||||
if (source === "youtube") {
|
||||
// track here would be youtube track
|
||||
return (await playdl.stream(track.url)).stream;
|
||||
// we must return readable stream or void (returning void means telling discord-player to look for default extractor)
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
`<Queue>.createStream` is called before actually downloading the stream. It is a different concept from extractors, where you are **just** downloading
|
||||
streams. `source` here will be a video source. Streams from `createStream` are then piped to `FFmpeg` and finally sent to Discord voice servers.
|
||||
`<Queue>.onBeforeCreateStream` is called before actually downloading the stream. It is a different concept from extractors, where you are **just** downloading
|
||||
streams. `source` here will be a video source. Streams from `onBeforeCreateStream` are then piped to `FFmpeg` and finally sent to Discord voice servers.
|
|
@ -12,28 +12,27 @@ Here's an example on how you can use **[play-dl](https://npmjs.com/package/play-
|
|||
const playdl = require("play-dl");
|
||||
|
||||
// other code
|
||||
|
||||
const queue = player.createQueue(...);
|
||||
if (!queue.createStream) {
|
||||
queue.createStream = async (track, source, _queue) => {
|
||||
const queue = player.createQueue(..., {
|
||||
...,
|
||||
async onBeforeCreateStream(track, source, _queue) {
|
||||
// only trap youtube source
|
||||
if (source === "youtube") {
|
||||
// track here would be youtube track
|
||||
return (await playdl.stream(track.url)).stream;
|
||||
// we must return readable stream or void (returning void means telling discord-player to look for default extractor)
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
`<Queue>.createStream` is called before actually downloading the stream. It is a different concept from extractors, where you are **just** downloading
|
||||
streams. `source` here will be a video source. Streams from `createStream` are then piped to `FFmpeg` and finally sent to Discord voice servers.
|
||||
`<Queue>.onBeforeCreateStream` is called before actually downloading the stream. It is a different concept from extractors, where you are **just** downloading
|
||||
streams. `source` here will be a video source. Streams from `onBeforeCreateStream` are then piped to `FFmpeg` and finally sent to Discord voice servers.
|
||||
|
||||
# FAQ
|
||||
## How can I remove this?
|
||||
|
||||
> If you already made this change and want to switch to default mode in runtime,
|
||||
> you can set `queue.createStream` to `null` which will make `discord-player` use default config.
|
||||
> you can set `queue.onBeforeCreateStream` to `null` which will make `discord-player` use default config.
|
||||
|
||||
## Which stream format should I return?
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ class Queue<T = unknown> {
|
|||
private _filtersUpdate = false;
|
||||
#lastVolume = 0;
|
||||
#destroyed = false;
|
||||
public createStream: (track: Track, source: TrackSource, queue: Queue) => Promise<Readable> | Readable = null;
|
||||
public onBeforeCreateStream: (track: Track, source: TrackSource, queue: Queue) => Promise<Readable> = null;
|
||||
|
||||
/**
|
||||
* Queue constructor
|
||||
|
@ -110,6 +110,8 @@ class Queue<T = unknown> {
|
|||
options
|
||||
);
|
||||
|
||||
if ("onBeforeCreateStream" in this.options) this.onBeforeCreateStream = this.options.onBeforeCreateStream;
|
||||
|
||||
this.player.emit("debug", this, `Queue initialized:\n\n${this.player.scanDeps()}`);
|
||||
}
|
||||
|
||||
|
@ -633,7 +635,7 @@ class Queue<T = unknown> {
|
|||
}
|
||||
|
||||
let stream = null;
|
||||
const customDownloader = typeof this.createStream === "function";
|
||||
const customDownloader = typeof this.onBeforeCreateStream === "function";
|
||||
|
||||
if (["youtube", "spotify"].includes(track.raw.source)) {
|
||||
if (track.raw.source === "spotify" && !track.raw.engine) {
|
||||
|
@ -645,7 +647,7 @@ class Queue<T = unknown> {
|
|||
if (!link) return void this.play(this.tracks.shift(), { immediate: true });
|
||||
|
||||
if (customDownloader) {
|
||||
stream = (await this.createStream(track, "youtube", this)) ?? null;
|
||||
stream = (await this.onBeforeCreateStream(track, "youtube", this)) ?? null;
|
||||
if (stream)
|
||||
stream = ytdl
|
||||
.arbitraryStream(stream, {
|
||||
|
@ -670,7 +672,7 @@ class Queue<T = unknown> {
|
|||
});
|
||||
}
|
||||
} else {
|
||||
const tryArb = (customDownloader && (await this.createStream(track, track.raw.source || track.raw.engine, this))) || null;
|
||||
const tryArb = (customDownloader && (await this.onBeforeCreateStream(track, track.raw.source || track.raw.engine, this))) || null;
|
||||
const arbitrarySource = tryArb
|
||||
? tryArb
|
||||
: track.raw.source === "soundcloud"
|
||||
|
|
|
@ -128,6 +128,7 @@ export interface PlayerProgressbarOptions {
|
|||
* @property {YTDLDownloadOptions} [ytdlOptions={}] The youtube download options
|
||||
* @property {number} [initialVolume=100] The initial player volume
|
||||
* @property {number} [bufferingTimeout=3000] Buffering timeout for the stream
|
||||
* @property {Function} [onBeforeCreateStream] Runs before creating stream
|
||||
*/
|
||||
export interface PlayerOptions {
|
||||
leaveOnEnd?: boolean;
|
||||
|
@ -138,6 +139,7 @@ export interface PlayerOptions {
|
|||
ytdlOptions?: downloadOptions;
|
||||
initialVolume?: number;
|
||||
bufferingTimeout?: number;
|
||||
onBeforeCreateStream?: (track: Track, source: TrackSource, queue: Queue) => Promise<Readable>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue