Skip to content

Commit

Permalink
Merge pull request Streamedian#110 from Shepherdog/master
Browse files Browse the repository at this point in the history
Aggregate multi-slices which belong to one frame
  • Loading branch information
MakdDevelop authored Jan 18, 2019
2 parents 8c02a61 + 8332ce1 commit 0097fc7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/core/remuxer/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ export class BaseRemuxer {
return (a.dts-b.dts);
}

static groupByDts(gop) {
const groupBy = (xs, key) => {
return xs.reduce((rv, x) => {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
return groupBy(gop, 'dts');
}

getPayloadBase(sampleFunction, setupSample) {
if (!this.readyToDecode || !this.initialized || !this.samples.length) return null;
this.samples.sort(BaseRemuxer.dtsSortFunc);
Expand Down
14 changes: 14 additions & 0 deletions src/core/remuxer/h264.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ export class H264Remuxer extends BaseRemuxer {
remux(nalu) {
if (this.lastGopDTS < nalu.dts) {
this.gop.sort(BaseRemuxer.dtsSortFunc);

if (this.gop.length > 1) {
// Aggregate multi-slices which belong to one frame
const groupedGop = BaseRemuxer.groupByDts(this.gop);
this.gop = Object.values(groupedGop).map(group => {
return group.reduce((preUnit, curUnit) => {
const naluData = curUnit.getData();
naluData.set(new Uint8Array([0x0, 0x0, 0x0, 0x1]));
preUnit.appendData(naluData);
return preUnit;
});
});
}

for (let unit of this.gop) {
// if (this.firstUnit) {
// unit.ntype = 5;//NALU.IDR;
Expand Down

0 comments on commit 0097fc7

Please sign in to comment.