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::queue<uint32_t> inIdxQueue_; 40 std::queue<std::shared_ptr<AVBuffer>> inBufferQueue_; 41 }; 42 43 class VDecServerSample : public NoCopyable { 44 public: 45 VDecServerSample() = default; 46 ~VDecServerSample(); 47 48 void RunVideoServerSurfaceDecoder(); 49 int32_t ConfigServerDecoder(); 50 int32_t SetParameter(); 51 int32_t SetCallback(); 52 void GetOutputFormat(); 53 void Flush(); 54 void Stop(); 55 void Reset(); 56 void InputFunc(); 57 void WaitForEos(); 58 int32_t SetOutputSurface(); 59 int32_t InitDecoder(); 60 std::shared_ptr<VDecSignal> signal_; 61 const uint8_t *fuzzData; 62 size_t fuzzSize; 63 int32_t sendFrameIndex = 0; 64 std::vector<sptr<Surface>> cs_vector; 65 std::vector<sptr<Surface>> ps_vector; 66 const char *outDIR = "/data/test/media/VDecTest.yuv"; 67 protected: 68 std::shared_ptr<CodecBase> codec_; 69 std::atomic<bool> isRunning_ { false }; 70 std::unique_ptr<std::thread> inputLoop_; 71 struct CallBack : public MediaCodecCallback { CallBackCallBack72 explicit CallBack(VDecServerSample* tester) : tester(tester) {} 73 ~CallBack() override = default; 74 void OnError(AVCodecErrorType errorType, int32_t errorCode) override; 75 void OnOutputFormatChanged(const Format &format) override; 76 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override; 77 void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override; 78 private: 79 VDecServerSample* tester; 80 }; 81 }; 82 83 class ConsumerListener : public IBufferConsumerListener { 84 public: ConsumerListener(sptr<Surface> cs)85 ConsumerListener(sptr<Surface> cs) : cs_(cs){}; ~ConsumerListener()86 ~ConsumerListener() {} OnBufferAvailable()87 void OnBufferAvailable() override 88 { 89 sptr<SurfaceBuffer> buffer; 90 int32_t flushFence; 91 cs_->AcquireBuffer(buffer, flushFence, timestamp_, damage_); 92 cs_->ReleaseBuffer(buffer, -1); 93 } 94 95 private: 96 int64_t timestamp_ = 0; 97 Rect damage_ = {}; 98 sptr<Surface> cs_{nullptr}; 99 }; 100 } // namespace Media 101 } // namespace OHOS 102 103 #endif // SERVERDEC_SAMPLE_H 104