rx-player
Version:
Canal+ HTML5 Video Player
108 lines (107 loc) • 5.06 kB
JavaScript
;
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Manages the one-time notification of encryption/DRM data for a Representation.
*
* This class collects encryption data from parsed segments and ensures that
* the encryption notification callback is triggered exactly once, as soon as
* sufficient data is available.
*
* The notification may occur in two ways:
* 1. **Early notification** (constructor): If the DRM system ID is known and
* complete encryption data (with all key IDs) is already available, the
* notification is sent immediately to enable faster license negotiation.
* 2. **Deferred notification** (onNewProtectionData): If early notification
* doesn't occur, encryption data is collected from parsed segments and
* the notification is sent once any encryption data becomes available.
*
* @example
* const notifier = new EncryptionDataNotifier({
* drmSystemId: "edef8ba979d64acea3c827dcd51d21ed",
* representation,
* notify: (data) => callbacks.encryptionDataEncountered(data),
* });
*
* // Later, when a segment is parsed:
* notifier.onNewProtectionData(parsedSegmentEvent.protectionData);
*/
var EncryptionDataNotifier = /** @class */ (function () {
/**
* Create a new `EncryptionDataNotifier`.
* @param {Object} param0
* @param {string|undefined} param0.drmSystemId - If known the ID in hex
* format of the used DRM system (playready, widevine etc.).
* @param {Object} param0.representation - Object describing the
* Representation (including protection-related information). New encryption
* data might be added to it by this class.
* @param {Function} param0.notify - Function that will be called once new
* protection data has been detected.
*/
function EncryptionDataNotifier(_a) {
var drmSystemId = _a.drmSystemId, representation = _a.representation, notify = _a.notify;
this._hasSentEncryptionData = false;
this._representation = representation;
this._notify = notify;
// If the DRM system id is already known, and if we already have encryption data
// for it, we may not need to wait until the initialization segment is loaded to
// signal required protection data, thus performing License negotiations sooner
if (drmSystemId !== undefined) {
var encryptionData = representation.getEncryptionData(drmSystemId);
// If some key ids are not known yet, it may be safer to wait for this initialization
// segment to be loaded first
if (encryptionData.length > 0 &&
encryptionData.every(function (e) { return e.keyIds !== undefined; })) {
this._hasSentEncryptionData = true;
notify(encryptionData);
}
}
}
/**
* Signal that new protection data information is available.
* If necessary, that data will be added to the Representation and sent
* through the `notifiy` callback passed in constructor.
* @param {Array.<Object>} data - The new protection data encountered.
*/
EncryptionDataNotifier.prototype.onNewProtectionData = function (data) {
var e_1, _a;
try {
// Supplementary encryption information might have been parsed.
for (var data_1 = __values(data), data_1_1 = data_1.next(); !data_1_1.done; data_1_1 = data_1.next()) {
var protInfo = data_1_1.value;
// TODO better handle use cases like key rotation by not always grouping
// every protection data together? To check.
this._representation.addProtectionData(protInfo.initDataType, protInfo.keyId, protInfo.initData);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (data_1_1 && !data_1_1.done && (_a = data_1.return)) _a.call(data_1);
}
finally { if (e_1) throw e_1.error; }
}
// Now that the initialization segment has been parsed - which may have
// included encryption information - take care of the encryption event
// if not already done.
if (!this._hasSentEncryptionData) {
var allEncryptionData = this._representation.getAllEncryptionData();
if (allEncryptionData.length > 0) {
this._notify(allEncryptionData);
this._hasSentEncryptionData = true;
}
}
};
return EncryptionDataNotifier;
}());
exports.default = EncryptionDataNotifier;