Skip to content

Commit

Permalink
Merge pull request #1 from Eyevinn/feature/merge-ads
Browse files Browse the repository at this point in the history
Handle that two ads in a row can be merged into one break
  • Loading branch information
birme authored Apr 14, 2020
2 parents bf0bf6a + 571ff13 commit bb4d410
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
21 changes: 21 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class HLSSpliceVod {
this.playlists = {};
this.baseUrl = null;
this.targetDuration = 0;
this.mergeBreaks = false; // Merge ad breaks at the same position into one single break
if (options && options.baseUrl) {
this.baseUrl = options.baseUrl;
}
Expand All @@ -34,6 +35,9 @@ class HLSSpliceVod {
}

}
if (options && options.merge) {
this.mergeBreaks = true;
}
}

/**
Expand Down Expand Up @@ -124,6 +128,23 @@ class HLSSpliceVod {
this.playlists[bw].addPlaylistItem({ 'cuein': true });
}
this.playlists[bw].set('targetDuration', this.targetDuration);
if (this.mergeBreaks) {
let adBreakDuration = 0;
let itemToUpdate = null;
for (let i = 0; i < this.playlists[bw].items.PlaylistItem.length; i++) {
if (this.playlists[bw].items.PlaylistItem[i].get('cueout') && this.playlists[bw].items.PlaylistItem[i].get('cuein')) {
adBreakDuration += this.playlists[bw].items.PlaylistItem[i].get('cueout');
this.playlists[bw].items.PlaylistItem[i].set('cueout', null);
this.playlists[bw].items.PlaylistItem[i].set('cuein', false);
} else if (this.playlists[bw].items.PlaylistItem[i].get('cueout') && !this.playlists[bw].items.PlaylistItem[i].get('cuein')) {
adBreakDuration = 0;
itemToUpdate = this.playlists[bw].items.PlaylistItem[i];
} else if (!this.playlists[bw].items.PlaylistItem[i].get('cueout') && this.playlists[bw].items.PlaylistItem[i].get('cuein')) {
const cueOut = itemToUpdate.get('cueout');
itemToUpdate.set('cueout', cueOut + adBreakDuration);
}
}
}
}
//console.log(this.playlists[bandwidths[0]].toString());
resolve();
Expand Down
38 changes: 38 additions & 0 deletions spec/hls_splice_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,44 @@ describe("HLSSpliceVod", () => {
});
});

it("handles two ads in a row merged into one break", done => {
const mockVod = new HLSSpliceVod('http://mock.com/mock.m3u8', { merge: true });
mockVod.load(mockMasterManifest, mockMediaManifest)
.then(() => {
return mockVod.insertAdAt(0, 'http://mock.com/ad/mockad.m3u8', mockAdMasterManifest, mockAdMediaManifest);
})
.then(() => {
// This one will go first
return mockVod.insertAdAt(0, 'http://mock.com/ad/mockad.m3u8', mockAdMasterManifest3, mockAdMediaManifest3);
})
.then(() => {
const m3u8 = mockVod.getMediaManifest(4497000);
const lines = m3u8.split('\n');
expect(lines[8]).toEqual("#EXT-X-CUE-OUT:DURATION=18");
done();
});
});

it("handles two ads that should not be merged into one break", done => {
const mockVod = new HLSSpliceVod('http://mock.com/mock.m3u8', { merge: true });
mockVod.load(mockMasterManifest, mockMediaManifest)
.then(() => {
return mockVod.insertAdAt(9, 'http://mock.com/ad/mockad.m3u8', mockAdMasterManifest, mockAdMediaManifest);
})
.then(() => {
// This one will go first
return mockVod.insertAdAt(0, 'http://mock.com/ad/mockad.m3u8', mockAdMasterManifest3, mockAdMediaManifest3);
})
.then(() => {
const m3u8 = mockVod.getMediaManifest(4497000);
const lines = m3u8.split('\n');
expect(lines[8]).toEqual("#EXT-X-CUE-OUT:DURATION=3");
expect(lines[16]).toEqual("#EXT-X-CUE-OUT:DURATION=15");
done();
});
});


it("handles post-roll ads", done => {
const mockVod = new HLSSpliceVod('http://mock.com/mock.m3u8');
mockVod.load(mockMasterManifest, mockMediaManifest)
Expand Down

0 comments on commit bb4d410

Please sign in to comment.