From 7b1f082c277a13550ee6afc4c66ff630ca2260e4 Mon Sep 17 00:00:00 2001 From: killer069 <65385476+killer069@users.noreply.github.com> Date: Mon, 30 Aug 2021 15:58:23 +0530 Subject: [PATCH] Socket issue tried to fix --- play-dl/YouTube/classes/LiveStream.ts | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/play-dl/YouTube/classes/LiveStream.ts b/play-dl/YouTube/classes/LiveStream.ts index 37f0183..da262f1 100644 --- a/play-dl/YouTube/classes/LiveStream.ts +++ b/play-dl/YouTube/classes/LiveStream.ts @@ -1,7 +1,7 @@ import { PassThrough } from 'stream' import got from 'got' -import Request from 'got/dist/source/core'; import { StreamType } from '../stream'; +import { Socket } from 'net' export interface FormatInterface{ url : string; @@ -17,6 +17,7 @@ export class LiveStreaming{ private interval : number private packet_count : number private timer : NodeJS.Timer | null + private socket : Socket | null private segments_urls : string[] constructor(dash_url : string, target_interval : number){ this.type = StreamType.Arbitrary @@ -26,6 +27,7 @@ export class LiveStreaming{ this.segments_urls = [] this.packet_count = 0 this.timer = null + this.socket = null this.interval = target_interval * 1000 || 0 this.stream.on('close', () => { this.cleanup() @@ -45,6 +47,8 @@ export class LiveStreaming{ private cleanup(){ clearTimeout(this.timer as NodeJS.Timer) + this.socket?.destroy() + this.socket = null this.timer = null this.url = '' this.base_url = '' @@ -88,6 +92,7 @@ export class LiveEnded{ private base_url : string; private packet_count : number private segments_urls : string[] + private socket : Socket | null constructor(dash_url : string){ this.type = StreamType.Arbitrary this.url = dash_url @@ -95,6 +100,7 @@ export class LiveEnded{ this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 }) this.segments_urls = [] this.packet_count = 0 + this.socket = null this.stream.on('close', () => { this.cleanup() }) @@ -112,6 +118,8 @@ export class LiveEnded{ } private cleanup(){ + this.socket?.destroy() + this.socket = null this.url = '' this.base_url = '' this.segments_urls = [] @@ -126,6 +134,10 @@ export class LiveEnded{ await this.dash_getter() if(this.packet_count === 0) this.packet_count = Number(this.segments_urls[0].split('sq/')[1].split('/')[0]) for await (let segment of this.segments_urls){ + if(this.stream.destroyed){ + this.cleanup() + break + } if(Number(segment.split('sq/')[1].split('/')[0]) !== this.packet_count){ continue } @@ -151,6 +163,7 @@ export class Stream { private per_sec_bytes : number; private duration : number; private timer : NodeJS.Timer | null + private socket : Socket | null constructor(url : string, type : StreamType, duration : number){ this.url = url this.type = type @@ -158,12 +171,14 @@ export class Stream { this.bytes_count = 0 this.per_sec_bytes = 0 this.timer = null - this.duration = duration; + this.duration = duration + this.socket = null; (duration > 300) ? this.loop_start() : this.normal_start() } private cleanup(){ clearTimeout(this.timer as NodeJS.Timer) + this.socket?.destroy() this.timer = null this.url = '' this.bytes_count = 0 @@ -176,6 +191,7 @@ export class Stream { return } let stream = got.stream(this.url) + this.socket = stream.socket as Socket stream.pipe(this.stream) } @@ -193,9 +209,10 @@ export class Stream { this.bytes_count += chunk.length this.stream.write(chunk) }) - + this.socket = stream.socket as Socket stream.on('data', () => { if(this.bytes_count > (this.per_sec_bytes * 300)){ + this.socket?.destroy() stream.destroy() } }) @@ -222,9 +239,10 @@ export class Stream { this.bytes_count += chunk.length this.stream.write(chunk) }) - + this.socket = stream.socket as Socket stream.on('data', () => { if(absolute_bytes > (this.per_sec_bytes * 300)){ + this.socket?.destroy() stream.destroy() } })