1/*
2 * Copyright (C) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/**
17 * @addtogroup Multimedia_Drm
18 * @{
19 *
20 * @brief This feature enables third-party applications to implement the
21 * media decapsulation and demultiplexing functions by themselves instead
22 * of using the functions provided by the system.
23 *
24 * After the DRM instance and session are created, the decryption interface
25 * provided by the DRM can be invoked for decryption. The decryption parameter
26 * structure defines the transmission format of decryption parameters.
27 *
28 * @since 12
29 */
30
31/**
32 * @file native_cencinfo.h
33 *
34 * @brief Provides a unified entry for the native module APIs.
35 *
36 * @kit AVCodecKit
37 * @library libnative_media_avcencinfo.so
38 * @syscap SystemCapability.Multimedia.Media.Spliter
39 * @since 12
40 */
41
42#ifndef NATIVE_AVCENCINFO_H
43#define NATIVE_AVCENCINFO_H
44
45#include <stdint.h>
46#include "native_averrors.h"
47
48#ifdef __cplusplus
49extern "C" {
50#endif
51
52/**
53 * @brief AVBuffer Structure.
54 * @since 12
55 * @version 1.0
56 */
57typedef struct OH_AVBuffer OH_AVBuffer;
58/**
59 * @brief AVCencInfo Structure.
60 * @since 12
61 * @version 1.0
62 */
63typedef struct OH_AVCencInfo OH_AVCencInfo;
64/**
65 * @brief Key id size.
66 * @since 12
67 * @version 1.0
68 */
69#define DRM_KEY_ID_SIZE 16
70/**
71 * @brief Iv size.
72 * @since 12
73 * @version 1.0
74 */
75#define DRM_KEY_IV_SIZE 16
76/**
77 * @brief Max subsample num.
78 * @since 12
79 * @version 1.0
80 */
81#define DRM_KEY_MAX_SUB_SAMPLE_NUM 64
82
83/**
84 * @brief Drm cenc algorithm type.
85 * @since 12
86 * @version 1.0
87 */
88typedef enum DrmCencAlgorithm {
89    /**
90     * Unencrypted.
91     */
92    DRM_ALG_CENC_UNENCRYPTED = 0x0,
93    /**
94     * Aes ctr.
95     */
96    DRM_ALG_CENC_AES_CTR = 0x1,
97    /**
98     * Aes wv.
99     */
100    DRM_ALG_CENC_AES_WV = 0x2,
101    /**
102     * Aes cbc.
103     */
104    DRM_ALG_CENC_AES_CBC = 0x3,
105    /**
106     * Sm4 cbc.
107     */
108    DRM_ALG_CENC_SM4_CBC = 0x4,
109    /**
110     * Sm4 ctr.
111     */
112    DRM_ALG_CENC_SM4_CTR = 0x5
113} DrmCencAlgorithm;
114
115/**
116 * @brief Mode of cend info like set or not.
117 * @since 12
118 * @version 1.0
119 */
120typedef enum DrmCencInfoMode {
121    /* key/iv/subsample set. */
122    DRM_CENC_INFO_KEY_IV_SUBSAMPLES_SET = 0x0,
123    /* key/iv/subsample not set. */
124    DRM_CENC_INFO_KEY_IV_SUBSAMPLES_NOT_SET = 0x1
125} DrmCencInfoMode;
126
127/**
128 * @brief Subsample info of media.
129 * @since 12
130 * @version 1.0
131 */
132typedef struct DrmSubsample {
133    /* Clear header len. */
134    uint32_t clearHeaderLen;
135    /* Payload Len. */
136    uint32_t payLoadLen;
137} DrmSubsample;
138
139/**
140 * @brief Creates an OH_AVCencInfo instance for setting cencinfo.
141 *
142 * Free the resources of the instance by calling OH_AVCencInfo_Destory.
143 * @syscap SystemCapability.Multimedia.Media.Spliter
144 * @return Returns the newly created OH_AVCencInfo object. If nullptr is returned, the object failed to be created.
145 *         The possible failure is due to the application address space being full,
146 *         or the data in the initialization object has failed.
147 * @since 12
148 * @version 1.0
149 */
150OH_AVCencInfo *OH_AVCencInfo_Create();
151
152/**
153 * @brief Destroy the OH_AVCencInfo instance and free the internal resources.
154 *
155 * The same instance can only be destroyed once. The destroyed instance
156 * should not be used before it is created again. It is recommended setting
157 * the instance pointer to NULL right after the instance is destroyed successfully.
158 * @syscap SystemCapability.Multimedia.Media.Spliter
159 * @param cencInfo Pointer to an OH_AVCencInfo instance.
160 * @return {@link AV_ERR_OK} 0 - Success
161 *         {@link AV_ERR_INVALID_VAL} 3 - cencInfo is nullptr.
162 * @since 12
163 * @version 1.0
164*/
165OH_AVErrCode OH_AVCencInfo_Destroy(OH_AVCencInfo *cencInfo);
166
167/**
168 * @brief Method to set algo of cencinfo.
169 *
170 * @syscap SystemCapability.Multimedia.Media.Spliter
171 * @param cencInfo Pointer to an OH_AVCencInfo instance.
172 * @param algo Cenc algo.
173 * @return {@link AV_ERR_OK} 0 - Success
174 *         {@link AV_ERR_INVALID_VAL} 3 - cencInfo is nullptr.
175 * @since 12
176 * @version 1.0
177 */
178OH_AVErrCode OH_AVCencInfo_SetAlgorithm(OH_AVCencInfo *cencInfo, enum DrmCencAlgorithm algo);
179
180/**
181 * @brief Method to set key id and iv of cencinfo.
182 *
183 * @syscap SystemCapability.Multimedia.Media.Spliter
184 * @param cencInfo Pointer to an OH_AVCencInfo instance.
185 * @param keyId Key id.
186 * @param keyIdLen Key id len.
187 * @param iv Iv.
188 * @param ivLen Iv len.
189 * @return {@link AV_ERR_OK} 0 - Success
190 *         {@link AV_ERR_INVALID_VAL} 3 - If cencInfo is nullptr, or keyId is nullptr, or keyIdLen != DRM_KEY_ID_SIZE,
191 *         or iv is nullptr, or ivLen != DRM_KEY_IV_SIZE, or keyId copy fails, or iv copy fails.
192 * @since 12
193 * @version 1.0
194 */
195OH_AVErrCode OH_AVCencInfo_SetKeyIdAndIv(OH_AVCencInfo *cencInfo, uint8_t *keyId,
196    uint32_t keyIdLen, uint8_t *iv, uint32_t ivLen);
197
198/**
199 * @brief Method to set subsample info of cencinfo.
200 *
201 * @syscap SystemCapability.Multimedia.Media.Spliter
202 * @param cencInfo Pointer to an OH_AVCencInfo instance.
203 * @param encryptedBlockCount Number of encrypted blocks.
204 * @param skippedBlockCount Number of skip(clear) blocks.
205 * @param firstEncryptedOffset Offset of first encrypted payload.
206 * @param subsampleCount Subsample num.
207 * @param subsamples Subsample info
208 * @return {@link AV_ERR_OK} 0 - Success
209 *         {@link AV_ERR_INVALID_VAL} 3 - If cencInfo is nullptr, or subsampleCount > DRM_KEY_MAX_SUB_SAMPLE_NUM,
210 *         or subsamples is nullptr.
211 * @since 12
212 * @version 1.0
213 */
214OH_AVErrCode OH_AVCencInfo_SetSubsampleInfo(OH_AVCencInfo *cencInfo, uint32_t encryptedBlockCount,
215    uint32_t skippedBlockCount, uint32_t firstEncryptedOffset, uint32_t subsampleCount, DrmSubsample *subsamples);
216
217/**
218 * @brief Method to set mode of cencinfo.
219 *
220 * @syscap SystemCapability.Multimedia.Media.Spliter
221 * @param cencInfo Pointer to an OH_AVCencInfo instance.
222 * @param mode Cenc mode, indicate whether key/iv/subsample set or not.
223 * @return {@link AV_ERR_OK} 0 - Success
224 *         {@link AV_ERR_INVALID_VAL} 3 - cencInfo is nullptr.
225 * @since 12
226 * @version 1.0
227 */
228OH_AVErrCode OH_AVCencInfo_SetMode(OH_AVCencInfo *cencInfo, enum DrmCencInfoMode mode);
229
230/**
231 * @brief Method to attach cencinfo to AVBuffer.
232 *
233 * @syscap SystemCapability.Multimedia.Media.Spliter
234 * @param cencInfo Pointer to an OH_AVCencInfo instance.
235 * @param buffer AVBuffer to attach cencinfo.
236 * @return {@link AV_ERR_OK} 0 - Success
237 *         {@link AV_ERR_INVALID_VAL} 3 - If cencInfo is nullptr, or buffer is nullptr, or buffer->buffer_ is nullptr,
238 *         or buffer->buffer_->meta_ is nullptr.
239 * @since 12
240 * @version 1.0
241 */
242OH_AVErrCode OH_AVCencInfo_SetAVBuffer(OH_AVCencInfo *cencInfo, OH_AVBuffer *buffer);
243
244#ifdef __cplusplus
245}
246#endif
247
248#endif // NATIVE_AVCENCINFO_H
249