1 /* 2 * Copyright (C) 2021 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 VIDEO_CAPTURE_SF_IMPL_H 17 #define VIDEO_CAPTURE_SF_IMPL_H 18 19 #include <fstream> 20 #include <atomic> 21 #include <thread> 22 #include "video_capture.h" 23 #include "nocopyable.h" 24 25 namespace OHOS { 26 namespace Media { 27 class VideoCaptureSfImpl : public VideoCapture, public NoCopyable { 28 public: 29 VideoCaptureSfImpl(); 30 virtual ~VideoCaptureSfImpl(); 31 32 int32_t Prepare() override; 33 int32_t Start() override; 34 int32_t Pause() override; 35 int32_t Resume() override; 36 int32_t Stop() override; 37 int32_t SetVideoWidth(uint32_t width) override; 38 int32_t SetVideoHeight(uint32_t height) override; 39 int32_t SetStreamType(VideoStreamType streamType) override; 40 sptr<Surface> GetSurface() override; 41 std::shared_ptr<EsAvcCodecBuffer> GetCodecBuffer() override; 42 std::shared_ptr<VideoFrameBuffer> GetFrameBuffer() override; 43 int32_t SetFrameRate(uint32_t frameRate) override; 44 void UnLock(bool start) override; 45 46 protected: 47 virtual std::shared_ptr<EsAvcCodecBuffer> DoGetCodecBuffer() = 0; 48 virtual std::shared_ptr<VideoFrameBuffer> DoGetFrameBuffer() = 0; 49 50 class ConsumerListenerProxy : public IBufferConsumerListener, public NoCopyable { 51 public: ConsumerListenerProxy(VideoCaptureSfImpl & owner)52 explicit ConsumerListenerProxy(VideoCaptureSfImpl &owner) : owner_(owner) {} 53 ~ConsumerListenerProxy() = default; 54 void OnBufferAvailable() override; 55 private: 56 VideoCaptureSfImpl &owner_; 57 }; 58 void OnBufferAvailable(); 59 int32_t GetSufferExtraData(); 60 bool CheckPauseResumeTime(); 61 bool DropThisFrame(uint32_t fps, int64_t oldTimeStamp, int64_t newTimeStamp, bool cacheFlag); 62 63 uint32_t videoWidth_; 64 uint32_t videoHeight_; 65 int32_t fence_; 66 int32_t bufferAvailableCount_; 67 int64_t timestamp_; 68 Rect damage_; 69 sptr<SurfaceBuffer> surfaceBuffer_; 70 std::atomic<bool> started_; 71 bool paused_; 72 std::mutex mutex_; 73 std::mutex pauseMutex_; 74 std::condition_variable bufferAvailableCondition_; 75 VideoStreamType streamType_; 76 bool streamTypeUnknown_; 77 sptr<Surface> dataConSurface_; 78 sptr<Surface> producerSurface_; 79 int32_t dataSize_ = 0; 80 int64_t pts_ = 0; 81 int32_t isCodecFrame_ = 0; 82 uint32_t pixelFormat_ = 0; 83 84 private: 85 void SetSurfaceUserData(); 86 uint64_t GetCurrentTime(); 87 int32_t AcquireSurfaceBuffer(); 88 std::shared_ptr<VideoFrameBuffer> GetFrameBufferInner(); 89 void ProbeStreamType(); 90 uint32_t bufferNumber_ = 0; 91 int64_t previousTimestamp_ = 0; 92 int64_t pauseTime_ = 0; 93 int64_t resumeTime_ = 0; 94 int64_t realTimeWhenResume_ = 0; 95 int64_t persistTime_ = 0; 96 uint32_t pauseCount_ = 0; 97 int64_t totalPauseTime_ = 0; 98 uint32_t framerate_ = 0; 99 int64_t minInterval_ = 0; 100 bool resourceLock_ = false; 101 bool isFirstBuffer_ = true; 102 bool isPause_ = false; 103 bool isResume_ = false; 104 bool isCheckRealTime_ = false; 105 }; 106 } // namespace Media 107 } // namespace OHOS 108 #endif // VIDEO_CAPTURE_AS_IMPL_H 109