1 /* 2 * Copyright (C) 2023 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 HCODEC_TESTER_COMMON_H 17 #define HCODEC_TESTER_COMMON_H 18 19 #include <fstream> 20 #include <mutex> 21 #include <condition_variable> 22 #include <memory> 23 #include "native_avcodec_base.h" 24 #include "surface.h" 25 #include "wm/window.h" // foundation/window/window_manager/interfaces/innerkits/ 26 #include "command_parser.h" 27 #include "start_code_detector.h" 28 #include "test_utils.h" 29 30 namespace OHOS::MediaAVCodec { 31 struct Span { 32 char* va; 33 size_t capacity; 34 }; 35 36 struct TesterCommon { 37 static bool Run(const CommandOpt& opt); 38 bool RunOnce(); 39 40 protected: TesterCommonTesterCommon41 explicit TesterCommon(const CommandOpt& opt) : opt_(opt) {} 42 virtual ~TesterCommon() = default; 43 static int64_t GetNowUs(); 44 virtual bool Create() = 0; 45 virtual bool SetCallback() = 0; 46 virtual bool GetInputFormat() = 0; 47 virtual bool GetOutputFormat() = 0; 48 virtual bool Start() = 0; 49 void EncoderInputLoop(); 50 void DecoderInputLoop(); 51 virtual std::optional<uint32_t> GetInputIndex(Span& span) = 0; 52 virtual bool QueueInput(uint32_t idx, OH_AVCodecBufferAttr attr) = 0; 53 void OutputLoop(); 54 virtual std::optional<uint32_t> GetOutputIndex() = 0; 55 virtual bool ReturnOutput(uint32_t idx) = 0; 56 virtual bool Flush() = 0; 57 virtual void ClearAllBuffer() = 0; 58 virtual bool Stop() = 0; 59 virtual bool Release() = 0; 60 61 const CommandOpt opt_; 62 std::ifstream ifs_; 63 sptr<Surface> surface_; 64 65 std::mutex inputMtx_; 66 std::condition_variable inputCond_; 67 uint32_t currInputCnt_ = 0; 68 69 std::mutex outputMtx_; 70 std::condition_variable outputCond_; 71 72 // encoder only 73 bool RunEncoder(); 74 virtual bool ConfigureEncoder() = 0; 75 virtual bool CreateInputSurface() = 0; 76 virtual bool NotifyEos() = 0; 77 virtual bool RequestIDR() = 0; 78 virtual std::optional<uint32_t> GetInputStride() = 0; 79 void InputSurfaceLoop(); 80 uint32_t ReadOneFrame(Span dstSpan); 81 uint32_t ReadOneFrameYUV420P(char* dst); 82 uint32_t ReadOneFrameYUV420SP(char* dst); 83 uint32_t ReadOneFrameRGBA(char* dst); 84 GraphicPixelFormat displayFmt_; 85 uint32_t stride_ = 0; 86 static constexpr uint32_t BYTES_PER_PIXEL_RBGA = 4; 87 static constexpr uint32_t SAMPLE_RATIO = 2; 88 89 // decoder only 90 class Listener : public IBufferConsumerListener { 91 public: ListenerTesterCommon92 explicit Listener(TesterCommon *test) : tester_(test) {} 93 void OnBufferAvailable() override; 94 private: 95 TesterCommon *tester_; 96 }; 97 98 bool RunDecoder(); 99 sptr<Surface> CreateSurfaceFromWindow(); 100 sptr<Surface> CreateSurfaceNormal(); 101 virtual bool SetOutputSurface(sptr<Surface>& surface) = 0; 102 void PrepareSeek(); 103 bool SeekIfNecessary(); // false means quit loop 104 virtual bool ConfigureDecoder() = 0; 105 int GetNextSample(Span dstSpan, size_t& sampleIdx, bool& isCsd); // return filledLen 106 sptr<OHOS::Rosen::Window> window_; 107 StartCodeDetector demuxer_; 108 size_t totalSampleCnt_ = 0; 109 size_t currSampleIdx_ = 0; 110 std::list<std::pair<size_t, size_t>> userSeekPos_; // seek from which index to which index 111 }; 112 } // namespace OHOS::MediaAVCodec 113 #endif