Skip to content
This repository has been archived by the owner on Jan 12, 2019. It is now read-only.

Cache encryption keys #1433

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/media-segment-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ const handleErrors = (error, request) => {
return null;
};

const cachedEncryptionKeys_ = {};

/**
* Handle responses for key data and convert the key data to the correct format
* for the decryption step later
Expand Down Expand Up @@ -160,6 +162,7 @@ const handleKeyResponse = (segment, finishProcessingFn) => (error, request) => {
view.getUint32(8),
view.getUint32(12)
]);
cachedEncryptionKeys_[segment.key.resolvedUri] = segment.key.bytes.slice(0);
return finishProcessingFn(null, segment);
};

Expand Down Expand Up @@ -401,14 +404,18 @@ export const mediaSegmentRequest = (xhr,

// optionally, request the decryption key
if (segment.key) {
const keyRequestOptions = videojs.mergeOptions(xhrOptions, {
uri: segment.key.resolvedUri,
responseType: 'arraybuffer'
});
const keyRequestCallback = handleKeyResponse(segment, finishProcessingFn);
const keyXhr = xhr(keyRequestOptions, keyRequestCallback);

activeXhrs.push(keyXhr);
if (segment.key.resolvedUri in cachedEncryptionKeys_) {
segment.key.bytes = cachedEncryptionKeys_[segment.key.resolvedUri].slice(0);
} else {
const keyRequestOptions = videojs.mergeOptions(xhrOptions, {
uri: segment.key.resolvedUri,
responseType: 'arraybuffer'
});
const keyRequestCallback = handleKeyResponse(segment, finishProcessingFn);
const keyXhr = xhr(keyRequestOptions, keyRequestCallback);

activeXhrs.push(keyXhr);
}
}

// optionally, request the associated media init segment
Expand Down