mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-09-27 10:53:50 +02:00
Add StreamSource
This commit is contained in:
+1
-1
@@ -41,7 +41,7 @@ const input = new Metamuxer.Input({
|
||||
source
|
||||
});
|
||||
|
||||
console.log(await input.getMimeType())
|
||||
console.log(await input.getMimeType());
|
||||
|
||||
let videoTrack = await input.getPrimaryVideoTrack();
|
||||
let audioTrack = await input.getPrimaryAudioTrack();
|
||||
|
||||
+1
-1
@@ -68,7 +68,7 @@ export {
|
||||
} from './codec';
|
||||
export { Target, BufferTarget, StreamTarget, StreamTargetChunk, StreamTargetOptions } from './target';
|
||||
export { Rotation, TransformationMatrix, AnyIterable, setVideoFrameTiming } from './misc';
|
||||
export { Source, BufferSource, BlobSource, UrlSource } from './source';
|
||||
export { Source, BufferSource, StreamSource, StreamSourceOptions, BlobSource, UrlSource } from './source';
|
||||
export {
|
||||
InputFormat,
|
||||
IsobmffInputFormat,
|
||||
|
||||
+44
-6
@@ -32,16 +32,54 @@ export class BufferSource extends Source {
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
override async _read(start: number, end: number) {
|
||||
async _read(start: number, end: number) {
|
||||
return this._bytes.subarray(start, end);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
override async _retrieveSize() {
|
||||
async _retrieveSize() {
|
||||
return this._bytes.byteLength;
|
||||
}
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export type StreamSourceOptions = {
|
||||
read: (start: number, end: number) => Uint8Array | Promise<Uint8Array>;
|
||||
getSize: () => number | Promise<number>;
|
||||
};
|
||||
|
||||
/** @public */
|
||||
export class StreamSource extends Source {
|
||||
/** @internal */
|
||||
_options: StreamSourceOptions;
|
||||
|
||||
constructor(options: StreamSourceOptions) {
|
||||
if (!options || typeof options !== 'object') {
|
||||
throw new TypeError('options must be an object.');
|
||||
}
|
||||
if (typeof options.read !== 'function') {
|
||||
throw new TypeError('options.read must be a function.');
|
||||
}
|
||||
if (typeof options.getSize !== 'function') {
|
||||
throw new TypeError('options.getSize must be a function.');
|
||||
}
|
||||
|
||||
super();
|
||||
|
||||
this._options = options;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
async _read(start: number, end: number) {
|
||||
return this._options.read(start, end);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
async _retrieveSize() {
|
||||
return this._options.getSize();
|
||||
}
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export class BlobSource extends Source {
|
||||
/** @internal */
|
||||
@@ -58,14 +96,14 @@ export class BlobSource extends Source {
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
override async _read(start: number, end: number) {
|
||||
async _read(start: number, end: number) {
|
||||
const slice = this._blob.slice(start, end);
|
||||
const buffer = await slice.arrayBuffer();
|
||||
return new Uint8Array(buffer);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
override async _retrieveSize() {
|
||||
async _retrieveSize() {
|
||||
return this._blob.size;
|
||||
}
|
||||
}
|
||||
@@ -140,7 +178,7 @@ export class UrlSource extends Source {
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
override async _read(start: number, end: number): Promise<Uint8Array> {
|
||||
async _read(start: number, end: number): Promise<Uint8Array> {
|
||||
if (this._fullData) {
|
||||
return new Uint8Array(this._fullData, start, end - start);
|
||||
}
|
||||
@@ -158,7 +196,7 @@ export class UrlSource extends Source {
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
override async _retrieveSize(): Promise<number> {
|
||||
async _retrieveSize(): Promise<number> {
|
||||
if (this._fullData) {
|
||||
return this._fullData.byteLength;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user