Skip to content

Commit

Permalink
Mark Version 2 of DB Engine
Browse files Browse the repository at this point in the history
externs/shaka/offline.js now represents the data types for Shaka Player
Offline V2. All offline shaka player code uses the new version and all
V1 and transitional code has been removed.

Issue #1047

Change-Id: Ia43f8d8d11426e823629e5fcd27c4e1e0ce400d3
  • Loading branch information
vaage committed Dec 16, 2017
1 parent f4d89cb commit 967f339
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 229 deletions.
103 changes: 4 additions & 99 deletions lib/offline/db_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ shaka.offline.DBEngine.Operation;


/** @private @const {number} */
shaka.offline.DBEngine.DB_VERSION_ = 1;
shaka.offline.DBEngine.DB_VERSION_ = 2;


/**
Expand Down Expand Up @@ -146,14 +146,9 @@ shaka.offline.DBEngine.prototype.destroy = function() {

/** @override */
shaka.offline.DBEngine.prototype.getManifest = function(key) {
/** @const */
var MANIFEST = shaka.offline.DBUtils.Store.MANIFEST;

return this.get_(MANIFEST, key).then(function(manifest) {
return manifest ?
shaka.offline.DBEngine.sanitizeManifest_(manifest) :
null;
});
return this.get_(
shaka.offline.DBUtils.Store.MANIFEST,
key);
};


Expand Down Expand Up @@ -524,93 +519,3 @@ shaka.offline.DBEngine.onError_ = function(errorSource, promise, event) {
// Firefox will raise an error which will cause a karma failure.
event.preventDefault();
};


