mirror of
https://github.com/shaka-project/shaka-player.git
synced 2026-06-18 16:36:56 +03:00
7623b58fea
The circular dependency, ContentDatabase -> RangeRequest -> AjaxRequest -> ContentDatabase, causes issues for the upstream Closure compiler. This patch breaks that cycle by factoring out reading and writing operations from ContentDatabase into ContentDatabaseReader and ContentDatabaseWriter respectively. The following minor changes have been made to code that has moved: * Removed retrieveInitSegment() since it's not used. * Changed .stream_ids to ['stream_ids'] in retriveGroup(). * Reworked deleteGroup() so that it doesn't depend on retrieveGroup(). * Made deleteStream() private. * Made minor formatting changes to meet the 80 character limit. Change-Id: Idfc5d04ad32225a915b1531e0f4205137de5cc73
116 lines
3.6 KiB
JavaScript
116 lines
3.6 KiB
JavaScript
/**
|
|
* Copyright 2015 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.
|
|
*
|
|
* @fileoverview Implements a content database reader.
|
|
*/
|
|
|
|
goog.provide('shaka.util.ContentDatabaseReader');
|
|
|
|
goog.require('shaka.util.ContentDatabase');
|
|
goog.require('shaka.util.PublicPromise');
|
|
goog.require('shaka.util.TypedBind');
|
|
|
|
|
|
|
|
/**
|
|
* Creates a new ContentDatabaseReader.
|
|
*
|
|
* @constructor
|
|
* @struct
|
|
* @extends {shaka.util.ContentDatabase}
|
|
*/
|
|
shaka.util.ContentDatabaseReader = function() {
|
|
shaka.util.ContentDatabase.call(this, 'readonly', null);
|
|
};
|
|
goog.inherits(shaka.util.ContentDatabaseReader, shaka.util.ContentDatabase);
|
|
|
|
|
|
/**
|
|
* Retrieves an array of all stored group IDs.
|
|
* @return {!Promise.<!Array.<number>>} The unique IDs of all of the
|
|
* stored groups.
|
|
*/
|
|
shaka.util.ContentDatabaseReader.prototype.retrieveGroupIds = function() {
|
|
var p = new shaka.util.PublicPromise();
|
|
var groupIds = [];
|
|
var request = this.getGroupStore().openCursor();
|
|
|
|
request.onerror = function(e) { p.reject(request.error); };
|
|
request.onsuccess = function(e) {
|
|
var cursor = e.target.result;
|
|
if (cursor) {
|
|
groupIds.push(cursor.key);
|
|
cursor.continue();
|
|
} else {
|
|
p.resolve(groupIds);
|
|
}
|
|
};
|
|
|
|
return p;
|
|
};
|
|
|
|
|
|
/**
|
|
* Retrieves the group for a stream from the database.
|
|
* @param {number} groupId The unique id of the group.
|
|
* @return {!Promise.<shaka.util.ContentDatabase.GroupInformation>} The unique
|
|
* ids of the streams the group.
|
|
*/
|
|
shaka.util.ContentDatabaseReader.prototype.retrieveGroup = function(groupId) {
|
|
var p = this.retrieveItem(this.getGroupStore(), groupId);
|
|
return p.then(shaka.util.TypedBind(this,
|
|
/** @param {shaka.util.ContentDatabase.GroupInformation} groupInfo */
|
|
function(groupInfo) {
|
|
if (!groupInfo.hasOwnProperty('duration') &&
|
|
!groupInfo.hasOwnProperty('key_system')) {
|
|
return this.retrieveStreamIndex(groupInfo['stream_ids'][0]).then(
|
|
function(index) {
|
|
groupInfo['duration'] = index['duration'];
|
|
groupInfo['key_system'] = index['key_system'];
|
|
return Promise.resolve(groupInfo);
|
|
});
|
|
} else {
|
|
return Promise.resolve(groupInfo);
|
|
}
|
|
}));
|
|
};
|
|
|
|
|
|
/**
|
|
* Retrieves the index for a stream from the database.
|
|
* @param {number} streamId The unique id of the stream.
|
|
* @return {!Promise.<shaka.util.ContentDatabase.StreamIndex>}
|
|
*/
|
|
shaka.util.ContentDatabaseReader.prototype.retrieveStreamIndex = function(
|
|
streamId) {
|
|
return this.retrieveItem(this.getIndexStore(), streamId);
|
|
};
|
|
|
|
|
|
/**
|
|
* Retrieves the segment with |segmentId| from the stream with |streamId| in
|
|
* the database.
|
|
* @param {number} streamId The unique id of the stream to retrieve.
|
|
* @param {number} segmentId The id of the segment to retrieve.
|
|
* @return {!Promise.<ArrayBuffer>}
|
|
*/
|
|
shaka.util.ContentDatabaseReader.prototype.retrieveSegment = function(
|
|
streamId, segmentId) {
|
|
var p = this.retrieveItem(
|
|
this.getContentStore().index('segment'), [streamId, segmentId]);
|
|
return p.then(function(data) { return Promise.resolve(data.content); });
|
|
};
|
|
|