Stream Improvements
This commit is contained in:
Killer069 2021-09-21 22:06:00 +05:30 committed by GitHub
commit d06abe12c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 40 additions and 37 deletions

View File

@ -51,7 +51,7 @@ _This is basic to create a stream from a youtube or soundcloud url._
```js
let source = await stream("url") // This will create a stream Class.
let resource = createAudioResource(source, {
let resource = createAudioResource(source.stream, {
inputType : source.type
}) // This creates resource for playing
```
@ -66,12 +66,12 @@ _This is basic to create a stream from a info [ from [video_info](https://github
```js
let source = await stream_from_info(info) // This will create a stream Class from video_info or SoundCoudTrack Class.
/* OR
let source = await stream_from_info(info, cookie) This will create a stream Class and also give cookies if retrying.
*/
let resource = createAudioResource(source, {
let resource = createAudioResource(source.stream, {
inputType : source.type
}) // This creates resource for playing
```

View File

@ -25,7 +25,7 @@ client.on('messageCreate', async message => {
let stream = await play.stream_from_info(so_info)
*/
let resource = createAudioResource(stream, {
let resource = createAudioResource(stream.stream, {
inputType : stream.type
})
let player = createAudioPlayer({

View File

@ -22,7 +22,7 @@ client.on('messageCreate', async message => {
let searched = await play.search(`${sp_data.name}`, { limit : 1 }) // This will search the found track on youtube.
let stream = await play.stream(searched[0].url) // This will create stream from the above search
let resource = createAudioResource(stream, {
let resource = createAudioResource(stream.stream, {
inputType : stream.type
})
let player = createAudioPlayer({

View File

@ -26,7 +26,7 @@ client.on('messageCreate', async message => {
let stream = await play.stream_from_info(yt_info, COOKIE)
*/
let resource = createAudioResource(stream, {
let resource = createAudioResource(stream.stream, {
inputType : stream.type
})
let player = createAudioPlayer({

View File

@ -18,7 +18,7 @@ client.on('messageCreate', async message => {
let args = message.content.split('play')[1]
let yt_info = await play.search(args, { limit : 1 })
let stream = await play.stream(yt_info[0].url)
let resource = createAudioResource(stream, {
let resource = createAudioResource(stream.stream, {
inputType : stream.type
})
let player = createAudioPlayer({

View File

@ -25,7 +25,7 @@ client.on('messageCreate', async message => {
let stream = await play.stream_from_info(yt_info)
*/
let resource = createAudioResource(stream, {
let resource = createAudioResource(stream.stream, {
inputType : stream.type
})
let player = createAudioPlayer({

View File

@ -194,7 +194,8 @@ export class SoundCloudPlaylist {
}
}
export class Stream extends PassThrough {
export class Stream {
stream : PassThrough;
type: StreamType;
private url: string;
private playing_count: number;
@ -205,7 +206,7 @@ export class Stream extends PassThrough {
private time: number[];
private segment_urls: string[];
constructor(url: string, type: StreamType = StreamType.Arbitrary) {
super({ highWaterMark: 10 * 1000 * 1000 });
this.stream = new PassThrough({ highWaterMark: 10 * 1000 * 1000 });
this.type = type;
this.url = url;
this.playing_count = 0;
@ -215,14 +216,14 @@ export class Stream extends PassThrough {
this.data_ended = false;
this.time = [];
this.segment_urls = [];
this.on('close', () => {
this.stream.on('close', () => {
this.cleanup();
});
this.on('pause', () => {
this.stream.on('pause', () => {
this.playing_count++;
if (this.data_ended) {
this.cleanup();
this.removeAllListeners('pause');
this.stream.removeAllListeners('pause');
} else if (this.playing_count === 110) {
this.playing_count = 0;
this.start();
@ -248,7 +249,7 @@ export class Stream extends PassThrough {
}
private async start() {
if (this.destroyed) {
if (this.stream.destroyed) {
this.cleanup();
return;
}
@ -261,7 +262,7 @@ export class Stream extends PassThrough {
}
private async loop() {
if (this.destroyed) {
if (this.stream.destroyed) {
this.cleanup();
return;
}
@ -274,18 +275,18 @@ export class Stream extends PassThrough {
const stream = await request_stream(this.segment_urls.shift() as string).catch((err: Error) => err);
if (stream instanceof Error) throw stream;
stream.pipe(this, { end: false });
stream.pipe(this.stream, { end: false });
stream.on('end', () => {
if (this.downloaded_time >= 300) return;
else this.loop();
});
stream.once('error', (err) => {
this.emit('error', err);
this.stream.emit('error', err);
});
}
private cleanup() {
this.request?.unpipe(this);
this.request?.unpipe(this.stream);
this.request?.destroy();
this.url = '';
this.playing_count = 0;

View File

@ -10,7 +10,8 @@ export interface FormatInterface {
maxDvrDurationSec: number;
}
export class LiveStreaming extends PassThrough {
export class LiveStreaming {
stream : PassThrough;
type: StreamType;
private base_url: string;
private url: string;
@ -22,7 +23,7 @@ export class LiveStreaming extends PassThrough {
private segments_urls: string[];
private request: IncomingMessage | null;
constructor(dash_url: string, target_interval: number, video_url: string) {
super({ highWaterMark: 10 * 1000 * 1000 });
this.stream = new PassThrough({ highWaterMark: 10 * 1000 * 1000 })
this.type = StreamType.Arbitrary;
this.url = dash_url;
this.base_url = '';
@ -35,7 +36,7 @@ export class LiveStreaming extends PassThrough {
this.dash_timer = setTimeout(() => {
this.dash_updater();
}, 1800000);
this.on('close', () => {
this.stream.on('close', () => {
this.cleanup();
});
this.start();
@ -71,7 +72,7 @@ export class LiveStreaming extends PassThrough {
private cleanup() {
clearTimeout(this.timer as NodeJS.Timer);
clearTimeout(this.dash_timer as NodeJS.Timer);
this.request?.unpipe(this);
this.request?.unpipe(this.stream);
this.request?.destroy();
this.dash_timer = null;
this.video_url = '';
@ -85,7 +86,7 @@ export class LiveStreaming extends PassThrough {
}
private async start() {
if (this.destroyed) {
if (this.stream.destroyed) {
this.cleanup();
return;
}
@ -99,17 +100,17 @@ export class LiveStreaming extends PassThrough {
await new Promise(async (resolve, reject) => {
const stream = await request_stream(this.base_url + segment).catch((err: Error) => err);
if (stream instanceof Error) {
this.emit('error', stream);
this.stream.emit('error', stream);
return;
}
this.request = stream;
stream.pipe(this, { end: false });
stream.pipe(this.stream, { end: false });
stream.on('end', () => {
this.packet_count++;
resolve('');
});
stream.once('error', (err) => {
this.emit('error', err);
this.stream.emit('error', err);
});
});
}
@ -119,7 +120,8 @@ export class LiveStreaming extends PassThrough {
}
}
export class Stream extends PassThrough {
export class Stream {
stream : PassThrough;
type: StreamType;
private url: string;
private bytes_count: number;
@ -139,7 +141,7 @@ export class Stream extends PassThrough {
video_url: string,
cookie: string
) {
super({ highWaterMark: 10 * 1000 * 1000 });
this.stream = new PassThrough({ highWaterMark: 10 * 1000 * 1000 });
this.url = url;
this.type = type;
this.bytes_count = 0;
@ -153,16 +155,16 @@ export class Stream extends PassThrough {
this.request = null;
this.data_ended = false;
this.playing_count = 0;
this.on('close', () => {
this.stream.on('close', () => {
this.cleanup();
});
this.on('pause', () => {
this.stream.on('pause', () => {
this.playing_count++;
if (this.data_ended) {
this.bytes_count = 0;
this.per_sec_bytes = 0;
this.cleanup();
this.removeAllListeners('pause');
this.stream.removeAllListeners('pause');
} else if (this.playing_count === 280) {
this.playing_count = 0;
this.loop();
@ -178,7 +180,7 @@ export class Stream extends PassThrough {
private cleanup() {
clearInterval(this.timer as NodeJS.Timer);
this.request?.unpipe(this);
this.request?.unpipe(this.stream);
this.request?.destroy();
this.timer = null;
this.request = null;
@ -186,7 +188,7 @@ export class Stream extends PassThrough {
}
private async loop() {
if (this.destroyed) {
if (this.stream.destroyed) {
this.cleanup();
return;
}
@ -197,7 +199,7 @@ export class Stream extends PassThrough {
}
}).catch((err: Error) => err);
if (stream instanceof Error) {
this.emit('error', stream);
this.stream.emit('error', stream);
this.data_ended = true;
this.bytes_count = 0;
this.per_sec_bytes = 0;
@ -216,10 +218,10 @@ export class Stream extends PassThrough {
return;
}
this.request = stream;
stream.pipe(this, { end: false });
stream.pipe(this.stream, { end: false });
stream.once('error', (err) => {
this.emit('error', err);
this.stream.emit('error', err);
});
stream.on('data', (chunk: any) => {