MediaKeySession Class

A key session of a MediaKeys object (https://w3c.github.io/encrypted-media/#mediakeysession-interface): the application generates a license request from Initialization Data (MediaKeySession.GenerateRequest), sends the message event to the license server, and gives the license to MediaKeySession.Update. With Clear Key, the license is a JSON Web Key Set that contains the keys.

public sealed class MediaKeySession : EventTarget
Inheritance
EventTarget MediaKeySession

Properties

NameDescription
ClosedGets the task that completes with the reason when the session is closed (it never fails).
ExpirationGets the expiration time of the keys, in milliseconds since the epoch, or NaN (always NaN with Clear Key).
KeyStatusesGets the statuses of the keys known to the session.
OnKeyStatusesChangeGets or sets the event handler of the keystatuseschange event.
OnMessageGets or sets the event handler of the message event.
SessionIdGets the session ID, or the empty string until the license request is generated or the session is loaded.

Gets the task that completes with the reason when the session is closed (it never fails).

public Task<MediaKeySessionClosedReason> Closed { get; }

Gets the expiration time of the keys, in milliseconds since the epoch, or NaN (always NaN with Clear Key).

public double Expiration { get; }

Gets the statuses of the keys known to the session.

public MediaKeyStatusMap KeyStatuses { get; }

OnKeyStatusesChange

MediaKeySession.cs

Gets or sets the event handler of the keystatuseschange event.

public Action<Event>? OnKeyStatusesChange { get; set; }

Gets or sets the event handler of the message event.

public Action<Event>? OnMessage { get; set; }

Gets the session ID, or the empty string until the license request is generated or the session is loaded.

public string SessionId { get; }

Methods

NameDescription
Close()Closes the session (https://w3c.github.io/encrypted-media/#dom-mediakeysession-close): its keys are no longer usable, and MediaKeySession.Closed completes with MediaKeySessionClosedReason.ClosedByApplication. The data of a persistent session stays stored.
GenerateRequest(string, ReadOnlySpan<byte>)Generates a license request from Initialization Data (https://w3c.github.io/encrypted-media/#dom-mediakeysession-generaterequest): the request is the MediaKeyMessageEvent fired once the task completes. With Clear Key, the request lists the key IDs of the Initialization Data. The task fails with a TypeErrorException when the data is empty or invalid, a NotSupportedError when the type is not supported or the data has no key ID for Clear Key, and an InvalidStateError when the session is already initialized or closed.
Load(string)Loads the data stored for a persistent session (https://w3c.github.io/encrypted-media/#dom-mediakeysession-load): the task completes with false when no data is stored for the session ID. The keys of the license are usable; a session whose license was removed has its record of license destruction, sent again as a license-release message.
Remove()Removes the license and the keys of the session (https://w3c.github.io/encrypted-media/#dom-mediakeysession-remove): the key statuses are cleared. A persistent session stores a record of license destruction, sent as a license-release message until its acknowledgement is given to MediaKeySession.Update.
Update(ReadOnlySpan<byte>)Provides a message of the license server to the CDM (https://w3c.github.io/encrypted-media/#dom-mediakeysession-update): with Clear Key, a license (a JSON Web Key Set, https://w3c.github.io/encrypted-media/#clear-key-license-format) whose keys become usable, or the acknowledgement of a license release, which closes the session. The task fails with a TypeErrorException when the response is empty or invalid, and an InvalidStateError when the session has no license request or is closed.

Closes the session (https://w3c.github.io/encrypted-media/#dom-mediakeysession-close): its keys are no longer usable, and MediaKeySession.Closed completes with MediaKeySessionClosedReason.ClosedByApplication. The data of a persistent session stays stored.

public Task Close()

GenerateRequest(string, ReadOnlySpan<byte>)

MediaKeySession.cs

Generates a license request from Initialization Data (https://w3c.github.io/encrypted-media/#dom-mediakeysession-generaterequest): the request is the MediaKeyMessageEvent fired once the task completes. With Clear Key, the request lists the key IDs of the Initialization Data. The task fails with a TypeErrorException when the data is empty or invalid, a NotSupportedError when the type is not supported or the data has no key ID for Clear Key, and an InvalidStateError when the session is already initialized or closed.

public Task GenerateRequest(string initDataType, ReadOnlySpan<byte> initData)

Parameters

initDataType string
The Initialization Data Type: cenc, keyids or webm.
initData ReadOnlySpan<byte>
The Initialization Data.

Returns

Task

See also

Load(string)

MediaKeySession.cs

Loads the data stored for a persistent session (https://w3c.github.io/encrypted-media/#dom-mediakeysession-load): the task completes with false when no data is stored for the session ID. The keys of the license are usable; a session whose license was removed has its record of license destruction, sent again as a license-release message.

public Task<bool> Load(string sessionId)

Removes the license and the keys of the session (https://w3c.github.io/encrypted-media/#dom-mediakeysession-remove): the key statuses are cleared. A persistent session stores a record of license destruction, sent as a license-release message until its acknowledgement is given to MediaKeySession.Update.

public Task Remove()

Update(ReadOnlySpan<byte>)

MediaKeySession.cs

Provides a message of the license server to the CDM (https://w3c.github.io/encrypted-media/#dom-mediakeysession-update): with Clear Key, a license (a JSON Web Key Set, https://w3c.github.io/encrypted-media/#clear-key-license-format) whose keys become usable, or the acknowledgement of a license release, which closes the session. The task fails with a TypeErrorException when the response is empty or invalid, and an InvalidStateError when the session has no license request or is closed.

public Task Update(ReadOnlySpan<byte> response)

Parameters

response ReadOnlySpan<byte>
The response (UTF-8 JSON for Clear Key, at most 64 KiB).

Returns

Task

See also