1e41f4b71Sopenharmony_ci# DRM Media Key System Management (ArkTS)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciUsing the **MediaKeySystem** class of the DRM module, you can manage **MediaKeySystem** instances, generate media key system requests to obtain DRM certificates, process responses to these requests, manage media key sessions, manage offline media keys, and obtain DRM statistics and device configuration information.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciBefore using DRM Kit, check whether the device supports the DRM capabilities of a specific DRM scheme.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciIn DRM Kit, the DRM scheme exists as a plug-in.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci## How to Develop
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ciRead [DRM](../../reference/apis-drm-kit/js-apis-drm.md) for the API reference.
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci1. Import the module.
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci    ```ts
16e41f4b71Sopenharmony_ci    import { drm } from '@kit.DrmKit';
17e41f4b71Sopenharmony_ci    ```
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci2. Import the **BusinessError** module, which provides the error codes thrown by the APIs of the DRM module.
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci    ```ts
22e41f4b71Sopenharmony_ci    import { BusinessError } from '@kit.BasicServicesKit';
23e41f4b71Sopenharmony_ci    ```
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci3. Check whether the device supports the specified DRM scheme.
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci   > **NOTE**
28e41f4b71Sopenharmony_ci   >
29e41f4b71Sopenharmony_ci   > The value **false** means that the device does not support the specified DRM scheme.
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci   ```ts
32e41f4b71Sopenharmony_ci   let isSupported: boolean = drm.isMediaKeySystemSupported("com.clearplay.drm", "video/avc", drm.ContentProtectionLevel.CONTENT_PROTECTION_LEVEL_SW_CRYPTO);
33e41f4b71Sopenharmony_ci   ```
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci4. (Optional) Obtain the name and ID list of the DRM schemes on the device.
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci   > **NOTE**
38e41f4b71Sopenharmony_ci   >
39e41f4b71Sopenharmony_ci   > If the returned array is empty, no DRM scheme is supported by the device.
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci   ```ts
42e41f4b71Sopenharmony_ci   let description: drm.MediaKeySystemDescription[] = drm.getMediaKeySystems();
43e41f4b71Sopenharmony_ci   ```
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci5. Create a **MediaKeySystem** instance.
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci   > **NOTE**
48e41f4b71Sopenharmony_ci   >
49e41f4b71Sopenharmony_ci   > If the creation fails, **undefined** is returned, indicating that the device does not support the DRM capability.
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci   ```ts
52e41f4b71Sopenharmony_ci   let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
53e41f4b71Sopenharmony_ci   ```
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci6. (Optional) Obtain the UUID corresponding to the specified DRM scheme name.
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ci   > **NOTE**
58e41f4b71Sopenharmony_ci   >
59e41f4b71Sopenharmony_ci   > If the length of the returned UUID is 0, no DRM scheme is supported by the device.
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci   ```ts
62e41f4b71Sopenharmony_ci   let uuid: string = drm.getMediaKeySystemUuid("com.clearplay.drm");
63e41f4b71Sopenharmony_ci   ```
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_ci7. (Optional) Set and obtain the configuration items supported by the DRM scheme.
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci    ```ts
68e41f4b71Sopenharmony_ci    // If the DRM scheme supports configuration item setting, set the value of a configuration item of the string type supported by the DRM scheme.
69e41f4b71Sopenharmony_ci    mediaKeySystem.setConfigurationString("configName", "configValue");
70e41f4b71Sopenharmony_ci    // Obtain the value of a configuration item in the form of a string.
71e41f4b71Sopenharmony_ci    let configValueString : string = mediaKeySystem.getConfigurationString("version");
72e41f4b71Sopenharmony_ci    let configValueUint8ArrayA: Uint8Array = new Uint8Array([0x00, 0x00, 0x00, 0x00]);
73e41f4b71Sopenharmony_ci    // If the DRM scheme supports configuration item setting, set the value of a configuration item of the array type supported by the DRM scheme.
74e41f4b71Sopenharmony_ci    mediaKeySystem.setConfigurationByteArray("Uint8ArrayConfigName", configValueUint8ArrayA);
75e41f4b71Sopenharmony_ci    // Obtain the value of a configuration item in the form of an array.
76e41f4b71Sopenharmony_ci    let configValueUint8ArrayB: Uint8Array = mediaKeySystem.getConfigurationByteArray("Uint8ArrayConfigName");
77e41f4b71Sopenharmony_ci    ```
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci8. (Optional) Obtain the maximum content protection level supported by the device.
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci    ```ts
82e41f4b71Sopenharmony_ci    let contentProtectionLevel: drm.ContentProtectionLevel = drm.ContentProtectionLevel.CONTENT_PROTECTION_LEVEL_UNKNOWN;
83e41f4b71Sopenharmony_ci    try {
84e41f4b71Sopenharmony_ci      contentProtectionLevel = mediaKeySystem.getMaxContentProtectionLevel();
85e41f4b71Sopenharmony_ci    } catch (err) {
86e41f4b71Sopenharmony_ci      let error = err as BusinessError;
87e41f4b71Sopenharmony_ci      console.error(`getMaxContentProtectionLevel ERROR: ${error}`);
88e41f4b71Sopenharmony_ci    }
89e41f4b71Sopenharmony_ci    ```
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ci9. Start listening.
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ci    Listen for the event indicating that the application requests a DRM certificate.
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci    Register the event **'keySystemRequired'**. This event can be listened for when a **MediaKeySystem** instance is created and is triggered when the application requests a DRM certificate.
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci    ```ts
98e41f4b71Sopenharmony_ci    mediaKeySystem.on('keySystemRequired', (eventInfo: drm.EventInfo) => {
99e41f4b71Sopenharmony_ci      console.log('keySystemRequired' + 'extra:' + eventInfo.extraInfo + ' data:' + eventInfo.info);
100e41f4b71Sopenharmony_ci    });
101e41f4b71Sopenharmony_ci    ```
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci10. (Optional) Obtain the status of the DRM certificate.
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci    ```ts
106e41f4b71Sopenharmony_ci    let certificateStatus: drm.CertificateStatus = mediaKeySystem.getCertificateStatus();
107e41f4b71Sopenharmony_ci    ```
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci11. Generate a provision request.
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_ci    During the creation of a **MediaKeySession** session, if no DRM certificate is available, the **keySystemRequired** event is triggered. In this case, the DRM certificate status on the device is obtained first. If the device does not have a DRM certificate or the DRM certificate status is abnormal (not **drm.CertificateStatus.CERT_STATUS_PROVISIONED**), a provision request is generated to obtain a DRM certificate.
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci       ```ts
114e41f4b71Sopenharmony_ci         if(certificateStatus != drm.CertificateStatus.CERT_STATUS_PROVISIONED){
115e41f4b71Sopenharmony_ci           mediaKeySystem.generateKeySystemRequest().then(async (drmRequest: drm.ProvisionRequest) => {
116e41f4b71Sopenharmony_ci             console.info("generateKeySystemRequest success", drmRequest.data, drmRequest.defaultURL);
117e41f4b71Sopenharmony_ci           }).catch((err:BusinessError) =>{
118e41f4b71Sopenharmony_ci               console.info("generateKeySystemRequest err end", err.code);
119e41f4b71Sopenharmony_ci            });
120e41f4b71Sopenharmony_ci         } else {
121e41f4b71Sopenharmony_ci           console.info("The certificate already exists.");
122e41f4b71Sopenharmony_ci         }
123e41f4b71Sopenharmony_ci       ```
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ci12. Process the provision response.
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci    A response to the provision request is received. You need to process this response.
128e41f4b71Sopenharmony_ci
129e41f4b71Sopenharmony_ci       ```ts
130e41f4b71Sopenharmony_ci         // Send drmRequest.data returned by the provision request to the DRM certificate service through a network request to obtain a provision response and process the response.
131e41f4b71Sopenharmony_ci         let provisionResponseByte = new Uint8Array([0x00, 0x00, 0x00, 0x00]);
132e41f4b71Sopenharmony_ci         mediaKeySystem.processKeySystemResponse(provisionResponseByte).then(() => {
133e41f4b71Sopenharmony_ci           console.info("processKeySystemResponse success");
134e41f4b71Sopenharmony_ci         }).catch((err:BusinessError) =>{
135e41f4b71Sopenharmony_ci           console.info("processKeySystemResponse err end", err.code);
136e41f4b71Sopenharmony_ci         });
137e41f4b71Sopenharmony_ci       ```
138e41f4b71Sopenharmony_ci
139e41f4b71Sopenharmony_ci13. Create a **MediaKeySession** instance.
140e41f4b71Sopenharmony_ci
141e41f4b71Sopenharmony_ci    Create a **MediaKeySession** instance with the specified content protection level or a **MediaKeySession** instance with the default content protection level of the DRM scheme.
142e41f4b71Sopenharmony_ci     ```ts
143e41f4b71Sopenharmony_ci     let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession();
144e41f4b71Sopenharmony_ci     ```
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ci14. (Optional) Obtain the list of offline media key IDs, which are used to manage offline media keys.
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ci     ```ts
149e41f4b71Sopenharmony_ci     let offlineMediaKeyIds: Uint8Array[] = mediaKeySystem.getOfflineMediaKeyIds();
150e41f4b71Sopenharmony_ci     ```
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ci15. (Optional) Obtain the status of offline media keys.
153e41f4b71Sopenharmony_ci
154e41f4b71Sopenharmony_ci     ```ts
155e41f4b71Sopenharmony_ci     try {
156e41f4b71Sopenharmony_ci       let offlineMediaKeyStatus: drm.OfflineMediaKeyStatus = mediaKeySystem.getOfflineMediaKeyStatus(offlineMediaKeyIds[0]);
157e41f4b71Sopenharmony_ci     } catch (err) {
158e41f4b71Sopenharmony_ci       let error = err as BusinessError;
159e41f4b71Sopenharmony_ci       console.error(`getOfflineMediaKeyStatus ERROR: ${error}`);
160e41f4b71Sopenharmony_ci     }
161e41f4b71Sopenharmony_ci     ```
162e41f4b71Sopenharmony_ci
163e41f4b71Sopenharmony_ci16. (Optional) Clear offline media keys.
164e41f4b71Sopenharmony_ci
165e41f4b71Sopenharmony_ci     ```ts
166e41f4b71Sopenharmony_ci     try {
167e41f4b71Sopenharmony_ci       mediaKeySystem.clearOfflineMediaKeys(offlineMediaKeyIds[0]);
168e41f4b71Sopenharmony_ci     } catch (err) {
169e41f4b71Sopenharmony_ci       let error = err as BusinessError;
170e41f4b71Sopenharmony_ci       console.error(`clearOfflineMediaKeys ERROR: ${error}`);
171e41f4b71Sopenharmony_ci     }
172e41f4b71Sopenharmony_ci     ```
173e41f4b71Sopenharmony_ci
174e41f4b71Sopenharmony_ci17. (Optional) Obtain DRM statistical information, including the number of current sessions, decryption times, and decryption failures, as well as the plug-in version.
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ci     ```ts
177e41f4b71Sopenharmony_ci     let statisticKeyValue: drm.StatisticKeyValue[] = mediaKeySystem.getStatistics();
178e41f4b71Sopenharmony_ci     ```
179e41f4b71Sopenharmony_ci
180e41f4b71Sopenharmony_ci18. Destroy the **MediaKeySession** instance.
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ci    Destroy the **MediaKeySession** instance when the encrypted content is decrypted and the instance is no longer needed.
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_ci     ```ts
185e41f4b71Sopenharmony_ci     // Release resources when the MediaKeySession instance is no longer needed.
186e41f4b71Sopenharmony_ci     mediaKeySession.destroy();
187e41f4b71Sopenharmony_ci     ```
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ci19. Destroy this **MediaKeySystem** instance.
190e41f4b71Sopenharmony_ci
191e41f4b71Sopenharmony_ci    Destroy the **MediaKeySystem** instance when it is no longer used.
192e41f4b71Sopenharmony_ci
193e41f4b71Sopenharmony_ci     ```ts
194e41f4b71Sopenharmony_ci     // Release resources when the MediaKeySystem instance is no longer needed.
195e41f4b71Sopenharmony_ci     mediaKeySystem.destroy();
196e41f4b71Sopenharmony_ci     ```