• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "demuxersetdatasourcewithprobsize_sample.h"
16 
17 using namespace OHOS;
18 using namespace OHOS::Media;
19 using namespace std;
20 
21 const int BUFFER_PADDING_SIZE = 1024;
22 const std::string prefix = "avdemux";
23 
CreateDataSource(const std::string & filePath)24 bool DemuxerPluginTest::CreateDataSource(const std::string& filePath)
25 {
26     mediaSource_ = std::make_shared<MediaSource>(filePath);
27     realSource_ = std::make_shared<Source>();
28     realSource_->SetSource(mediaSource_);
29 
30     realStreamDemuxer_ = std::make_shared<StreamDemuxer>();
31     realStreamDemuxer_->SetSourceType(Plugins::SourceType::SOURCE_TYPE_URI);
32     realStreamDemuxer_->SetSource(realSource_);
33     realStreamDemuxer_->Init(filePath);
34 
35     realStreamDemuxer_->SetDemuxerState(streamId_, DemuxerState::DEMUXER_STATE_PARSE_HEADER);
36     dataSourceImpl_ = std::make_shared<DataSourceImpl>(realStreamDemuxer_, streamId_);
37     realSource_->NotifyInitSuccess();
38 
39     return true;
40 }
41 
CreateDemuxerPluginByName(const std::string & typeName,const std::string & filePath,int probSize)42 bool DemuxerPluginTest::CreateDemuxerPluginByName(
43     const std::string& typeName, const std::string& filePath, int probSize)
44 {
45     if (typeName.compare(0, prefix.size(), prefix) != 0) {
46         return false;
47     }
48 
49     if (!CreateDataSource(filePath)) {
50         return false;
51     }
52     pluginBase_ = Plugins::PluginManagerV2::Instance().CreatePluginByName(typeName);
53     if (pluginBase_ == nullptr) {
54         return false;
55     }
56 
57     auto demuxerPlugin = std::static_pointer_cast<Plugins::DemuxerPlugin>(pluginBase_);
58     if (demuxerPlugin->SetDataSourceWithProbSize(dataSourceImpl_, probSize) != Status::OK) {
59         return false;
60     }
61 
62     realStreamDemuxer_->SetDemuxerState(streamId_, DemuxerState::DEMUXER_STATE_PARSE_FIRST_FRAME);
63 
64     return true;
65 }
66 
PluginSelectTracks()67 bool DemuxerPluginTest::PluginSelectTracks()
68 {
69     MediaInfo mediaInfo;
70     auto demuxerPlugin = std::static_pointer_cast<Plugins::DemuxerPlugin>(pluginBase_);
71     if (demuxerPlugin->GetMediaInfo(mediaInfo) != Status::OK) {
72         return false;
73     }
74 
75     for (size_t i = 0; i < mediaInfo.tracks.size(); i++) {
76         demuxerPlugin->SelectTrack(static_cast<uint32_t>(i));
77         selectedTrackIds_.push_back(static_cast<uint32_t>(i));
78     }
79 
80     return true;
81 }
82 
PluginReadSample(uint32_t idx,uint32_t & flag)83 bool DemuxerPluginTest::PluginReadSample(uint32_t idx, uint32_t& flag)
84 {
85     int bufSize = 0;
86     auto demuxerPlugin = std::static_pointer_cast<Plugins::DemuxerPlugin>(pluginBase_);
87     demuxerPlugin->GetNextSampleSize(idx, bufSize);
88     if (static_cast<size_t>(bufSize) > buffer_.size()) {
89         buffer_.resize(bufSize + BUFFER_PADDING_SIZE);
90     }
91 
92     auto avBuf = AVBuffer::CreateAVBuffer(buffer_.data(), bufSize, bufSize);
93     if (avBuf == nullptr) {
94         return false;
95     }
96 
97     demuxerPlugin->ReadSample(idx, avBuf);
98     flag = avBuf->flag_;
99 
100     return true;
101 }
102 
PluginReadAllSample()103 bool DemuxerPluginTest::PluginReadAllSample()
104 {
105     bool end = false;
106     while (!end) {
107         for (auto idx : selectedTrackIds_) {
108             uint32_t flag = 0;
109             if (!PluginReadSample(idx, flag)) {
110                 return false;
111             }
112             if (flag & static_cast<uint32_t>(AVBufferFlag::EOS)) {
113                 end = true;
114                 break;
115             }
116         }
117     }
118 
119     return true;
120 }
121 
Run(const std::string & typeName,const std::string & filePath,int probSize)122 bool DemuxerPluginTest::Run(const std::string& typeName, const std::string& filePath, int probSize)
123 {
124     if (!CreateDemuxerPluginByName(typeName, filePath, probSize)) {
125         return false;
126     }
127     if (!PluginSelectTracks()) {
128         return false;
129     }
130     if (!PluginReadAllSample()) {
131         return false;
132     }
133     return true;
134 }
135