• 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 "hevc_decoder.h"
30 #include "codecbase.h"
31 #include "surface/window.h"
32 namespace OHOS {
33 namespace MediaAVCodec {
34 
35 class VDecSignal {
36 public:
37     std::mutex inMutex_;
38     std::condition_variable inCond_;
39     std::mutex outMutex_;
40     std::condition_variable endCond_;
41     std::queue<uint32_t> inIdxQueue_;
42     std::queue<std::shared_ptr<AVBuffer>> inBufferQueue_;
43 };
44 
45 class VDecServerSample : public NoCopyable {
46 public:
47     VDecServerSample() = default;
48     ~VDecServerSample();
49 
50     void RunVideoServerDecoder();
51     const char *inpDir = "/data/test/media/720_1280_25_avcc.h265";
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     uint32_t defaultPixelFormat = 1;
58     uint32_t frameCount_ = 0;
59     uint32_t errCount = 0;
60     int32_t ConfigServerDecoder();
61     int32_t SetCallback();
62     void GetOutputFormat();
63     void Flush();
64     void Stop();
65     void Reset();
66     void InputFunc();
67     void WaitForEos();
68     void NotifyMemoryRecycle();
69     void NotifyMemoryWriteBack();
70     int32_t SetOutputSurface();
71     int32_t InitDecoder();
72     std::shared_ptr<VDecSignal> signal_;
73     bool repeatRun = false;
74     bool isSurfMode = false;
75     int32_t sendFrameIndex = 0;
76     std::atomic<bool> isEOS_ { false };
77     std::vector<sptr<Surface>> cs_vector;
78     std::vector<sptr<Surface>> ps_vector;
79 protected:
80     std::shared_ptr<CodecBase> 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     private:
92         VDecServerSample* tester;
93     };
94 private:
95     int64_t GetSystemTimeUs();
96     void ReleaseInFile();
97     void StopInloop();
98     void SetEOS(uint32_t index, std::shared_ptr<AVBuffer> buffer);
99     void CopyStartCode(uint8_t *frameBuffer, uint32_t bufferSize, std::shared_ptr<AVBuffer> buffer);
100     int32_t ReadData(uint32_t index, std::shared_ptr<AVBuffer> buffer);
101     int32_t SendData(uint32_t bufferSize, uint32_t index, std::shared_ptr<AVBuffer> buffer);
102 };
103 
104 class ConsumerListener : public IBufferConsumerListener {
105 public:
ConsumerListener(sptr<Surface> cs)106     ConsumerListener(sptr<Surface> cs) : cs_(cs){};
~ConsumerListener()107     ~ConsumerListener() {}
OnBufferAvailable()108     void OnBufferAvailable() override
109     {
110         sptr<SurfaceBuffer> buffer;
111         int32_t flushFence;
112         cs_->AcquireBuffer(buffer, flushFence, timestamp_, damage_);
113         cs_->ReleaseBuffer(buffer, -1);
114     }
115 
116 private:
117     int64_t timestamp_ = 0;
118     Rect damage_ = {};
119     sptr<Surface> cs_{nullptr};
120 };
121 } // namespace Media
122 } // namespace OHOS
123 
124 #endif // SERVERDEC_SAMPLE_H
125