• 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 #ifndef MEDIA_DEMUXER_MOCK_FUZZ_H
17 #define MEDIA_DEMUXER_MOCK_FUZZ_H
18 
19 #include "media_demuxer.h"
20 #include "demuxer_plugin_manager.h"
21 #include "stream_demuxer.h"
22 #include "source/source.h"
23 #include "common/media_source.h"
24 #include "buffer/avbuffer_queue.h"
25 
26 namespace OHOS {
27 namespace Media {
28 const int NUMBER_CACHE = 5000000;
29 class DemuxerPluginMock : public Plugins::DemuxerPlugin {
30 public:
DemuxerPluginMock(std::string name)31     explicit DemuxerPluginMock(std::string name) : DemuxerPlugin(name)
32     {
33         mapStatus_["StatusOK"] = Status::OK;
34         mapStatus_["StatusErrorUnknown"] = Status::ERROR_UNKNOWN;
35         mapStatus_["StatusErrorNoMemory"] = Status::ERROR_NO_MEMORY;
36         mapStatus_["StatusAgain"] = Status::ERROR_AGAIN;
37         mapStatus_["StatusErrorNullPoint"] = Status::ERROR_NULL_POINTER;
38         mapStatus_["StatusErrorNotEnoughData"] = Status::ERROR_NOT_ENOUGH_DATA;
39         name_ = name;
40     }
~DemuxerPluginMock()41     ~DemuxerPluginMock()
42     {
43     }
Reset()44     Status Reset() override
45     {
46         return mapStatus_[name_];
47     }
Start()48     Status Start() override
49     {
50         return mapStatus_[name_];
51     }
Stop()52     Status Stop() override
53     {
54         return mapStatus_[name_];
55     }
Flush()56     Status Flush() override
57     {
58         return mapStatus_[name_];
59     }
SetDataSource(const std::shared_ptr<DataSource> & source)60     Status SetDataSource(const std::shared_ptr<DataSource>& source) override
61     {
62         return mapStatus_[name_];
63     }
SetDataSourceWithProbSize(const std::shared_ptr<DataSource> & source,const int32_t probSize)64     Status SetDataSourceWithProbSize(const std::shared_ptr<DataSource>& source,
65         const int32_t probSize) override
66     {
67         return mapStatus_[name_];
68     }
GetMediaInfo(MediaInfo & mediaInfo)69     Status GetMediaInfo(MediaInfo& mediaInfo) override
70     {
71         return mapStatus_[name_];
72     }
GetUserMeta(std::shared_ptr<Meta> meta)73     Status GetUserMeta(std::shared_ptr<Meta> meta) override
74     {
75         return mapStatus_[name_];
76     }
SelectTrack(uint32_t trackId)77     Status SelectTrack(uint32_t trackId) override
78     {
79         return mapStatus_[name_];
80     }
UnselectTrack(uint32_t trackId)81     Status UnselectTrack(uint32_t trackId) override
82     {
83         return mapStatus_[name_];
84     }
SeekTo(int32_t trackId,int64_t seekTime,SeekMode mode,int64_t & realSeekTime)85     Status SeekTo(int32_t trackId, int64_t seekTime, SeekMode mode,
86         int64_t& realSeekTime) override
87     {
88         return mapStatus_[name_];
89     }
ReadSample(uint32_t trackId,std::shared_ptr<AVBuffer> sample)90     Status ReadSample(uint32_t trackId, std::shared_ptr<AVBuffer> sample) override
91     {
92         return mapStatus_[name_];
93     }
ReadSample(uint32_t trackId,std::shared_ptr<AVBuffer> sample,uint32_t timeout)94     Status ReadSample(uint32_t trackId, std::shared_ptr<AVBuffer> sample, uint32_t timeout) override
95     {
96         return mapStatus_[name_];
97     }
GetNextSampleSize(uint32_t trackId,int32_t & size)98     Status GetNextSampleSize(uint32_t trackId, int32_t& size) override
99     {
100         return mapStatus_[name_];
101     }
GetNextSampleSize(uint32_t trackId,int32_t & size,uint32_t timeout)102     Status GetNextSampleSize(uint32_t trackId, int32_t& size, uint32_t timeout) override
103     {
104         return mapStatus_[name_];
105     }
GetLastPTSByTrackId(uint32_t trackId,int64_t & lastPTS)106     Status GetLastPTSByTrackId(uint32_t trackId, int64_t &lastPTS) override
107     {
108         return mapStatus_[name_];
109     }
Pause()110     Status Pause() override
111     {
112         return mapStatus_[name_];
113     }
GetDrmInfo(std::multimap<std::string,std::vector<uint8_t>> & drmInfo)114     Status GetDrmInfo(std::multimap<std::string, std::vector<uint8_t>>& drmInfo) override
115     {
116         return mapStatus_[name_];
117     }
ResetEosStatus()118     void ResetEosStatus() override
119     {
120         return;
121     }
IsRefParserSupported()122     bool IsRefParserSupported() override
123     {
124         return false;
125     }
126     Status ParserRefUpdatePos(int64_t timeStampMs, bool isForward = true) override
127     {
128         return mapStatus_[name_];
129     }
ParserRefInfo()130     Status ParserRefInfo() override
131     {
132         return mapStatus_[name_];
133     }
GetFrameLayerInfo(std::shared_ptr<AVBuffer> videoSample,FrameLayerInfo & frameLayerInfo)134     Status GetFrameLayerInfo(std::shared_ptr<AVBuffer> videoSample,
135         FrameLayerInfo &frameLayerInfo) override
136     {
137         return mapStatus_[name_];
138     }
GetFrameLayerInfo(uint32_t frameId,FrameLayerInfo & frameLayerInfo)139     Status GetFrameLayerInfo(uint32_t frameId, FrameLayerInfo &frameLayerInfo) override
140     {
141         return mapStatus_[name_];
142     }
GetGopLayerInfo(uint32_t gopId,GopLayerInfo & gopLayerInfo)143     Status GetGopLayerInfo(uint32_t gopId, GopLayerInfo &gopLayerInfo) override
144     {
145         return mapStatus_[name_];
146     }
GetIFramePos(std::vector<uint32_t> & IFramePos)147     Status GetIFramePos(std::vector<uint32_t> &IFramePos) override
148     {
149         return mapStatus_[name_];
150     }
Dts2FrameId(int64_t dts,uint32_t & frameId)151     Status Dts2FrameId(int64_t dts, uint32_t &frameId) override
152     {
153         return mapStatus_[name_];
154     }
SeekMs2FrameId(int64_t seekMs,uint32_t & frameId)155     Status SeekMs2FrameId(int64_t seekMs, uint32_t &frameId) override
156     {
157         return mapStatus_[name_];
158     }
FrameId2SeekMs(uint32_t frameId,int64_t & seekMs)159     Status FrameId2SeekMs(uint32_t frameId, int64_t &seekMs) override
160     {
161         return mapStatus_[name_];
162     }
GetIndexByRelativePresentationTimeUs(const uint32_t trackIndex,const uint64_t relativePresentationTimeUs,uint32_t & index)163     Status GetIndexByRelativePresentationTimeUs(const uint32_t trackIndex,
164         const uint64_t relativePresentationTimeUs, uint32_t &index) override
165     {
166         return mapStatus_[name_];
167     }
GetRelativePresentationTimeUsByIndex(const uint32_t trackIndex,const uint32_t index,uint64_t & relativePresentationTimeUs)168     Status GetRelativePresentationTimeUsByIndex(const uint32_t trackIndex,
169         const uint32_t index, uint64_t &relativePresentationTimeUs) override
170     {
171         return mapStatus_[name_];
172     }
SetCacheLimit(uint32_t limitSize)173     void SetCacheLimit(uint32_t limitSize) override
174     {
175         return;
176     }
GetProbeSize(int32_t & offset,int32_t & size)177     bool GetProbeSize(int32_t &offset, int32_t &size) override
178     {
179         offset = 0;
180         size = NUMBER_CACHE; // cache for 5000000
181         return true;
182     }
BoostReadThreadPriority()183     Status BoostReadThreadPriority() override
184     {
185         return mapStatus_[name_];
186     }
187 private:
188     std::map<std::string, Status> mapStatus_;
189     std::string name_;
190 };
191 
192 template<size_t MaxFailCount>
193 class DemuxerPluginSetDataSourceFailMock : public DemuxerPluginMock {
194 public:
DemuxerPluginSetDataSourceFailMock(std::string name)195     explicit DemuxerPluginSetDataSourceFailMock(std::string name) : DemuxerPluginMock(name)
196     {
197     }
~DemuxerPluginSetDataSourceFailMock()198     ~DemuxerPluginSetDataSourceFailMock()
199     {
200     }
SetDataSource(const std::shared_ptr<DataSource> & source)201     Status SetDataSource(const std::shared_ptr<DataSource>& source) override
202     {
203         if (failCount_ < MaxFailCount) {
204             failCount_++;
205             return Status::ERROR_NOT_ENOUGH_DATA;
206         }
207         return Status::OK;
208     }
209 private:
210     size_t failCount_ = 0;
211 };
212 
213 class StreamDemuxerMock : public StreamDemuxer {
SetSourceInitialBufferSize(int32_t offset,int32_t size)214     bool SetSourceInitialBufferSize(int32_t offset, int32_t size) override
215     {
216         return true;
217     }
218 };
219 
220 class SourcePluginMock : public Plugins::SourcePlugin {
221 public:
SourcePluginMock(std::string name)222     explicit SourcePluginMock(std::string name) : SourcePlugin(name)
223     {
224         mapStatus_["StatusOK"] = Status::OK;
225         mapStatus_["StatusErrorUnknown"] = Status::ERROR_UNKNOWN;
226         mapStatus_["StatusErrorNoMemory"] = Status::ERROR_NO_MEMORY;
227         mapStatus_["StatusAgain"] = Status::ERROR_AGAIN;
228         mapStatus_["StatusErrorNullPoint"] = Status::ERROR_NULL_POINTER;
229         name_ = name;
230     }
~SourcePluginMock()231     ~SourcePluginMock()
232     {
233     }
SetSource(std::shared_ptr<MediaSource> source)234     Status SetSource(std::shared_ptr<MediaSource> source) override
235     {
236         return mapStatus_[name_];
237     }
Read(std::shared_ptr<Buffer> & buffer,uint64_t offset,size_t expectedLen)238     Status Read(std::shared_ptr<Buffer>& buffer, uint64_t offset, size_t expectedLen) override
239     {
240         return mapStatus_[name_];
241     }
Read(int32_t streamId,std::shared_ptr<Buffer> & buffer,uint64_t offset,size_t expectedLen)242     Status Read(int32_t streamId, std::shared_ptr<Buffer>& buffer, uint64_t offset, size_t expectedLen) override
243     {
244         return mapStatus_[name_];
245     }
GetSize(uint64_t & size)246     Status GetSize(uint64_t& size) override
247     {
248         return mapStatus_[name_];
249     }
GetSeekable()250     Seekable GetSeekable() override
251     {
252         return Seekable::SEEKABLE;
253     }
SeekTo(uint64_t offset)254     Status SeekTo(uint64_t offset) override
255     {
256         return mapStatus_[name_];
257     }
Reset()258     Status Reset() override
259     {
260         return mapStatus_[name_];
261     }
262 private:
263     std::map<std::string, Status> mapStatus_;
264     std::string name_;
265 };
266 
267 class SourceCallback : public Plugins::Callback {
268 public:
SourceCallback(std::shared_ptr<DemuxerPluginManager> demuxerPluginManager)269     explicit SourceCallback(std::shared_ptr<DemuxerPluginManager> demuxerPluginManager)
270         : demuxerPluginManager_(demuxerPluginManager) {}
OnEvent(const Plugins::PluginEvent & event)271     void OnEvent(const Plugins::PluginEvent &event) override
272     {
273         switch (event.type) {
274             case PluginEventType::INITIAL_BUFFER_SUCCESS: {
275                 demuxerPluginManager_->NotifyInitialBufferingEnd(true);
276                 break;
277             }
278             default:
279                 break;
280         }
281     }
282     std::shared_ptr<DemuxerPluginManager> demuxerPluginManager_;
283 };
284 
285 template<size_t FailOffset, size_t MaxFailCount>
286 class StreamDemuxerPullDataFailMock : public StreamDemuxer {
287 public:
StreamDemuxerPullDataFailMock()288     StreamDemuxerPullDataFailMock() : StreamDemuxer() {}
289 private:
CallbackReadAt(int32_t streamID,int64_t offset,std::shared_ptr<Buffer> & buffer,size_t expectedLen)290     Status CallbackReadAt(int32_t streamID, int64_t offset, std::shared_ptr<Buffer>& buffer,
291         size_t expectedLen) override
292     {
293         num += expectedLen;
294         if (num > FailOffset && failCount_ < MaxFailCount) {
295             failCount_++;
296             return Status::ERROR_AGAIN;
297         }
298         return StreamDemuxer::CallbackReadAt(streamID, offset, buffer, expectedLen);
299     }
300     size_t failCount_ = 0;
301     int num = 0;
302 };
303 }
304 }
305 
306 #endif