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