• 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 SAMPLE_H
17 #define 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 #include "surface/window.h"
31 namespace OHOS {
32 namespace MediaAVCodec {
33 
34 class VDecSignal {
35 public:
36     std::mutex inMutex_;
37     std::condition_variable inCond_;
38     std::mutex outMutex_;
39     std::condition_variable endCond_;
40     std::queue<uint32_t> inIdxQueue_;
41     std::queue<std::shared_ptr<AVBuffer>> inBufferQueue_;
42 };
43 
44 class VDecServerSample : public NoCopyable {
45 public:
46     VDecServerSample() = default;
47     ~VDecServerSample();
48 
49     void PrepareDecoder();
50     void RunVideoServerDecoder();
51     const char *inpDir = "/data/test/media/1920_1080_10_30Mb.h264";
52     const char *outDir = "/data/test/media/VDecTest.yuv";
53     uint32_t defaultWidth = 1920;
54     uint32_t defaultHeight = 1080;
55     uint32_t defaultFrameRate = 30;
56     uint32_t defaultRotation = 0;
57 
58     uint32_t defaultPixelFormat = 1;
59     uint32_t frameCount_ = 0;
60     uint32_t errCount = 0;
61     int32_t ConfigServerDecoder();
62     int32_t SetCallback();
63     void GetOutputFormat();
64     void Flush();
65     void Stop();
66     void Reset();
67     void InputFunc();
68     void WaitForEos();
69     void NotifyMemoryRecycle();
70     void NotifyMemoryWriteBack();
71     int32_t SetOutputSurface();
72     std::shared_ptr<VDecSignal> signal_;
73     bool repeatRun = false;
74     bool isSurfMode = false;
75     std::atomic<bool> isEOS_{false};
76     std::vector<sptr<Surface>> cs_vector;
77     std::vector<sptr<Surface>> ps_vector;
78 
79 protected:
80     sptr<Codec::FCodec> codec_;
81     std::atomic<bool> isRunning_{false};
82     std::unique_ptr<std::ifstream> inFile_;
83     std::unique_ptr<std::thread> inputLoop_;
84     struct CallBack : public MediaCodecCallback {
CallBackCallBack85         explicit CallBack(VDecServerSample *tester) : tester(tester) {}
86         ~CallBack() override = default;
87         void OnError(AVCodecErrorType errorType, int32_t errorCode) override;
88         void OnOutputFormatChanged(const Format &format) override;
89         void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override;
90         void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override;
91 
92     private:
93         VDecServerSample *tester;
94     };
95 
96 private:
97     int64_t GetSystemTimeUs();
98     void ReleaseInFile();
99     void StopInloop();
100     void SetEOS(uint32_t index, std::shared_ptr<AVBuffer> buffer);
101     void CopyStartCode(uint8_t *frameBuffer, uint32_t bufferSize, std::shared_ptr<AVBuffer> buffer);
102     int32_t ReadData(uint32_t index, std::shared_ptr<AVBuffer> buffer);
103     int32_t SendData(uint32_t bufferSize, uint32_t index, std::shared_ptr<AVBuffer> buffer);
104 };
105 
106 class ConsumerListener : public IBufferConsumerListener {
107 public:
ConsumerListener(sptr<Surface> cs)108     ConsumerListener(sptr<Surface> cs) : cs_(cs){};
~ConsumerListener()109     ~ConsumerListener() {}
OnBufferAvailable()110     void OnBufferAvailable() override
111     {
112         sptr<SurfaceBuffer> buffer;
113         int32_t flushFence;
114         cs_->AcquireBuffer(buffer, flushFence, timestamp_, damage_);
115         cs_->ReleaseBuffer(buffer, -1);
116     }
117 
118 private:
119     int64_t timestamp_ = 0;
120     Rect damage_ = {};
121     sptr<Surface> cs_{nullptr};
122 };
123 } // namespace MediaAVCodec
124 } // namespace OHOS
125 #endif // SAMPLE_H