Skip to content

Commit

Permalink
Added setNetworkCallback method to dash and offline video sources.
Browse files Browse the repository at this point in the history
This allows the app to intercept media requests to modify its URLs.
The callback accepts the URL for the request and returns a modified
URL or null to use the original.

Closes #148

Change-Id: I08352754ace05f318706fd93910097c0fa7696f0
  • Loading branch information
TheModMaker committed Aug 24, 2015
1 parent 47948f4 commit e74ad98
Show file tree
Hide file tree
Showing 19 changed files with 250 additions and 73 deletions.
13 changes: 10 additions & 3 deletions lib/dash/container_segment_index_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ goog.require('shaka.util.TypedBind');
* for MP4 containers.
* @param {number} manifestCreationTime The time, in seconds, when the manifest
* was created.
* @param {shaka.util.FailoverUri.NetworkCallback} networkCallback
* @constructor
* @struct
* @implements {shaka.media.ISegmentIndexSource}
*/
shaka.dash.ContainerSegmentIndexSource = function(
mpd, period, containerType, indexMetadata, initMetadata,
manifestCreationTime) {
manifestCreationTime, networkCallback) {
shaka.asserts.assert(containerType != 'webm' || initMetadata);

/** @private {!shaka.dash.mpd.Mpd} */
Expand All @@ -78,6 +79,9 @@ shaka.dash.ContainerSegmentIndexSource = function(

/** @private {shaka.media.SegmentIndex} */
this.segmentIndex_ = null;

/** @private {shaka.util.FailoverUri.NetworkCallback} */
this.networkCallback_ = networkCallback;
};


Expand All @@ -88,6 +92,7 @@ shaka.dash.ContainerSegmentIndexSource = function(
shaka.dash.ContainerSegmentIndexSource.prototype.destroy = function() {
this.mpd_ = null;
this.period_ = null;
this.networkCallback_ = null;

this.indexMetadata_.abortFetch();
this.indexMetadata_ = null;
Expand Down Expand Up @@ -128,13 +133,15 @@ shaka.dash.ContainerSegmentIndexSource.prototype.create = function() {
var parser = new shaka.media.Mp4SegmentIndexParser();
references = parser.parse(new DataView(indexData),
this.indexMetadata_.startByte,
this.indexMetadata_.urls);
this.indexMetadata_.urls,
this.networkCallback_);
} else if (this.containerType_ == 'webm') {
shaka.asserts.assert(initData);
var parser = new shaka.media.WebmSegmentIndexParser();
references = parser.parse(new DataView(indexData),
new DataView(initData),
this.indexMetadata_.urls);
this.indexMetadata_.urls,
this.networkCallback_);
} else {
shaka.asserts.unreachable();
}
Expand Down
11 changes: 8 additions & 3 deletions lib/dash/duration_segment_index_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ goog.require('shaka.util.TypedBind');
* @param {!shaka.dash.mpd.Representation} representation
* @param {number} manifestCreationTime The time, in seconds, when the manifest
* was created.
* @param {shaka.util.FailoverUri.NetworkCallback} networkCallback
* @constructor
* @struct
* @implements {shaka.media.ISegmentIndexSource}
*/
shaka.dash.DurationSegmentIndexSource = function(
mpd, period, representation, manifestCreationTime) {
mpd, period, representation, manifestCreationTime, networkCallback) {
shaka.asserts.assert(period.start != null);
shaka.asserts.assert((mpd.type == 'dynamic') || (period.duration != null));
shaka.asserts.assert(representation.segmentTemplate);
Expand All @@ -64,6 +65,9 @@ shaka.dash.DurationSegmentIndexSource = function(

/** @private {shaka.media.SegmentIndex} */
this.segmentIndex_ = null;

/** @private {shaka.util.FailoverUri.NetworkCallback} */
this.networkCallback_ = networkCallback;
};


Expand All @@ -75,6 +79,7 @@ shaka.dash.DurationSegmentIndexSource.prototype.destroy = function() {
this.mpd_ = null;
this.period_ = null;
this.representation_ = null;
this.networkCallback_ = null;

if (this.segmentIndex_) {
this.segmentIndex_.destroy();
Expand All @@ -93,7 +98,7 @@ shaka.dash.DurationSegmentIndexSource.prototype.create = function() {
try {
this.segmentIndex_ = new shaka.dash.DynamicLiveSegmentIndex(
this.mpd_, this.period_, this.representation_,
this.manifestCreationTime_);
this.manifestCreationTime_, this.networkCallback_);
} catch (exception) {
return Promise.reject(exception);
}
Expand All @@ -103,7 +108,7 @@ shaka.dash.DurationSegmentIndexSource.prototype.create = function() {
segmentTemplate.segmentDuration / segmentTemplate.timescale;
var numSegments = Math.ceil(this.period_.duration / scaledSegmentDuration);
var references = shaka.dash.MpdUtils.generateSegmentReferences(
this.representation_, 1, numSegments);
this.networkCallback_, this.representation_, 1, numSegments);
if (!references) {
var error = new Error('Failed to generate SegmentReferences');
error.type = 'stream';
Expand Down
12 changes: 9 additions & 3 deletions lib/dash/dynamic_live_segment_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ goog.require('shaka.util.ArrayUtils');
* @param {!shaka.dash.mpd.Representation} representation
* @param {number} manifestCreationTime The time, in seconds, when the manifest
* was created.
* @param {shaka.util.FailoverUri.NetworkCallback} networkCallback
* @throws {Error} If the SegmentIndex's corresponding stream is available but
* the initial SegmentReferences could not be generated.
* @constructor
* @struct
* @extends {shaka.dash.LiveSegmentIndex}
*/
shaka.dash.DynamicLiveSegmentIndex = function(
mpd, period, representation, manifestCreationTime) {
mpd, period, representation, manifestCreationTime, networkCallback) {
shaka.asserts.assert(mpd.availabilityStartTime != null);
shaka.asserts.assert(period.start != null);
shaka.asserts.assert(representation.segmentTemplate);
Expand All @@ -70,7 +71,7 @@ shaka.dash.DynamicLiveSegmentIndex = function(
}

var references = shaka.dash.MpdUtils.generateSegmentReferences(
representation, earliestSegmentNumber, numSegments);
networkCallback, representation, earliestSegmentNumber, numSegments);
if (references == null) {
var error = new Error('Failed to generate SegmentReferences.');
error.type = 'stream';
Expand Down Expand Up @@ -114,6 +115,9 @@ shaka.dash.DynamicLiveSegmentIndex = function(
* @private {?number}
*/
this.nextSegmentNumber_ = pair ? pair.current + 1 : null;

/** @private {shaka.util.FailoverUri.NetworkCallback} */
this.networkCallback_ = networkCallback;
};
goog.inherits(shaka.dash.DynamicLiveSegmentIndex,
shaka.dash.LiveSegmentIndex);
Expand Down Expand Up @@ -295,6 +299,7 @@ shaka.dash.DynamicLiveSegmentIndex.computeAvailableSegmentRange_ =
*/
shaka.dash.DynamicLiveSegmentIndex.prototype.destroy = function() {
this.representation_ = null;
this.networkCallback_ = null;
shaka.dash.LiveSegmentIndex.prototype.destroy.call(this);
};

Expand Down Expand Up @@ -410,7 +415,8 @@ shaka.dash.DynamicLiveSegmentIndex.prototype.generateSegmentReferences_ =

// Generate and correct the new SegmentReferences.
var newReferences = shaka.dash.MpdUtils.generateSegmentReferences(
this.representation_, this.nextSegmentNumber_, numNewSegments);
this.networkCallback_, this.representation_,
this.nextSegmentNumber_, numNewSegments);

// |newReferences| should never be null since generateSegmentReferences()
// should have been called at least once successfully with |representation_|.
Expand Down
10 changes: 8 additions & 2 deletions lib/dash/list_segment_index_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ goog.require('shaka.util.TypedBind');
* @param {!shaka.dash.mpd.Representation} representation
* @param {number} manifestCreationTime The time, in seconds, when the manifest
* was created.
* @param {shaka.util.FailoverUri.NetworkCallback} networkCallback
* @constructor
* @struct
* @implements {shaka.media.ISegmentIndexSource}
*/
shaka.dash.ListSegmentIndexSource = function(
mpd, period, representation, manifestCreationTime) {
mpd, period, representation, manifestCreationTime, networkCallback) {
shaka.asserts.assert(representation.segmentList);

// Alias
Expand All @@ -67,6 +68,9 @@ shaka.dash.ListSegmentIndexSource = function(

/** @private {number} */
this.manifestCreationTime_ = manifestCreationTime;

/** @private {shaka.util.FailoverUri.NetworkCallback} */
this.networkCallback_ = networkCallback;
};


Expand All @@ -78,6 +82,7 @@ shaka.dash.ListSegmentIndexSource.prototype.destroy = function() {
this.mpd_ = null;
this.period_ = null;
this.representation_ = null;
this.networkCallback_ = null;

if (this.segmentIndex_) {
this.segmentIndex_.destroy();
Expand Down Expand Up @@ -164,7 +169,8 @@ shaka.dash.ListSegmentIndexSource.prototype.create = function() {
scaledStartTime,
scaledEndTime,
new shaka.util.FailoverUri(
segmentUrl.mediaUrl, startByte, endByte)));
this.networkCallback_, segmentUrl.mediaUrl,
startByte, endByte)));
}

this.segmentIndex_ = this.mpd_.type == 'dynamic' ?
Expand Down
Loading

0 comments on commit e74ad98

Please sign in to comment.