Skip to content

Commit

Permalink
fix(offline): offline downloads are slow
Browse files Browse the repository at this point in the history
  • Loading branch information
Alvaro Velad committed Apr 27, 2022
1 parent feefd7b commit 82f3f76
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions demo/common/message_ids.js
Expand Up @@ -223,6 +223,7 @@ shakaDemo.MessageIds = {
NUMBER_INTEGER_WARNING: 'DEMO_NUMBER_INTEGER_WARNING',
NUMBER_NONZERO_DECIMAL_WARNING: 'DEMO_NUMBER_NONZERO_DECIMAL_WARNING',
NUMBER_NONZERO_INTEGER_WARNING: 'DEMO_NUMBER_NONZERO_INTEGER_WARNING',
NUMBER_OF_PARALLEL_SEGMENTS_BY_STREAM: 'DEMO_NUMBER_OF_PARALLEL_SEGMENTS_BY_STREAM',
OFFLINE_SECTION_HEADER: 'DEMO_OFFLINE_SECTION_HEADER',
PREFER_FORCED_SUBS: 'DEMO_PREFER_FORCED_SUBS',
PREFER_NATIVE_HLS: 'DEMO_PREFER_NATIVE_HLS',
Expand Down
4 changes: 3 additions & 1 deletion demo/config.js
Expand Up @@ -341,7 +341,9 @@ shakaDemo.Config = class {
const docLink = this.resolveExternLink_('.OfflineConfiguration');
this.addSection_(MessageIds.OFFLINE_SECTION_HEADER, docLink)
.addBoolInput_(MessageIds.USE_PERSISTENT_LICENSES,
'offline.usePersistentLicense');
'offline.usePersistentLicense')
.addNumberInput_(MessageIds.NUMBER_OF_PARALLEL_SEGMENTS_BY_STREAM,
'offline.numberOfParallelSegmentsByStream');
}

/** @private */
Expand Down
1 change: 1 addition & 0 deletions demo/locales/en.json
Expand Up @@ -159,6 +159,7 @@
"DEMO_NUMBER_INTEGER_WARNING": "Must be a positive integer.",
"DEMO_NUMBER_NONZERO_DECIMAL_WARNING": "Must be a positive, nonzero number.",
"DEMO_NUMBER_NONZERO_INTEGER_WARNING": "Must be a positive, nonzero integer.",
"DEMO_NUMBER_OF_PARALLEL_SEGMENTS_BY_STREAM": "Number of parallel segments by stream",
"DEMO_OBSERVE_QUALITY_CHANGES": "Observe media quality changes",
"DEMO_OFFLINE": "Downloadable",
"DEMO_OFFLINE_SEARCH": "Filters for assets that can be stored offline.",
Expand Down
4 changes: 4 additions & 0 deletions demo/locales/source.json
Expand Up @@ -639,6 +639,10 @@
"description": "A warning on number inputs, telling the user what the expected input format is.",
"message": "Must be a positive, nonzero integer."
},
"DEMO_NUMBER_OF_PARALLEL_SEGMENTS_BY_STREAM": {
"description": "The name of a configuration value.",
"message": "Number of parallel segments by stream."
},
"DEMO_OBSERVE_QUALITY_CHANGES": {
"description": "The name of a configuration value.",
"message": "Observe media quality changes"
Expand Down
3 changes: 3 additions & 0 deletions demo/main.js
Expand Up @@ -582,7 +582,10 @@ shakaDemo.Main = class {
};
asset.storedProgress = 0;
this.dispatchEventWithName_('shaka-main-offline-progress');
const start = Date.now();
const stored = await storage.store(asset.manifestUri, metadata).promise;
const end = Date.now();
console.log('Download time:', end - start);
asset.storedContent = stored;
} catch (error) {
this.onError_(/** @type {!shaka.util.Error} */ (error));
Expand Down
9 changes: 8 additions & 1 deletion externs/shaka/player.js
Expand Up @@ -1075,7 +1075,8 @@ shaka.extern.CmcdConfiguration;
* function(shaka.extern.TrackList):!Promise<shaka.extern.TrackList>,
* downloadSizeCallback: function(number):!Promise<boolean>,
* progressCallback: function(shaka.extern.StoredContent,number),
* usePersistentLicense: boolean
* usePersistentLicense: boolean,
* numberOfParallelSegmentsByStream: number
* }}
*
* @property {function(shaka.extern.TrackList):!Promise<shaka.extern.TrackList>}
Expand All @@ -1098,6 +1099,12 @@ shaka.extern.CmcdConfiguration;
* license. A network will be required to retrieve a temporary license to
* view.
* Defaults to <code>true</code>.
* @property {number} numberOfParallelSegmentsByStream
* Number of parallel segments by stream. Putting a number other than the
* default can help reduce download time.
* Note: normally browsers limit to 5 request in parallel, so putting a
* number higher than this will not help it download faster.
* Defaults to <code>1</code>.
* @exportDoc
*/
shaka.extern.OfflineConfiguration;
Expand Down
11 changes: 10 additions & 1 deletion lib/offline/storage.js
Expand Up @@ -1334,10 +1334,19 @@ shaka.offline.Storage = class {
if (!toDownload.has(pendingSegmentRefId)) {
const estimateId = downloader.addDownloadEstimate(
estimator.getSegmentEstimate(stream.id, segment));
let groupId = stream.id;
const numberOfParallelSegmentsByStream =
config.offline.numberOfParallelSegmentsByStream;
if (numberOfParallelSegmentsByStream > 1) {
// Note: 1000 is a random number to not coincide with other groups
// of other streams
groupId += 1000 + Math.floor(Math.random() *
numberOfParallelSegmentsByStream);
}
const segmentDownload = new shaka.offline.DownloadInfo(
segment,
estimateId,
stream.id,
groupId,
/* isInitSegment= */ false);
toDownload.set(pendingSegmentRefId, segmentDownload);
}
Expand Down
2 changes: 2 additions & 0 deletions lib/util/player_configuration.js
Expand Up @@ -222,6 +222,8 @@ shaka.util.PlayerConfiguration = class {
// unexpected behaviours when someone tries to plays downloaded content
// without a persistent license.
usePersistentLicense: true,

numberOfParallelSegmentsByStream: 1,
};

const abr = {
Expand Down

0 comments on commit 82f3f76

Please sign in to comment.