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 printf("OnDrmInfoChangedInApp info count: %d \n", drmInfo->psshCount);
147 for (uint32_t i = 0; i < drmInfo->psshCount; i++) {
148 printf("OnDrmInfoChangedInApp print");
149 const uint32_t uuidLen = 16;
150 for (uint32_t index = 0; index < uuidLen; index++) {
151 printf("OnDrmInfoChangedInApp print uuid %x \n", drmInfo->psshInfo[i].uuid[index]);
152 }
153 printf("OnDrmInfoChangedInApp print pssh length %d \n", drmInfo->psshInfo[i].dataLen);
154 for (uint32_t k = 0; k < drmInfo->psshInfo[i].dataLen; k++) {
155 unsigned char *pssh = static_cast<unsigned char*>(drmInfo->psshInfo[i].data);
156 printf("OnDrmInfoChangedInApp print pssh %x \n", pssh[k]);
157 }
158 }
159 }
160
SetDrmAppCallback()161 int32_t AVDemuxerDemo::SetDrmAppCallback()
162 {
163 printf("SetDrmAppCallback \n");
164 DRM_MediaKeySystemInfoCallback callback = &OnDrmInfoChangedInApp;
165 int32_t ret = OH_AVDemuxer_SetMediaKeySystemInfoCallback(this->avdemxuer_, callback);
166 printf("SetDrmAppCallback ret %d \n", ret);
167 return ret;
168 }
169
GetMediaKeySystemInfo()170 void AVDemuxerDemo::GetMediaKeySystemInfo()
171 {
172 DRM_MediaKeySystemInfo mediaKeySystemInfo;
173 OH_AVDemuxer_GetMediaKeySystemInfo(this->avdemxuer_, &mediaKeySystemInfo);
174 printf("GetMediaKeySystemInfo count %d", mediaKeySystemInfo.psshCount);
175 for (uint32_t i = 0; i < mediaKeySystemInfo.psshCount; i++) {
176 printf("GetMediaKeySystemInfo print");
177 const uint32_t uuidLen = 16;
178 for (uint32_t index = 0; index < uuidLen; index++) {
179 printf("GetMediaKeySystemInfo print uuid %x \n", mediaKeySystemInfo.psshInfo[i].uuid[index]);
180 }
181 printf("GetMediaKeySystemInfo print pssh length %d \n", mediaKeySystemInfo.psshInfo[i].dataLen);
182 for (uint32_t k = 0; k < mediaKeySystemInfo.psshInfo[i].dataLen; k++) {
183 unsigned char *pssh = static_cast<unsigned char*>(mediaKeySystemInfo.psshInfo[i].data);
184 printf("GetMediaKeySystemInfo print pssh %x \n", pssh[k]);
185 }
186 }
187 }
188
189 } // namespace MediaAVCodec
190 } // namespace OHOS
191