Skip to content

Commit

Permalink
feat(HLS): Add support for MediaQualityInfo events (#6927)
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad authored Jun 28, 2024
1 parent 3be95a8 commit 0895e2e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
29 changes: 29 additions & 0 deletions lib/hls/hls_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ goog.require('shaka.media.DrmEngine');
goog.require('shaka.media.InitSegmentReference');
goog.require('shaka.media.ManifestParser');
goog.require('shaka.media.PresentationTimeline');
goog.require('shaka.media.QualityObserver');
goog.require('shaka.media.SegmentIndex');
goog.require('shaka.media.SegmentReference');
goog.require('shaka.net.DataUriPlugin');
Expand Down Expand Up @@ -515,6 +516,14 @@ shaka.hls.HlsParser = class {
stream.bandwidth = bandwidth;
}

const qualityInfo =
shaka.media.QualityObserver.createQualityInfo(stream);
for (const segment of segments) {
if (segment.initSegmentReference) {
segment.initSegmentReference.mediaQuality = qualityInfo;
}
}

stream.segmentIndex.mergeAndEvict(
segments, this.presentationTimeline_.getSegmentAvailabilityStart());
if (segments.length) {
Expand Down Expand Up @@ -855,6 +864,18 @@ shaka.hls.HlsParser = class {
closedCaptions, characteristics, forced, sampleRate, spatialAudio);
this.uriToStreamInfosMap_.set(uri, streamInfo);


if (streamInfo.stream) {
const qualityInfo =
shaka.media.QualityObserver.createQualityInfo(streamInfo.stream);
streamInfo.stream.segmentIndex.forEachTopLevelReference(
(reference) => {
if (reference.initSegmentReference) {
reference.initSegmentReference.mediaQuality = qualityInfo;
}
});
}

mediaPlaylistType = streamInfo.stream.type;

// Wrap the stream from that stream info with a variant.
Expand Down Expand Up @@ -2493,6 +2514,14 @@ shaka.hls.HlsParser = class {
}
}

const qualityInfo =
shaka.media.QualityObserver.createQualityInfo(stream);
stream.segmentIndex.forEachTopLevelReference((reference) => {
if (reference.initSegmentReference) {
reference.initSegmentReference.mediaQuality = qualityInfo;
}
});

// Add finishing touches to the stream that can only be done once we have
// more full context on the media as a whole.
if (this.hasEnoughInfoToFinalizeStreams_()) {
Expand Down
2 changes: 1 addition & 1 deletion lib/media/segment_reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ shaka.media.InitSegmentReference = class {
/** @const {?number} */
this.endByte = endByte;

/** @const {shaka.extern.MediaQualityInfo|null} */
/** @type {shaka.extern.MediaQualityInfo|null} */
this.mediaQuality = mediaQuality;

/** @type {number|null} */
Expand Down
2 changes: 0 additions & 2 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ goog.requireType('shaka.media.PresentationTimeline');
* @description Fired when the media quality changes at the playhead.
* That may be caused by an adaptation change or a DASH period transition.
* Separate events are emitted for audio and video contentTypes.
* This is supported for only DASH and MSS streams at this time.
* @property {string} type
* 'mediaqualitychanged'
* @property {shaka.extern.MediaQualityInfo} mediaQuality
Expand All @@ -188,7 +187,6 @@ goog.requireType('shaka.media.PresentationTimeline');
* @event shaka.Player.AudioTrackChangedEvent
* @description Fired when the audio track changes at the playhead.
* That may be caused by a user requesting to chang audio tracks.
* This is supported for only DASH and MSS streams at this time.
* @property {string} type
* 'audiotrackchanged'
* @property {shaka.extern.MediaQualityInfo} mediaQuality
Expand Down
16 changes: 16 additions & 0 deletions test/hls/hls_parser_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2911,7 +2911,23 @@ describe('HlsParser', () => {
*/
function makeReference(startTime, endTime, syncTime) {
const initUris = () => ['test:/init.mp4'];
const mediaQuality = {
bandwidth: 200,
audioSamplingRate: null,
codecs: 'avc1.4d401f',
contentType: 'video',
frameRate: 60,
height: 540,
mimeType: 'video/mp4',
channelsCount: null,
pixelAspectRatio: null,
width: 960,
label: null,
roles: [],
language: null,
};
const init = new shaka.media.InitSegmentReference(initUris, 0, 615);
init.mediaQuality = mediaQuality;
const uris = () => ['test:/main.mp4'];
return new shaka.media.SegmentReference(
startTime, endTime, uris, 0, null, init, 0, 0, Infinity,
Expand Down

0 comments on commit 0895e2e

Please sign in to comment.