mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-10-06 07:13:47 +02:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
21cff32831 | ||
|
|
73e936f325 | ||
|
|
3c8d503158 | ||
|
|
fe4eebd487 | ||
|
|
b5f5b5588c | ||
|
|
b1f17b25e3 | ||
|
|
efcf5d0069 | ||
|
|
aad31009fe | ||
|
|
68c37b49dc | ||
|
|
b12fee70a3 | ||
|
|
90d5aed051 | ||
|
|
d2d893ff30 | ||
|
|
3217490d81 | ||
|
|
5039c730bf | ||
|
|
a14c0885d7 | ||
|
|
c7b45eabf0 | ||
|
|
8629709cd7 | ||
|
|
754af6f90f | ||
|
|
fa1fc27577 | ||
|
|
e676de4129 | ||
|
|
0c4c13cea2 | ||
|
|
3767aea7d9 | ||
|
|
cdeeb6c6fa | ||
|
|
c27042b24d | ||
|
|
f8cd66727c |
@@ -45,12 +45,16 @@ Mediabunny is a JavaScript library for reading, writing, and converting media fi
|
||||
|
||||
<div align="center">
|
||||
<a href="https://www.reactvideoeditor.com/" target="_blank" rel="sponsored">
|
||||
<img src="./docs/public/sponsors/rve.svg" width="40" height="40" alt="React Video Editor">
|
||||
<img src="./docs/public/sponsors/rve.png" width="40" height="40" alt="React Video Editor">
|
||||
</a>
|
||||
|
||||
<a href="https://www.mux.com/" target="_blank" rel="sponsored">
|
||||
<img src="./docs/public/sponsors/mux.jpg" width="40" height="40" alt="Mux">
|
||||
</a>
|
||||
|
||||
<a href="https://jellypod.ai/" target="_blank" rel="sponsored">
|
||||
<img src="./docs/public/sponsors/jellypod.png" width="40" height="40" alt="Jellypod">
|
||||
</a>
|
||||
</div>
|
||||
|
||||
[Sponsor Mediabunny's development](https://github.com/sponsors/Vanilagy)
|
||||
@@ -200,7 +204,7 @@ npm run lint # ESLint
|
||||
|
||||
npm run docs:generate # Generates API docs
|
||||
npm run docs:dev # Start docs development server
|
||||
npm run dev # Start examples development server
|
||||
|
||||
npm run dev # Start examples development server, will run at http://localhost:5173/examples/[name]/
|
||||
|
||||
npm run docs:build # Build docs and examples
|
||||
```
|
||||
+1
-1
@@ -115,7 +115,7 @@
|
||||
return ctx.canvas;
|
||||
},
|
||||
*/
|
||||
width: 300,
|
||||
//width: 300,
|
||||
//alpha: 'keep',
|
||||
//width: 320,
|
||||
//discard: true,
|
||||
|
||||
+22
-3
@@ -14,6 +14,24 @@
|
||||
source: new Mediabunny.BlobSource(file),
|
||||
});
|
||||
|
||||
const videoTrack = await input.getPrimaryVideoTrack();
|
||||
const sink = new Mediabunny.VideoSampleSink(videoTrack);
|
||||
|
||||
console.log(await videoTrack.getDecoderConfig())
|
||||
|
||||
const sink2 = new Mediabunny.EncodedPacketSink(videoTrack);
|
||||
for await (const packet of sink2.packets()) {
|
||||
console.log(packet.data.join(', '))//, window.findNalUnitsInLengthPrefixed(packet.data, 4).map(x => window.extractNalUnitTypeForAvc(x)));
|
||||
if (packet.timestamp > 1) break;
|
||||
}
|
||||
|
||||
//return;
|
||||
|
||||
for await (const sample of sink.samples()) {
|
||||
console.log(sample);
|
||||
}
|
||||
|
||||
/*
|
||||
let total = 0;
|
||||
input.source.onread = (start, end) => {
|
||||
total += end - start;
|
||||
@@ -22,6 +40,7 @@
|
||||
|
||||
const videoTrack = await input.getPrimaryVideoTrack();
|
||||
const sink = new Mediabunny.EncodedPacketSink(videoTrack);
|
||||
*/
|
||||
|
||||
//console.log(await sink.getPacket(0));
|
||||
/*
|
||||
@@ -38,7 +57,7 @@
|
||||
|
||||
|
||||
|
||||
const stats = await videoTrack.computePacketStats();
|
||||
//const stats = await videoTrack.computePacketStats();
|
||||
|
||||
/*
|
||||
const sink = new Mediabunny.EncodedPacketSink(videoTrack);
|
||||
@@ -47,9 +66,9 @@
|
||||
}
|
||||
*/
|
||||
//console.log(await videoTrack.computeDuration());
|
||||
console.log("Done", stats, total, file.size)
|
||||
//console.log("Done", stats, total, file.size)
|
||||
|
||||
console.log(input);
|
||||
//console.log(input);
|
||||
|
||||
/*
|
||||
const videoTrack = await input.getPrimaryVideoTrack();
|
||||
|
||||
+3
-1
@@ -100,10 +100,12 @@ const sponsors = {
|
||||
{ image: '/sponsors/pintura-labs.png', name: 'Pintura Labs', url: 'https://pqina.nl/pintura/' },
|
||||
],
|
||||
bronze: [
|
||||
{ image: '/sponsors/rve.svg', name: 'React Video Editor', url: 'https://www.reactvideoeditor.com/' },
|
||||
{ image: '/sponsors/rve.png', name: 'React Video Editor', url: 'https://www.reactvideoeditor.com/' },
|
||||
{ image: '/sponsors/mux.jpg', name: 'Mux', url: 'https://www.mux.com/' },
|
||||
{ image: '/sponsors/jellypod.png', name: 'Jellypod', url: 'https://jellypod.ai/' },
|
||||
],
|
||||
individual: [
|
||||
{ image: 'https://avatars.githubusercontent.com/u/197597', name: 'Christopher Chedeau', url: 'https://github.com/vjeux' },
|
||||
{ image: 'https://avatars.githubusercontent.com/u/84167135', name: 'Memenome', url: 'https://github.com/memenome' },
|
||||
{ image: 'https://avatars.githubusercontent.com/u/5913254', name: 'Brandon McConnell', url: 'https://github.com/brandonmcconnell' },
|
||||
{ image: 'https://avatars.githubusercontent.com/u/9549394', name: 'studnitz', url: 'https://github.com/studnitz' },
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 8.7 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 8.6 KiB |
@@ -1,4 +0,0 @@
|
||||
<svg width="52" height="51" viewBox="0 0 52 51" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||
<path d="M25.1186 1.50109C23.3805 0.147773 21.1954 -0.253724 19.1203 0.397437L5.08373 4.80289C2.19204 5.71105 0.248047 8.44236 0.248047 11.6022V42.9894C0.248047 45.2558 1.24465 47.3313 2.98266 48.6825C4.72099 50.036 6.9084 50.4373 8.98111 49.7863L19.4595 46.4967C19.1318 45.366 18.9658 44.1862 18.9658 42.9894V28.735C18.9658 25.7417 20.0093 22.8829 21.9088 20.6899C23.303 19.0744 25.1116 17.8826 27.1329 17.2463L27.8534 17.0309V7.19654C27.8534 4.93006 26.8568 2.85458 25.1185 1.50109H25.1186Z" fill="#1C2AC5"/>
|
||||
<path d="M51.2479 24.3293V38.5836C51.2479 41.7411 49.3061 44.4749 46.4122 45.3806L32.3756 49.7862C31.7253 49.9917 31.0632 50.0922 30.4081 50.0922C28.9739 50.0922 27.5703 49.6125 26.3771 48.6824C25.832 48.2565 25.3595 47.7645 24.9711 47.2139C24.4986 46.553 24.1453 45.8164 23.923 45.0257C23.9206 45.0257 23.9184 45.0257 23.916 45.0281L23.9206 45.0232C23.7406 44.3746 23.6423 43.6917 23.6423 42.9893V28.7349C23.6423 27.2712 24.0588 25.8983 24.7935 24.7576C25.528 23.6169 26.5807 22.7066 27.8534 22.1656C28.057 22.08 28.2652 22.0015 28.4782 21.9356L30.1953 21.3971L42.515 17.53C44.5899 16.8788 46.7751 17.2803 48.5132 18.6338C50.2516 19.9873 51.248 22.0628 51.248 24.3293H51.2479Z" fill="#3E8AF5"/>
|
||||
</svg>
|
||||
|
Before Width: | Height: | Size: 1.3 KiB |
Generated
+6
-6
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "mediabunny",
|
||||
"version": "1.24.0",
|
||||
"version": "1.24.4",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "mediabunny",
|
||||
"version": "1.24.0",
|
||||
"version": "1.24.4",
|
||||
"license": "MPL-2.0",
|
||||
"workspaces": [
|
||||
"packages/*"
|
||||
@@ -7749,9 +7749,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/mediabunny": {
|
||||
"version": "1.23.0",
|
||||
"resolved": "https://registry.npmjs.org/mediabunny/-/mediabunny-1.23.0.tgz",
|
||||
"integrity": "sha512-mWdhhdRquePfTgZ+18DLCFWmqwO6oGP/YudmRU8Puh+whMUJ3VSb9KMAHPl2utim66ixKdZw+DbmdqBfBxRp0A==",
|
||||
"version": "1.24.3",
|
||||
"resolved": "https://registry.npmjs.org/mediabunny/-/mediabunny-1.24.3.tgz",
|
||||
"integrity": "sha512-uQu5lXg6c3z2YUKLimAsem1qYPdGfGziQ5CKaAMJAs0C0NNw5T/Zkn0foPML0gWqQJ5ZeC0wTgCxpiBQY8m1Tw==",
|
||||
"license": "MPL-2.0",
|
||||
"peer": true,
|
||||
"workspaces": [
|
||||
@@ -12242,7 +12242,7 @@
|
||||
},
|
||||
"packages/mp3-encoder": {
|
||||
"name": "@mediabunny/mp3-encoder",
|
||||
"version": "1.24.0",
|
||||
"version": "1.24.4",
|
||||
"license": "MPL-2.0",
|
||||
"devDependencies": {
|
||||
"@types/emscripten": "^1.40.1"
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "mediabunny",
|
||||
"author": "Vanilagy",
|
||||
"version": "1.24.0",
|
||||
"version": "1.24.4",
|
||||
"description": "Pure TypeScript media toolkit for reading, writing, and converting media files, directly in the browser.",
|
||||
"type": "module",
|
||||
"workspaces": [
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@mediabunny/mp3-encoder",
|
||||
"author": "Vanilagy",
|
||||
"version": "1.24.0",
|
||||
"version": "1.24.4",
|
||||
"description": "MP3 encoder extension for Mediabunny, based on LAME.",
|
||||
"main": "./dist/bundles/mediabunny-mp3-encoder.mjs",
|
||||
"module": "./dist/bundles/mediabunny-mp3-encoder.mjs",
|
||||
|
||||
+3
-3
@@ -186,7 +186,7 @@ export type ConversionVideoOptions = {
|
||||
*
|
||||
* This function can also be used to manually resize frames. When doing so, you should signal the post-process
|
||||
* dimensions using the `processedWidth` and `processedHeight` fields, which enables the encoder to better know what
|
||||
* to expect.
|
||||
* to expect. If these fields aren't set, Mediabunny will assume you won't perform any resizing.
|
||||
*/
|
||||
process?: (sample: VideoSample) => MaybePromise<
|
||||
CanvasImageSource | VideoSample | (CanvasImageSource | VideoSample)[] | null
|
||||
@@ -229,7 +229,8 @@ export type ConversionAudioOptions = {
|
||||
*
|
||||
* This function can also be used to manually perform remixing or resampling. When doing so, you should signal the
|
||||
* post-process parameters using the `processedNumberOfChannels` and `processedSampleRate` fields, which enables the
|
||||
* encoder to better know what to expect.
|
||||
* encoder to better know what to expect. If these fields aren't set, Mediabunny will assume you won't perform
|
||||
* remixing or resampling.
|
||||
*/
|
||||
process?: (sample: AudioSample) => MaybePromise<
|
||||
AudioSample | AudioSample[] | null
|
||||
@@ -1350,7 +1351,6 @@ export class Conversion {
|
||||
!encodableCodecs.some(codec => (NON_PCM_AUDIO_CODECS as readonly string[]).includes(codec))
|
||||
&& audioCodecs.some(codec => (NON_PCM_AUDIO_CODECS as readonly string[]).includes(codec))
|
||||
&& (numberOfChannels !== FALLBACK_NUMBER_OF_CHANNELS || sampleRate !== FALLBACK_SAMPLE_RATE)
|
||||
&& !trackOptions.process
|
||||
) {
|
||||
// We could not find a compatible non-PCM codec despite the container supporting them. This can be
|
||||
// caused by strange channel count or sample rate configurations. Therefore, let's try again but with
|
||||
|
||||
@@ -339,16 +339,34 @@ export class FlacDemuxer extends Demuxer {
|
||||
slice.skip(-2);
|
||||
const lengthIfNextFlacFrameHeaderIsLegit = slice.filePos - startPos;
|
||||
|
||||
const nextIsLegit = this.readFlacFrameHeader({
|
||||
const nextFrameHeader = this.readFlacFrameHeader({
|
||||
slice,
|
||||
isFirstPacket: false,
|
||||
});
|
||||
|
||||
if (!nextIsLegit) {
|
||||
if (!nextFrameHeader) {
|
||||
slice.skip(-1);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Ensure the frameOrSampleNum is consecutive.
|
||||
// https://github.com/Vanilagy/mediabunny/issues/194
|
||||
|
||||
if (this.blockingBit === 0) {
|
||||
// Case A: If the stream is fixed block size, this is the frame number, which increments by 1
|
||||
if (nextFrameHeader.num - frameHeader.num !== 1) {
|
||||
slice.skip(-1);
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
// Case B: If the stream is variable block size, this is the sample number, which increments by
|
||||
// amount of samples in a frame.
|
||||
if (nextFrameHeader.num - frameHeader.num !== frameHeader.blockSize) {
|
||||
slice.skip(-1);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
num: frameHeader.num,
|
||||
blockSize: frameHeader.blockSize,
|
||||
@@ -442,6 +460,11 @@ export class FlacDemuxer extends Demuxer {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (sampleRate !== this.audioInfo.sampleRate) {
|
||||
// This cannot be a valid FLAC frame, the sample rate is not the same as in the stream info
|
||||
return null;
|
||||
}
|
||||
|
||||
const size = slice.filePos - startOffset;
|
||||
const crc = readU8(slice);
|
||||
|
||||
|
||||
@@ -70,6 +70,11 @@ export const readDataBox = (slice: FileSlice) => {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (slice.remainingLength < 8) {
|
||||
// Box is too small
|
||||
return null;
|
||||
}
|
||||
|
||||
const typeIndicator = readU32Be(slice);
|
||||
slice.skip(4); // Locale indicator
|
||||
const data = readBytes(slice, header.contentSize - 8);
|
||||
|
||||
@@ -637,7 +637,10 @@ export class MatroskaDemuxer extends Demuxer {
|
||||
this.currentCluster = cluster;
|
||||
|
||||
if (dataSlice) {
|
||||
this.readContiguousElements(dataSlice);
|
||||
// Read the children of the cluster, stopping early at level 0 or 1 EBML elements. We do this because some
|
||||
// clusters have incorrect sizes that are too large
|
||||
const endPos = this.readContiguousElements(dataSlice, LEVEL_0_AND_1_EBML_IDS);
|
||||
cluster.elementEndPos = endPos;
|
||||
}
|
||||
|
||||
for (const [, trackData] of cluster.trackData) {
|
||||
@@ -913,24 +916,31 @@ export class MatroskaDemuxer extends Demuxer {
|
||||
}
|
||||
}
|
||||
|
||||
readContiguousElements(slice: FileSlice) {
|
||||
readContiguousElements(slice: FileSlice, stopIds?: number[]) {
|
||||
const startIndex = slice.filePos;
|
||||
|
||||
while (slice.filePos - startIndex <= slice.length - MIN_HEADER_SIZE) {
|
||||
const foundElement = this.traverseElement(slice);
|
||||
const startPos = slice.filePos;
|
||||
const foundElement = this.traverseElement(slice, stopIds);
|
||||
|
||||
if (!foundElement) {
|
||||
break;
|
||||
return startPos;
|
||||
}
|
||||
}
|
||||
|
||||
return slice.filePos;
|
||||
}
|
||||
|
||||
traverseElement(slice: FileSlice): boolean {
|
||||
traverseElement(slice: FileSlice, stopIds?: number[]): boolean {
|
||||
const header = readElementHeader(slice);
|
||||
if (!header) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (stopIds && stopIds.includes(header.id)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const { id, size } = header;
|
||||
const dataStartPos = slice.filePos;
|
||||
assertDefinedSize(size);
|
||||
@@ -2174,6 +2184,8 @@ abstract class MatroskaTrackBacking implements InputTrackBacking {
|
||||
|
||||
if (id === EBMLId.Cluster) {
|
||||
currentCluster = await demuxer.readCluster(elementStartPos, segment);
|
||||
// readCluster computes the proper size even if it's undefined in the header, so let's use that instead
|
||||
size = currentCluster.elementEndPos - dataStartPos;
|
||||
|
||||
const { blockIndex, correctBlockFound } = getMatchInCluster(currentCluster);
|
||||
if (correctBlockFound) {
|
||||
@@ -2190,44 +2202,37 @@ abstract class MatroskaTrackBacking implements InputTrackBacking {
|
||||
// Undefined element size (can happen in livestreamed files). In this case, we need to do some
|
||||
// searching to determine the actual size of the element.
|
||||
|
||||
if (id === EBMLId.Cluster) {
|
||||
// The cluster should have already computed its length, we can just copy that result
|
||||
assert(currentCluster);
|
||||
size = currentCluster.elementEndPos - dataStartPos;
|
||||
} else {
|
||||
// Search for the next element at level 0 or 1
|
||||
const nextElementPos = await searchForNextElementId(
|
||||
demuxer.reader,
|
||||
dataStartPos,
|
||||
LEVEL_0_AND_1_EBML_IDS,
|
||||
segment.elementEndPos,
|
||||
);
|
||||
assert(id !== EBMLId.Cluster); // Undefined cluster sizes are fixed further up
|
||||
|
||||
size = nextElementPos.pos - dataStartPos;
|
||||
}
|
||||
// Search for the next element at level 0 or 1
|
||||
const nextElementPos = await searchForNextElementId(
|
||||
demuxer.reader,
|
||||
dataStartPos,
|
||||
LEVEL_0_AND_1_EBML_IDS,
|
||||
segment.elementEndPos,
|
||||
);
|
||||
|
||||
const endPos = dataStartPos + size;
|
||||
if (segment.elementEndPos !== null && endPos > segment.elementEndPos - MIN_HEADER_SIZE) {
|
||||
// No more elements fit in this segment
|
||||
size = nextElementPos.pos - dataStartPos;
|
||||
}
|
||||
|
||||
const endPos = dataStartPos + size;
|
||||
if (segment.elementEndPos === null) {
|
||||
// Check the next element. If it's a new segment, we know this segment ends here. The new
|
||||
// segment is just ignored, since we're likely in a livestreamed file and thus only care about
|
||||
// the first segment.
|
||||
|
||||
let slice = demuxer.reader.requestSliceRange(endPos, MIN_HEADER_SIZE, MAX_HEADER_SIZE);
|
||||
if (slice instanceof Promise) slice = await slice;
|
||||
if (!slice) break;
|
||||
|
||||
const elementId = readElementId(slice);
|
||||
if (elementId === EBMLId.Segment) {
|
||||
segment.elementEndPos = endPos; // We now know the segment's size
|
||||
break;
|
||||
} else {
|
||||
// Check the next element. If it's a new segment, we know this segment ends here. The new
|
||||
// segment is just ignored, since we're likely in a livestreamed file and thus only care about
|
||||
// the first segment.
|
||||
|
||||
let slice = demuxer.reader.requestSliceRange(endPos, MIN_HEADER_SIZE, MAX_HEADER_SIZE);
|
||||
if (slice instanceof Promise) slice = await slice;
|
||||
if (!slice) break;
|
||||
|
||||
const elementId = readElementId(slice);
|
||||
if (elementId === EBMLId.Segment) {
|
||||
segment.elementEndPos = endPos;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
currentPos = dataStartPos + size;
|
||||
currentPos = endPos;
|
||||
}
|
||||
|
||||
// Catch faulty cue points
|
||||
|
||||
+8
-4
@@ -1159,7 +1159,7 @@ export class MediaStreamVideoTrackSource extends VideoSource {
|
||||
type: 'videoTrack',
|
||||
trackId: this._workerTrackId,
|
||||
track: this._track,
|
||||
}, [this._track]);
|
||||
});
|
||||
|
||||
this._workerListener = (event: MessageEvent) => {
|
||||
const message = event.data as MediaStreamTrackProcessorWorkerMessage;
|
||||
@@ -1978,17 +1978,19 @@ const mediaStreamTrackProcessorWorkerCode = () => {
|
||||
});
|
||||
|
||||
const abortControllers = new Map<number, AbortController>();
|
||||
const stoppedTracks = new Set<number>();
|
||||
const activeTracks = new Map<number, MediaStreamVideoTrack>();
|
||||
|
||||
self.addEventListener('message', (event) => {
|
||||
const message = event.data as MediaStreamTrackProcessorControllerMessage;
|
||||
|
||||
switch (message.type) {
|
||||
case 'videoTrack': {
|
||||
activeTracks.set(message.trackId, message.track);
|
||||
|
||||
const processor = new MediaStreamTrackProcessor({ track: message.track });
|
||||
const consumer = new WritableStream<VideoFrame>({
|
||||
write: (videoFrame) => {
|
||||
if (stoppedTracks.has(message.trackId)) {
|
||||
if (!activeTracks.has(message.trackId)) {
|
||||
videoFrame.close();
|
||||
return;
|
||||
}
|
||||
@@ -2026,7 +2028,9 @@ const mediaStreamTrackProcessorWorkerCode = () => {
|
||||
abortControllers.delete(message.trackId);
|
||||
}
|
||||
|
||||
stoppedTracks.add(message.trackId);
|
||||
const track = activeTracks.get(message.trackId);
|
||||
track?.stop();
|
||||
activeTracks.delete(message.trackId);
|
||||
|
||||
sendMessage({
|
||||
type: 'trackStopped',
|
||||
|
||||
+30
-6
@@ -234,11 +234,19 @@ export class BlobSource extends Source {
|
||||
break;
|
||||
}
|
||||
|
||||
if (worker.aborted) {
|
||||
break;
|
||||
}
|
||||
|
||||
this.onread?.(worker.currentPos, worker.currentPos + value.length);
|
||||
this._orchestrator.supplyWorkerData(worker, value);
|
||||
} else {
|
||||
const data = await this._blob.slice(worker.currentPos, worker.targetPos).arrayBuffer();
|
||||
|
||||
if (worker.aborted) {
|
||||
break;
|
||||
}
|
||||
|
||||
this.onread?.(worker.currentPos, worker.currentPos + data.byteLength);
|
||||
this._orchestrator.supplyWorkerData(worker, new Uint8Array(data));
|
||||
}
|
||||
@@ -457,7 +465,7 @@ export class UrlSource extends Source {
|
||||
/** @internal */
|
||||
private async _runWorker(worker: ReadWorker) {
|
||||
// The outer loop is for resuming a request if it dies mid-response
|
||||
while (!worker.aborted) {
|
||||
while (true) {
|
||||
const existing = this._existingResponses.get(worker);
|
||||
this._existingResponses.delete(worker);
|
||||
|
||||
@@ -534,6 +542,10 @@ export class UrlSource extends Source {
|
||||
}
|
||||
}
|
||||
|
||||
if (worker.aborted) {
|
||||
break;
|
||||
}
|
||||
|
||||
const { done, value } = readResult;
|
||||
|
||||
if (done) {
|
||||
@@ -552,6 +564,10 @@ export class UrlSource extends Source {
|
||||
this.onread?.(worker.currentPos, worker.currentPos + value.length);
|
||||
this._orchestrator.supplyWorkerData(worker, value);
|
||||
}
|
||||
|
||||
if (worker.aborted) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
worker.running = false;
|
||||
@@ -796,6 +812,10 @@ export class StreamSource extends Source {
|
||||
let data = this._options.read(worker.currentPos, originalTargetPos);
|
||||
if (data instanceof Promise) data = await data;
|
||||
|
||||
if (worker.aborted) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (data instanceof Uint8Array) {
|
||||
data = toUint8Array(data); // Normalize things like Node.js Buffer to Uint8Array
|
||||
|
||||
@@ -833,6 +853,10 @@ export class StreamSource extends Source {
|
||||
throw new TypeError('ReadableStream returned by options.read must yield Uint8Array chunks.');
|
||||
}
|
||||
|
||||
if (worker.aborted) {
|
||||
break;
|
||||
}
|
||||
|
||||
const data = toUint8Array(value); // Normalize things like Node.js Buffer to Uint8Array
|
||||
|
||||
this.onread?.(worker.currentPos, worker.currentPos + data.length);
|
||||
@@ -1420,7 +1444,10 @@ class ReadOrchestrator {
|
||||
currentPos: startPos,
|
||||
targetPos,
|
||||
running: false,
|
||||
aborted: false,
|
||||
// Due to async shenanigans, it can happen that workers are started after disposal. In this case, instead of
|
||||
// simply not creating the worker, we allow it to run but immediately label it as aborted, so it can then
|
||||
// shut itself down.
|
||||
aborted: this.disposed,
|
||||
pendingSlices: [],
|
||||
age: this.nextAge++,
|
||||
};
|
||||
@@ -1473,10 +1500,7 @@ class ReadOrchestrator {
|
||||
|
||||
/** Called by a worker when it has read some data. */
|
||||
supplyWorkerData(worker: ReadWorker, bytes: Uint8Array) {
|
||||
if (this.disposed) {
|
||||
// Writes may still come in after disposal, but we just ignore those
|
||||
return;
|
||||
}
|
||||
assert(!worker.aborted);
|
||||
|
||||
const start = worker.currentPos;
|
||||
const end = start + bytes.length;
|
||||
|
||||
@@ -139,17 +139,19 @@ test('Can encode transparent video', async () => {
|
||||
probeContext.drawImage(video, 0, 0);
|
||||
|
||||
let imageData = probeContext.getImageData(0, 0, probeCanvas.width, probeCanvas.height);
|
||||
expect(imageData.data[3]).lessThanOrEqual(2); // Transparent (within error)
|
||||
const pos1 = { x: 100, y: 100 };
|
||||
const index1 = (pos1.x + pos1.y * probeCanvas.width) * 4;
|
||||
expect(imageData.data[index1 + 3]).toBe(0); // Transparent
|
||||
|
||||
const pos = { x: 300, y: 300 }; // Dead center in the red square
|
||||
const index = (pos.x + pos.y * probeCanvas.width) * 4;
|
||||
const pos2 = { x: 300, y: 300 }; // Dead center in the red square
|
||||
const index2 = (pos2.x + pos2.y * probeCanvas.width) * 4;
|
||||
|
||||
// Red (within error)
|
||||
expect(imageData.data[index + 0]).greaterThanOrEqual(253);
|
||||
expect(imageData.data[index + 1]).lessThanOrEqual(2);
|
||||
expect(imageData.data[index + 2]).lessThanOrEqual(2);
|
||||
expect(imageData.data[index2 + 0]).greaterThanOrEqual(253);
|
||||
expect(imageData.data[index2 + 1]).lessThanOrEqual(3);
|
||||
expect(imageData.data[index2 + 2]).lessThanOrEqual(3);
|
||||
|
||||
expect(imageData.data[index + 3]).greaterThanOrEqual(253); // Opaque (within error)
|
||||
expect(imageData.data[index2 + 3]).toBe(255); // Opaque
|
||||
|
||||
// Let's also check it's read correctly by Mediabunny
|
||||
using input = new Input({
|
||||
@@ -169,7 +171,8 @@ test('Can encode transparent video', async () => {
|
||||
firstSample.draw(probeContext, 0, 0);
|
||||
|
||||
imageData = probeContext.getImageData(0, 0, probeCanvas.width, probeCanvas.height);
|
||||
expect(imageData.data[3]).lessThanOrEqual(2); // Transparent (within error)
|
||||
|
||||
expect(imageData.data[3]).toBe(0); // Transparent
|
||||
});
|
||||
|
||||
test('Can encode video with alternating transparency', async () => {
|
||||
|
||||
Reference in New Issue
Block a user