• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 
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     int32_t SetOutputSurface();
70     int32_t InitDecoder();
71     std::shared_ptr<VDecSignal> signal_;
72     bool repeatRun = false;
73     bool isSurfMode = false;
74     int32_t sendFrameIndex = 0;
75     std::atomic<bool> isEOS_ { false };
76     std::vector<sptr<Surface>> cs_vector;
77     std::vector<sptr<Surface>> ps_vector;
78 protected:
79     std::shared_ptr<CodecBase> codec_;
80     std::atomic<bool> isRunning_ { false };
81     std::unique_ptr<std::ifstream> inFile_;
82     std::unique_ptr<std::thread> inputLoop_;
83     struct CallBack : public MediaCodecCallback {
CallBackCallBack84         explicit CallBack(VDecServerSample* tester) : tester(tester) {}
85         ~CallBack() override = default;
86         void OnError(AVCodecErrorType errorType, int32_t errorCode) override;
87         void OnOutputFormatChanged(const Format &format) override;
88         void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override;
89         void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override;
90     private:
91         VDecServerSample* tester;
92     };
93 private:
94     int64_t GetSystemTimeUs();
95     void ReleaseInFile();
96     void StopInloop();
97     void SetEOS(uint32_t index, std::shared_ptr<AVBuffer> buffer);
98     void CopyStartCode(uint8_t *frameBuffer, uint32_t bufferSize, std::shared_ptr<AVBuffer> buffer);
99     int32_t ReadData(uint32_t index, std::shared_ptr<AVBuffer> buffer);
100     int32_t SendData(uint32_t bufferSize, uint32_t index, std::shared_ptr<AVBuffer> buffer);
101 };
102 
103 class ConsumerListener : public IBufferConsumerListener {
104 public:
ConsumerListener(sptr<Surface> cs)105     ConsumerListener(sptr<Surface> cs) : cs_(cs){};
~ConsumerListener()106     ~ConsumerListener() {}
OnBufferAvailable()107     void OnBufferAvailable() override
108     {
109         sptr<SurfaceBuffer> buffer;
110         int32_t flushFence;
111         cs_->AcquireBuffer(buffer, flushFence, timestamp_, damage_);
112         cs_->ReleaseBuffer(buffer, -1);
113     }
114 
115 private:
116     int64_t timestamp_ = 0;
117     Rect damage_ = {};
118     sptr<Surface> cs_{nullptr};
119 };
120 } // namespace Media
121 } // namespace OHOS
122 
123 #endif // SERVERDEC_SAMPLE_H
124