Skip to content

Commit

Permalink
Moving Offline Uri
Browse files Browse the repository at this point in the history
Moving offline uri code to avoid an upcoming circular dependency in
the upgrade code.

Change-Id: I952386f17c6d4e7806d97e2f540887b915f68de7
  • Loading branch information
vaage committed Dec 14, 2017
1 parent b8e0f80 commit 46685fb
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 228 deletions.
1 change: 1 addition & 0 deletions build/types/offline
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
+../../lib/offline/i_storage_engine.js
+../../lib/offline/offline_manifest_parser.js
+../../lib/offline/offline_scheme.js
+../../lib/offline/offline_uri.js
+../../lib/offline/offline_utils.js
+../../lib/offline/storage.js
+../../lib/offline/storage_engine_factory.js
4 changes: 2 additions & 2 deletions lib/offline/offline_manifest_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ goog.require('goog.asserts');
goog.require('shaka.log');
goog.require('shaka.media.ManifestParser');
goog.require('shaka.media.PresentationTimeline');
goog.require('shaka.offline.OfflineScheme');
goog.require('shaka.offline.OfflineUri');
goog.require('shaka.offline.OfflineUtils');
goog.require('shaka.offline.StorageEngineFactory');
goog.require('shaka.util.Error');
Expand Down Expand Up @@ -49,7 +49,7 @@ shaka.offline.OfflineManifestParser.prototype.configure = function(config) {
/** @override */
shaka.offline.OfflineManifestParser.prototype.start =
function(uri, playerInterface) {
var manifestId = shaka.offline.OfflineScheme.uriToManifestId(uri);
var manifestId = shaka.offline.OfflineUri.uriToManifestId(uri);
if (manifestId == null) {
return Promise.reject(new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
Expand Down
100 changes: 3 additions & 97 deletions lib/offline/offline_scheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

goog.provide('shaka.offline.OfflineScheme');

goog.require('shaka.log');
goog.require('shaka.net.NetworkingEngine');
goog.require('shaka.offline.OfflineUri');
goog.require('shaka.offline.StorageEngineFactory');
goog.require('shaka.util.Error');

Expand All @@ -32,12 +32,12 @@ goog.require('shaka.util.Error');
* @export
*/
shaka.offline.OfflineScheme = function(uri, request) {
var manifestId = shaka.offline.OfflineScheme.uriToManifestId(uri);
var manifestId = shaka.offline.OfflineUri.uriToManifestId(uri);
if (manifestId != null) {
return shaka.offline.OfflineScheme.onManifest_(uri);
}

var segmentId = shaka.offline.OfflineScheme.uriToSegmentId(uri);
var segmentId = shaka.offline.OfflineUri.uriToSegmentId(uri);
if (segmentId != null) {
return shaka.offline.OfflineScheme.onSegment_(segmentId, uri);
}
Expand Down Expand Up @@ -108,99 +108,5 @@ shaka.offline.OfflineScheme.onSegment_ = function(id, uri) {
};


/**
* @param {!number} id
* @return {!string}
*/
shaka.offline.OfflineScheme.manifestIdToUri = function(id) {
return 'offline:manifest/' + id;
};


/**
* @param {!string} uri
* @return {?number}
*/
shaka.offline.OfflineScheme.uriToManifestId = function(uri) {
var parts = /^offline:manifest\/([0-9]+)$/.exec(uri);

/** @type {?number} */
var id = parts ? Number(parts[1]) : null;

if (id != null) {
return id;
}

// TODO (vaage) : Remove legacy support once database upgrades are supported.

/** @type {?number} */
var legacyId = shaka.offline.OfflineScheme.legacyUriToManifestId(uri);

if (legacyId != null) {
shaka.log.alwaysWarn(
'Legacy uri detected. Download content again to update uris.');
}

return legacyId;
};


/**
* @param {!string} uri
* @return {?number}
*/
shaka.offline.OfflineScheme.legacyUriToManifestId = function(uri) {
var parts = /^offline:([0-9]+)$/.exec(uri);
return parts ? Number(parts[1]) : null;
};


/**
* @param {number} id
* @return {!string}
*/
shaka.offline.OfflineScheme.segmentIdToUri = function(id) {
return 'offline:segment/' + id;
};


/**
* @param {!string} uri
* @return {?number}
*/
shaka.offline.OfflineScheme.uriToSegmentId = function(uri) {
var parts = /^offline:segment\/([0-9]+)$/.exec(uri);

/** @type {?number} */
var id = parts ? Number(parts[1]) : null;

if (id != null) {
return id;
}

// TODO (vaage) : Remove legacy support once database upgrades are supported.

/** @type {?number} */
var legacyId = shaka.offline.OfflineScheme.legacyUriToSegmentId(uri);

if (legacyId != null) {
shaka.log.alwaysWarn(
'Legacy uri detected. Download content again to update uris.');
}

return legacyId;
};


/**
* @param {!string} uri
* @return {?number}
*/
shaka.offline.OfflineScheme.legacyUriToSegmentId = function(uri) {
var parts = /^offline:[0-9]+\/[0-9]+\/([0-9]+)$/.exec(uri);
return parts ? Number(parts[1]) : null;
};


shaka.net.NetworkingEngine.registerScheme(
'offline', shaka.offline.OfflineScheme);
114 changes: 114 additions & 0 deletions lib/offline/offline_uri.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* @license
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

goog.provide('shaka.offline.OfflineUri');

goog.require('shaka.log');


/**
* @param {!number} id
* @return {!string}
*/
shaka.offline.OfflineUri.manifestIdToUri = function(id) {
return 'offline:manifest/' + id;
};


/**
* @param {!string} uri
* @return {?number}
*/
shaka.offline.OfflineUri.uriToManifestId = function(uri) {
var parts = /^offline:manifest\/([0-9]+)$/.exec(uri);

/** @type {?number} */
var id = parts ? Number(parts[1]) : null;

if (id != null) {
return id;
}

// TODO (vaage) : Remove legacy support once database upgrades are supported.

/** @type {?number} */
var legacyId = shaka.offline.OfflineUri.legacyUriToManifestId(uri);

if (legacyId != null) {
shaka.log.alwaysWarn(
'Legacy uri detected. Download content again to update uris.');
}

return legacyId;
};


/**
* @param {!string} uri
* @return {?number}
*/
shaka.offline.OfflineUri.legacyUriToManifestId = function(uri) {
var parts = /^offline:([0-9]+)$/.exec(uri);
return parts ? Number(parts[1]) : null;
};


/**
* @param {number} id
* @return {!string}
*/
shaka.offline.OfflineUri.segmentIdToUri = function(id) {
return 'offline:segment/' + id;
};


/**
* @param {!string} uri
* @return {?number}
*/
shaka.offline.OfflineUri.uriToSegmentId = function(uri) {
var parts = /^offline:segment\/([0-9]+)$/.exec(uri);

/** @type {?number} */
var id = parts ? Number(parts[1]) : null;

if (id != null) {
return id;
}

// TODO (vaage) : Remove legacy support once database upgrades are supported.

/** @type {?number} */
var legacyId = shaka.offline.OfflineUri.legacyUriToSegmentId(uri);

if (legacyId != null) {
shaka.log.alwaysWarn(
'Legacy uri detected. Download content again to update uris.');
}

return legacyId;
};


/**
* @param {!string} uri
* @return {?number}
*/
shaka.offline.OfflineUri.legacyUriToSegmentId = function(uri) {
var parts = /^offline:[0-9]+\/[0-9]+\/([0-9]+)$/.exec(uri);
return parts ? Number(parts[1]) : null;
};
24 changes: 12 additions & 12 deletions lib/offline/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ goog.require('shaka.media.ManifestParser');
goog.require('shaka.offline.DownloadManager');
goog.require('shaka.offline.IStorageEngine');
goog.require('shaka.offline.OfflineManifestParser');
goog.require('shaka.offline.OfflineScheme');
goog.require('shaka.offline.OfflineUri');
goog.require('shaka.offline.OfflineUtils');
goog.require('shaka.offline.StorageEngineFactory');
goog.require('shaka.util.ConfigUtils');
Expand All @@ -41,7 +41,7 @@ goog.require('shaka.util.StreamUtils');
/**
* This manages persistent offline data including storage, listing, and deleting
* stored manifests. Playback of offline manifests are done using Player
* using the special URI (see shaka.offline.OfflineScheme).
* using the special URI (see shaka.offline.OfflineUri).
*
* First, check support() to see if offline is supported by the platform.
* Second, configure() the storage object with callbacks to your application.
Expand Down Expand Up @@ -260,12 +260,12 @@ shaka.offline.Storage.prototype.downloadAndStoreManifest_ = function(
return this.downloadManager_.downloadAndStore(manifestDb)
.then(function(id) {
/** @const */
var OfflineScheme = shaka.offline.OfflineScheme;
var OfflineUri = shaka.offline.OfflineUri;
/** @const */
var OfflineUtils = shaka.offline.OfflineUtils;

/** @type {string} */
var uri = OfflineScheme.manifestIdToUri(id);
var uri = OfflineUri.manifestIdToUri(id);
return OfflineUtils.createStoredContentFromManifestDB(uri, manifestDb);
});
};
Expand Down Expand Up @@ -298,7 +298,7 @@ shaka.offline.Storage.prototype.remove = function(contentUri) {
* @private
*/
shaka.offline.Storage.prototype.removeByUrl_ = function(offlineUri) {
var manifestId = shaka.offline.OfflineScheme.uriToManifestId(offlineUri);
var manifestId = shaka.offline.OfflineUri.uriToManifestId(offlineUri);
if (manifestId == null) {
return Promise.reject(new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
Expand Down Expand Up @@ -427,7 +427,7 @@ shaka.offline.Storage.prototype.list = function() {
this.checkDestroyed_();
return this.storageEngine_.forEachManifest(function(id, manifestDB) {
var content = OfflineUtils.createStoredContentFromManifestDB(
shaka.offline.OfflineScheme.manifestIdToUri(id),
shaka.offline.OfflineUri.manifestIdToUri(id),
manifestDB);
storedContents.push(content);
});
Expand Down Expand Up @@ -955,7 +955,7 @@ shaka.offline.Storage.prototype.createPeriod_ = function(period) {
shaka.offline.Storage.prototype.createStream_ = function(
period, stream, estimatedStreamBandwidth, opt_variantId) {
/** @const */
var OfflineScheme = shaka.offline.OfflineScheme;
var OfflineUri = shaka.offline.OfflineUri;

/** @type {shakaExtern.StreamDB} */
var streamDb = {
Expand Down Expand Up @@ -1005,7 +1005,7 @@ shaka.offline.Storage.prototype.createStream_ = function(
var segmentDb = {
startTime: startTime,
endTime: endTime,
uri: OfflineScheme.segmentIdToUri(id)
uri: OfflineUri.segmentIdToUri(id)
};

streamDb.segments.push(segmentDb);
Expand All @@ -1022,7 +1022,7 @@ shaka.offline.Storage.prototype.createStream_ = function(
initSegment,
noBandwidth,
function(id) {
streamDb.initSegmentUri = OfflineScheme.segmentIdToUri(id);
streamDb.initSegmentUri = OfflineUri.segmentIdToUri(id);
});
}

Expand Down Expand Up @@ -1069,7 +1069,7 @@ shaka.offline.Storage.prototype.checkDestroyed_ = function() {
* @private
*/
shaka.offline.Storage.getAllSegmentIds_ = function(manifest) {
var OfflineScheme = shaka.offline.OfflineScheme;
var OfflineUri = shaka.offline.OfflineUri;

/** @type {!Array.<number>} */
var ids = [];
Expand All @@ -1081,13 +1081,13 @@ shaka.offline.Storage.getAllSegmentIds_ = function(manifest) {
manifest.periods.forEach(function(period) {
period.streams.forEach(function(stream) {
if (stream.initSegmentUri) {
id = OfflineScheme.uriToSegmentId(stream.initSegmentUri);
id = OfflineUri.uriToSegmentId(stream.initSegmentUri);
goog.asserts.assert(id != null, 'Invalid offline URI');
ids.push(id);
}

stream.segments.forEach(function(segment) {
id = OfflineScheme.uriToSegmentId(segment.uri);
id = OfflineUri.uriToSegmentId(segment.uri);
goog.asserts.assert(id != null, 'Invalid offline URI');
ids.push(id);
});
Expand Down
Loading

0 comments on commit 46685fb

Please sign in to comment.