• 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 
19 namespace OHOS {
20 namespace MediaAVCodec {
AVDemuxerDemo()21 AVDemuxerDemo::AVDemuxerDemo()
22 {
23     printf("AVDemuxerDemo constructor \n");
24 }
25 
~AVDemuxerDemo()26 AVDemuxerDemo::~AVDemuxerDemo()
27 {
28     printf("AVDemuxerDemo deconstructor \n");
29 }
30 
Destroy()31 int32_t AVDemuxerDemo::Destroy()
32 {
33     int32_t ret = static_cast<int32_t>(OH_AVDemuxer_Destroy(avdemxuer_));
34     if (ret != 0) {
35         printf("OH_AVDemuxer_Destroy is failed\n");
36     }
37     return -1;
38 }
39 
CreateWithSource(OH_AVSource * avsource)40 int32_t AVDemuxerDemo::CreateWithSource(OH_AVSource *avsource)
41 {
42     this->avsource_ = avsource;
43     if (!this->avsource_) {
44         printf("this avsource is nullptr\n");
45         return -1;
46     }
47     avdemxuer_ = OH_AVDemuxer_CreateWithSource(avsource);
48     if (!avdemxuer_) {
49         printf("OH_AVDemuxer_CreateWithSource is failed\n");
50         return -1;
51     }
52     return 0;
53 }
54 
SelectTrackByID(uint32_t trackIndex)55 int32_t AVDemuxerDemo::SelectTrackByID(uint32_t trackIndex)
56 {
57     int32_t ret = static_cast<int32_t>(OH_AVDemuxer_SelectTrackByID(this->avdemxuer_, trackIndex));
58     if (ret != 0) {
59             printf("OH_AVDemuxer_SelectTrackByID is faild \n");
60     }
61     return ret;
62 }
63 
UnselectTrackByID(uint32_t trackIndex)64 int32_t AVDemuxerDemo::UnselectTrackByID(uint32_t trackIndex)
65 {
66     int32_t ret = OH_AVDemuxer_UnselectTrackByID(this->avdemxuer_, trackIndex);
67     if (ret != 0) {
68         printf("OH_AVDemuxer_UnselectTrackByID is faild \n");
69     }
70     return ret;
71 }
72 
PrintInfo(int32_t tracks)73 int32_t AVDemuxerDemo::PrintInfo(int32_t tracks)
74 {
75     for (int32_t i = 0; i < tracks; i++) {
76         printf("streams[%d]==>total frames=%" PRId64 ",KeyFrames=%" PRId64 "\n", i,
77             frames_[i] + key_frames_[i], key_frames_[i]);
78     }
79     return 0;
80 }
81 
ReadSample(uint32_t trackIndex,OH_AVMemory * sample,OH_AVCodecBufferAttr * bufferAttr)82 int32_t AVDemuxerDemo::ReadSample(uint32_t trackIndex, OH_AVMemory *sample, OH_AVCodecBufferAttr *bufferAttr)
83 {
84     int32_t ret = OH_AVDemuxer_ReadSample(this->avdemxuer_, trackIndex, sample, bufferAttr);
85     if (ret != 0) {
86         return ret;
87     }
88     return ret;
89 }
90 
isEOS(std::map<uint32_t,bool> & countFlag)91 bool AVDemuxerDemo::isEOS(std::map<uint32_t, bool>& countFlag)
92 {
93     for (auto iter = countFlag.begin(); iter != countFlag.end(); ++iter) {
94         if (!iter->second) {
95             return false;
96         }
97     }
98     return true;
99 }
100 
ReadAllSamples(OH_AVMemory * sample,int32_t tracks)101 int32_t AVDemuxerDemo::ReadAllSamples(OH_AVMemory *sample, int32_t tracks)
102 {
103     int32_t ret = -1;
104     std::map<uint32_t, bool> eosFlag;
105     for (int i = 0; i < tracks; i++) {
106         frames_[i] = 0;
107         key_frames_[i] = 0;
108         eosFlag[i] = false;
109     }
110     while (!isEOS(eosFlag)) {
111         for (int32_t i = 0; i < tracks; i++) {
112             ret = ReadSample(i, sample, &bufferInfo);
113             if (ret == 0 && bufferInfo.flags == AVCODEC_BUFFER_FLAGS_SYNC_FRAME) {
114                 key_frames_[i]++;
115             } else if (ret == 0 && bufferInfo.flags == AVCODEC_BUFFER_FLAGS_NONE) {
116                 frames_[i]++;
117             } else if (ret == 0 && bufferInfo.flags == AVCODEC_BUFFER_FLAGS_EOS) {
118                 eosFlag[i] = true;
119             } else {
120                 printf("the value or flags is error, ret = %d\n", ret);
121                 printf("the bufferInfo.flags=%d,bufferInfo.size=%d,bufferInfo.pts=%" PRId64 "\n",
122                 bufferInfo.flags, bufferInfo.size, bufferInfo.pts);
123                 return ret;
124             }
125         }
126     }
127     PrintInfo(tracks);
128     return ret;
129 }
130 
SeekToTime(int64_t millisecond,OH_AVSeekMode mode)131 int32_t AVDemuxerDemo::SeekToTime(int64_t millisecond, OH_AVSeekMode mode)
132 {
133     int32_t ret = OH_AVDemuxer_SeekToTime(this->avdemxuer_, millisecond, mode);
134     if (ret != 0) {
135         printf("OH_AVDemuxer_SeekToTime is faild \n");
136     }
137     return ret;
138 }
139 }  // namespace MediaAVCodec
140 }  // namespace OHOS