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 #include "native_averrors.h"
17 #include "avmemory_capi_mock.h"
18 #include "demuxer_capi_mock.h"
19 #ifdef SUPPORT_DRM
20 #include "native_drm_common.h"
21 #endif
22 namespace OHOS {
23 namespace MediaAVCodec {
24 
25 #ifdef SUPPORT_DRM
OnMediaKeySystemInfoUpdated(DRM_MediaKeySystemInfo * drmInfo)26 static void OnMediaKeySystemInfoUpdated(DRM_MediaKeySystemInfo *drmInfo)
27 {
28     printf("OnMediaKeySystemInfoUpdated \n");
29     if (drmInfo == nullptr || drmInfo->psshCount > MAX_PSSH_INFO_COUNT) {
30         return;
31     }
32     printf("OnMediaKeySystemInfoUpdated info count: %d \n", drmInfo->psshCount);
33     for (uint32_t i = 0; i < drmInfo->psshCount; i++) {
34         const uint32_t uuidLen = DRM_UUID_LEN;
35         printf("OnMediaKeySystemInfoUpdated print uuid: \n");
36         for (uint32_t index = 0; index < uuidLen; index++) {
37             printf("%x ", drmInfo->psshInfo[i].uuid[index]);
38         }
39         printf(" \n");
40         printf("OnMediaKeySystemInfoUpdated print pssh length %d \n", drmInfo->psshInfo[i].dataLen);
41         if (drmInfo->psshInfo[i].dataLen > MAX_PSSH_DATA_LEN) {
42             return;
43         }
44         unsigned char *pssh = static_cast<unsigned char*>(drmInfo->psshInfo[i].data);
45         for (uint32_t k = 0; k < drmInfo->psshInfo[i].dataLen; k++) {
46             printf("%x ", pssh[k]);
47         }
48         printf(" \n");
49     }
50 }
51 #endif
52 
53 #ifdef SUPPORT_DRM
OnMediaKeySystemInfoUpdatedWithObj(OH_AVDemuxer * demuxer,DRM_MediaKeySystemInfo * drmInfo)54 static void OnMediaKeySystemInfoUpdatedWithObj(OH_AVDemuxer *demuxer, DRM_MediaKeySystemInfo *drmInfo)
55 {
56     printf("OnMediaKeySystemInfoUpdatedWithObj \n");
57     if (drmInfo == nullptr || drmInfo->psshCount > MAX_PSSH_INFO_COUNT) {
58         return;
59     }
60     printf("OnMediaKeySystemInfoUpdatedWithObj info count: %d \n", drmInfo->psshCount);
61     for (uint32_t i = 0; i < drmInfo->psshCount; i++) {
62         const uint32_t uuidLen = DRM_UUID_LEN;
63         printf("OnMediaKeySystemInfoUpdatedWithObj print uuid: \n");
64         for (uint32_t index = 0; index < uuidLen; index++) {
65             printf("%x ", drmInfo->psshInfo[i].uuid[index]);
66         }
67         printf(" \n");
68         printf("OnMediaKeySystemInfoUpdatedWithObj print pssh length %d \n", drmInfo->psshInfo[i].dataLen);
69         if (drmInfo->psshInfo[i].dataLen > MAX_PSSH_DATA_LEN) {
70             return;
71         }
72         unsigned char *pssh = static_cast<unsigned char*>(drmInfo->psshInfo[i].data);
73         for (uint32_t k = 0; k < drmInfo->psshInfo[i].dataLen; k++) {
74             printf("%x ", pssh[k]);
75         }
76         printf(" \n");
77     }
78 }
79 #endif
80 
81 
Destroy()82 int32_t DemuxerCapiMock::Destroy()
83 {
84     if (demuxer_ != nullptr) {
85         int32_t ret = OH_AVDemuxer_Destroy(demuxer_);
86         demuxer_ = nullptr;
87         return ret;
88     }
89     return AV_ERR_UNKNOWN;
90 }
91 
SelectTrackByID(uint32_t trackIndex)92 int32_t DemuxerCapiMock::SelectTrackByID(uint32_t trackIndex)
93 {
94     if (demuxer_ != nullptr) {
95         return OH_AVDemuxer_SelectTrackByID(demuxer_, trackIndex);
96     }
97     return AV_ERR_UNKNOWN;
98 }
99 
UnselectTrackByID(uint32_t trackIndex)100 int32_t DemuxerCapiMock::UnselectTrackByID(uint32_t trackIndex)
101 {
102     if (demuxer_ != nullptr) {
103         return OH_AVDemuxer_UnselectTrackByID(demuxer_, trackIndex);
104     }
105     return AV_ERR_UNKNOWN;
106 }
107 
ReadSample(uint32_t trackIndex,std::shared_ptr<AVMemoryMock> sample,AVCodecBufferInfo * bufferInfo,uint32_t & flag,bool checkBufferInfo)108 int32_t DemuxerCapiMock::ReadSample(uint32_t trackIndex, std::shared_ptr<AVMemoryMock> sample,
109     AVCodecBufferInfo *bufferInfo, uint32_t &flag, bool checkBufferInfo)
110 {
111     (void)checkBufferInfo;
112     auto mem = std::static_pointer_cast<AVMemoryCapiMock>(sample);
113     OH_AVMemory *avMemory = (mem != nullptr) ? mem->GetAVMemory() : nullptr;
114     if (demuxer_ != nullptr) {
115         OH_AVCodecBufferAttr bufferAttr;
116         int32_t ret = OH_AVDemuxer_ReadSample(demuxer_, trackIndex, avMemory, &bufferAttr);
117         bufferInfo->presentationTimeUs = bufferAttr.pts;
118         bufferInfo->size = bufferAttr.size;
119         bufferInfo->offset = bufferAttr.offset;
120         flag = bufferAttr.flags;
121         return ret;
122     }
123     return AV_ERR_UNKNOWN;
124 }
125 
SeekToTime(int64_t mSeconds,Media::SeekMode mode)126 int32_t DemuxerCapiMock::SeekToTime(int64_t mSeconds, Media::SeekMode mode)
127 {
128     if (demuxer_ != nullptr) {
129         OH_AVSeekMode seekMode = static_cast<OH_AVSeekMode>(mode);
130         return OH_AVDemuxer_SeekToTime(demuxer_, mSeconds, seekMode);
131     }
132     return AV_ERR_UNKNOWN;
133 }
134 
SetMediaKeySystemInfoCallback(bool isNull)135 int32_t DemuxerCapiMock::SetMediaKeySystemInfoCallback(bool isNull)
136 {
137     if (demuxer_ != nullptr) {
138         if (!isNull) {
139 #ifdef SUPPORT_DRM
140             return OH_AVDemuxer_SetMediaKeySystemInfoCallback(demuxer_, &OnMediaKeySystemInfoUpdated);
141         } else {
142             return OH_AVDemuxer_SetMediaKeySystemInfoCallback(demuxer_, nullptr);
143 #endif
144         }
145     }
146     return AV_ERR_OK;
147 }
148 
SetDemuxerMediaKeySystemInfoCallback(bool isNull)149 int32_t DemuxerCapiMock::SetDemuxerMediaKeySystemInfoCallback(bool isNull)
150 {
151     if (demuxer_ != nullptr) {
152         if (!isNull) {
153 #ifdef SUPPORT_DRM
154             return OH_AVDemuxer_SetDemuxerMediaKeySystemInfoCallback(demuxer_, &OnMediaKeySystemInfoUpdatedWithObj);
155         } else {
156             return OH_AVDemuxer_SetDemuxerMediaKeySystemInfoCallback(demuxer_, nullptr);
157 #endif
158         }
159     }
160     return AV_ERR_OK;
161 }
162 
GetMediaKeySystemInfo()163 int32_t DemuxerCapiMock::GetMediaKeySystemInfo()
164 {
165     if (demuxer_ != nullptr) {
166 #ifdef SUPPORT_DRM
167         DRM_MediaKeySystemInfo mediaKeySystemInfo;
168         return OH_AVDemuxer_GetMediaKeySystemInfo(demuxer_, &mediaKeySystemInfo);
169 #endif
170     }
171     return AV_ERR_OK;
172 }
173 
GetIndexByRelativePresentationTimeUs(const uint32_t trackIndex,const uint64_t relativePresentationTimeUs,uint32_t & index)174 int32_t DemuxerCapiMock::GetIndexByRelativePresentationTimeUs(const uint32_t trackIndex,
175     const uint64_t relativePresentationTimeUs, uint32_t &index)
176 {
177     return AV_ERR_OK;
178 }
179 
GetRelativePresentationTimeUsByIndex(const uint32_t trackIndex,const uint32_t index,uint64_t & relativePresentationTimeUs)180 int32_t DemuxerCapiMock::GetRelativePresentationTimeUsByIndex(const uint32_t trackIndex,
181     const uint32_t index, uint64_t &relativePresentationTimeUs)
182 {
183     return AV_ERR_OK;
184 }
185 } // namespace MediaAVCodec
186 } // namespace OHOS