Skip to content

Commit

Permalink
add cmaf id3 in emsg support for hls.js
Browse files Browse the repository at this point in the history
  • Loading branch information
monyone committed Jul 2, 2021
1 parent c36cf9f commit f361dd9
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/remux/passthrough-remuxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getStartDTS,
offsetStartDTS,
parseInitSegment,
parseId3TrackSamples,
} from '../utils/mp4-tools';
import { ElementaryStreamTypes } from '../loader/fragment';
import { logger } from '../utils/logger';
Expand Down Expand Up @@ -199,6 +200,28 @@ class PassThroughRemuxer implements Remuxer {
dropped: 0,
};

if (this.initPTS != null) {
const samples = parseId3TrackSamples(data);

for (let index = 0; index < samples.length; index++) {
const sample = samples[index];
if (sample == null) {
continue;
}
if (sample.timescale == null) {
continue;
}

sample.pts = sample.pts / sample.timescale - this.initPTS;
sample.dts = sample.dts / sample.timescale - this.initPTS;
sample.duration =
sample.duration != null
? sample.duration / sample.timescale
: undefined;
id3Track.samples.push(sample);
}
}

result.audio = track.type === 'audio' ? track : undefined;
result.video = track.type !== 'audio' ? track : undefined;
result.text = textTrack;
Expand Down
69 changes: 69 additions & 0 deletions src/utils/mp4-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,26 @@ type Mp4BoxData = {

const UINT32_MAX = Math.pow(2, 32) - 1;
const push = [].push;
const ID3_SCHEME_ID_URIS = [
'https://aomedia.org/emsg/ID3',
'https://developer.apple.com/streaming/emsg-id3',
];

export function bin2str(data: Uint8Array): string {
return String.fromCharCode.apply(null, data);
}

export function readNullTerminatedString(buffer, offset): string {
let i = offset;

while (String.fromCharCode(buffer[i]) !== '\0' && i < buffer.byteLength) {
i++;
}

const val = new Uint8Array(buffer.subarray(offset, i));
return bin2str(val);
}

export function readUint16(
buffer: Uint8Array | Mp4BoxData,
offset: number
Expand Down Expand Up @@ -112,6 +127,60 @@ export function findBox(
return results;
}

export function parseId3TrackSamples(data) {
const emsgs = findBox(data, ['emsg']);
return emsgs.map((emsg) => {
try {
const data = emsg.data.subarray(emsg.start, emsg.end);
let offset = 0;

const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
const version = view.getUint8(offset);
if (version !== 1) {
return undefined;
}

// skip over 3 bytes of flags
offset += 4;
const timescale = view.getUint32(offset);
offset += 4;
const presentationTime = Number(view.getBigUint64(offset));
offset += 8;
const eventDuration = view.getUint32(offset);
offset += 4;
const id = view.getUint32(offset);
offset += 4;
const schemeIdUri = readNullTerminatedString(data, offset);
if (!ID3_SCHEME_ID_URIS.includes(schemeIdUri)) {
return undefined;
}

// skip over the null byte
offset += schemeIdUri.length + 1;
const value = readNullTerminatedString(data, offset);
// skip over the null byte
offset += value.length + 1;
// the rest is id3 payload
const messageData = new Uint8Array(
data.subarray(offset, data.byteLength)
);

return {
timescale,
pts: presentationTime,
dts: presentationTime,
duration: eventDuration !== 0xffffffff ? eventDuration : undefined,
id,
schemeIdUri,
value,
data: messageData,
};
} catch (e) {
return undefined;
}
});
}

type SidxInfo = {
earliestPresentationTime: number;
timescale: number;
Expand Down

0 comments on commit f361dd9

Please sign in to comment.