Skip to content

Commit

Permalink
Isolate Progress Code
Browse files Browse the repository at this point in the history
Isolated all progress code into functions in Storage to help abstract
progress from the storage logic.

Change-Id: I7a72c25dbd126cb4a5e682763fb1e46afa0703ef
  • Loading branch information
vaage committed Dec 8, 2017
1 parent 0a59da4 commit 6a03ceb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 54 deletions.
100 changes: 50 additions & 50 deletions lib/offline/download_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,31 +71,11 @@ shaka.offline.DownloadManager = function(
/** @private {Promise} */
this.promise_ = null;

/**
* The total number of bytes for segments that include a byte range.
* @private {number}
*/
this.givenBytesTotal_ = 0;

/**
* The number of bytes downloaded for segments that include a byte range.
* @private {number}
*/
this.givenBytesDownloaded_ = 0;
/** @private {number} */
this.downloadExpected_ = 0;

/**
* The total number of bytes estimated based on bandwidth for segments that
* do not include a byte range.
* @private {number}
*/
this.bandwidthBytesTotal_ = 0;

/**
* The estimated number of bytes downloaded for segments that do not have
* a byte range.
* @private {number}
*/
this.bandwidthBytesDownloaded_ = 0;
/** @private {number} */
this.downloadActual_ = 0;
};


Expand Down Expand Up @@ -180,23 +160,16 @@ shaka.offline.DownloadManager.prototype.addSegment = function(
*/
shaka.offline.DownloadManager.prototype.downloadAndStore = function(manifest) {
var MapUtils = shaka.util.MapUtils;
// Calculate progress estimates.
this.givenBytesTotal_ = 0;
this.givenBytesDownloaded_ = 0;
this.bandwidthBytesTotal_ = 0;
this.bandwidthBytesDownloaded_ = 0;

// Clear any old progress.
this.downloadExpected_ = 0;
this.downloadActual_ = 0;

MapUtils.values(this.segments_).forEach(function(segments) {
segments.forEach(function(segment) {
if (segment.endByte != null)
this.givenBytesTotal_ += (segment.endByte - segment.startByte + 1);
else
this.bandwidthBytesTotal_ += segment.bandwidthSize;
}.bind(this));
segments.forEach(this.markAsPending_.bind(this));
}.bind(this));

this.manifest_ = manifest;
// Will be updated as we download for segments without a byte-range.
this.manifest_.size = this.givenBytesTotal_;

// Create separate download chains for different content types. This will
// allow audio and video to be downloaded in parallel.
Expand Down Expand Up @@ -273,30 +246,57 @@ shaka.offline.DownloadManager.prototype.downloadSegment_ = function(segment) {
shaka.util.Error.Category.STORAGE,
shaka.util.Error.Code.OPERATION_ABORTED));
}
if (segment.endByte == null) {
// We didn't know the size, so it was an estimate.
this.manifest_.size += byteCount;
this.bandwidthBytesDownloaded_ += segment.bandwidthSize;
} else {
goog.asserts.assert(
byteCount == (segment.endByte - segment.startByte + 1),
'Incorrect download size');
this.givenBytesDownloaded_ += byteCount;
}

this.manifest_.size += byteCount;

this.markAsDone_(segment);
this.updateProgress_();
}.bind(this));
};


/**
* @param {!shaka.offline.DownloadManager.Segment} segment
* @private
*/
shaka.offline.DownloadManager.prototype.markAsPending_ = function(segment) {
/** @type {number} */
var estimatedSize = segment.endByte == null ?
segment.bandwidthSize :
(segment.endByte - segment.startByte + 1);

this.downloadExpected_ += estimatedSize;
};


/**
* @param {!shaka.offline.DownloadManager.Segment} segment
* @private
*/
shaka.offline.DownloadManager.prototype.markAsDone_ = function(segment) {
/** @type {number} */
var estimatedSize = segment.endByte == null ?
segment.bandwidthSize :
(segment.endByte - segment.startByte + 1);

this.downloadActual_ += estimatedSize;
};


/**
* Calls the progress callback.
* @private
*/
shaka.offline.DownloadManager.prototype.updateProgress_ = function() {
var progress = (this.givenBytesDownloaded_ + this.bandwidthBytesDownloaded_) /
(this.givenBytesTotal_ + this.bandwidthBytesTotal_);

goog.asserts.assert(this.manifest_, 'Must not be destroyed');

/** @type {shakaExtern.StoredContent} */
var content = shaka.offline.OfflineUtils.createStoredContent(this.manifest_);

/** @type {number} */
var progress = this.downloadExpected_ == 0 ?
0 :
(this.downloadActual_ / this.downloadExpected_);

this.config_.progressCallback(content, progress);
};
12 changes: 8 additions & 4 deletions test/offline/storage_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ describe('Storage', function() {
offlineUri: shaka.offline.OfflineScheme.manifestIdToUri(0),
originalManifestUri: originalUri,
duration: 20, // original manifest duration
size: 150,
size: jasmine.any(Number),
expiration: Infinity,
tracks: tracks,
appMetadata: {}
Expand All @@ -411,15 +411,19 @@ describe('Storage', function() {
switch (progress.calls.count()) {
case 1:
expect(percent).toBeCloseTo(54 / 150);
expect(storedContent.size).toBeCloseTo(54);
break;
case 2:
expect(percent).toBeCloseTo(67 / 150);
expect(storedContent.size).toBeCloseTo(67);
break;
case 3:
expect(percent).toBeCloseTo(133 / 150);
expect(storedContent.size).toBeCloseTo(133);
break;
default:
expect(percent).toBeCloseTo(1);
expect(storedContent.size).toBeCloseTo(150);
break;
}
});
Expand Down Expand Up @@ -466,15 +470,15 @@ describe('Storage', function() {
switch (progress.calls.count()) {
case 1:
expect(percent).toBeCloseTo(54 / 101);
expect(storedContent.size).toBe(71);
expect(storedContent.size).toBe(54);
break;
case 2:
expect(percent).toBeCloseTo(64 / 101);
expect(storedContent.size).toBe(84);
expect(storedContent.size).toBe(67);
break;
case 3:
expect(percent).toBeCloseTo(84 / 101);
expect(storedContent.size).toBe(150);
expect(storedContent.size).toBe(133);
break;
default:
expect(percent).toBeCloseTo(1);
Expand Down

0 comments on commit 6a03ceb

Please sign in to comment.