Cleanup seeking code a bit

This commit is contained in:
absidue 2021-12-29 21:43:33 +01:00
parent e24c430790
commit 210971495c
2 changed files with 13 additions and 16 deletions

View File

@ -1,4 +1,4 @@
import { IncomingMessage } from 'http'; import { IncomingMessage } from 'node:http';
import { request_stream } from '../../Request'; import { request_stream } from '../../Request';
import { parseAudioFormats, StreamOptions, StreamType } from '../stream'; import { parseAudioFormats, StreamOptions, StreamType } from '../stream';
import { video_stream_info } from '../utils/extractor'; import { video_stream_info } from '../utils/extractor';
@ -102,11 +102,11 @@ export class SeekStream {
}).catch((err: Error) => err); }).catch((err: Error) => err);
if (stream instanceof Error) { if (stream instanceof Error) {
rej(stream) rej(stream);
return; return;
} }
if (Number(stream.statusCode) >= 400) { if (Number(stream.statusCode) >= 400) {
rej(400) rej(400);
return; return;
} }
this.request = stream; this.request = stream;
@ -118,16 +118,15 @@ export class SeekStream {
}); });
} else res(''); } else res('');
}).catch((err) => err); }).catch((err) => err);
if(parse instanceof Error){ if (parse instanceof Error) {
this.stream.emit('error', parse); this.stream.emit('error', parse);
this.bytes_count = 0; this.bytes_count = 0;
this.per_sec_bytes = 0; this.per_sec_bytes = 0;
this.cleanup(); this.cleanup();
return; return;
} } else if (parse === 400) {
else if(parse === 400){
await this.retry(); await this.retry();
this.timer.reuse() this.timer.reuse();
return this.seek(sec); return this.seek(sec);
} }
const bytes = this.stream.seek(sec); const bytes = this.stream.seek(sec);

View File

@ -1,5 +1,5 @@
import { WebmElements, WebmHeader } from 'play-audio'; import { WebmElements, WebmHeader } from 'play-audio';
import { Duplex, DuplexOptions } from 'stream'; import { Duplex, DuplexOptions } from 'node:stream';
enum DataType { enum DataType {
master, master,
@ -54,7 +54,7 @@ export class WebmSeeker extends Duplex {
return ++i; return ++i;
} }
private get vint_value(): boolean { private vint_value(): boolean {
if (!this.chunk) return false; if (!this.chunk) return false;
const length = this.vint_length; const length = this.vint_length;
if (this.chunk.length < this.cursor + length) return false; if (this.chunk.length < this.cursor + length) return false;
@ -75,7 +75,7 @@ export class WebmSeeker extends Duplex {
seek(sec: number): Error | number { seek(sec: number): Error | number {
let position = 0; let position = 0;
let time = Math.floor(sec / 10) * 10; const time = Math.floor(sec / 10) * 10;
this.time_left = (sec - time) * 1000 || 0; this.time_left = (sec - time) * 1000 || 0;
if (!this.header.segment.cues) return new Error('Failed to Parse Cues'); if (!this.header.segment.cues) return new Error('Failed to Parse Cues');
@ -98,7 +98,7 @@ export class WebmSeeker extends Duplex {
let err: Error | undefined; let err: Error | undefined;
if (this.state === WebmSeekerState.READING_HEAD) err = this.readHead(); if (this.state === WebmSeekerState.READING_HEAD) err = this.readHead();
else if (!this.seekfound) err = this.getClosetCluster(); else if (!this.seekfound) err = this.getClosestCluster();
else err = this.readTag(); else err = this.readTag();
if (err) callback(err); if (err) callback(err);
@ -115,9 +115,8 @@ export class WebmSeeker extends Duplex {
const ebmlID = this.parseEbmlID(this.chunk.slice(this.cursor, this.cursor + id).toString('hex')); const ebmlID = this.parseEbmlID(this.chunk.slice(this.cursor, this.cursor + id).toString('hex'));
this.cursor += id; this.cursor += id;
const vint = this.vint_value;
if (!vint) { if (!this.vint_value()) {
this.cursor = oldCursor; this.cursor = oldCursor;
break; break;
} }
@ -161,9 +160,8 @@ export class WebmSeeker extends Duplex {
const ebmlID = this.parseEbmlID(this.chunk.slice(this.cursor, this.cursor + id).toString('hex')); const ebmlID = this.parseEbmlID(this.chunk.slice(this.cursor, this.cursor + id).toString('hex'));
this.cursor += id; this.cursor += id;
const vint = this.vint_value;
if (!vint) { if (!this.vint_value()) {
this.cursor = oldCursor; this.cursor = oldCursor;
break; break;
} }
@ -203,7 +201,7 @@ export class WebmSeeker extends Duplex {
this.cursor = 0; this.cursor = 0;
} }
private getClosetCluster(): Error | undefined { private getClosestCluster(): Error | undefined {
if (!this.chunk) return new Error('Chunk is missing'); if (!this.chunk) return new Error('Chunk is missing');
const count = this.chunk.indexOf('1f43b675', 0, 'hex'); const count = this.chunk.indexOf('1f43b675', 0, 'hex');
if (count === -1) throw new Error('Failed to find nearest Cluster.'); if (count === -1) throw new Error('Failed to find nearest Cluster.');