• 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 
16 #ifndef SERVERDEC_SAMPLE_H
17 #define SERVERDEC_SAMPLE_H
18 
19 #include <atomic>
20 #include <cstdio>
21 #include <fstream>
22 #include <iostream>
23 #include <mutex>
24 #include <queue>
25 #include <string>
26 #include <thread>
27 #include <unistd.h>
28 #include <unordered_map>
29 #include "fcodec.h"
30 namespace OHOS {
31 namespace MediaAVCodec {
32 
33 class TestConsumerListener : public IBufferConsumerListener {
34 public:
TestConsumerListener(sptr<Surface> cs)35     TestConsumerListener(sptr<Surface> cs) : cs_(cs){};
~TestConsumerListener()36     ~TestConsumerListener() {}
OnBufferAvailable()37     void OnBufferAvailable() override
38     {
39         sptr<SurfaceBuffer> buffer;
40         int32_t flushFence;
41         cs_->AcquireBuffer(buffer, flushFence, timestamp_, damage_);
42         cs_->ReleaseBuffer(buffer, -1);
43     }
44 
45 private:
46     int64_t timestamp_ = 0;
47     Rect damage_ = {};
48     sptr<Surface> cs_{nullptr};
49 };
50 
51 class VDecSignal {
52 public:
53     std::mutex inMutex_;
54     std::condition_variable inCond_;
55     std::queue<uint32_t> inIdxQueue_;
56     std::queue<std::shared_ptr<AVBuffer>> inBufferQueue_;
57 };
58 
59 class FCodecServerSample : public NoCopyable {
60 public:
61     FCodecServerSample() = default;
62     ~FCodecServerSample();
63 
64     void RunFCodecDecoder();
65     int32_t Configure();
66     int32_t SetSurface();
67     int32_t SetCallback();
68     void GetOutputFormat();
69     void Flush();
70     void Reset();
71     void InputFunc();
72     void WaitForEos();
73     VDecSignal *signal_;
74     const uint8_t *fuzzData;
75     size_t fuzzSize;
76     int sendFrameIndex = 0;
77     int32_t width = 1920;
78     int32_t height = 1080;
79     int32_t frameRate = 30;
80     int32_t frameIndex = 10;
81     sptr<Surface> cs = nullptr;
82     sptr<Surface> ps = nullptr;
83 
84 protected:
85     sptr<Codec::FCodec> fcodec_;
86     std::atomic<bool> isRunning_ = false;
87     std::unique_ptr<std::thread> inputLoop_;
88     struct CallBack : public MediaCodecCallback {
CallBackCallBack89         explicit CallBack(FCodecServerSample *tester) : tester(tester) {}
90         ~CallBack() override = default;
91         void OnError(AVCodecErrorType errorType, int32_t errorCode) override;
92         void OnOutputFormatChanged(const Format &format) override;
93         void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override;
94         void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override;
95 
96     private:
97         FCodecServerSample *tester;
98     };
99 };
100 } // namespace MediaAVCodec
101 } // namespace OHOS
102 #endif // FCODEC_SAMPLE_H
103