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

User hooks for hls parser #1998

Closed
Closed
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 AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Google Inc. <*@google.com>
Edgeware AB <*@edgeware.tv>
Gil Gonen <gil.gonen@gmail.com>
Giuseppe Samela <giuseppe.samela@gmail.com>
Hotstar <*@hotstar.com>
Itay Kinnrot <Itay.Kinnrot@Kaltura.com>
Jason Palmer <jason@jason-palmer.com>
Jesper Haug Karsrud <jesper.karsrud@gmail.com>
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Aaron Vaage <vaage@google.com>
Alvaro Velad Galvan <alvaro.velad@mirada.tv>
Andy Hochhaus <ahochhaus@samegoal.com>
Anurag Kalia <anurag.kalia@hotstar.com>
Benjamin Wallberg <benjamin.wallberg@bonnierbroadcasting.com>
Boris Cupac <borisrt2309@gmail.com>
Bryan Huh <bhh1988@gmail.com>
Expand Down
3 changes: 3 additions & 0 deletions externs/shaka/manifest_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ shaka.extern.ManifestParser = function() {};
* filterNewPeriod: function(shaka.extern.Period),
* filterAllPeriods: function(!Array.<!shaka.extern.Period>),
* onTimelineRegionAdded: function(shaka.extern.TimelineRegionInfo),
* onSegmentParsed: function(string, number, *),
* onEvent: function(!Event),
* onError: function(!shaka.util.Error)
* }}
Expand All @@ -77,6 +78,8 @@ shaka.extern.ManifestParser = function() {};
* Should be called on all Periods so that they can be filtered.
* @property {function(shaka.extern.TimelineRegionInfo)} onTimelineRegionAdded
* Should be called when a new timeline region is added.
* @property {function(string, number, *)} onSegmentParsed
* Should be called with segment metadata when a new segment is parsed.
* @property {function(!Event)} onEvent
* Should be called to raise events.
* @property {function(!shaka.util.Error)} onError
Expand Down
44 changes: 39 additions & 5 deletions lib/hls/hls_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ shaka.hls.HlsParser = class {
playlist,
startPosition,
stream.mimeType,
stream.codecs);
stream.codecs,
stream.id,
streamInfo.segmentIndex.getAll());

streamInfo.segmentIndex.replace(segments);

Expand Down Expand Up @@ -1167,8 +1169,11 @@ shaka.hls.HlsParser = class {

const startPosition = mediaSequenceTag ? Number(mediaSequenceTag.value) : 0;

const nextStreamId = this.globalId_++;

const segments = await this.createSegments_(
verbatimMediaPlaylistUri, playlist, startPosition, mimeType, codecs);
verbatimMediaPlaylistUri, playlist, startPosition, mimeType, codecs,
nextStreamId, []);

