1# @ohos.multimedia.drm (Digital Rights Management)
2
3The Digital Rights Management (DRM) framework provides APIs for you to develop digital rights management for your audio and video applications. You can call the DRM schemes (which exist as DRM plug-ins) provided by the system to implement the following features:
4
5* DRM certificate management: generates provision requests and processes responses to these requests. Such a request/response exchange occurs between an application and a provisioning server to retrieve a DRM certificate.
6* DRM media key management: generates media key requests, processes responses to these requests, and manages offline media keys. Such a request/response exchange occurs between an application and a license server to obtain or release a media key, which is used to decrypt DRM-protected content.
7* DRM authorization: authorizes access to DRM-protected content based on media keys.
8* DRM decryption: decrypts DRM-protected content.
9
10> **NOTE**
11> The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version.
12
13## Modules to Import
14
15```ts
16import { drm } from '@kit.DrmKit';
17```
18
19## DrmErrorCode
20
21Enumerates the DRM error codes.
22
23**System capability**: SystemCapability.Multimedia.Drm.Core
24
25| Name                      | Value  | Description           |
26| ------------------------- | ---- | ------------    |
27| ERROR_UNKNOWN       | 24700101    | Unknown error.  |
28| MAX_SYSTEM_NUM_REACHED   | 24700103    | The number of **MediaKeySystem** instances reaches the upper limit (64).   |
29| MAX_SESSION_NUM_REACHED    | 24700104    | The number of **MediaKeySession** instances reaches the upper limit (64).    |
30| SERVICE_FATAL_ERROR  | 24700201    | DRM service error.    |
31
32## PreDefinedConfigName
33
34Enumerates the names of predefined configuration items.
35
36**System capability**: SystemCapability.Multimedia.Drm.Core
37
38| Name                      | Value  | Description           |
39| ------------------------- | ---- | ------------    |
40| CONFIG_DEVICE_VENDOR        | 'vendor'    | Plug-in vendor name, which corresponds to the value of **vendor** in the return value of [getConfigurationString](#getconfigurationstring).  |
41| CONFIG_DEVICE_VERSION    | 'version'    | Plug-in version number, which corresponds to the value of **version** in the return value of [getConfigurationString](#getconfigurationstring).    |
42| CONFIG_DEVICE_DESCRIPTION     | 'description'    | Device descriptor, which corresponds to the value of **description** in the return value of [getConfigurationString](#getconfigurationstring).     |
43| CONFIG_DEVICE_ALGORITHMS   | 'algorithms'    | Supported algorithm names, which correspond to the value of **algorithms** in the return value of [getConfigurationString](#getconfigurationstring).    |
44| CONFIG_DEVICE_UNIQUE_ID    | 'deviceUniqueId'    | Unique device ID, which corresponds to the value of **deviceUniqueId** in the return value of [getConfigurationByteArray](#getconfigurationbytearray).    |
45| CONFIG_SESSION_MAX         | 'maxSessionNum'    | Maximum number of sessions supported by the device, which corresponds to the value of **maxSessionNum** in the return value of [getConfigurationString](#getconfigurationstring).    |
46| CONFIG_SESSION_CURRENT   | 'currentSessionNum'    | Number of existing sessions, which corresponds to the value of **currentSessionNum** in the return value of [getConfigurationString](#getconfigurationstring).    |
47
48## MediaKeyType
49
50Enumerates the types of media keys.
51
52**System capability**: SystemCapability.Multimedia.Drm.Core
53
54| Name                      | Value  | Description           |
55| ------------------------- | ---- | ------------    |
56| MEDIA_KEY_TYPE_OFFLINE        | 0    | Offline.  |
57| MEDIA_KEY_TYPE_ONLINE   | 1    | Online.    |
58
59## OfflineMediaKeyStatus
60
61Enumerates the statuses of offline media keys.
62
63**System capability**: SystemCapability.Multimedia.Drm.Core
64
65| Name                      | Value  | Description           |
66| ------------------------- | ---- | ------------    |
67| OFFLINE_MEDIA_KEY_STATUS_UNKNOWN        | 0    | Unknown status.  |
68| OFFLINE_MEDIA_KEY_STATUS_USABLE   | 1    | The media key is available.    |
69| OFFLINE_MEDIA_KEY_STATUS_INACTIVE     | 2    | The media key is inactive.      |
70
71## CertificateStatus
72
73Enumerates the statuses of DRM certificates.
74
75**System capability**: SystemCapability.Multimedia.Drm.Core
76
77| Name                      | Value  | Description           |
78| ------------------------- | ---- | ------------    |
79| CERT_STATUS_PROVISIONED        | 0    | A DRM certificate has been installed on the device.  |
80| CERT_STATUS_NOT_PROVISIONED   | 1    | No DRM certificate is installed on the device.    |
81| CERT_STATUS_EXPIRED    | 2    | The DRM certificate has expired.      |
82| CERT_STATUS_INVALID  | 3    | The DRM certificate is invalid.    |
83| CERT_STATUS_UNAVAILABLE  | 4    | The DRM certificate is unavailable.    |
84
85## MediaKeyRequestType
86
87Enumerates the types of media key requests.
88
89**Atomic service API**: This API can be used in atomic services since API version 12.
90
91**System capability**: SystemCapability.Multimedia.Drm.Core
92
93| Name                      | Value  | Description           |
94| ------------------------- | ---- | ------------    |
95| MEDIA_KEY_REQUEST_TYPE_UNKNOWN        | 0    | Unknown type.  |
96| MEDIA_KEY_REQUEST_TYPE_INITIAL    | 1    | Initial request.    |
97| MEDIA_KEY_REQUEST_TYPE_RENEWAL     | 2    | Renewal request.      |
98| MEDIA_KEY_REQUEST_TYPE_RELEASE   | 3    | Release request.    |
99| MEDIA_KEY_REQUEST_TYPE_NONE    | 4    | None.    |
100| MEDIA_KEY_REQUEST_TYPE_UPDATE    | 5    | Update request.    |
101
102## ContentProtectionLevel
103
104Enumerates the content protection levels.
105
106**Atomic service API**: This API can be used in atomic services since API version 12.
107
108**System capability**: SystemCapability.Multimedia.Drm.Core
109
110| Name                      | Value  | Description           |
111| ------------------------- | ---- | ------------    |
112| CONTENT_PROTECTION_LEVEL_UNKNOWN        | 0    | Unknown content protection level.  |
113| CONTENT_PROTECTION_LEVEL_SW_CRYPTO   | 1    | Software content protection level.    |
114| CONTENT_PROTECTION_LEVEL_HW_CRYPTO    | 2    | Hardware content protection level.      |
115| CONTENT_PROTECTION_LEVEL_ENHANCED_HW  | 3    | Enhanced hardware content protection level.    |
116| CONTENT_PROTECTION_LEVEL_MAX  | 4    | Highest content protection level.    |
117
118## ProvisionRequest
119
120Describes a provision request, which is used to request a DRM certificate from a provisioning server.
121
122**System capability**: SystemCapability.Multimedia.Drm.Core
123
124| Name     | Type                          | Mandatory| Description        |
125| -------- | ----------------------------- |---- | ------------- |
126| data   | Uint8Array | Yes | Data carried in the provision request.     |
127| defaultURL     | string                 | Yes | URL of the provisioning server (which provisions DRM certificates).      |
128
129## OptionsData
130
131Describes the optional data carried in a media key request.
132
133**Atomic service API**: This API can be used in atomic services since API version 12.
134
135**System capability**: SystemCapability.Multimedia.Drm.Core
136
137| Name     | Type                          | Mandatory| Description        |
138| -------- | ----------------------------- |---- | ------------- |
139| name   | string | Yes | Name of the optional data.     |
140| value     | string                 | Yes | Value of the optional data.      |
141
142## MediaKeyRequest
143
144Describes a media key request.
145
146**Atomic service API**: This API can be used in atomic services since API version 12.
147
148**System capability**: SystemCapability.Multimedia.Drm.Core
149
150| Name     | Type                          | Mandatory| Description        |
151| -------- | ----------------------------- |---- | ------------- |
152| mediaKeyRequestType   | [MediaKeyRequestType](#mediakeyrequesttype) | Yes | Type of the media key request.     |
153| data     | Uint8Array                 | Yes | Data carried in the media key request.      |
154| defaultURL     | string                 | Yes | URL of the license server (which provisions media keys).      |
155
156## EventInfo
157
158Describes the event information.
159
160**Atomic service API**: This API can be used in atomic services since API version 12.
161
162**System capability**: SystemCapability.Multimedia.Drm.Core
163
164| Name     | Type                          | Mandatory| Description        |
165| -------- | ----------------------------- |---- | ------------- |
166| info   | Uint8Array | Yes | Event information.     |
167| extraInfo     | string                 | Yes | Extended event information.      |
168
169## StatisticKeyValue
170
171Describes the statistical information.
172
173**System capability**: SystemCapability.Multimedia.Drm.Core
174
175| Name     | Type                          | Mandatory| Description        |
176| -------- | ----------------------------- |---- | ------------- |
177| name   | string | Yes | Name of the statistical item.     |
178| value     | string                 | Yes | Value of the statistical item.      |
179
180## MediaKeyStatus
181
182Describes the media key status.
183
184**Atomic service API**: This API can be used in atomic services since API version 12.
185
186**System capability**: SystemCapability.Multimedia.Drm.Core
187
188| Name     | Type                          | Mandatory| Description        |
189| -------- | ----------------------------- |---- | ------------- |
190| name   | string | Yes | Name of the media key status (such as the media key expiration time and content protection level).     |
191| value     | string                 | Yes | Value of the media key status.      |
192
193## KeysInfo
194
195Describes the information about media keys.
196
197**Atomic service API**: This API can be used in atomic services since API version 12.
198
199**System capability**: SystemCapability.Multimedia.Drm.Core
200
201| Name     | Type                          | Mandatory| Description        |
202| -------- | ----------------------------- |---- | ------------- |
203| keyId   | Uint8Array | Yes | Media key ID.     |
204| value     | string                 | Yes | Media key status.      |
205
206## MediaKeySystemInfo
207
208Describes the DRM information, which is used to encrypt content.
209
210**Atomic service API**: This API can be used in atomic services since API version 12.
211
212**System capability**: SystemCapability.Multimedia.Drm.Core
213
214| Name     | Type                          | Mandatory| Description        |
215| -------- | ----------------------------- |---- | ------------- |
216| uuid   | string | Yes | UUID of the DRM content protection system.     |
217| pssh     | Uint8Array                 | Yes | Protection System Specific Header (PSSH) in the DRM information.      |
218
219## MediaKeySystemDescription<sup>12+</sup>
220
221Describes the plug-in information.
222
223**System capability**: SystemCapability.Multimedia.Drm.Core
224
225| Name     | Type                          | Mandatory| Description        |
226| -------- | ----------------------------- |---- | ------------- |
227| name   | string | Yes | Name of the plug-in.     |
228| uuid   | string | Yes | UUID of the plug-in.     |
229
230## drm.createMediaKeySystem
231
232createMediaKeySystem(name: string): MediaKeySystem
233
234Creates a **MediaKeySystem** instance.
235
236**System capability**: SystemCapability.Multimedia.Drm.Core
237
238**Parameters**
239
240| Name    | Type                                            | Mandatory| Description                          |
241| -------- | ----------------------------------------------- | ---- | ---------------------------- |
242| name  | string     | Yes  | DRM scheme name.                  |
243
244**Return value**
245
246| Type                                            | Description                          |
247| ----------------------------------------------- | ---------------------------- |
248| [MediaKeySystem](#mediakeysystem)           | **MediaKeySystem** instance.                  |
249
250**Error codes**
251
252For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
253
254| ID        | Error Message       |
255| --------------- | --------------- |
256| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified. 2.Parameter verification failed.               |
257| 24700101                |  All unknown errors                  |
258| 24700103                |  Meet max MediaKeySystem num limit                  |
259| 24700201                |  Fatal service error, for example, service died                  |
260
261**Example**
262
263```ts
264import { drm } from '@kit.DrmKit';
265import { BusinessError } from '@kit.BasicServicesKit';
266try {
267  let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
268} catch (err) {
269  let error = err as BusinessError;
270  console.error(`createMediaKeySystem ERROR: ${error}`);
271}
272```
273
274## drm.isMediaKeySystemSupported
275
276isMediaKeySystemSupported(name: string): boolean
277
278Checks whether the device supports the specified DRM scheme.
279
280**System capability**: SystemCapability.Multimedia.Drm.Core
281
282**Parameters**
283
284| Name    | Type                                            | Mandatory| Description                          |
285| -------- | ----------------------------------------------- | ---- | ---------------------------- |
286| name  | string     | Yes  | DRM scheme name.                  |
287
288**Return value**
289
290| Type                                            | Description                          |
291| ----------------------------------------------- | ---------------------------- |
292| boolean          | Returns **true** if the device supports the DRM scheme; returns **false** otherwise.                  |
293
294**Error codes**
295
296For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
297
298| ID        | Error Message       |
299| --------------- | --------------- |
300| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified. 2.Parameter verification failed, the param name's length is zero or too big(exceeds 4096 Bytes).               |
301| 24700101                |  All unknown errors                  |
302| 24700201                |  Fatal service error, for example, service died                  |
303
304**Example**
305
306```ts
307import { drm } from '@kit.DrmKit';
308import { BusinessError } from '@kit.BasicServicesKit';
309
310try {
311  let supported: boolean = drm.isMediaKeySystemSupported("com.clearplay.drm");
312  console.log("isMediaKeySystemSupported: ", supported);
313} catch (err) {
314  let error = err as BusinessError;
315  console.error(`isMediaKeySystemSupported ERROR: ${error}`);
316}
317```
318
319## drm.isMediaKeySystemSupported
320
321isMediaKeySystemSupported(name: string, mimeType: string): boolean
322
323Checks whether the device supports the combination of the DRM scheme and MIME type.
324
325**System capability**: SystemCapability.Multimedia.Drm.Core
326
327**Parameters**
328
329| Name    | Type                                            | Mandatory| Description                          |
330| -------- | ----------------------------------------------- | ---- | ---------------------------- |
331| name  | string     | Yes  | DRM scheme name.                  |
332| mimeType  | string     | Yes  | MIME type, which is determined by the DRM scheme.                  |
333
334**Return value**
335
336| Type                                            | Description                          |
337| ----------------------------------------------- | ---------------------------- |
338| boolean          | Returns **true** if the device supports the combination; returns **false** otherwise.                  |
339
340**Error codes**
341
342For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
343
344| ID        | Error Message       |
345| --------------- | --------------- |
346| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed.                |
347| 24700101                |  All unknown errors                  |
348| 24700201                |  Fatal service error, for example, service died                  |
349
350**Example**
351
352```ts
353import { drm } from '@kit.DrmKit';
354import { BusinessError } from '@kit.BasicServicesKit';
355
356try {
357  let supported: boolean = drm.isMediaKeySystemSupported("com.clearplay.drm", "video/avc");
358  console.log("isMediaKeySystemSupported: ", supported);
359} catch (err) {
360  let error = err as BusinessError;
361  console.error(`isMediaKeySystemSupported ERROR: ${error}`);
362}
363```
364
365## drm.isMediaKeySystemSupported
366
367isMediaKeySystemSupported(name: string, mimeType: string, level: ContentProtectionLevel): boolean
368
369Checks whether the device supports the combination of the DRM scheme, MIME type, and content protection level.
370
371**System capability**: SystemCapability.Multimedia.Drm.Core
372
373**Parameters**
374
375| Name    | Type                                            | Mandatory| Description                          |
376| -------- | ----------------------------------------------- | ---- | ---------------------------- |
377| name  | string     | Yes  | DRM scheme name.                  |
378| mimeType  | string     | Yes  | MIME type, which is determined by the DRM scheme.                  |
379| level  | [ContentProtectionLevel](#contentprotectionlevel)     | Yes  | Content protection level.                  |
380
381**Return value**
382
383| Type                                            | Description                          |
384| ----------------------------------------------- | ---------------------------- |
385| boolean          | Returns **true** if the device supports the combination; returns **false** otherwise.                  |
386
387**Error codes**
388
389For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
390
391| ID        | Error Message       |
392| --------------- | --------------- |
393| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed.               |
394| 24700101                |  All unknown errors                  |
395| 24700201                |  Fatal service error, for example, service died                  |
396
397**Example**
398
399```ts
400import { drm } from '@kit.DrmKit';
401import { BusinessError } from '@kit.BasicServicesKit';
402
403try {
404  let supported: boolean = drm.isMediaKeySystemSupported("com.clearplay.drm", "video/avc", drm.ContentProtectionLevel.CONTENT_PROTECTION_LEVEL_SW_CRYPTO);
405  console.log("isMediaKeySystemSupported: ", supported);
406} catch (err) {
407  let error = err as BusinessError;
408  console.error(`isMediaKeySystemSupported ERROR: ${error}`);
409}
410```
411
412## drm.getMediaKeySystemUuid<sup>12+</sup>
413
414getMediaKeySystemUuid(name: string): string;
415
416Obtains the UUID of the DRM content protection system supported by the specified DRM scheme.
417
418**System capability**: SystemCapability.Multimedia.Drm.Core
419
420**Parameters**
421
422| Name    | Type                                            | Mandatory| Description                          |
423| -------- | ----------------------------------------------- | ---- | ---------------------------- |
424| name  | string     | Yes  | DRM scheme name.                  |
425
426**Return value**
427
428| Type                                            | Description                          |
429| ----------------------------------------------- | ---------------------------- |
430| uuid  | string     | Yes  | UUID of the DRM content protection system.                  |
431
432**Error codes**
433
434For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
435
436| ID        | Error Message       |
437| --------------- | --------------- |
438| 401                |  The parameter check failed.Possibly because: 1.Mandatory parameters are left unspecified. 2.Parameter verification failed.                |
439| 24700101                |  All unknown errors                  |
440| 24700201                |  Fatal service error, for example, service died                  |
441
442**Example**
443
444```ts
445import { drm } from '@kit.DrmKit';
446import { BusinessError } from '@kit.BasicServicesKit';
447try {
448  let uuid: String = drm.getMediaKeySystemUuid("com.clearplay.drm");
449  console.log("getMediaKeySystemUuid: ", uuid);
450} catch (err) {
451  let error = err as BusinessError;
452  console.error(`getMediaKeySystemUuid ERROR: ${error}`);
453}
454```
455
456## drm.getMediaKeySystems<sup>12+</sup>
457
458getMediaKeySystems(): MediaKeySystemDescription[]
459
460Obtains the list of plug-ins supported by the device.
461
462**System capability**: SystemCapability.Multimedia.Drm.Core
463
464**Return value**
465
466| Type                                            | Description                          |
467| ----------------------------------------------- | ---------------------------- |
468| [MediaKeySystemDescription[]](#mediakeysystemdescription12)           | Array of the supported plug-ins.                  |
469
470**Error codes**
471
472For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
473
474| ID        | Error Message       |
475| --------------- | --------------- |
476| 24700101                |  All unknown errors                  |
477| 24700201                |  Fatal service error, for example, service died                  |
478
479**Example**
480
481```ts
482import { drm } from '@kit.DrmKit';
483import { BusinessError } from '@kit.BasicServicesKit';
484try {
485  let description: drm.MediaKeySystemDescription[] = drm.getMediaKeySystems();
486} catch (err) {
487  let error = err as BusinessError;
488  console.error(`getMediaKeySystems ERROR: ${error}`);
489}
490```
491
492## MediaKeySystem
493Implements **MediaKeySystem** instance management. Specifically, it provides APIs to request and process DRM certificates, creates session, manages offline media key, obtain DRM statistical information, and obtain device configuration information. Before calling any API in **MediaKeySystem**, you must use [createMediaKeySystem](#drmcreatemediakeysystem) to create a **MediaKeySystem** instance.
494
495### setConfigurationString
496
497setConfigurationString(configName: string, value: string): void
498
499Sets a configuration item in the form of a string.
500
501**System capability**: SystemCapability.Multimedia.Drm.Core
502
503**Parameters**
504
505| Name    | Type                                            | Mandatory| Description                          |
506| -------- | ----------------------------------------------- | ---- | ---------------------------- |
507| configName  | string     | Yes  | Name of the configuration item, which is determined by the DRM scheme on the device and cannot be empty. For details about available options, see [PreDefinedConfigName](#predefinedconfigname).                  |
508| value  | string     | Yes  | Value of the configuration item.                  |
509
510**Error codes**
511
512For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
513
514| ID        | Error Message       |
515| --------------- | --------------- |
516| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed.|
517| 24700101                |  All unknown errors                  |
518| 24700201                |  Fatal service error, for example, service died                  |
519
520**Example**
521
522```ts
523import { drm } from '@kit.DrmKit';
524import { BusinessError } from '@kit.BasicServicesKit';
525
526let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
527try {
528  mediaKeySystem.setConfigurationString("stringConfigName", "stringConfigValue"); // Ensure that stringConfigName is configurable.
529} catch (err) {
530  let error = err as BusinessError;
531  console.error(`setConfigurationString ERROR: ${error}`);
532}
533```
534
535### getConfigurationString
536
537getConfigurationString(configName: string): string
538
539Obtains the value of a configuration item in the form of a string.
540
541**System capability**: SystemCapability.Multimedia.Drm.Core
542
543**Parameters**
544
545| Name    | Type                                            | Mandatory| Description                          |
546| -------- | ----------------------------------------------- | ---- | ---------------------------- |
547| configName  | string     | Yes  | Name of the configuration item, which is determined by the DRM scheme on the device and cannot be empty. For details about available options, see [PreDefinedConfigName](#predefinedconfigname).                  |
548
549**Return value**
550
551| Type                                            | Description                          |
552| ----------------------------------------------- | ---------------------------- |
553| string          | Value of the configuration item in the form of a string.                  |
554
555**Error codes**
556
557For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
558
559| ID        | Error Message       |
560| --------------- | --------------- |
561| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified. 2.Parameter verification failed, the param's length is zero or too big(exceeds 4096 Bytes).                              |
562| 24700101                |  All unknown errors                  |
563| 24700201                |  Fatal service error, for example, service died                  |
564
565**Example**
566
567```ts
568import { drm } from '@kit.DrmKit';
569import { BusinessError } from '@kit.BasicServicesKit';
570
571let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
572try {
573  let configValue: string = mediaKeySystem.getConfigurationString("vendor");
574} catch (err) {
575  let error = err as BusinessError;
576  console.error(`getConfigurationString ERROR: ${error}`);
577}
578```
579
580### setConfigurationByteArray
581
582setConfigurationByteArray(configName: string, value: Uint8Array): void
583
584Sets a configuration item in the form of a byte array.
585
586**System capability**: SystemCapability.Multimedia.Drm.Core
587
588**Parameters**
589
590| Name    | Type                                            | Mandatory| Description                          |
591| -------- | ----------------------------------------------- | ---- | ---------------------------- |
592| configName  | string     | Yes  | Name of the configuration item, which is determined by the DRM scheme on the device and cannot be empty. For details about available options, see [PreDefinedConfigName](#predefinedconfigname).                  |
593| value  | Uint8Array     | Yes  | Value of the configuration item in the form of an array. The specific value is determined by the DRM scheme on the device.                  |
594
595**Error codes**
596
597For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
598
599| ID        | Error Message       |
600| --------------- | --------------- |
601| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed.               |
602| 24700101                |  All unknown errors.                  |
603| 24700201                |  Fatal service error, for example, service died.                  |
604
605**Example**
606
607```ts
608import { drm } from '@kit.DrmKit';
609import { BusinessError } from '@kit.BasicServicesKit';
610
611let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
612// Set configValue based on project requirements.
613let configValue: Uint8Array = new Uint8Array([0x00, 0x00, 0x00, 0x00]);
614try {
615  // Ensure that byteArrayConfigName of the current DRM scheme is configurable.
616  mediaKeySystem.setConfigurationByteArray("byteArrayConfigName", configValue);
617} catch (err) {
618  let error = err as BusinessError;
619  console.error(`setConfigurationByteArray ERROR: ${error}`);
620}
621```
622
623### getConfigurationByteArray
624
625getConfigurationByteArray(configName: string): Uint8Array
626
627Obtains the value of a configuration item in the form of a byte array.
628
629**System capability**: SystemCapability.Multimedia.Drm.Core
630
631**Parameters**
632
633| Name    | Type                                            | Mandatory| Description                          |
634| -------- | ----------------------------------------------- | ---- | ---------------------------- |
635| configName  | string     | Yes  | Name of the configuration item, which is determined by the DRM scheme on the device and cannot be empty. For details about available options, see [PreDefinedConfigName](#predefinedconfigname).                  |
636
637**Return value**
638
639| Type                                            | Description                          |
640| ----------------------------------------------- | ---------------------------- |
641| Uint8Array          | Value of the configuration item in the form of an array.                  |
642
643**Error codes**
644
645For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
646
647| ID        | Error Message       |
648| --------------- | --------------- |
649| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified. 2.Parameter verification failed.        |
650| 24700101                |  All unknown errors                  |
651| 24700201                |  Fatal service error, for example, service died                  |
652
653**Example**
654
655```ts
656import { drm } from '@kit.DrmKit';
657import { BusinessError } from '@kit.BasicServicesKit';
658
659let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
660try {
661  let configValue: Uint8Array = mediaKeySystem.getConfigurationByteArray("deviceUniqueId"); // Ensure that deviceUniqueId exists.
662} catch (err) {
663  let error = err as BusinessError;
664  console.error(`getConfigurationByteArray ERROR: ${error}`);
665}
666```
667
668### getStatistics
669
670getStatistics(): StatisticKeyValue[]
671
672Obtains the statistical information, including the number of current sessions, plug-in version, maximum decryption duration for each session, number of decryption times, and number of decryption failures.
673
674**System capability**: SystemCapability.Multimedia.Drm.Core
675
676**Return value**
677
678| Type                                            | Description                          |
679| ----------------------------------------------- | ---------------------------- |
680| [StatisticKeyValue[]](#statistickeyvalue)          | Statistical information.                  |
681
682**Error codes**
683
684For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
685
686| ID        | Error Message       |
687| --------------- | --------------- |
688| 24700101                |  All unknown errors                  |
689| 24700201                |  Fatal service error, for example, service died                  |
690
691**Example**
692
693```ts
694import { drm } from '@kit.DrmKit';
695import { BusinessError } from '@kit.BasicServicesKit';
696
697let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
698try {
699  let statisticKeyValue: drm.StatisticKeyValue[] = mediaKeySystem.getStatistics();
700} catch (err) {
701  let error = err as BusinessError;
702  console.error(`getConfigurationByteArray ERROR: ${error}`);
703}
704```
705
706### getMaxContentProtectionLevel
707
708getMaxContentProtectionLevel(): ContentProtectionLevel
709
710Obtains the maximum content protection level supported by the current DRM scheme.
711
712**System capability**: SystemCapability.Multimedia.Drm.Core
713
714**Return value**
715
716| Type                                            | Description                          |
717| ----------------------------------------------- | ---------------------------- |
718| [ContentProtectionLevel](#contentprotectionlevel)          | Maximum content protection level.                  |
719
720**Error codes**
721
722For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
723
724| ID        | Error Message       |
725| --------------- | --------------- |
726| 24700101                |  All unknown errors                  |
727| 24700201                |  Fatal service error, for example, service died                  |
728
729**Example**
730
731```ts
732import { drm } from '@kit.DrmKit';
733import { BusinessError } from '@kit.BasicServicesKit';
734
735let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
736try {
737  let maxLevel: drm.ContentProtectionLevel = mediaKeySystem.getMaxContentProtectionLevel();
738} catch (err) {
739  let error = err as BusinessError;
740  console.error(`getConfigurationByteArray ERROR: ${error}`);
741}
742```
743
744### generateKeySystemRequest
745
746generateKeySystemRequest(): Promise<ProvisionRequest\>
747
748Generates a provision request.
749
750**System capability**: SystemCapability.Multimedia.Drm.Core
751
752**Return value**
753
754| Type                                            | Description                          |
755| ----------------------------------------------- | ---------------------------- |
756| Promise<[ProvisionRequest](#provisionrequest)\>          | Promise used to return the provision request obtained. If a DRM certificate already exists on the device, a failure message is returned.  |
757
758**Error codes**
759
760For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
761
762| ID        | Error Message       |
763| --------------- | --------------- |
764| 24700101                |  All unknown errors                  |
765| 24700201                |  Fatal service error, for example, service died                  |
766
767**Example**
768
769```ts
770import { drm } from '@kit.DrmKit';
771import { BusinessError } from '@kit.BasicServicesKit';
772
773let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
774// Do not call this API if a DRM certificate already exists on the device.
775mediaKeySystem.generateKeySystemRequest().then((ProvisionRequest: drm.ProvisionRequest) => {
776  console.log("generateKeySystemRequest");
777}).catch((err: BusinessError) => {
778  console.error(`generateKeySystemRequest: ERROR: ${err}`);
779});
780```
781
782### processKeySystemResponse
783
784processKeySystemResponse(response: Uint8Array): Promise<void\>
785
786Processes a provision response.
787
788**System capability**: SystemCapability.Multimedia.Drm.Core
789
790**Parameters**
791
792| Name    | Type                                            | Mandatory| Description                          |
793| -------- | ----------------------------------------------- | ---- | ---------------------------- |
794| response  | Uint8Array     | Yes  | Provision response.                  |
795
796**Return value**
797
798| Type                                            | Description                          |
799| ----------------------------------------------- | ---------------------------- |
800| Promise<void\>          | Promise                  |
801
802**Error codes**
803
804For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
805
806| ID        | Error Message       |
807| --------------- | --------------- |
808| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed.         |
809| 24700101                |  All unknown errors                  |
810| 24700201                |  Fatal service error, for example, service died                  |
811
812**Example**
813
814```ts
815import { drm } from '@kit.DrmKit';
816import { BusinessError } from '@kit.BasicServicesKit';
817
818let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
819// keySystemResponse is the response obtained from the DRM service. Pass in the actual data obtained.
820let keySystemResponse = new Uint8Array([0x00, 0x00, 0x00, 0x00]);
821mediaKeySystem.processKeySystemResponse(keySystemResponse).then(() => {
822  console.log("processKeySystemResponse");
823}).catch((err: BusinessError) => {
824  console.error(`processKeySystemResponse: ERROR: ${err}`);
825});
826```
827
828### getCertificateStatus
829
830getCertificateStatus():CertificateStatus
831
832Obtains the status of the DRM certificate.
833
834**System capability**: SystemCapability.Multimedia.Drm.Core
835
836**Return value**
837
838| Type                                            | Description                          |
839| ----------------------------------------------- | ---------------------------- |
840| [CertificateStatus](#certificatestatus)          | Certificate status.                  |
841
842**Error codes**
843
844For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
845
846| ID        | Error Message       |
847| --------------- | --------------- |
848| 24700101                |  All unknown errors                  |
849| 24700201                |  Fatal service error, for example, service died                  |
850
851**Example**
852
853```ts
854import { drm } from '@kit.DrmKit';
855import { BusinessError } from '@kit.BasicServicesKit';
856
857let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
858try {
859  let certificateStatus: drm.CertificateStatus = mediaKeySystem.getCertificateStatus();
860} catch (err) {
861  let error = err as BusinessError;
862  console.error(`getCertificateStatus ERROR: ${error}`);
863}
864```
865
866### on('keySystemRequired')
867
868on(type: 'keySystemRequired', callback: (eventInfo: EventInfo) => void): void
869
870Subscribes to events indicating that the application requires a DRM certificate.
871
872**System capability**: SystemCapability.Multimedia.Drm.Core
873
874**Parameters**
875
876| Name     | Type                 | Mandatory| Description                                 |
877| -------- | -------------------- | ---- | ------------------------------------- |
878| type     | string               | Yes  | Event type. The event can be listened for when a **MediaKeySystem** instance is created. This event is triggered when the application requests a DRM certificate.|
879| callback | Callback\<[EventInfo](#eventinfo)\> | Yes  | Callback used to return the event information. If this event callback is returned, a DRM certificate must be requested.                |
880
881**Error codes**
882
883For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
884
885| ID        | Error Message       |
886| --------------- | --------------- |
887| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.               |
888| 24700101                |  All unknown errors                  |
889
890**Example**
891
892```ts
893import { drm } from '@kit.DrmKit';
894
895let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
896mediaKeySystem.on('keySystemRequired', (eventInfo: drm.EventInfo) => {
897  console.log('keySystemRequired ' + 'extra: ' + eventInfo.extraInfo + 'data: ' + eventInfo.info);
898});
899```
900
901### off('keySystemRequired')
902
903off(type: 'keySystemRequired', callback?: (eventInfo: EventInfo) => void): void
904
905Unsubscribes from events indicating that the application requests a DRM certificate.
906
907**System capability**: SystemCapability.Multimedia.Drm.Core
908
909**Parameters**
910
911| Name     | Type                 | Mandatory| Description                                 |
912| -------- | -------------------- | ---- | ------------------------------------- |
913| type     | string               | Yes  | Event type. The event can be listened for when a **MediaKeySystem** instance is created.|
914| callback | Callback\<[EventInfo](#eventinfo)\> | No  | Callback used to return the event information.                 |
915
916**Error codes**
917
918For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
919
920| ID        | Error Message       |
921| --------------- | --------------- |
922| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.               |
923| 24700101                |  All unknown errors                  |
924
925**Example**
926
927```ts
928let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
929mediaKeySystem.off('keySystemRequired');
930```
931
932### createMediaKeySession
933
934createMediaKeySession(level: ContentProtectionLevel): MediaKeySession
935
936Creates a **MediaKeySession** instance with the specified content protection level.
937
938**System capability**: SystemCapability.Multimedia.Drm.Core
939
940**Parameters**
941
942| Name    | Type                                            | Mandatory| Description                          |
943| -------- | ----------------------------------------------- | ---- | ---------------------------- |
944| level  | [ContentProtectionLevel](#contentprotectionlevel)     | Yes  | Content protection level.                  |
945
946**Return value**
947
948| Type                                            | Description                          |
949| ----------------------------------------------- | ---------------------------- |
950| [MediaKeySession](#mediakeysession)          | **MediaKeySession** instance.                  |
951
952**Error codes**
953
954For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
955
956| ID        | Error Message       |
957| --------------- | --------------- |
958| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified. 2.The param level exceeds reasonable range, please use value in ContentProtectionLevel.          |
959| 24700101                 |  All unknown errors                  |
960| 24700104                 |  Meet max MediaKeySession num limit                  |
961| 24700201                |  Fatal service error, for example, service died                  |
962
963**Example**
964
965```ts
966import { drm } from '@kit.DrmKit';
967import { BusinessError } from '@kit.BasicServicesKit';
968
969let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
970try {
971  let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession(drm.ContentProtectionLevel.CONTENT_PROTECTION_LEVEL_SW_CRYPTO);
972} catch (err) {
973  let error = err as BusinessError;
974  console.error(`createMediaKeySession ERROR: ${error}`);
975}
976```
977
978### createMediaKeySession
979
980createMediaKeySession(): MediaKeySession
981
982Creates a **MediaKeySession** instance with the default content protection level of the DRM scheme.
983
984**System capability**: SystemCapability.Multimedia.Drm.Core
985
986**Return value**
987
988| Type                                            | Description                          |
989| ----------------------------------------------- | ---------------------------- |
990| [MediaKeySession](#mediakeysession)          | **MediaKeySession** instance.                  |
991
992**Error codes**
993
994For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
995
996| ID        | Error Message       |
997| --------------- | --------------- |
998| 24700101                |  All unknown errors                  |
999| 24700104                 |  Meet max MediaKeySession num limit                  |
1000| 24700201                |  Fatal service error, for example, service died                  |
1001
1002**Example**
1003
1004```ts
1005import { drm } from '@kit.DrmKit';
1006import { BusinessError } from '@kit.BasicServicesKit';
1007
1008let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1009try {
1010  let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession();
1011} catch (err) {
1012  let error = err as BusinessError;
1013  console.error(`createMediaKeySession ERROR: ${error}`);
1014}
1015```
1016
1017### getOfflineMediaKeyIds
1018
1019getOfflineMediaKeyIds(): Uint8Array[]
1020
1021Obtains the IDs of offline media keys.
1022
1023**System capability**: SystemCapability.Multimedia.Drm.Core
1024
1025
1026**Return value**
1027
1028| Type                                            | Description                          |
1029| ----------------------------------------------- | ---------------------------- |
1030| Uint8Array[]          | Array holding the IDs of offline media keys.                  |
1031
1032**Error codes**
1033
1034For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1035
1036| ID        | Error Message       |
1037| --------------- | --------------- |
1038| 24700101                |  All unknown errors                  |
1039| 24700201                |  Fatal service error, for example, service died                  |
1040
1041**Example**
1042
1043```ts
1044import { drm } from '@kit.DrmKit';
1045import { BusinessError } from '@kit.BasicServicesKit';
1046
1047let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1048try {
1049  let offlineMediaKeyIds: Uint8Array[] = mediaKeySystem.getOfflineMediaKeyIds();
1050} catch (err) {
1051  let error = err as BusinessError;
1052  console.error(`getOfflineMediaKeyIds ERROR: ${error}`);
1053}
1054```
1055
1056### getOfflineMediaKeyStatus
1057
1058getOfflineMediaKeyStatus(mediaKeyId: Uint8Array): OfflineMediaKeyStatus
1059
1060Obtains the status of offline media keys with the specified IDs.
1061
1062**System capability**: SystemCapability.Multimedia.Drm.Core
1063
1064**Parameters**
1065
1066| Name    | Type                                            | Mandatory| Description                          |
1067| -------- | ----------------------------------------------- | ---- | ---------------------------- |
1068| mediaKeyId | Uint8Array     | Yes  | Array holding the IDs of offline media keys.                  |
1069
1070**Return value**
1071
1072| Type                                            | Description                          |
1073| ----------------------------------------------- | ---------------------------- |
1074| [OfflineMediaKeyStatus](#offlinemediakeystatus)          | Status of the offline media keys.                  |
1075
1076**Error codes**
1077
1078For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1079
1080| ID        | Error Message       |
1081| --------------- | --------------- |
1082| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed.            |
1083| 24700101                |  All unknown errors                  |
1084| 24700201                |  Fatal service error, for example, service died                  |
1085
1086**Example**
1087
1088```ts
1089import { drm } from '@kit.DrmKit';
1090import { BusinessError } from '@kit.BasicServicesKit';
1091
1092let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1093// mediaKeyId is the return value of processMediaKeyResponse or getOfflineMediaKeyIds. Pass in the actual data returned.
1094let mediaKeyId = new Uint8Array([0x00, 0x00, 0x00, 0x00]);
1095try {
1096  let configValue: drm.OfflineMediaKeyStatus = mediaKeySystem.getOfflineMediaKeyStatus(mediaKeyId);
1097} catch (err) {
1098  let error = err as BusinessError;
1099  console.error(`getOfflineMediaKeyStatus ERROR: ${error}`);
1100}
1101```
1102
1103### clearOfflineMediaKeys
1104
1105clearOfflineMediaKeys(mediaKeyId: Uint8Array): void
1106
1107Clears offline media keys by ID.
1108
1109**System capability**: SystemCapability.Multimedia.Drm.Core
1110
1111**Parameters**
1112
1113| Name    | Type                                            | Mandatory| Description                          |
1114| -------- | ----------------------------------------------- | ---- | ---------------------------- |
1115| mediaKeyId  | Uint8Array     | Yes  | Array holding the IDs of offline media keys.           |
1116
1117**Error codes**
1118
1119For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1120
1121| ID        | Error Message       |
1122| --------------- | --------------- |
1123| 401                |  The parameter check failed.Possibly because: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.           |
1124| 24700101                |  All unknown errors                  |
1125| 24700201                |  Fatal service error, for example, service died                  |
1126
1127**Example**
1128
1129```ts
1130import { drm } from '@kit.DrmKit';
1131import { BusinessError } from '@kit.BasicServicesKit';
1132
1133let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1134// mediaKeyId is the return value of processMediaKeyResponse or getOfflineMediaKeyIds. Pass in the actual data returned.
1135let mediaKeyId = new Uint8Array([0x00, 0x00, 0x00, 0x00]);
1136try {
1137  mediaKeySystem.clearOfflineMediaKeys(mediaKeyId);
1138} catch (err) {
1139  let error = err as BusinessError;
1140  console.error(`clearOfflineMediaKeys ERROR: ${error}`);
1141}
1142```
1143
1144### destroy
1145
1146destroy(): void
1147
1148Destroys this **MediaKeySystem** instance.
1149
1150**System capability**: SystemCapability.Multimedia.Drm.Core
1151
1152**Error codes**
1153
1154For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1155
1156| ID        | Error Message       |
1157| --------------- | --------------- |
1158| 24700101                |  All unknown errors                  |
1159| 24700201                |  Fatal service error, for example, service died                  |
1160
1161**Example**
1162
1163```ts
1164import { drm } from '@kit.DrmKit';
1165import { BusinessError } from '@kit.BasicServicesKit';
1166
1167let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1168try {
1169  mediaKeySystem.destroy();
1170} catch (err) {
1171  let error = err as BusinessError;
1172  console.error(`mediaKeySystem destroy ERROR: ${error}`);
1173}
1174```
1175
1176## MediaKeySession
1177Implements media key management. Before calling any API in **MediaKeySession**, you must use [createMediaKeySession](#createmediakeysession) to create a **MediaKeySession** instance.
1178
1179### generateMediaKeyRequest
1180
1181generateMediaKeyRequest(mimeType: string, initData: Uint8Array, mediaKeyType: number, options?: OptionsData[]): Promise<MediaKeyRequest\>
1182
1183Generates a media key request.
1184
1185**Atomic service API**: This API can be used in atomic services since API version 12.
1186
1187**System capability**: SystemCapability.Multimedia.Drm.Core
1188
1189**Parameters**
1190
1191| Name    | Type                                            | Mandatory| Description                          |
1192| -------- | ----------------------------------------------- | ---- | ---------------------------- |
1193| mimeType  | string     | Yes  | MIME type, which is determined by the DRM scheme.                  |
1194| initData  | Uint8Array     | Yes  | Initial data.                  |
1195| mediaKeyType| number     | Yes  | Type of the media key.                  | The value **0** means an online media key, and **1** means an offline media key.|
1196| options  | [OptionsData[]](#optionsdata)     | No  | Optional data.                  |
1197
1198**Return value**
1199
1200| Type                                            | Description                          |
1201| ----------------------------------------------- | ---------------------------- |
1202| Promise<[MediaKeyRequest](#mediakeyrequest)\>          | Promise used to return the media key request generated.                  |
1203
1204**Error codes**
1205
1206For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1207
1208| ID        | Error Message       |
1209| --------------- | --------------- |
1210| 401                | The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified or too many parameters. 2.Incorrect parameter types. 3.Parameter verification failed.              |
1211| 24700101                |  All unknown errors                  |
1212| 24700201                |  Fatal service error, for example, service died                  |
1213
1214**Example**
1215
1216```ts
1217import { drm } from '@kit.DrmKit';
1218import { BusinessError } from '@kit.BasicServicesKit';
1219
1220let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1221let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession();
1222// PSSH data is the descriptive header of the copyright protection system and is encapsulated in encrypted streams. Specifically, in MP4 file, the PSSH data is found within the PSSH box; for DASH streams, the PSSH data is located in the MPD and MP4 PSSH box; for HLS+TS streams, the PSSH data is located in the M3U8 file and each TS segment. Pass in the actual value.
1223let uint8pssh = new Uint8Array([0x00, 0x00, 0x00, 0x00]);
1224mediaKeySession.generateMediaKeyRequest("video/avc", uint8pssh, drm.MediaKeyType.MEDIA_KEY_TYPE_ONLINE).then((mediaKeyRequest: drm.MediaKeyRequest) =>{
1225  console.log('generateMediaKeyRequest' + mediaKeyRequest);
1226}).catch((err: BusinessError) => {
1227  console.error(`generateMediaKeyRequest: ERROR: ${err}`);
1228});
1229```
1230
1231### processMediaKeyResponse
1232
1233processMediaKeyResponse(response: Uint8Array): Promise<Uint8Array\>
1234
1235Processes a media key response.
1236
1237**Atomic service API**: This API can be used in atomic services since API version 12.
1238
1239**System capability**: SystemCapability.Multimedia.Drm.Core
1240
1241**Parameters**
1242
1243| Name    | Type                                            | Mandatory| Description                          |
1244| -------- | ----------------------------------------------- | ---- | ---------------------------- |
1245| response  | Uint8Array     | Yes  | Media key response.                  |
1246
1247**Return value**
1248
1249| Type                                            | Description                          |
1250| ----------------------------------------------- | ---------------------------- |
1251| Promise<Uint8Array\>          | Promise used to return the media key IDs.                  |
1252
1253**Error codes**
1254
1255For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1256
1257| ID        | Error Message       |
1258| --------------- | --------------- |
1259| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed.            |
1260| 24700101                |  All unknown errors                  |
1261| 24700201                |  Fatal service error, for example, service died                  |
1262
1263**Example**
1264
1265```ts
1266import { drm } from '@kit.DrmKit';
1267import { BusinessError } from '@kit.BasicServicesKit';
1268
1269let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1270let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession();
1271// mediaKeyResponse is obtained from the DRM service. Pass in the actual value obtained.
1272let mediaKeyResponse = new Uint8Array([0x00, 0x00, 0x00, 0x00]);
1273mediaKeySession.processMediaKeyResponse(mediaKeyResponse).then((mediaKeyId: Uint8Array) => {
1274  console.log('processMediaKeyResponse:' + mediaKeyId);
1275}).catch((err: BusinessError) => {
1276  console.error(`processMediaKeyResponse: ERROR: ${err}`);
1277});
1278```
1279
1280### checkMediaKeyStatus
1281
1282 checkMediaKeyStatus(): MediaKeyStatus[]
1283
1284Checks the status of the media keys in use.
1285
1286**Atomic service API**: This API can be used in atomic services since API version 12.
1287
1288**System capability**: SystemCapability.Multimedia.Drm.Core
1289
1290**Return value**
1291
1292| Type                                            | Description                          |
1293| ----------------------------------------------- | ---------------------------- |
1294| [MediaKeyStatus[]](#mediakeystatus)          | Media key status.                  |
1295
1296**Error codes**
1297
1298For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1299
1300| ID        | Error Message       |
1301| --------------- | --------------- |
1302| 24700101                |  All unknown errors                  |
1303| 24700201                |  Fatal service error, for example, service died                  |
1304
1305**Example**
1306
1307```ts
1308import { drm } from '@kit.DrmKit';
1309import { BusinessError } from '@kit.BasicServicesKit';
1310
1311let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1312let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession();
1313try {
1314  let keyStatus: drm.MediaKeyStatus[] =  mediaKeySession.checkMediaKeyStatus();
1315} catch (err) {
1316  let error = err as BusinessError;
1317  console.error(`checkMediaKeyStatus ERROR: ${error}`);
1318}
1319```
1320
1321### clearMediaKeys
1322
1323clearMediaKeys(): void
1324
1325Clears the media keys in use.
1326
1327**Atomic service API**: This API can be used in atomic services since API version 12.
1328
1329**System capability**: SystemCapability.Multimedia.Drm.Core
1330
1331**Error codes**
1332
1333For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1334
1335| ID        | Error Message       |
1336| --------------- | --------------- |
1337| 24700101                |  All unknown errors                  |
1338| 24700201                |  Fatal service error, for example, service died                  |
1339
1340**Example**
1341
1342```ts
1343import { drm } from '@kit.DrmKit';
1344import { BusinessError } from '@kit.BasicServicesKit';
1345
1346let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1347let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession();
1348// mediaKeyResponse is obtained from the DRM service. Pass in the actual value obtained.
1349let mediaKeyResponse = new Uint8Array([0x00, 0x00, 0x00, 0x00]);
1350mediaKeySession.processMediaKeyResponse(mediaKeyResponse).then((mediaKeyId: Uint8Array) => {
1351  console.log('processMediaKeyResponse:' + mediaKeyId);
1352}).catch((err: BusinessError) => {
1353  console.error(`processMediaKeyResponse: ERROR: ${err}`);
1354});
1355try {
1356  mediaKeySession.clearMediaKeys();
1357} catch (err) {
1358  let error = err as BusinessError;
1359  console.error(`clearMediaKeys ERROR: ${error}`);
1360}
1361```
1362
1363### generateOfflineReleaseRequest
1364
1365generateOfflineReleaseRequest(mediaKeyId: Uint8Array): Promise<Uint8Array\>
1366
1367Generates a request to release offline media keys.
1368
1369**Atomic service API**: This API can be used in atomic services since API version 12.
1370
1371**System capability**: SystemCapability.Multimedia.Drm.Core
1372
1373**Parameters**
1374
1375| Name    | Type                                            | Mandatory| Description                          |
1376| -------- | ----------------------------------------------- | ---- | ---------------------------- |
1377| mediaKeyId  | Uint8Array    | Yes  | Array holding the IDs of offline media keys.                  |
1378
1379**Return value**
1380
1381| Type                                            | Description                          |
1382| ----------------------------------------------- | ---------------------------- |
1383| Promise<Uint8Array\>          | Promise used to return the request generated if the DRM scheme on the device supports offline media key release.                  |
1384
1385**Error codes**
1386
1387For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1388
1389| ID        | Error Message       |
1390| --------------- | --------------- |
1391| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified or too many parameters. 2.Incorrect parameter types. 3.Parameter verification failed.         |
1392| 24700101                |  All unknown errors                  |
1393| 24700201                |  Fatal service error, for example, service died                  |
1394
1395**Example**
1396
1397```ts
1398import { drm } from '@kit.DrmKit';
1399import { BusinessError } from '@kit.BasicServicesKit';
1400
1401let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1402let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession();
1403// mediaKeyId is the return value of processMediaKeyResponse or getOfflineMediaKeyIds. Pass in the actual data returned.
1404let mediaKeyId = new Uint8Array([0x00, 0x00, 0x00, 0x00]);
1405mediaKeySession.generateOfflineReleaseRequest(mediaKeyId).then((offlineReleaseRequest: Uint8Array) => {
1406  console.log('generateOfflineReleaseRequest:' + offlineReleaseRequest);
1407}).catch((err: BusinessError) => {
1408  console.error(`generateOfflineReleaseRequest: ERROR: ${err}`);
1409});
1410```
1411
1412### processOfflineReleaseResponse
1413
1414processOfflineReleaseResponse(mediaKeyId: Uint8Array, response: Uint8Array): Promise<void\>
1415
1416Processes a response to a request for releasing offline media keys.
1417
1418**Atomic service API**: This API can be used in atomic services since API version 12.
1419
1420**System capability**: SystemCapability.Multimedia.Drm.Core
1421
1422**Parameters**
1423
1424| Name    | Type                                            | Mandatory| Description                          |
1425| -------- | ----------------------------------------------- | ---- | ---------------------------- |
1426| mediaKeyId  | Uint8Array     | Yes  | Array holding the IDs of offline media keys.                  |
1427| response  | Uint8Array     | Yes  | Response to the request for releasing offline media keys.                  |
1428
1429**Return value**
1430
1431| Type                                            | Description                          |
1432| ----------------------------------------------- | ---------------------------- |
1433| Promise<void\>          | Promise used to return the result if the DRM scheme on the device supports offline media key release.                  |
1434
1435**Error codes**
1436
1437For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1438
1439| ID        | Error Message       |
1440| --------------- | --------------- |
1441| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified or too many parameters. 2.Incorrect parameter types. 3.Parameter verification failed.            |
1442| 24700101                |  All unknown errors                  |
1443| 24700201                |  Fatal service error, for example, service died                  |
1444
1445**Example**
1446
1447```ts
1448import { drm } from '@kit.DrmKit';
1449import { BusinessError } from '@kit.BasicServicesKit';
1450
1451let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1452let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession();
1453// mediaKeyId is the return value of processMediaKeyResponse or getOfflineMediaKeyIds. Apply for memory based on the actual length.
1454let mediaKeyId = new Uint8Array([0x00, 0x00, 0x00, 0x00]);
1455mediaKeySession.generateOfflineReleaseRequest(mediaKeyId).then((offlineReleaseRequest: Uint8Array) => {
1456  console.log('generateOfflineReleaseRequest:' + offlineReleaseRequest);
1457}).catch((err: BusinessError) => {
1458  console.error(`generateOfflineReleaseRequest: ERROR: ${err}`);
1459});
1460// offlineReleaseResponse is obtained from the DRM service. Apply for memory based on the actual length.
1461let offlineReleaseResponse = new Uint8Array([0x00, 0x00, 0x00, 0x00]);
1462mediaKeySession.processOfflineReleaseResponse(mediaKeyId, offlineReleaseResponse).then(() => {
1463  console.log('processOfflineReleaseResponse');
1464}).catch((err: BusinessError) => {
1465  console.error(`processOfflineReleaseResponse: ERROR: ${err}`);
1466});
1467```
1468
1469### restoreOfflineMediaKeys
1470
1471restoreOfflineMediaKeys(mediaKeyId: Uint8Array): Promise<void\>
1472
1473Restores offline media keys.
1474
1475**Atomic service API**: This API can be used in atomic services since API version 12.
1476
1477**System capability**: SystemCapability.Multimedia.Drm.Core
1478
1479**Parameters**
1480
1481| Name    | Type                                            | Mandatory| Description                          |
1482| -------- | ----------------------------------------------- | ---- | ---------------------------- |
1483| mediaKeyId  | Uint8Array     | Yes  | Array holding the IDs of offline media keys.                  |
1484
1485**Return value**
1486
1487| Type                                            | Description                          |
1488| ----------------------------------------------- | ---------------------------- |
1489| Promise<void\>          | Promise                  |
1490
1491**Error codes**
1492
1493For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1494
1495| ID        | Error Message       |
1496| --------------- | --------------- |
1497| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified or too many parameters. 2.Incorrect parameter types. 3.Parameter verification failed.              |
1498| 24700101                |  All unknown errors                  |
1499| 24700201                |  Fatal service error, for example, service died                  |
1500
1501**Example**
1502
1503```ts
1504import { drm } from '@kit.DrmKit';
1505import { BusinessError } from '@kit.BasicServicesKit';
1506
1507let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1508let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession();
1509// mediaKeyId is the return value of processMediaKeyResponse or getOfflineMediaKeyIds. Pass in the actual data returned.
1510let mediaKeyId = new Uint8Array([0x00, 0x00, 0x00, 0x00]);
1511mediaKeySession.restoreOfflineMediaKeys(mediaKeyId).then(() => {
1512  console.log("restoreOfflineMediaKeys");
1513}).catch((err: BusinessError) => {
1514  console.error(`restoreOfflineMediaKeys: ERROR: ${err}`);
1515});
1516```
1517
1518### getContentProtectionLevel
1519
1520getContentProtectionLevel(): ContentProtectionLevel
1521
1522Obtains the content protection level of this media key session.
1523
1524**Atomic service API**: This API can be used in atomic services since API version 12.
1525
1526**System capability**: SystemCapability.Multimedia.Drm.Core
1527
1528**Return value**
1529
1530| Type                                            | Description                          |
1531| ----------------------------------------------- | ---------------------------- |
1532| [ContentProtectionLevel](#contentprotectionlevel)          | Content protection level.                  |
1533
1534**Error codes**
1535
1536For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1537
1538| ID        | Error Message       |
1539| --------------- | --------------- |
1540| 24700101                |  All unknown errors                  |
1541| 24700201                |  Fatal service error, for example, service died                  |
1542
1543**Example**
1544
1545```ts
1546import { drm } from '@kit.DrmKit';
1547import { BusinessError } from '@kit.BasicServicesKit';
1548
1549let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1550let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession();
1551try {
1552  let contentProtectionLevel: drm.ContentProtectionLevel = mediaKeySession.getContentProtectionLevel();
1553} catch (err) {
1554  let error = err as BusinessError;
1555  console.error(`getContentProtectionLevel ERROR: ${error}`);
1556}
1557```
1558
1559### requireSecureDecoderModule
1560
1561requireSecureDecoderModule(mimeType: string): boolean
1562
1563Checks whether secure decoding is required.
1564
1565**Atomic service API**: This API can be used in atomic services since API version 12.
1566
1567**System capability**: SystemCapability.Multimedia.Drm.Core
1568
1569**Parameters**
1570
1571| Name    | Type                                            | Mandatory| Description                          |
1572| -------- | ----------------------------------------------- | ---- | ---------------------------- |
1573| mimeType  | string     | Yes  | MIME type, which is determined by the DRM scheme.                  |
1574
1575**Return value**
1576
1577| Type                                            | Description                          |
1578| ----------------------------------------------- | ---------------------------- |
1579| boolean          | Whether secure decoding is required. The value **true** means that secure decoding is required, and **false** means the opposite.                  |
1580
1581**Error codes**
1582
1583For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1584
1585| ID        | Error Message       |
1586| --------------- | --------------- |
1587| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified or too many parameters. 2.Incorrect parameter types. 3.Parameter verification failed.      |
1588| 24700101                |  All unknown errors                  |
1589| 24700201                |  Fatal service error, for example, service died                  |
1590
1591**Example**
1592
1593```ts
1594import { drm } from '@kit.DrmKit';
1595import { BusinessError } from '@kit.BasicServicesKit';
1596
1597let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1598let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession();
1599try {
1600  let status: boolean = mediaKeySession.requireSecureDecoderModule("video/avc");
1601} catch (err) {
1602  let error = err as BusinessError;
1603  console.error(`requireSecureDecoderModule ERROR: ${error}`);
1604}
1605```
1606
1607### on('keyRequired')
1608
1609on(type: 'keyRequired', callback: (eventInfo: EventInfo) => void): void
1610
1611Subscribes to events indicating that the application requests a media key.
1612
1613**Atomic service API**: This API can be used in atomic services since API version 12.
1614
1615**System capability**: SystemCapability.Multimedia.Drm.Core
1616
1617**Parameters**
1618
1619| Name     | Type                 | Mandatory| Description                                 |
1620| -------- | -------------------- | ---- | ------------------------------------- |
1621| type     | string               | Yes  | Event type. The value is fixed at **'keyRequired'**. This event is triggered when the application requires a media key.|
1622| callback | Callback\<[EventInfo](#eventinfo)\> | Yes  | Callback used to return the event information.                |
1623
1624**Error codes**
1625
1626For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1627
1628| ID        | Error Message       |
1629| --------------- | --------------- |
1630| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified or too many parameters. 2.Incorrect parameter types. 3.Parameter verification failed.         |
1631| 24700101                |  All unknown errors                  |
1632
1633**Example**
1634
1635```ts
1636import { drm } from '@kit.DrmKit';
1637
1638let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1639let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession();
1640mediaKeySession.on('keyRequired', (eventInfo: drm.EventInfo) => {
1641  console.log('keyRequired ' + 'extra: ' + eventInfo.extraInfo + 'data: ' + eventInfo.info);
1642});
1643```
1644
1645### off('keyRequired')
1646
1647off(type: 'keyRequired', callback?: (eventInfo: EventInfo) => void): void
1648
1649Unsubscribes from events indicating that the application requests a media key.
1650
1651**Atomic service API**: This API can be used in atomic services since API version 12.
1652
1653**System capability**: SystemCapability.Multimedia.Drm.Core
1654
1655**Parameters**
1656
1657| Name     | Type                 | Mandatory| Description                                 |
1658| -------- | -------------------- | ---- | ------------------------------------- |
1659| type     | string               | Yes  | Event type. The value is fixed at **'keyRequired'**.|
1660| callback | Callback\<[EventInfo](#eventinfo)\> | No  | Callback used to return the event information.                 |
1661
1662**Error codes**
1663
1664For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1665
1666| ID        | Error Message       |
1667| --------------- | --------------- |
1668| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified or too many parameters. 2.Incorrect parameter types. 3.Parameter verification failed.             |
1669| 24700101                |  All unknown errors                  |
1670
1671**Example**
1672
1673```ts
1674import { drm } from '@kit.DrmKit';
1675
1676let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1677let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession();
1678mediaKeySession.off('keyRequired');
1679```
1680
1681### on('keyExpired')
1682
1683on(type: 'keyExpired', callback: (eventInfo: EventInfo) => void): void
1684
1685Subscribes to events indicating that a media key expires.
1686
1687**Atomic service API**: This API can be used in atomic services since API version 12.
1688
1689**System capability**: SystemCapability.Multimedia.Drm.Core
1690
1691**Parameters**
1692
1693| Name     | Type                 | Mandatory| Description                                 |
1694| -------- | -------------------- | ---- | ------------------------------------- |
1695| type     | string               | Yes  | Event type. The value is fixed at **'keyExpired'**. This event is triggered when a media key expires.|
1696| callback | Callback\<[EventInfo](#eventinfo)\> | Yes  | Callback used to return the event information.                |
1697
1698**Error codes**
1699
1700For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1701
1702| ID        | Error Message       |
1703| --------------- | --------------- |
1704| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified or too many parameters. 2.Incorrect parameter types. 3.Parameter verification failed.          |
1705| 24700101                |  All unknown errors                  |
1706
1707**Example**
1708
1709```ts
1710import { drm } from '@kit.DrmKit';
1711
1712let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1713let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession();
1714mediaKeySession.on('keyExpired', (eventInfo: drm.EventInfo) => {
1715  console.log('keyExpired ' + 'extra: ' + eventInfo.extraInfo + 'data: ' + eventInfo.info);
1716});
1717```
1718
1719### off('keyExpired')
1720
1721off(type: 'keyExpired', callback?: (eventInfo: EventInfo) => void): void
1722
1723Unsubscribes from events indicating that a media key expires.
1724
1725**Atomic service API**: This API can be used in atomic services since API version 12.
1726
1727**System capability**: SystemCapability.Multimedia.Drm.Core
1728
1729**Parameters**
1730
1731| Name     | Type                 | Mandatory| Description                                 |
1732| -------- | -------------------- | ---- | ------------------------------------- |
1733| type     | string               | Yes  | Event type. The value is fixed at **'keyExpired'**.|
1734| callback | Callback\<[EventInfo](#eventinfo)\> | No  | Callback used to return the event information.                 |
1735
1736**Error codes**
1737
1738For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1739
1740| ID        | Error Message       |
1741| --------------- | --------------- |
1742| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified or too many parameters. 2.Incorrect parameter types. 3.Parameter verification failed.            |
1743| 24700101                |  All unknown errors                  |
1744
1745**Example**
1746
1747```ts
1748import { drm } from '@kit.DrmKit';
1749
1750let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1751let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession();
1752mediaKeySession.off('keyExpired');
1753```
1754
1755### on('vendorDefined')
1756
1757on(type: 'vendorDefined', callback: (eventInfo: EventInfo) => void): void
1758
1759Subscribes to vendor-defined events.
1760
1761**Atomic service API**: This API can be used in atomic services since API version 12.
1762
1763**System capability**: SystemCapability.Multimedia.Drm.Core
1764
1765**Parameters**
1766
1767| Name     | Type                 | Mandatory| Description                                 |
1768| -------- | -------------------- | ---- | ------------------------------------- |
1769| type     | string               | Yes  | Event type. The value is fixed at **'vendorDefined'**. This event is triggered when a vendor-defined event occurs.|
1770| callback | Callback\<[EventInfo](#eventinfo)\> | Yes  | Callback used to return the event information.                |
1771
1772**Error codes**
1773
1774For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1775
1776| ID        | Error Message       |
1777| --------------- | --------------- |
1778| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified or too many parameters. 2.Incorrect parameter types. 3.Parameter verification failed.              |
1779| 24700101                |  All unknown errors                  |
1780
1781**Example**
1782
1783```ts
1784import { drm } from '@kit.DrmKit';
1785
1786let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1787let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession();
1788mediaKeySession.on('vendorDefined', (eventInfo: drm.EventInfo) => {
1789  console.log('vendorDefined ' + 'extra: ' + eventInfo.extraInfo + 'data: ' + eventInfo.info);
1790});
1791```
1792
1793### off('vendorDefined')
1794
1795off(type: 'vendorDefined', callback?: (eventInfo: EventInfo) => void): void
1796
1797Unsubscribes from vendor-defined events.
1798
1799**Atomic service API**: This API can be used in atomic services since API version 12.
1800
1801**System capability**: SystemCapability.Multimedia.Drm.Core
1802
1803**Parameters**
1804
1805| Name     | Type                 | Mandatory| Description                                 |
1806| -------- | -------------------- | ---- | ------------------------------------- |
1807| type     | string               | Yes  | Event type. The value is fixed at **'vendorDefined'**.|
1808| callback | Callback\<[EventInfo](#eventinfo)\> | No  | Callback used to return the event information.                 |
1809
1810**Error codes**
1811
1812For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1813
1814| ID        | Error Message       |
1815| --------------- | --------------- |
1816| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified or too many parameters. 2.Incorrect parameter types. 3.Parameter verification failed.      |
1817| 24700101                |  All unknown errors                  |
1818
1819**Example**
1820
1821```ts
1822import { drm } from '@kit.DrmKit';
1823
1824let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1825let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession();
1826mediaKeySession.off('vendorDefined');
1827```
1828
1829### on('expirationUpdate')
1830
1831on(type: 'expirationUpdate', callback: (eventInfo: EventInfo) => void): void
1832
1833Subscribes to events indicating that a media key updates on expiry.
1834
1835**Atomic service API**: This API can be used in atomic services since API version 12.
1836
1837**System capability**: SystemCapability.Multimedia.Drm.Core
1838
1839**Parameters**
1840
1841| Name     | Type                 | Mandatory| Description                                 |
1842| -------- | -------------------- | ---- | ------------------------------------- |
1843| type     | string               | Yes  | Event type. The value is fixed at **'expirationUpdate'**. This event is triggered when a media key updates on expiry.|
1844| callback | Callback\<[EventInfo](#eventinfo)\> | Yes  | Callback used to return the event information.                |
1845
1846**Error codes**
1847
1848For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1849
1850| ID        | Error Message       |
1851| --------------- | --------------- |
1852| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified or too many parameters. 2.Incorrect parameter types. 3.Parameter verification failed.        |
1853| 24700101                |  All unknown errors                  |
1854
1855**Example**
1856
1857```ts
1858import { drm } from '@kit.DrmKit';
1859
1860let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1861let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession();
1862mediaKeySession.on('expirationUpdate', (eventInfo: drm.EventInfo) => {
1863  console.log('expirationUpdate ' + 'extra: ' + eventInfo.extraInfo + 'data: ' + eventInfo.info);
1864});
1865```
1866
1867### off('expirationUpdate')
1868
1869off(type: 'expirationUpdate', callback?: (eventInfo: EventInfo) => void): void
1870
1871Unsubscribes from events indicating that a media key updates on expiry.
1872
1873**Atomic service API**: This API can be used in atomic services since API version 12.
1874
1875**System capability**: SystemCapability.Multimedia.Drm.Core
1876
1877**Parameters**
1878
1879| Name     | Type                 | Mandatory| Description                                 |
1880| -------- | -------------------- | ---- | ------------------------------------- |
1881| type     | string               | Yes  | Event type. The value is fixed at **'expirationUpdate'**.|
1882| callback | Callback\<[EventInfo](#eventinfo)\> | No  | Callback used to return the event information.                 |
1883
1884**Error codes**
1885
1886For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1887
1888| ID        | Error Message       |
1889| --------------- | --------------- |
1890| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified or too many parameters. 2.Incorrect parameter types. 3.Parameter verification failed.       |
1891| 24700101                |  All unknown errors                  |
1892
1893**Example**
1894
1895```ts
1896import { drm } from '@kit.DrmKit';
1897
1898let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1899let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession();
1900mediaKeySession.off('expirationUpdate');
1901```
1902
1903### on('keysChange')
1904
1905on(type: 'keysChange', callback: (keyInfo: KeysInfo[], newKeyAvailable: boolean) => void): void
1906
1907Subscribes to events indicating that a media key changes.
1908
1909**Atomic service API**: This API can be used in atomic services since API version 12.
1910
1911**System capability**: SystemCapability.Multimedia.Drm.Core
1912
1913**Parameters**
1914
1915| Name     | Type                 | Mandatory| Description                                 |
1916| -------- | -------------------- | ---- | ------------------------------------- |
1917| type     | string               | Yes  | Event type. The value is fixed at **'keysChange'**. This event is triggered when a media key changes.|
1918| callback | Callback\<[KeysInfo[]](#keysinfo), boolean\> | Yes  | Callback used to return the event information, including the lists of key IDs, statuses, and availability.                |
1919
1920**Error codes**
1921
1922For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1923
1924| ID        | Error Message       |
1925| --------------- | --------------- |
1926| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified or too many parameters. 2.Incorrect parameter types. 3.Parameter verification failed.             |
1927| 24700101                |  All unknown errors                  |
1928
1929**Example**
1930
1931```ts
1932import { drm } from '@kit.DrmKit';
1933
1934let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1935let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession();
1936mediaKeySession.on('keysChange', (keyInfo: drm.KeysInfo[], newKeyAvailable: boolean) => {
1937  for (let i = 0; i < keyInfo.length; i++) {
1938    console.log('keysChange' + 'keyId:' + keyInfo[i].keyId + ' data:' + keyInfo[i].value);
1939  }
1940});
1941```
1942
1943### off('keysChange')
1944
1945off(type: 'keysChange', callback?: (keyInfo: KeysInfo[], newKeyAvailable: boolean) => void): void
1946
1947Unsubscribes from events indicating that a media key changes.
1948
1949**Atomic service API**: This API can be used in atomic services since API version 12.
1950
1951**System capability**: SystemCapability.Multimedia.Drm.Core
1952
1953**Parameters**
1954
1955| Name     | Type                 | Mandatory| Description                                 |
1956| -------- | -------------------- | ---- | ------------------------------------- |
1957| type     | string               | Yes  | Event type. The value is fixed at **'keysChange'**.|
1958| callback | Callback\<[KeysInfo[]](#keysinfo), boolean\> | No  | Callback used to return the event information, including the lists of key IDs, statuses, and availability.               |
1959
1960**Error codes**
1961
1962For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1963
1964| ID        | Error Message       |
1965| --------------- | --------------- |
1966| 401                |  The parameter check failed. Possibly because: 1.Mandatory parameters are left unspecified or too many parameters. 2.Incorrect parameter types. 3.Parameter verification failed.            |
1967| 24700101                |  All unknown errors                  |
1968
1969**Example**
1970
1971```ts
1972import { drm } from '@kit.DrmKit';
1973
1974let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
1975let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession();
1976mediaKeySession.off('keysChange');
1977```
1978
1979### destroy
1980
1981destroy(): void
1982
1983Destroys this **MediaKeySession** instance.
1984
1985**Atomic service API**: This API can be used in atomic services since API version 12.
1986
1987**System capability**: SystemCapability.Multimedia.Drm.Core
1988
1989**Error codes**
1990
1991For details about the error codes, see [DRM Error Codes](errorcode-drm.md).
1992
1993| ID        | Error Message       |
1994| --------------- | --------------- |
1995| 24700101                |  All unknown errors                  |
1996| 24700201                |  Fatal service error, for example, service died                  |
1997
1998**Example**
1999
2000```ts
2001import { drm } from '@kit.DrmKit';
2002import { BusinessError } from '@kit.BasicServicesKit';
2003
2004let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
2005let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession();
2006try {
2007  mediaKeySession.destroy();
2008} catch (err) {
2009  let error = err as BusinessError;
2010  console.error(`mediaKeySession destroy ERROR: ${error}`);
2011}
2012
2013```
2014