Some more updates

This commit is contained in:
killer069
2021-11-18 13:06:55 +05:30
parent 4285f1a733
commit e53e892606
48 changed files with 569 additions and 135 deletions

View File

@@ -9,19 +9,67 @@ export interface FormatInterface {
targetDurationSec: number;
maxDvrDurationSec: number;
}
export class LiveStreaming {
/**
* YouTube Live Stream class for playing audio from Live Stream videos.
*/
export class LiveStream {
/**
* Readable Stream through which data passes
*/
stream: Readable;
/**
* Type of audio data that we recieved from live stream youtube url.
*/
type: StreamType;
/**
* Base URL in dash manifest file.
*/
private base_url: string;
/**
* Given Dash URL.
*/
private url: string;
/**
* Interval to fetch data again to dash url.
*/
private interval: number;
/**
* Sequence count of urls in dash file.
*/
private packet_count: number;
/**
* Timer that creates loop from interval time provided.
*/
private timer: Timer;
/**
* Live Stream Video url.
*/
private video_url: string;
/**
* Timer used to update dash url so as to avoid 404 errors after long hours of streaming.
*
* It updates dash_url every 30 minutes.
*/
private dash_timer: Timer;
/**
* Segments of url that we recieve in dash file.
*
* base_url + segment_urls[0] = One complete url for one segment.
*/
private segments_urls: string[];
/**
* Incoming message that we recieve.
*
* Storing this is essential.
* This helps to destroy the TCP connection completely if you stopped player in between the stream
*/
private request: IncomingMessage | null;
/**
* Live Stream Class Constructor
* @param dash_url dash manifest URL
* @param target_interval interval time for fetching dash data again
* @param video_url Live Stream video url.
*/
constructor(dash_url: string, target_interval: number, video_url: string) {
this.stream = new Readable({ highWaterMark: 10 * 1000 * 1000, read() {} });
this.type = StreamType.Arbitrary;
@@ -44,7 +92,11 @@ export class LiveStreaming {
});
this.start();
}
/**
* Updates dash url.
*
* Used by dash_timer for updating dash_url every 30 minutes.
*/
private async dash_updater() {
const info = await video_info(this.video_url);
if (
@@ -55,7 +107,11 @@ export class LiveStreaming {
this.url = info.LiveStreamData.dashManifestUrl;
}
}
/**
* Parses data recieved from dash_url.
*
* Updates base_url , segments_urls array.
*/
private async dash_getter() {
const response = await request(this.url);
const audioFormat = response
@@ -68,7 +124,11 @@ export class LiveStreaming {
this.segments_urls = list.replace(new RegExp('<SegmentURL media="', 'g'), '').split('"/>');
if (this.segments_urls[this.segments_urls.length - 1] === '') this.segments_urls.pop();
}
/**
* This cleans every used variable in class.
*
* This is used to prevent re-use of this class and helping garbage collector to collect it.
*/
private cleanup() {
this.timer.destroy();
this.dash_timer.destroy();
@@ -81,7 +141,12 @@ export class LiveStreaming {
this.packet_count = 0;
this.interval = 0;
}
/**
* This starts function in Live Stream Class.
*
* Gets data from dash url and pass it to dash getter function.
* Get data from complete segment url and pass data to Stream.
*/
private async start() {
if (this.stream.destroyed) {
this.cleanup();
@@ -116,25 +181,75 @@ export class LiveStreaming {
this.timer.reuse();
}
/**
* Deprecated Functions
*/
pause() {}
/**
* Deprecated Functions
*/
resume() {}
}
/**
* Class for YouTube Stream
* YouTube Stream Class for playing audio from normal videos.
*/
export class Stream {
stream: Readable;
type: StreamType;
/**
* Readable Stream through which data passes
*/
stream: Readable;
/**
* Type of audio data that we recieved from normal youtube url.
*/
type: StreamType;
/**
* Audio Endpoint Format Url to get data from.
*/
private url: string;
/**
* Used to calculate no of bytes data that we have recieved
*/
private bytes_count: number;
/**
* Calculate per second bytes by using contentLength (Total bytes) / Duration (in seconds)
*/
private per_sec_bytes: number;
/**
* Total length of audio file in bytes
*/
private content_length: number;
/**
* YouTube video url. [ Used only for retrying purposes only. ]
*/
private video_url: string;
/**
* Timer for looping data every 265 seconds.
*/
private timer: Timer;
/**
* Quality given by user. [ Used only for retrying purposes only. ]
*/
private quality: number;
/**
* Proxy config given by user. [ Used only for retrying purposes only. ]
*/
private proxy: Proxy[] | undefined;
/**
* Incoming message that we recieve.
*
* Storing this is essential.
* This helps to destroy the TCP connection completely if you stopped player in between the stream
*/
private request: IncomingMessage | null;
/**
* YouTube Stream Class constructor
* @param url Audio Endpoint url.
* @param type Type of Stream
* @param duration Duration of audio playback [ in seconds ]
* @param contentLength Total length of Audio file in bytes.
* @param video_url YouTube video url.
* @param options Options provided to stream function.
*/
constructor(
url: string,
type: StreamType,
@@ -163,19 +278,29 @@ export class Stream {
});
this.loop();
}
/**
* Retry if we get 404 or 403 Errors.
*/
private async retry() {
const info = await video_info(this.video_url, { proxy: this.proxy });
const audioFormat = parseAudioFormats(info.format);
this.url = audioFormat[this.quality].url;
}
/**
* This cleans every used variable in class.
*
* This is used to prevent re-use of this class and helping garbage collector to collect it.
*/
private cleanup() {
this.request?.destroy();
this.request = null;
this.url = '';
}
/**
* Getting data from audio endpoint url and passing it to stream.
*
* If 404 or 403 occurs, it will retry again.
*/
private async loop() {
if (this.stream.destroyed) {
this.timer.destroy();
@@ -226,24 +351,62 @@ export class Stream {
}
});
}
/**
* Pauses timer.
* Stops running of loop.
*
* Useful if you don't want to get excess data to be stored in stream.
*/
pause() {
this.timer.pause();
}
/**
* Resumes timer.
* Starts running of loop.
*/
resume() {
this.timer.resume();
}
}
/**
* Timer Class.
*
* setTimeout + extra features ( re-starting, pausing, resuming ).
*/
export class Timer {
/**
* Boolean for checking if Timer is destroyed or not.
*/
private destroyed: boolean;
/**
* Boolean for checking if Timer is paused or not.
*/
private paused: boolean;
/**
* setTimeout function
*/
private timer: NodeJS.Timer;
/**
* Callback to be executed once timer finishes.
*/
private callback: () => void;
/**
* Seconds time when it is started.
*/
private time_start: number;
/**
* Total time left.
*/
private time_left: number;
/**
* Total time given by user [ Used only for re-using timer. ]
*/
private time_total: number;
/**
* Constructor for Timer Class
* @param callback Function to execute when timer is up.
* @param time Total time to wait before execution.
*/
constructor(callback: () => void, time: number) {
this.callback = callback;
this.time_total = time;
@@ -253,7 +416,10 @@ export class Timer {
this.time_start = process.hrtime()[0];
this.timer = setTimeout(this.callback, this.time_total * 1000);
}
/**
* Pauses Timer
* @returns Boolean to tell that if it is paused or not.
*/
pause() {
if (!this.paused && !this.destroyed) {
this.paused = true;
@@ -262,7 +428,10 @@ export class Timer {
return true;
} else return false;
}
/**
* Resumes Timer
* @returns Boolean to tell that if it is resumed or not.
*/
resume() {
if (this.paused && !this.destroyed) {
this.paused = false;
@@ -271,7 +440,10 @@ export class Timer {
return true;
} else return false;
}
/**
* Reusing of timer
* @returns Boolean to tell if it is re-used or not.
*/
reuse() {
if (!this.destroyed) {
clearTimeout(this.timer);
@@ -282,7 +454,11 @@ export class Timer {
return true;
} else return false;
}
/**
* Destroy timer.
*
* It can't be used again.
*/
destroy() {
clearTimeout(this.timer);
this.destroyed = true;