-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: mp4box demuxer back, remove flush on sample, which caused mem leak
- Loading branch information
Showing
4 changed files
with
8,474 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
Copyright (c) 2012. Telecom ParisTech/TSI/MM/GPAC Cyril Concolato | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above copyright | ||
notice, this list of conditions and the following disclaimer in the | ||
documentation and/or other materials provided with the distribution. | ||
* Neither the name of the copyright holder nor the | ||
names of its contributors may be used to endorse or promote products | ||
derived from this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY | ||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import {Status} from "../../context/controllers/timeline/types.js" | ||
import MP4Box, {MP4File, MP4Info, MP4MediaTrack} from "./mp4box.adapter.js" | ||
|
||
type SetStatus = (status: Status, message: string) => void | ||
type OnChunk = (chunk: EncodedVideoChunk) => void | ||
type OnConfig = (config: VideoDecoderConfig) => void | ||
|
||
export class MP4FileSink { | ||
#setStatus: SetStatus | ||
#file: MP4File | ||
#offset = 0 | ||
|
||
constructor(file: MP4File, setStatus: SetStatus) { | ||
this.#file = file | ||
this.#setStatus = setStatus | ||
} | ||
|
||
write(chunk: ArrayBuffer) { | ||
const buffer = new ArrayBuffer(chunk.byteLength) | ||
//@ts-ignore | ||
new Uint8Array(buffer).set(chunk) | ||
//@ts-ignore | ||
buffer.fileStart = this.#offset | ||
this.#offset += buffer.byteLength | ||
this.#setStatus("fetch", (this.#offset / (1024 ** 2)).toFixed(1) + " MiB")! | ||
//@ts-ignore | ||
this.#file!.appendBuffer(buffer) | ||
} | ||
|
||
close() { | ||
this.#setStatus("fetch", "Done") | ||
this.#file.flush() | ||
} | ||
} | ||
|
||
export class MP4Demuxer { | ||
#onConfig: OnConfig | ||
#onChunk: OnChunk | ||
#setStatus: SetStatus | ||
#file: MP4File | ||
|
||
constructor(file: File, {onConfig, onChunk, setStatus}: {onConfig: OnConfig, onChunk: OnChunk, setStatus: SetStatus}) { | ||
this.#onConfig = onConfig | ||
this.#onChunk = onChunk | ||
this.#setStatus = setStatus | ||
|
||
this.#file = MP4Box.createFile() | ||
this.#file.onError = error => setStatus("demux", error) | ||
this.#file.onReady = this.#onReady.bind(this) | ||
this.#file.onSamples = this.#onSamples.bind(this) | ||
const fileSink = new MP4FileSink(this.#file, setStatus) | ||
file.stream().pipeTo(new WritableStream(fileSink, {highWaterMark: 1})) | ||
} | ||
|
||
#description(track: MP4MediaTrack) { | ||
const trak = this.#file.getTrackById(track.id) | ||
for (const entry of trak.mdia!.minf!.stbl!.stsd!.entries) { | ||
//@ts-ignore | ||
const box = entry.avcC || entry.hvcC || entry.vpcC || entry.av1C | ||
if (box) { | ||
//@ts-ignore | ||
const stream = new MP4Box.DataStream(undefined, 0, MP4Box.DataStream.BIG_ENDIAN) | ||
box.write(stream) | ||
return new Uint8Array(stream.buffer!, 8) // Remove the box header. | ||
} | ||
} | ||
throw new Error("avcC, hvcC, vpcC, or av1C box not found") | ||
} | ||
|
||
#onReady(info: MP4Info) { | ||
this.#setStatus("demux", "Ready") | ||
const track = info.videoTracks[0] | ||
this.#onConfig({ | ||
codec: track.codec.startsWith('vp08') ? 'vp8' : track.codec, | ||
codedHeight: track.video.height, | ||
codedWidth: track.video.width, | ||
description: this.#description(track), | ||
}); | ||
|
||
this.#file.setExtractionOptions(track.id) | ||
this.#file.start() | ||
} | ||
|
||
#onSamples(track_id: number, ref: any, samples: any) { | ||
for (const sample of samples) { | ||
this.#onChunk(new EncodedVideoChunk({ | ||
type: sample.is_sync ? "key" : "delta", | ||
timestamp: 1e6 * sample.cts / sample.timescale, | ||
duration: 1e6 * sample.duration / sample.timescale, | ||
data: sample.data | ||
})) | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// @ts-ignore | ||
import * as bundle from "./mp4box.js" | ||
|
||
export interface MP4MediaTrack { | ||
id: number; | ||
created: Date; | ||
modified: Date; | ||
movie_duration: number; | ||
layer: number; | ||
alternate_group: number; | ||
volume: number; | ||
track_width: number; | ||
track_height: number; | ||
timescale: number; | ||
duration: number; | ||
bitrate: number; | ||
codec: string; | ||
language: string; | ||
nb_samples: number; | ||
type: string; | ||
} | ||
|
||
export interface MP4VideoData { | ||
width: number; | ||
height: number; | ||
} | ||
|
||
export interface MP4VideoTrack extends MP4MediaTrack { | ||
video: MP4VideoData; | ||
} | ||
export interface MP4AudioTrack extends MP4MediaTrack { | ||
audio: MP4AudioData; | ||
} | ||
|
||
export interface MP4Info { | ||
duration: number; | ||
timescale: number; | ||
fragment_duration: number; | ||
isFragmented: boolean; | ||
isProgressive: boolean; | ||
hasIOD: boolean; | ||
brands: string[]; | ||
created: Date; | ||
modified: Date; | ||
tracks: MP4Track[]; | ||
audioTracks: MP4AudioTrack[]; | ||
videoTracks: MP4VideoTrack[]; | ||
} | ||
|
||
export interface MP4AudioData { | ||
sample_rate: number; | ||
channel_count: number; | ||
sample_size: number; | ||
} | ||
|
||
export interface Moov { | ||
type: string; | ||
hdr_size: Number; | ||
size: Number; | ||
start: Number; | ||
traks: any; | ||
uuid: String | undefined; | ||
} | ||
|
||
export interface MP4File { | ||
onMoovStart?: () => void; | ||
onReady?: (info: MP4Info) => void; | ||
onError?: (e: string) => void; | ||
onSamples?: (track_id: number, _ref: any, samples: any) => void; | ||
setExtractionOptions: (id: number) => void; | ||
|
||
moov: Moov; | ||
|
||
appendBuffer(data: MP4ArrayBuffer): number; | ||
start(): void; | ||
stop(): void; | ||
flush(): void; | ||
getTrackById: (id: number) => Trak | ||
} | ||
|
||
type MP4Track = MP4VideoTrack | MP4AudioTrack; | ||
|
||
export interface MP4Box { | ||
createFile(): MP4File; | ||
DataStream: new (arrayBuffer: ArrayBuffer, byteOffset: number, endianness: boolean) => XDataStream | ||
} | ||
|
||
export type MP4ArrayBuffer = ArrayBuffer & {fileStart: number}; | ||
|
||
|
||
export interface TrackOptions { | ||
timescale: number; | ||
width: number; | ||
height: number; | ||
nb_samples: number; | ||
avcDecoderConfigRecord: AllowSharedBufferSource | undefined; | ||
} | ||
|
||
export interface SampleOptions { | ||
duration: number; | ||
dts: number; | ||
cts: number; | ||
is_sync: boolean; | ||
} | ||
|
||
export interface MP4File { | ||
onMoovStart?: () => void; | ||
onReady?: (info: MP4Info) => void; | ||
onError?: (e: string) => void; | ||
|
||
appendBuffer(data: MP4ArrayBuffer): number; | ||
start(): void; | ||
stop(): void; | ||
flush(): void; | ||
addTrack(trackOptions: TrackOptions): MP4Track; | ||
addSample(track: MP4Track, data: ArrayBuffer, sampleOptions: SampleOptions):void; | ||
save(file: string):void; | ||
} | ||
|
||
export class XDataStream { | ||
static BIG_ENDIAN: boolean; | ||
static LITTLE_ENDIAN: boolean; | ||
buffer?: ArrayBuffer; | ||
constructor(arrayBuffer: ArrayBuffer, byteOffset: number, endianness: boolean) {} | ||
// TODO: Complete interface | ||
} | ||
|
||
export interface Trak { | ||
mdia?: { | ||
minf?: { | ||
stbl?: { | ||
stsd?: { | ||
entries: { | ||
avcC?: { | ||
write: (stream: XDataStream) => void | ||
} | ||
hvcC?: { | ||
write: (stream: XDataStream) => void | ||
} | ||
}[] | ||
} | ||
} | ||
} | ||
} | ||
// TODO: Complete interface | ||
} | ||
export default bundle.mp4box as MP4Box; | ||
export const DataStream = bundle.DataStream as XDataStream |
Oops, something went wrong.