• 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 VENC_MOCK_H
17 #define VENC_MOCK_H
18 #include <iostream>
19 #include <atomic>
20 #include <thread>
21 #include <mutex>
22 #include <queue>
23 #include <string>
24 #include "avcodec_mock.h"
25 #include "test_params_config.h"
26 #include "unittest_log.h"
27 #include "securec.h"
28 namespace OHOS {
29 namespace Media {
30 struct VEncSignal {
31 public:
32     std::mutex mutex_;
33     std::mutex outMutex_;
34     std::condition_variable outCond_;
35     std::queue<std::shared_ptr<AVMemoryMock>> outBufferQueue_;
36     std::queue<uint32_t> outIndexQueue_;
37     std::queue<uint32_t> outSizeQueue_;
38     std::atomic<bool> isRunning_ = false;
39 };
40 
41 class VEncCallbackTest : public AVCodecCallbackMock {
42 public:
43     explicit VEncCallbackTest(std::shared_ptr<VEncSignal> signal);
44     virtual ~VEncCallbackTest();
45     void OnError(int32_t errorCode) override;
46     void OnStreamChanged(std::shared_ptr<FormatMock> format) override;
47     void OnNeedInputData(uint32_t index, std::shared_ptr<AVMemoryMock> data) override;
48     void OnNewOutputData(uint32_t index, std::shared_ptr<AVMemoryMock> data, AVCodecBufferAttrMock attr) override;
49 private:
50     std::shared_ptr<VEncSignal> signal_;
51 };
52 
53 class VEncMock : public NoCopyable {
54 public:
55     explicit VEncMock(std::shared_ptr<VEncSignal> signal);
56     virtual ~VEncMock();
57     bool CreateVideoEncMockByMime(const std::string &mime);
58     bool CreateVideoEncMockByName(const std::string &name);
59     int32_t SetCallback(std::shared_ptr<AVCodecCallbackMock> cb);
60     std::shared_ptr<SurfaceMock> GetInputSurface();
61     int32_t Configure(std::shared_ptr<FormatMock> format);
62     int32_t Prepare();
63     int32_t Start();
64     int32_t Stop();
65     int32_t Flush();
66     int32_t Reset();
67     int32_t Release();
68     int32_t NotifyEos();
69     std::shared_ptr<FormatMock> GetOutputMediaDescription();
70     int32_t SetParameter(std::shared_ptr<FormatMock> format);
71     int32_t FreeOutputData(uint32_t index);
72     void SetOutPath(const std::string &path);
73     void OutLoopFunc();
74 private:
75     void FlushInner();
76     std::unique_ptr<std::thread> outLoop_;
77     std::shared_ptr<VideoEncMock> videoEnc_ = nullptr;
78     std::shared_ptr<VEncSignal> signal_ = nullptr;
79     std::shared_ptr<SurfaceMock> surface_ = nullptr;
80     uint32_t frameCount_ = 0;
81     std::string outPath_ = "/data/test/media/vout.es";
82 };
83 }  // namespace Media
84 }  // namespace OHOS
85 #endif // VENC_MOCK_H