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 <cinttypes>
17 #include "avdemuxer_demo.h"
18 #include "native_drm_common.h"
19
20 namespace OHOS {
21 namespace MediaAVCodec {
AVDemuxerDemo()22 AVDemuxerDemo::AVDemuxerDemo()
23 {
24 printf("AVDemuxerDemo constructor \n");
25 }
26
~AVDemuxerDemo()27 AVDemuxerDemo::~AVDemuxerDemo()
28 {
29 printf("AVDemuxerDemo deconstructor \n");
30 }
31
Destroy()32 int32_t AVDemuxerDemo::Destroy()
33 {
34 int32_t ret = static_cast<int32_t>(OH_AVDemuxer_Destroy(avdemxuer_));
35 if (ret != 0) {
36 printf("OH_AVDemuxer_Destroy is failed\n");
37 }
38 return -1;
39 }
40
CreateWithSource(OH_AVSource * avsource)41 int32_t AVDemuxerDemo::CreateWithSource(OH_AVSource *avsource)
42 {
43 this->avsource_ = avsource;
44 if (!this->avsource_) {
45 printf("this avsource is nullptr\n");
46 return -1;
47 }
48 avdemxuer_ = OH_AVDemuxer_CreateWithSource(avsource);
49 if (!avdemxuer_) {
50 printf("OH_AVDemuxer_CreateWithSource is failed\n");
51 return -1;
52 }
53 return 0;
54 }
55
SelectTrackByID(uint32_t trackIndex)56 int32_t AVDemuxerDemo::SelectTrackByID(uint32_t trackIndex)
57 {
58 int32_t ret = static_cast<int32_t>(OH_AVDemuxer_SelectTrackByID(this->avdemxuer_, trackIndex));
59 if (ret != 0) {
60 printf("OH_AVDemuxer_SelectTrackByID is faild \n");
61 }
62 return ret;
63 }
64
UnselectTrackByID(uint32_t trackIndex)65 int32_t AVDemuxerDemo::UnselectTrackByID(uint32_t trackIndex)
66 {
67 int32_t ret = OH_AVDemuxer_UnselectTrackByID(this->avdemxuer_, trackIndex);
68 if (ret != 0) {
69 printf("OH_AVDemuxer_UnselectTrackByID is faild \n");
70 }
71 return ret;
72 }
73
PrintInfo(int32_t tracks)74 int32_t AVDemuxerDemo::PrintInfo(int32_t tracks)
75 {
76 for (int32_t i = 0; i < tracks; i++) {
77 printf("streams[%d]==>total frames=%" PRId64 ",KeyFrames=%" PRId64 "\n", i,
78 frames_[i] + key_frames_[i], key_frames_[i]);
79 }
80 return 0;
81 }
82
ReadSample(uint32_t trackIndex,OH_AVMemory * sample,OH_AVCodecBufferAttr * bufferAttr)83 int32_t AVDemuxerDemo::ReadSample(uint32_t trackIndex, OH_AVMemory *sample, OH_AVCodecBufferAttr *bufferAttr)
84 {
85 int32_t ret = OH_AVDemuxer_ReadSample(this->avdemxuer_, trackIndex, sample, bufferAttr);
86 if (ret != 0) {
87 return ret;
88 }
89 return ret;
90 }
91
isEOS(std::map<uint32_t,bool> & countFlag)92 bool AVDemuxerDemo::isEOS(std::map<uint32_t, bool>& countFlag)
93 {
94 for (auto iter = countFlag.begin(); iter != countFlag.end(); ++iter) {
95 if (!iter->second) {
96 return false;
97 }
98 }
99 return true;
100 }
101
ReadAllSamples(OH_AVMemory * sample,int32_t tracks)102 int32_t AVDemuxerDemo::ReadAllSamples(OH_AVMemory *sample, int32_t tracks)
103 {
104 int32_t ret = -1;
105 std::map<uint32_t, bool> eosFlag;
106 for (int i = 0; i < tracks; i++) {
107 frames_[i] = 0;
108 key_frames_[i] = 0;
109 eosFlag[i] = false;
110 }
111 while (!isEOS(eosFlag)) {
112 for (int32_t i = 0; i < tracks; i++) {
113 ret = ReadSample(i, sample, &bufferInfo);
114 if (ret == 0 && (bufferInfo.flags & AVCODEC_BUFFER_FLAGS_EOS)) {
115 eosFlag[i] = true;
116 continue;
117 }
118 if (ret == 0 && (bufferInfo.flags & AVCODEC_BUFFER_FLAGS_SYNC_FRAME)) {
119 key_frames_[i]++;
120 } else if (ret == 0 && (bufferInfo.flags & AVCODEC_BUFFER_FLAGS_NONE) == 0) {
121 frames_[i]++;
122 } else {
123 printf("the value or flags is error, ret = %d\n", ret);
124 printf("the bufferInfo.flags=%d,bufferInfo.size=%d,bufferInfo.pts=%" PRId64 "\n",
125 bufferInfo.flags, bufferInfo.size, bufferInfo.pts);
126 return ret;
127 }
128 }
129 }
130 PrintInfo(tracks);
131 return ret;
132 }
133
SeekToTime(int64_t millisecond,OH_AVSeekMode mode)134 int32_t AVDemuxerDemo::SeekToTime(int64_t millisecond, OH_AVSeekMode mode)
135 {
136 int32_t ret = OH_AVDemuxer_SeekToTime(this->avdemxuer_, millisecond, mode);
137 if (ret != 0) {
138 printf("OH_AVDemuxer_SeekToTime is faild \n");
139 }
140 return ret;
141 }
142
OnDrmInfoChangedInApp(DRM_MediaKeySystemInfo * drmInfo)143 static void OnDrmInfoChangedInApp(DRM_MediaKeySystemInfo *drmInfo)
144 {
145 printf("OnDrmInfoChangedInApp \n");
146 if (drmInfo == nullptr || drmInfo->psshCount > MAX_PSSH_INFO_COUNT) {
147 return;
148 }
149 printf("OnDrmInfoChangedInApp info count: %d \n", drmInfo->psshCount);
150 for (uint32_t i = 0; i < drmInfo->psshCount; i++) {
151 const uint32_t uuidLen = DRM_UUID_LEN;
152 printf("OnDrmInfoChangedInApp print uuid: \n");
153 for (uint32_t index = 0; index < uuidLen; index++) {
154 printf("%x ", drmInfo->psshInfo[i].uuid[index]);
155 }
156 printf(" \n");
157 printf("OnDrmInfoChangedInApp print pssh length %d \n", drmInfo->psshInfo[i].dataLen);
158 if (drmInfo->psshInfo[i].dataLen > MAX_PSSH_DATA_LEN) {
159 return;
160 }
161 unsigned char *pssh = static_cast<unsigned char*>(drmInfo->psshInfo[i].data);
162 for (uint32_t k = 0; k < drmInfo->psshInfo[i].dataLen; k++) {
163 printf("%x ", pssh[k]);
164 }
165 printf(" \n");
166 }
167 }
168
OnDrmInfoChangedWithObjInApp(OH_AVDemuxer * demuxer,DRM_MediaKeySystemInfo * drmInfo)169 static void OnDrmInfoChangedWithObjInApp(OH_AVDemuxer *demuxer, DRM_MediaKeySystemInfo *drmInfo)
170 {
171 printf("OnDrmInfoChangedWithObjInApp \n");
172 printf("OnDrmInfoChangedWithObjInApp demuxer is %p\n", static_cast<void*>(demuxer));
173 if (drmInfo == nullptr || drmInfo->psshCount > MAX_PSSH_INFO_COUNT) {
174 return;
175 }
176 printf("OnDrmInfoChangedWithObjInApp info count: %d \n", drmInfo->psshCount);
177 for (uint32_t i = 0; i < drmInfo->psshCount; i++) {
178 const uint32_t uuidLen = DRM_UUID_LEN;
179 printf("OnDrmInfoChangedWithObjInApp print uuid: \n");
180 for (uint32_t index = 0; index < uuidLen; index++) {
181 printf("%x ", drmInfo->psshInfo[i].uuid[index]);
182 }
183 printf(" \n");
184 printf("OnDrmInfoChangedWithObjInApp print pssh length %d \n", drmInfo->psshInfo[i].dataLen);
185 if (drmInfo->psshInfo[i].dataLen > MAX_PSSH_DATA_LEN) {
186 return;
187 }
188 unsigned char *pssh = static_cast<unsigned char*>(drmInfo->psshInfo[i].data);
189 for (uint32_t k = 0; k < drmInfo->psshInfo[i].dataLen; k++) {
190 printf("%x ", pssh[k]);
191 }
192 printf(" \n");
193 }
194 }
195
SetDrmAppCallback()196 int32_t AVDemuxerDemo::SetDrmAppCallback()
197 {
198 printf("SetDrmAppCallback \n");
199 DRM_MediaKeySystemInfoCallback callback = &OnDrmInfoChangedInApp;
200 int32_t ret = OH_AVDemuxer_SetMediaKeySystemInfoCallback(this->avdemxuer_, callback);
201 printf("SetDrmAppCallback ret %d \n", ret);
202 Demuxer_MediaKeySystemInfoCallback callbackObj = &OnDrmInfoChangedWithObjInApp;
203 ret = OH_AVDemuxer_SetDemuxerMediaKeySystemInfoCallback(this->avdemxuer_, callbackObj);
204 printf("SetDrmAppCallbackWithObj ret %d \n", ret);
205 return ret;
206 }
207
GetMediaKeySystemInfo()208 void AVDemuxerDemo::GetMediaKeySystemInfo()
209 {
210 DRM_MediaKeySystemInfo mediaKeySystemInfo;
211 OH_AVDemuxer_GetMediaKeySystemInfo(this->avdemxuer_, &mediaKeySystemInfo);
212 printf("GetMediaKeySystemInfo count %d", mediaKeySystemInfo.psshCount);
213 for (uint32_t i = 0; i < mediaKeySystemInfo.psshCount; i++) {
214 printf("GetMediaKeySystemInfo print");
215 const uint32_t uuidLen = 16;
216 for (uint32_t index = 0; index < uuidLen; index++) {
217 printf("GetMediaKeySystemInfo print uuid %x \n", mediaKeySystemInfo.psshInfo[i].uuid[index]);
218 }
219 printf("GetMediaKeySystemInfo print pssh length %d \n", mediaKeySystemInfo.psshInfo[i].dataLen);
220 for (uint32_t k = 0; k < mediaKeySystemInfo.psshInfo[i].dataLen; k++) {
221 unsigned char *pssh = static_cast<unsigned char*>(mediaKeySystemInfo.psshInfo[i].data);
222 printf("GetMediaKeySystemInfo print pssh %x \n", pssh[k]);
223 }
224 }
225 }
226
227 } // namespace MediaAVCodec
228 } // namespace OHOS
229