/**
* @param {!Object} manifest
* @return {!shakaExtern.ManifestDB}
* @private
*/
shaka.offline.DBEngine.sanitizeManifest_ = function(manifest) {
// Purposely do not use types here as it will allow us to "schrodinger's cat"
// the type until the end where we can conform the type to the final
// ManifestDB type.

/** @const */
var ContentType = shaka.util.ManifestParserUtils.ContentType;

// There are three cases:
// 1. All streams' variant ids are null
// 2. All streams' variant ids are non-null
// 3. Some streams' variant ids are null and other are non-null
// Case 3 is invalid and should never happen in production.

var allStreams = [];
manifest.periods.forEach(function(period) {
allStreams.push.apply(allStreams, period.streams);
});

var audioStreams = allStreams.filter(function(stream) {
return stream.contentType == ContentType.AUDIO;
});

var videoStreams = allStreams.filter(function(stream) {
return stream.contentType == ContentType.VIDEO;
});

var audioVideoStreams = [];
audioVideoStreams.push.apply(audioVideoStreams, audioStreams);
audioVideoStreams.push.apply(audioVideoStreams, videoStreams);

var allVariantIdsNull = allStreams.every(function(stream) {
var ids = stream.variantIds;
return ids == null;
});

var allVariantIdsNonNull = allStreams.every(function(stream) {
var ids = stream.variantIds;
return ids != null && ids != undefined;
});

// Case 3
goog.asserts.assert(
allVariantIdsNull || allVariantIdsNonNull,
'All variant ids should be null or non-null.');

// Convert Case 1 to Case 2
// TODO (vaage) : Move the conversion of case 1 to case 2 to database
// upgrade.
if (allVariantIdsNull) {
// Since all the variant ids are null, we need to first make them into
// valid arrays.
allStreams.forEach(function(stream) {
stream.variantIds = [];
});

/** @type {number} */
var currentVariantId = 0;

// It is not possible in the pre-variant world of shaka to have audio-only
// and video-only content mixed in with audio-video content. So we can
// assume that there is only audio-only or video-only if one group is empty.
if (audioStreams.length == 0 || videoStreams.length == 0) {
// Create all audio only and all video only variants.
audioVideoStreams.forEach(function(stream) {
stream.variantIds.push(currentVariantId);
currentVariantId++;
});
} else {
// Create all audio and video variants.
audioStreams.forEach(function(audio) {
videoStreams.forEach(function(video) {
audio.variantIds.push(currentVariantId);
video.variantIds.push(currentVariantId);

currentVariantId++;
});
});
}
}

return /** @type {shakaExtern.ManifestDB} */ (manifest);
};
12 changes: 12 additions & 0 deletions lib/offline/db_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ shaka.offline.DBUtils.Mode = {
* @enum {string}
*/
shaka.offline.DBUtils.Store = {
MANIFEST: 'manifest-v2',
SEGMENT: 'segment-v2'
};


/**
* The name for the stores that are used in the version 1 of our
* indexed db storage.
*
* @enum {string}
*/
shaka.offline.DBUtils.StoreV1 = {
MANIFEST: 'manifest',
SEGMENT: 'segment'
};
54 changes: 2 additions & 52 deletions lib/offline/offline_uri.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

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

goog.require('shaka.log');


/**
* @param {!number} id
Expand All @@ -39,31 +37,7 @@ shaka.offline.OfflineUri.uriToManifestId = function(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;
return id;
};


Expand All @@ -86,29 +60,5 @@ shaka.offline.OfflineUri.uriToSegmentId = function(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;
return id;
};
61 changes: 1 addition & 60 deletions test/offline/db_engine_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
* limitations under the License.
*/

describe('DBEngine', /** @suppress {accessControls} */ function() {
/** @const */
var ContentType = shaka.util.ManifestParserUtils.ContentType;

describe('DBEngine', function() {
/** @const {string} */
var dbName = 'shaka-player-test-db';

Expand Down Expand Up @@ -199,62 +196,6 @@ describe('DBEngine', /** @suppress {accessControls} */ function() {
.then(done).catch(fail);
}));

// TODO : Remove this test once we drop support for DB Engine Version 1
it('fills missing variant ids for old manifests', checkAndRun(function(done) {
// Create a manifest with four streams. Two video and two audio. When db
// engine recreates the variant ids, it should pair them together into
// four variants.

/** @type {shakaExtern.ManifestDB} */
var originalManifest = createManifest('original manifest');
originalManifest.periods.push({
startTime: 0,
streams: [
createStream(0, ContentType.AUDIO),
createStream(1, ContentType.AUDIO),
createStream(2, ContentType.VIDEO),
createStream(3, ContentType.VIDEO)
]
});

// Remove the variant ids field from all streams.
originalManifest.periods[0].streams.forEach(function(stream) {
delete stream['variantIds'];
expect(stream.variantIds).toBe(undefined);
});

Promise.resolve()
.then(function() {
return db.addManifest(originalManifest);
})
.then(function(id) {
return db.getManifest(id);
})
.then(function(manifest) {
expect(manifest.periods.length).toBe(1);

var streams = manifest.periods[0].streams;
expect(streams.length).toBe(4);

// Create a mapping of variants to stream ids.
var variants = {};
streams.forEach(function(stream) {
stream.variantIds.forEach(function(id) {
variants[id] = variants[id] || [];
variants[id].push(stream.id);
});
});

shaka.log.info(variants);

expect(variants[0]).toEqual([0, 2]);
expect(variants[1]).toEqual([0, 3]);
expect(variants[2]).toEqual([1, 2]);
expect(variants[3]).toEqual([1, 3]);
}).then(done).catch(fail);
}));


/**
* Before running the test, check if DBEngine is supported on this platform.
* @param {function(function())} test
Expand Down
18 changes: 0 additions & 18 deletions test/offline/offline_uri_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,6 @@ describe('OfflineUri', function() {
expect(id).toBe(123);
});

it('creates id from legacy manifest uri', function() {
/** @type {string} */
var uri = 'offline:123';
/** @type {?number} */
var id = OfflineUri.uriToManifestId(uri);

expect(id).toBe(123);
});

it('creates null id from non-segment uri', function() {
/** @type {string} */
var uri = 'invalid-uri';
Expand All @@ -81,13 +72,4 @@ describe('OfflineUri', function() {

expect(id).toBe(123);
});

it('creates id from legacy segment uri', function() {
/** @type {string} */
var uri = 'offline:1/2/3';
/** @type {?number} */
var id = OfflineUri.uriToSegmentId(uri);

expect(id).toBe(3);
});
});

0 comments on commit 967f339

Please sign in to comment.