Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add AC-3 and EC-3 support in Mp4Generator #5235

Merged
merged 1 commit into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/mss/mss_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@ shaka.mss.MssParser = class {
timescale: stream.mssPrivateData.timescale,
duration: stream.mssPrivateData.duration,
videoNalus: videoNalus,
audioConfig: new Uint8Array([]),
data: null, // Data is not necessary for init segement.
stream: stream,
};
Expand Down
1 change: 1 addition & 0 deletions lib/transmuxer/mp3_transmuxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ shaka.transmuxer.Mp3Transmuxer = class {
timescale: sampleRate,
duration: duration,
videoNalus: [],
audioConfig: new Uint8Array([]),
data: {
sequenceNumber: this.frameIndex_,
baseMediaDecodeTime: baseMediaDecodeTime,
Expand Down
61 changes: 60 additions & 1 deletion lib/util/mp4_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ shaka.util.Mp4Generator = class {
/** @private {!Array.<string>} */
this.videoNalus_ = streamInfo.videoNalus;

/** @private {!Uint8Array} */
this.audioConfig_ = streamInfo.audioConfig;

/** @private {number} */
this.sequenceNumber_ = 0;

Expand Down Expand Up @@ -310,6 +313,10 @@ shaka.util.Mp4Generator = class {
this.stream_.codecs.includes('mp3') ||
this.stream_.codecs.includes('mp4a.40.34')) {
bytes = this.mp3_();
} else if (this.stream_.codecs.includes('ac-3')) {
bytes = this.ac3_();
} else if (this.stream_.codecs.includes('ec-3')) {
bytes = this.ec3_();
} else {
bytes = this.mp4a_();
}
Expand Down Expand Up @@ -471,6 +478,50 @@ shaka.util.Mp4Generator = class {
return Mp4Generator.box('.mp3', this.audioStsd_());
}

/**
* Generate a AC-3 box
*
* @return {!Uint8Array}
* @private
*/
ac3_() {
const Mp4Generator = shaka.util.Mp4Generator;
const dac3Box = Mp4Generator.box('dac3', this.audioConfig_);

let sinfBox = new Uint8Array([]);
if (this.stream_.encrypted) {
sinfBox = this.sinf_();
}

let boxName = 'ac-3';
if (this.stream_.encrypted) {
boxName = 'enca';
}
return Mp4Generator.box(boxName, this.audioStsd_(), dac3Box, sinfBox);
}

/**
* Generate a EC-3 box
*
* @return {!Uint8Array}
* @private
*/
ec3_() {
const Mp4Generator = shaka.util.Mp4Generator;
const dec3Box = Mp4Generator.box('dec3', this.audioConfig_);

let sinfBox = new Uint8Array([]);
if (this.stream_.encrypted) {
sinfBox = this.sinf_();
}

let boxName = 'ec-3';
if (this.stream_.encrypted) {
boxName = 'enca';
}
return Mp4Generator.box(boxName, this.audioStsd_(), dec3Box, sinfBox);
}

/**
* Generate a MP4A box
*
Expand All @@ -479,7 +530,12 @@ shaka.util.Mp4Generator = class {
*/
mp4a_() {
const Mp4Generator = shaka.util.Mp4Generator;
const esdsBox = Mp4Generator.box('esds', this.esds_());
let esdsBox;
if (this.audioConfig_.byteLength > 0) {
esdsBox = Mp4Generator.box('esds', this.audioConfig_);
} else {
esdsBox = Mp4Generator.box('esds', this.esds_());
}

let sinfBox = new Uint8Array([]);
if (this.stream_.encrypted) {
Expand Down Expand Up @@ -1147,6 +1203,7 @@ shaka.util.Mp4Generator.DINF_ = new Uint8Array([]);
* timescale: number,
* duration: number,
* videoNalus: !Array.<string>,
* audioConfig: !Uint8Array,
* data: ?shaka.util.Mp4Generator.Data,
* stream: !shaka.extern.Stream
* }}
Expand All @@ -1157,6 +1214,8 @@ shaka.util.Mp4Generator.DINF_ = new Uint8Array([]);
* The Stream's duration.
* @property {!Array.<string>} videoNalus
* The stream's video nalus.
* @property {!Uint8Array} audioConfig
* The stream's audio config.
* @property {?shaka.util.Mp4Generator.Data} data
* The stream's data.
* @property {!shaka.extern.Stream} stream
Expand Down