• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 ACODEC_MOCK_H
17 #define ACODEC_MOCK_H
18 
19 #include <iostream>
20 #include <atomic>
21 #include <fstream>
22 #include <thread>
23 #include <mutex>
24 #include <queue>
25 #include <string>
26 #include "avcodec_mock.h"
27 #include "unittest_log.h"
28 #include "test_params_config.h"
29 #include "securec.h"
30 
31 namespace OHOS {
32 namespace Media {
33 struct ACodecSignal {
34 public:
35     std::mutex inMutexDec_;
36     std::condition_variable inCondDec_;
37     std::queue<uint32_t> inQueueDec_;
38     std::queue<uint32_t> outQueueDec_;
39     std::queue<uint32_t>  sizeQueueDec_;
40     std::queue<uint32_t>  flagQueueDec_;
41     std::queue<std::shared_ptr<AVMemoryMock>> inBufferQueueDec_;
42     std::queue<std::shared_ptr<AVMemoryMock>> outBufferQueueDec_;
43 
44     std::mutex inMutexEnc_;
45     std::mutex outMutexEnc_;
46     std::condition_variable inCondEnc_;
47     std::condition_variable outCondEnc_;
48     std::queue<uint32_t> inQueueEnc_;
49     std::queue<uint32_t> outQueueEnc_;
50     std::queue<uint32_t>  sizeQueueEnc_;
51     std::queue<uint32_t>  flagQueueEnc_;
52     std::queue<std::shared_ptr<AVMemoryMock>> inBufferQueueEnc_;
53     std::queue<std::shared_ptr<AVMemoryMock>> outBufferQueueEnc_;
54     int32_t errorNum_ = 0;
55     std::atomic<bool> isFlushing_ = false;
56 };
57 
58 class ADecCallbackTest : public AVCodecCallbackMock {
59 public:
60     explicit ADecCallbackTest(std::shared_ptr<ACodecSignal> signal);
61     virtual ~ADecCallbackTest();
62     void OnError(int32_t errorCode);
63     void OnStreamChanged(std::shared_ptr<FormatMock> format);
64     void OnNeedInputData(uint32_t index, std::shared_ptr<AVMemoryMock> data);
65     void OnNewOutputData(uint32_t index, std::shared_ptr<AVMemoryMock> data, AVCodecBufferAttrMock attr);
66 private:
67     std::shared_ptr<ACodecSignal> acodecSignal_;
68 };
69 
70 class AEncCallbackTest : public AVCodecCallbackMock {
71 public:
72     explicit AEncCallbackTest(std::shared_ptr<ACodecSignal> signal);
73     virtual ~AEncCallbackTest();
74     void OnError(int32_t errorCode);
75     void OnStreamChanged(std::shared_ptr<FormatMock> format);
76     void OnNeedInputData(uint32_t index, std::shared_ptr<AVMemoryMock> data);
77     void OnNewOutputData(uint32_t index, std::shared_ptr<AVMemoryMock> data, AVCodecBufferAttrMock attr);
78 private:
79     std::shared_ptr<ACodecSignal> acodecSignal_;
80 };
81 
82 class ACodecMock : public NoCopyable {
83 public:
84     ACodecMock(std::shared_ptr<ACodecSignal> signal);
85     ~ACodecMock();
86 
87     bool CreateAudioDecMockByMime(const std::string &mime);
88     bool CreateAudioDecMockByName(const std::string &name);
89     int32_t SetCallbackDec(std::shared_ptr<AVCodecCallbackMock> cb);
90     int32_t ConfigureDec(std::shared_ptr<FormatMock> format);
91     int32_t PrepareDec();
92     int32_t StartDec();
93     int32_t StopDec();
94     int32_t FlushDec();
95     int32_t ResetDec();
96     int32_t ReleaseDec();
97     std::shared_ptr<FormatMock> GetOutputMediaDescriptionDec();
98     int32_t SetParameterDec(std::shared_ptr<FormatMock> format);
99     int32_t PushInputDataDec(uint32_t index, AVCodecBufferAttrMock &attr);
100     int32_t FreeOutputDataDec(uint32_t index);
101     bool CreateAudioEncMockByMime(const std::string &mime);
102     bool CreateAudioEncMockByName(const std::string &name);
103     int32_t SetCallbackEnc(std::shared_ptr<AVCodecCallbackMock> cb);
104     int32_t ConfigureEnc(std::shared_ptr<FormatMock> format);
105     int32_t PrepareEnc();
106     int32_t StartEnc();
107     int32_t StopEnc();
108     int32_t FlushEnc();
109     int32_t ResetEnc();
110     int32_t ReleaseEnc();
111     int32_t PushInputDataEnc(uint32_t index, AVCodecBufferAttrMock &attr);
112     std::shared_ptr<FormatMock> GetOutputMediaDescriptionEnc();
113     int32_t SetParameterEnc(std::shared_ptr<FormatMock> format);
114     int32_t FreeOutputDataEnc(uint32_t index);
115     void InputLoopFunc();
116     void OutputLoopFunc();
117     void SetOutPath(const std::string &path);
118 
119 private:
120     void clearIntQueue (std::queue<uint32_t>& q);
121     void clearBufferQueue (std::queue<std::shared_ptr<AVMemoryMock>>& q);
122     std::shared_ptr<AudioDecMock> audioDec_;
123     std::shared_ptr<ACodecSignal> acodecSignal_;
124     void InputFuncDec();
125     void PopOutQueueDec();
126     void PopInQueueDec();
127     int32_t PushInputDataDecInner(uint32_t index, uint32_t bufferSize);
128     std::atomic<bool> isDecRunning_ = false;
129     std::unique_ptr<std::ifstream> testFile_;
130     std::unique_ptr<std::thread> inputLoopDec_;
131     std::unique_ptr<std::thread> outputLoopDec_;
132     int64_t timeStampDec_ = 0;
133     uint32_t decInCnt_ = 0;
134     uint32_t decOutCnt_ = 0;
135 
136     std::shared_ptr<AudioEncMock> audioEnc_ = nullptr;
137     void InputFuncEnc();
138     void OutputFuncEnc();
139     int32_t PushInputDataEncInner();
140     void PopInQueueEnc();
141     std::atomic<bool> isEncRunning_ = false;
142     std::unique_ptr<std::thread> inputLoopEnc_;
143     std::unique_ptr<std::thread> outputLoopEnc_;
144     bool isDecInputEOS_ = false;
145     bool isEncInputEOS_ = false;
146     bool isDecOutputEOS_ = false;
147     bool isEncOutputEOS_ = false;
148     int64_t timeStampEnc_ = 0;
149     std::string outPath_ = "/data/test/media/out.es";
150 };
151 }
152 }
153 #endif // ACODEC_MOCK_H
154