• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# DRM System Management (ArkTS)
2
3Using the **MediaKeySystem** class of the DRM module, you can manage **MediaKeySystem** instances, generate media key system requests, process responses to these requests, create media key sessions, manage offline media keys, and obtain DRM statistics and device configuration information.
4
5## How to Develop
6
7Read [DRM](../reference/apis-drm-kit/js-apis-drm.md) for the API reference.
8
91. Import the module.
10
11   ```ts
12   import drm from '@ohos.multimedia.drm';
13   ```
14
152. Call **setConfigurationString** in the **MediaKeySystem** class to set a configuration item in the form of a string. If the call fails, an error code is returned. For details about the error code types, see [DrmErrorCode](../reference/apis-drm-kit/js-apis-drm.md#drmerrorcode).
16
17   ```ts
18   function setConfigurationString(configName: string, value: string): void {
19     let mediaKeysystem: drm.mediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
20     try {
21       mediaKeysystem.setConfigurationString(configName, value);
22    } catch (err) {
23       let error = err as BusinessError;
24       console.error(`setConfigurationString ERROR: ${error}`);
25    }
26     return ;
27   }
28   ```
29
303. Call **getConfigurationString** in the **MediaKeySystem** class to obtain the value of a configuration item in the form of a string. If the call fails, an error code is returned. For details about the error code types, see [DrmErrorCode](../reference/apis-drm-kit/js-apis-drm.md#drmerrorcode).
31
32   ```ts
33   function getConfigurationString(configName: string): string {
34     let mediaKeysystem: drm.mediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
35     try {
36       let configValue: string = mediaKeysystem.getConfigurationString(configName);
37     } catch (err) {
38       let error = err as BusinessError;
39       console.error(`getConfigurationString ERROR: ${error}`);
40     }
41     return configValue;
42   }
43   ```
44
454. Call **setConfigurationByteArray** in the **MediaKeySystem** class to set a configuration item in the form of a byte array. If the call fails, an error code is returned. For details about the error code types, see [DrmErrorCode](../reference/apis-drm-kit/js-apis-drm.md#drmerrorcode).
46
47   ```ts
48   function setConfigurationByteArray(configName: string, value: Uint8Array): void {
49     let mediaKeysystem: drm.mediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
50     try {
51       mediaKeysystem.setConfigurationByteArray(configName, value);
52     } catch (err) {
53       let error = err as BusinessError;
54       console.error(`setConfigurationByteArray ERROR: ${error}`);
55     }
56     return;
57   }
58   ```
59
605. Call **getConfigurationByteArray** in the **MediaKeySystem** class to obtain the value of a configuration item in the form of a byte array. If the call fails, an error code is returned. For details about the error code types, see [DrmErrorCode](../reference/apis-drm-kit/js-apis-drm.md#drmerrorcode).
61
62   ```ts
63   function getConfigurationByteArray(configName: string): Uint8Array {
64     let mediaKeysystem: drm.mediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm");
65     try {
66       let configValue: Uint8Array = mediaKeysystem.getConfigurationByteArray(configName);
67     } catch (err) {
68       let error = err as BusinessError;
69       console.error(`getConfigurationByteArray ERROR: ${error}`);
70     }
71     return configValue;
72   }
73   ```
74
756. Call **getMetrics()** in the **MediaKeySystem** class to obtain the statistics information, including the number of current sessions, decryption times, and decryption failures, as well as the plug-in version. If the call fails, an error code is returned. For details about the error code types, see [DrmErrorCode](../reference/apis-drm-kit/js-apis-drm.md#drmerrorcode).
76
77   ```ts
78   function getStatistics(): StatisticKeyValue[] {
79     try {
80       let statisticKeyValue: StatisticKeyValue[] = mediaKeysystem.getStatistics();
81     } catch (err) {
82       let error = err as BusinessError;
83       console.error(`getConfigurationByteArray ERROR: ${error}`);
84     }
85     return statisticKeyValue;
86   }
87   ```
88
897. Obtains the maximum content protection level.
90
91   Call **getMaxContentProtectionLevel** in the **MediaKeySystem** class to obtain the maximum content protection level supported by the device. If the call fails, an error code is returned. For details about the error code types, see [DrmErrorCode](../reference/apis-drm-kit/js-apis-drm.md#drmerrorcode).
92
93     ```ts
94   function getMaxContentProtectionLevel(): ContentProtectionLevel {
95     try {
96       let contentProtectionLevel: drm.ContentProtectionLevel = mediaKeysystem.getMaxContentProtectionLevel();
97     } catch (err) {
98       let error = err as BusinessError;
99       console.error(`getConfigurationByteArray ERROR: ${error}`);
100     }
101     return contentProtectionLevel;
102   }
103     ```
104
1058. Generate a media key system request to obtain a provision request.
106
107   Call **generateKeySystemRequest** method in the **MediaKeySystem** class to generate a media key system request. If the call fails, an error code is returned. For details about the error code types, see [DrmErrorCode](../reference/apis-drm-kit/js-apis-drm.md#drmerrorcode).
108
109     ```ts
110   function generateKeySystemRequest(): Promise<ProvisionRequest> {
111     generateKeySystemRequest().then((ProvisionRequest: drm.ProvisionRequest) => {
112       console.log("generateKeySystemRequest");
113     }).catch((err: BusinessError) => {
114       console.error(`generateKeySystemRequest: ERROR: ${err}`);
115     });
116     return provisionRequest;
117   }
118     ```
119
1209. Process the response to the media key system request.
121
122   Call **processKeySystemResponse** in the **MediaKeySystem** class to process the response. If the call fails, an error code is returned. For details about the error code types, see [DrmErrorCode](../reference/apis-drm-kit/js-apis-drm.md#drmerrorcode).
123
124     ```ts
125   function processKeySystemResponse(response: Uint8Array): Promise<void> {
126     processKeySystemResponse(response).then(() => {
127       console.log("processKeySystemResponse");
128     }).catch((err: BusinessError) => {
129       console.error(`processKeySystemResponse: ERROR: ${err}`);
130     });
131     return;
132   }
133     ```
134
13510. Obtain the status of the device certificate.
136
137    Call **getCertificateStatus** in the **MediaKeySystem** class to obtain the device certificate status. If the call fails, an error code is returned. For details about the error code types, see [DrmErrorCode](../reference/apis-drm-kit/js-apis-drm.md#drmerrorcode).
138
139    ```ts
140    function getCertificateStatus(): CertificateStatus {
141      try {
142        let certificateStatus: drm.CertificateStatus = mediaKeysystem.getCertificateStatus();
143      } catch (err) {
144        let error = err as BusinessError;
145        console.error(`getCertificateStatus ERROR: ${error}`);
146      }
147      return certificateStatus;
148    }
149    ```
150
15111. Create a DRM session.
152
153    Call **createMediaKeySession** in the **MediaKeySystem** class to create a session based on the given content protection level. If the call fails, an error code is returned. For details about the error code types, see [DrmErrorCode](../reference/apis-drm-kit/js-apis-drm.md#drmerrorcode).
154
155    ```ts
156    function createMediaKeySession(level: ContentProtectionLevel): MediaKeySession {
157      try {
158        let mediaKeySession: drm.MediaKeySession = mediaKeysystem.createMediaKeySession(drm.level.CONTENT_PROTECTION_LEVEL_SW_CRYPTO);
159      } catch (err) {
160        let error = err as BusinessError;
161        console.error(`getCertificateStatus ERROR: ${error}`);
162      }
163      return mediaKeySession;
164    }
165    ```
166
16712. Create a DRM session.
168
169    Call **createMediaKeySession** in the **MediaKeySystem** class to create a session. If the call fails, an error code is returned. For details about the error code types, see [DrmErrorCode](../reference/apis-drm-kit/js-apis-drm.md#drmerrorcode).
170
171    ```ts
172    function createMediaKeySession(): MediaKeySession {
173      try {
174        let mediaKeySession: drm.MediaKeySession = mediaKeysystem.createMediaKeySession();
175      } catch (err) {
176        let error = err as BusinessError;
177        console.error(`getCertificateStatus ERROR: ${error}`);
178      }
179      return mediaKeySession;
180    }
181    ```
182
18313. Obtain the IDs of offline media keys.
184
185    Call **getOfflineMediaKeyIds** in the **MediaKeySystem** class to obtain the IDs of offline media keys. If the call fails, an error code is returned. For details about the error code types, see [DrmErrorCode](../reference/apis-drm-kit/js-apis-drm.md#drmerrorcode).
186
187    ```ts
188    function getOfflineMediaKeyIds(): Uint8Array[] {
189      try {
190        let offlineMediaKeyIds: Uint8Array[] = mediaKeysystem.getOfflineMediaKeyIds();
191      } catch (err) {
192        let error = err as BusinessError;
193        console.error(`getOfflineMediaKeyIds ERROR: ${error}`);
194      }
195      return offlineMediaKeyIds;
196    }
197    ```
198
19914. Obtain the status of the offline media keys.
200
201    Call **getOfflineMediaKeyStatus** in the **MediaKeySystem** class to obtain the status of the offline media keys. If the call fails, an error code is returned. For details about the error code types, see [DrmErrorCode](../reference/apis-drm-kit/js-apis-drm.md#drmerrorcode).
202
203    ```ts
204    function getOfflineMediaKeyStatus(mediakeyId: Uint8Array): OfflineMediaKeyStatus {
205      try {
206        let offlineMediaKeyStatus: drm.OfflineMediaKeyStatus = mediaKeysystem.getOfflineMediaKeyStatus(mediakeyId);
207      } catch (err) {
208        let error = err as BusinessError;
209        console.error(`getOfflineMediaKeyStatus ERROR: ${error}`);
210      }
211      return offlineMediaKeyStatus;
212    }
213    ```
214
21515. Clear the offline media keys.
216
217    Call **clearOfflineMediaKeys** in the **MediaKeySystem** class to clear the offline media keys. If the call fails, an error code is returned. For details about the error code types, see [DrmErrorCode](../reference/apis-drm-kit/js-apis-drm.md#drmerrorcode).
218
219    ```ts
220    function clearOfflineMediaKeys(mediakeyId: Uint8Array): void {
221      try {
222        mediaKeysystem.clearOfflineMediaKeys(mediakeyId);
223      } catch (err) {
224        let error = err as BusinessError;
225        console.error(`clearOfflineMediaKeys ERROR: ${error}`);
226      }
227      return;
228    }
229    ```
230
23116. Destroy this **MediaKeySystem** instance.
232
233    Call **destroy** in the **MediaKeySystem** class to destroy this **MediaKeySystem** instance. If the call fails, an error code is returned. For details about the error code types, see [DrmErrorCode](../reference/apis-drm-kit/js-apis-drm.md#drmerrorcode).
234
235    ```ts
236    function destroy(mediaKeySyste: MediaKeySyste): void {
237      try {
238        mediaKeysystem.destroy();
239      } catch (err) {
240        let error = err as BusinessError;
241        console.error(`mediaKeysystem destroy ERROR: ${error}`);
242      }
243      return;
244    }
245    ```
246
24717. Listen for the media key system status.
248
249    You can listen for the following events: device certificate request events.
250
251    To listen for key request events, register the **keySystemRequired** callback function. This event can be listened for when a **MediaKeySystem** instance is created. If this event callback is returned, a media key system request starts. You can use **off** to deregister the callback function when listening is no longer required.
252
253    ```ts
254    function onRegisterkeySystemRequired(mediaKeysystem: drm.MediaKeySystem): void {
255      mediaKeysystem.on('keySystemRequired', (eventInfo: EventInfo) => {
256        console.log('keySystemRequired' + 'extra:' + eventInfo.extraInfo + ' data:' + eventInfo.info);
257      });
258    }
259    ```
260
261    ```ts
262    function unregisterkeySystemRequired(mediaKeysystem: drm.MediaKeySystem): void {
263      mediaKeysystem.off('keySystemRequired');
264    }
265    ```