TCP connection fix
This commit is contained in:
parent
74de1739c8
commit
02fcb61c87
@ -1,6 +1,7 @@
|
|||||||
import { PassThrough } from 'stream'
|
import { PassThrough } from 'stream'
|
||||||
import got from 'got'
|
import got from 'got'
|
||||||
import { StreamType } from '../stream';
|
import { StreamType } from '../stream';
|
||||||
|
import { Socket } from 'net'
|
||||||
|
|
||||||
export interface FormatInterface{
|
export interface FormatInterface{
|
||||||
url : string;
|
url : string;
|
||||||
@ -17,6 +18,7 @@ export class LiveStreaming{
|
|||||||
private packet_count : number
|
private packet_count : number
|
||||||
private timer : NodeJS.Timer | null
|
private timer : NodeJS.Timer | null
|
||||||
private segments_urls : string[]
|
private segments_urls : string[]
|
||||||
|
private socket : Socket | null
|
||||||
constructor(dash_url : string, target_interval : number){
|
constructor(dash_url : string, target_interval : number){
|
||||||
this.type = StreamType.Arbitrary
|
this.type = StreamType.Arbitrary
|
||||||
this.url = dash_url
|
this.url = dash_url
|
||||||
@ -25,6 +27,7 @@ export class LiveStreaming{
|
|||||||
this.segments_urls = []
|
this.segments_urls = []
|
||||||
this.packet_count = 0
|
this.packet_count = 0
|
||||||
this.timer = null
|
this.timer = null
|
||||||
|
this.socket = null
|
||||||
this.interval = target_interval * 1000 || 0
|
this.interval = target_interval * 1000 || 0
|
||||||
this.stream.on('close', () => {
|
this.stream.on('close', () => {
|
||||||
this.cleanup()
|
this.cleanup()
|
||||||
@ -44,6 +47,8 @@ export class LiveStreaming{
|
|||||||
|
|
||||||
private cleanup(){
|
private cleanup(){
|
||||||
clearTimeout(this.timer as NodeJS.Timer)
|
clearTimeout(this.timer as NodeJS.Timer)
|
||||||
|
this.socket?.destroy()
|
||||||
|
this.socket = null
|
||||||
this.timer = null
|
this.timer = null
|
||||||
this.url = ''
|
this.url = ''
|
||||||
this.base_url = ''
|
this.base_url = ''
|
||||||
@ -67,6 +72,7 @@ export class LiveStreaming{
|
|||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
let stream = got.stream(this.base_url + segment)
|
let stream = got.stream(this.base_url + segment)
|
||||||
stream.on('data', (chunk: any) => this.stream.write(chunk))
|
stream.on('data', (chunk: any) => this.stream.write(chunk))
|
||||||
|
stream.once('data', () => {this.socket = stream.socket as Socket})
|
||||||
stream.on('end', () => {
|
stream.on('end', () => {
|
||||||
this.packet_count++
|
this.packet_count++
|
||||||
resolve('')
|
resolve('')
|
||||||
@ -87,6 +93,7 @@ export class LiveEnded{
|
|||||||
private base_url : string;
|
private base_url : string;
|
||||||
private packet_count : number
|
private packet_count : number
|
||||||
private segments_urls : string[]
|
private segments_urls : string[]
|
||||||
|
private socket : Socket | null
|
||||||
constructor(dash_url : string){
|
constructor(dash_url : string){
|
||||||
this.type = StreamType.Arbitrary
|
this.type = StreamType.Arbitrary
|
||||||
this.url = dash_url
|
this.url = dash_url
|
||||||
@ -94,6 +101,7 @@ export class LiveEnded{
|
|||||||
this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 })
|
this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 })
|
||||||
this.segments_urls = []
|
this.segments_urls = []
|
||||||
this.packet_count = 0
|
this.packet_count = 0
|
||||||
|
this.socket = null
|
||||||
this.stream.on('close', () => {
|
this.stream.on('close', () => {
|
||||||
this.cleanup()
|
this.cleanup()
|
||||||
})
|
})
|
||||||
@ -111,6 +119,8 @@ export class LiveEnded{
|
|||||||
}
|
}
|
||||||
|
|
||||||
private cleanup(){
|
private cleanup(){
|
||||||
|
this.socket?.destroy()
|
||||||
|
this.socket = null
|
||||||
this.url = ''
|
this.url = ''
|
||||||
this.base_url = ''
|
this.base_url = ''
|
||||||
this.segments_urls = []
|
this.segments_urls = []
|
||||||
@ -136,6 +146,7 @@ export class LiveEnded{
|
|||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
let stream = got.stream(this.base_url + segment)
|
let stream = got.stream(this.base_url + segment)
|
||||||
stream.on('data', (chunk: any) => this.stream.write(chunk))
|
stream.on('data', (chunk: any) => this.stream.write(chunk))
|
||||||
|
stream.once('data', () => {this.socket = stream.socket as Socket})
|
||||||
stream.on('end', () => {
|
stream.on('end', () => {
|
||||||
this.packet_count++
|
this.packet_count++
|
||||||
resolve('')
|
resolve('')
|
||||||
@ -154,6 +165,7 @@ export class Stream {
|
|||||||
private per_sec_bytes : number;
|
private per_sec_bytes : number;
|
||||||
private duration : number;
|
private duration : number;
|
||||||
private timer : NodeJS.Timer | null
|
private timer : NodeJS.Timer | null
|
||||||
|
private socket : Socket | null
|
||||||
constructor(url : string, type : StreamType, duration : number){
|
constructor(url : string, type : StreamType, duration : number){
|
||||||
this.url = url
|
this.url = url
|
||||||
this.type = type
|
this.type = type
|
||||||
@ -161,12 +173,15 @@ export class Stream {
|
|||||||
this.bytes_count = 0
|
this.bytes_count = 0
|
||||||
this.per_sec_bytes = 0
|
this.per_sec_bytes = 0
|
||||||
this.timer = null
|
this.timer = null
|
||||||
|
this.socket = null
|
||||||
this.duration = duration;
|
this.duration = duration;
|
||||||
(duration > 300) ? this.loop_start() : this.normal_start()
|
(duration > 300) ? this.loop_start() : this.normal_start()
|
||||||
}
|
}
|
||||||
|
|
||||||
private cleanup(){
|
private cleanup(){
|
||||||
clearTimeout(this.timer as NodeJS.Timer)
|
clearTimeout(this.timer as NodeJS.Timer)
|
||||||
|
this.socket?.destroy()
|
||||||
|
this.socket = null
|
||||||
this.timer = null
|
this.timer = null
|
||||||
this.url = ''
|
this.url = ''
|
||||||
this.bytes_count = 0
|
this.bytes_count = 0
|
||||||
@ -180,6 +195,7 @@ export class Stream {
|
|||||||
}
|
}
|
||||||
let stream = got.stream(this.url)
|
let stream = got.stream(this.url)
|
||||||
stream.pipe(this.stream)
|
stream.pipe(this.stream)
|
||||||
|
stream.once('data', () => {this.socket = stream.socket as Socket})
|
||||||
}
|
}
|
||||||
|
|
||||||
private loop_start(){
|
private loop_start(){
|
||||||
@ -190,6 +206,7 @@ export class Stream {
|
|||||||
let stream = got.stream(this.url)
|
let stream = got.stream(this.url)
|
||||||
stream.once('data', () => {
|
stream.once('data', () => {
|
||||||
this.per_sec_bytes = Math.ceil((stream.downloadProgress.total as number)/this.duration)
|
this.per_sec_bytes = Math.ceil((stream.downloadProgress.total as number)/this.duration)
|
||||||
|
this.socket = stream.socket as Socket
|
||||||
})
|
})
|
||||||
|
|
||||||
stream.on('data', (chunk: any) => {
|
stream.on('data', (chunk: any) => {
|
||||||
@ -224,6 +241,7 @@ export class Stream {
|
|||||||
this.bytes_count += chunk.length
|
this.bytes_count += chunk.length
|
||||||
this.stream.write(chunk)
|
this.stream.write(chunk)
|
||||||
})
|
})
|
||||||
|
stream.once('data', () => {this.socket = stream.socket as Socket})
|
||||||
stream.on('data', () => {
|
stream.on('data', () => {
|
||||||
if(absolute_bytes > (this.per_sec_bytes * 300)){
|
if(absolute_bytes > (this.per_sec_bytes * 300)){
|
||||||
stream.destroy()
|
stream.destroy()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user