Files
shaka-player/lib/offline/offline_scheme.js
T
Aaron Vaage b98ce99f08 Add Type Structure to Storage Engine
Changed the Storage Engine interface to know about the types that it
is expected to store. This allow the implementation to do more internally
as the scope of the object has been limited.

Change-Id: I388e2bde5e7e3f99e8b28bd16e08a8f6d5fc6453
2017-12-04 22:41:23 +00:00

207 lines
4.9 KiB
JavaScript

/**
* @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.OfflineScheme');
goog.require('shaka.log');
goog.require('shaka.net.NetworkingEngine');
goog.require('shaka.offline.StorageEngineFactory');
goog.require('shaka.util.Error');
/**
* @namespace
* @summary A plugin that handles requests for offline content.
* @param {string} uri
* @param {shakaExtern.Request} request
* @return {!Promise.<shakaExtern.Response>}
* @export
*/
shaka.offline.OfflineScheme = function(uri, request) {
var manifestId = shaka.offline.OfflineScheme.uriToManifestId(uri);
if (manifestId != null) {
return shaka.offline.OfflineScheme.onManifest_(uri);
}
var segmentId = shaka.offline.OfflineScheme.uriToSegmentId(uri);
if (segmentId != null) {
return shaka.offline.OfflineScheme.onSegment_(segmentId, uri);
}
return Promise.reject(new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.NETWORK,
shaka.util.Error.Code.MALFORMED_OFFLINE_URI,
uri));
};
/**
* @param {string} uri
* @return {!Promise<shakaExtern.Response>}
* @private
*/
shaka.offline.OfflineScheme.onManifest_ = function(uri) {
/** @type {shakaExtern.Response} */
var response = {
uri: uri,
data: new ArrayBuffer(0),
headers: {'content-type': 'application/x-offline-manifest'}
};
return Promise.resolve(response);
};
/**
* @param {number} id
* @param {string} uri
* @return {!Promise<shakaExtern.Response>}
* @private
*/
shaka.offline.OfflineScheme.onSegment_ = function(id, uri) {
/** @type {shaka.offline.IStorageEngine} */
var storageEngine;
var segment;
return shaka.offline.StorageEngineFactory.createStorageEngine()
.then(function(se) {
storageEngine = se;
return storageEngine.getSegment(id);
})
.then(function(seg) {
segment = seg;
return storageEngine.destroy();
})
.then(function() {
if (!segment) {
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.STORAGE,
shaka.util.Error.Code.REQUESTED_ITEM_NOT_FOUND,
id);
}
/** @type {shakaExtern.Response} */
var response = {
uri: uri,
data: segment.data,
headers: {}
};
return response;
});
};
/**
* @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);