const minTimestamp = segments[0].startTime;
const lastEndTime = segments[segments.length - 1].endTime;
Expand All @@ -1186,7 +1191,7 @@ shaka.hls.HlsParser = class {

/** @type {shaka.extern.Stream} */
const stream = {
id: this.globalId_++,
id: nextStreamId,
originalId: name,
createSegmentIndex: () => Promise.resolve(),
findSegmentPosition: (i) => segmentIndex.find(i),
Expand Down Expand Up @@ -1435,16 +1440,34 @@ shaka.hls.HlsParser = class {
* @param {number} startPosition
* @param {string} mimeType
* @param {string} codecs
* @param {number} streamId
* @param {Array.<!shaka.media.SegmentReference>} existingSegmentRefs
* @return {!Promise<!Array.<!shaka.media.SegmentReference>>}
* @private
*/
async createSegments_(
verbatimMediaPlaylistUri, playlist, startPosition, mimeType, codecs) {
verbatimMediaPlaylistUri, playlist, startPosition, mimeType, codecs,
streamId, existingSegmentRefs) {
/** @type {Array.<!shaka.hls.Segment>} */
const hlsSegments = playlist.segments;
/** @type {!Array.<!shaka.media.SegmentReference>} */
const references = [];

/**
* Number of segment references in both the last and the current playlist.
* @type {number}
*/
let reappearingRefsCount = 0;

if (existingSegmentRefs.length > 0) {
const earlierStartPosition = existingSegmentRefs[0].getPosition();
/**
* Number of segment references in the last but not the current playlist
* (in HLS, segments are dropped only from the beginning of the playlist).
* @type {number}
*/
const droppedRefsCount = startPosition - earlierStartPosition;
reappearingRefsCount = existingSegmentRefs.length - droppedRefsCount;
}
goog.asserts.assert(hlsSegments.length, 'Playlist should have segments!');
// We may need to look at the media itself to determine a segment start
// time.
Expand All @@ -1463,12 +1486,23 @@ shaka.hls.HlsParser = class {
initSegmentRef, firstSegmentRef, mimeType, codecs);
shaka.log.debug('First segment', firstSegmentUri.split('/').pop(),
'starts at', firstStartTime);

// Append brand-new segments
for (let i = 0; i < hlsSegments.length; ++i) {
const hlsSegment = hlsSegments[i];
const previousReference = references[references.length - 1];
const startTime = (i == 0) ? firstStartTime : previousReference.endTime;
const position = startPosition + i;

if (i >= reappearingRefsCount) {
// Segment references which appeared only in the current playlist:
// Fire 'segmentparsed' event for them.
this.playerInterface_.onSegmentParsed(
'application/vnd.apple.mpegurl',
streamId,
hlsSegment);
}

const reference = this.createSegmentReference_(
playlist,
previousReference,
Expand Down
10 changes: 10 additions & 0 deletions lib/media/segment_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ shaka.media.SegmentIndex = class {
return this.references_[index];
}

/**
* Get all the SegmentReferences
*
* @return {Array.<!shaka.media.SegmentReference>} existing segment references
* @export
*/
getAll() {
return this.references_;
}


/**
* Offset all segment references by a fixed amount.
Expand Down
1 change: 1 addition & 0 deletions lib/offline/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,7 @@ shaka.offline.Storage = class {
filterNewPeriod: () => {},

onTimelineRegionAdded: () => {},
onSegmentParsed: () => {},
onEvent: () => {},

// Used to capture an error from the manifest parser. We will check the
Expand Down
40 changes: 40 additions & 0 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,21 @@ goog.require('shaka.util.StreamUtils');
* @exportDoc
*/

/**
* @event shaka.Player.SegmentParsedEvent
* @description Optionally fired from inside a manifest parser plugin if a newly
* parsed segment has some extra metadata.
* @property {string} type
* 'segmentparsed'
* @property {string} mimeType
* Mime type of the manifest parser.
* @property {number} streamId
* Id of the stream that segment belonged to.
* @property {*} segmentData
* Its format depends on the manifest parser.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems particularly problematic to me. For one, it's not obvious what the DASH equivalent is to the HLS type you're sending through here. For another, the HLS type you're sending through here is an internal object, the format of which we should be free to change and the compiler should be free to rename. So I wouldn't want to send that outside the Player to the app.

Copy link
Author

@anuragkalia anuragkalia Jun 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still in the beginning of how DASH spec but, from my limited understanding, emsg is the equivalent in DASH. (I saw that emsg is handled separately in code right now). I would like to be corrected on this one. Moreover, given shaka does not intend to be restricted to HLS and DASH but intends to support any manifest, is it not good to provide a general purpose function for extracting custom data from the manifest?

I completely agree with the fact that right now we are sending an internal object directly. I did not account for the fact that users of the method could possibly mutate the object.

Thank you for your time and looking forward to your thoughts.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After thinking a little more about it, I realized that a user can always write their own parser if they have to read the custom data from the manifest. That exactly is something I wanted to avoid and thus came up with this PR.

Also, though emsg indicates segment specific details, it seems to be a feature of mp4 rather than DASH. Am I correct here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'emsg' box is an MP4 box, but it came out of the DASH spec if I remember correctly. There is a schema specified in DASH that means "reload the manifest when you see this". Shaka Player handles that directly, and passes any other schema up to the application.

* @exportDoc
anuragkalia marked this conversation as resolved.
Show resolved Hide resolved
*/


/**
* @event shaka.Player.StreamingEvent
Expand Down Expand Up @@ -1451,6 +1466,11 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
// manifest).
onTimelineRegionAdded: (region) => this.regionTimeline_.addRegion(region),

// Called to signal new segment metadata upon parsing in the manifest
// segment metadata can vary depending on mimeType and streamId
onSegmentParsed: (mimeType, streamId, segment) => this.onSegmentParsed_(
mimeType, streamId, segment),

onEvent: (event) => this.dispatchEvent(event),
onError: (error) => this.onError_(error),
};
Expand Down Expand Up @@ -4249,6 +4269,26 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
}
}

/**
* Callback from player
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a method on Player, so this comment can't be right.

*
* @param {string} mimeType Mime type of the manifest parser.
* @param {number} streamId Id of the stream that segment belonged to.
* @param {*} segmentData Its format depends on the manifest parser.
* @private
*/
onSegmentParsed_(mimeType, streamId, segmentData) {
const eventData = {
mimeType,
streamId,
segmentData,
};

this.dispatchEvent(
new shaka.util.FakeEvent('segmentparsed', eventData)
anuragkalia marked this conversation as resolved.
Show resolved Hide resolved
);
}

/**
* Callback from AbrManager.
*
Expand Down
1 change: 1 addition & 0 deletions test/dash/dash_parser_content_protection_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe('DashParser ContentProtection', () => {
filterNewPeriod: () => {},
filterAllPeriods: () => {},
onTimelineRegionAdded: fail, // Should not have any EventStream elements.
onSegmentParsed: fail,
onEvent: fail,
onError: fail,
};
Expand Down
1 change: 1 addition & 0 deletions test/dash/dash_parser_live_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe('DashParser Live', () => {
filterNewPeriod: () => {},
filterAllPeriods: () => {},
onTimelineRegionAdded: fail, // Should not have any EventStream elements.
onSegmentParsed: fail,
onEvent: fail,
onError: fail,
};
Expand Down
1 change: 1 addition & 0 deletions test/dash/dash_parser_manifest_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('DashParser Manifest', () => {
filterNewPeriod: () => {},
filterAllPeriods: () => {},
onTimelineRegionAdded: fail, // Should not have any EventStream elements.
onSegmentParsed: fail,
onEvent: shaka.test.Util.spyFunc(onEventSpy),
onError: fail,
};
Expand Down
1 change: 1 addition & 0 deletions test/dash/dash_parser_segment_base_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('DashParser SegmentBase', () => {
filterNewPeriod: () => {},
filterAllPeriods: () => {},
onTimelineRegionAdded: fail, // Should not have any EventStream elements.
onSegmentParsed: fail,
onEvent: fail,
onError: fail,
};
Expand Down
1 change: 1 addition & 0 deletions test/dash/dash_parser_segment_template_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe('DashParser SegmentTemplate', () => {
filterNewPeriod: () => {},
filterAllPeriods: () => {},
onTimelineRegionAdded: fail, // Should not have any EventStream elements.
onSegmentParsed: fail,
onEvent: fail,
onError: fail,
};
Expand Down
71 changes: 71 additions & 0 deletions test/hls/hls_live_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,22 @@ describe('HlsParser live', () => {
onError: fail,
onEvent: fail,
onTimelineRegionAdded: fail,
onSegmentParsed: fail,
};

parser = new shaka.hls.HlsParser();
parser.configure(config);
});

/** @type {!jasmine.Spy} */
let onSegmentParsedSpy;

beforeEach(() => {
onSegmentParsedSpy = jasmine.createSpy('onSegmentParsed');
playerInterface.onSegmentParsed =
shaka.test.Util.spyFunc(onSegmentParsedSpy);
});

afterEach(() => {
// HLS parser stop is synchronous.
parser.stop();
Expand Down Expand Up @@ -555,6 +565,67 @@ describe('HlsParser live', () => {
master, media, [ref1], mediaWithAdditionalSegment, [ref1, ref2]);
});

it('triggers segment parsed callbacks as segments appear', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding tests!

const mediaWithAdditionalSegment = [
'#EXTM3U\n',
'#EXT-X-PLAYLIST-TYPE:EVENT\n',
'#EXT-X-TARGETDURATION:5\n',
'#EXT-X-MAP:URI="init.mp4",BYTERANGE="616@0"\n',
'#EXTINF:2,\n',
'main.mp4\n',
'#EXT-X-CUE:DURATION="201.467",ID="0",TYPE="SpliceOut",',
'TIME="414.171"\n',
'#EXTINF:2,\n',
'main2.mp4\n',
].join('');

fakeNetEngine
.setResponseText('test:/master', master)
.setResponseText('test:/video', media)
.setResponseValue('test:/init.mp4', initSegmentData)
.setResponseValue('test:/main.mp4', segmentData);

const spy = jasmine.createSpy('start');
parser.start('test:/master', playerInterface)
.then(Util.spyFunc(spy), fail);
PromiseMock.flush();

expect(onSegmentParsedSpy).toHaveBeenCalledTimes(1);
expect(onSegmentParsedSpy).toHaveBeenCalledWith(
'application/vnd.apple.mpegurl',
1,
new shaka.hls.Segment(
'test:/main.mp4',
[
new shaka.hls.Tag(/* id */ 1, 'EXTINF', [], '2'),
])
);

// Replace the entries with the updated values.
fakeNetEngine
.setResponseText('test:/video', mediaWithAdditionalSegment);

delayForUpdatePeriod();

expect(onSegmentParsedSpy).toHaveBeenCalledTimes(2);
expect(onSegmentParsedSpy).toHaveBeenCalledWith(
'application/vnd.apple.mpegurl',
1,
new shaka.hls.Segment(
'test:/main2.mp4',
[
new shaka.hls.Tag(/* id */ 1, 'EXTINF', [], '2'),
new shaka.hls.Tag(/* id */ 2, 'EXT-X-CUE', [
new shaka.hls.Attribute('DURATION', '201.467'),
new shaka.hls.Attribute('ID', '0'),
new shaka.hls.Attribute('TYPE', 'SpliceOut'),
new shaka.hls.Attribute('TIME', '414.171'),
]),
]
)
);
});

it('evicts removed segments', () => {
const ref1 = ManifestParser.makeReference('test:/main.mp4',
0, 2, 4);
Expand Down
11 changes: 11 additions & 0 deletions test/hls/hls_parser_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,23 @@ describe('HlsParser', () => {
onError: fail,
onEvent: fail,
onTimelineRegionAdded: fail,
onSegmentParsed: fail,
};

parser = new shaka.hls.HlsParser();
parser.configure(config);
});

/** @type {!jasmine.Spy} */
let onSegmentParsedSpy;

beforeEach(() => {
onSegmentParsedSpy = jasmine.createSpy('onSegmentParsed');
playerInterface.onSegmentParsed =
shaka.test.Util.spyFunc(onSegmentParsedSpy);
});


/**
* @param {string} master
* @param {string} media
Expand Down
2 changes: 2 additions & 0 deletions test/test/util/dash_parser_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ shaka.test.Dash = class {
filterNewPeriod: () => {},
filterAllPeriods: () => {},
onTimelineRegionAdded: fail, // Should not have any EventStream elements.
onSegmentParsed: fail,
onEvent: fail,
onError: fail,
};
Expand Down Expand Up @@ -77,6 +78,7 @@ shaka.test.Dash = class {
filterNewPeriod: () => {},
filterAllPeriods: () => {},
onTimelineRegionAdded: fail, // Should not have any EventStream elements.
onSegmentParsed: fail,
onEvent: fail,
onError: fail,
};
Expand Down
Loading