1 /* 2 * Copyright (c) 2024-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 CAMERA_FRAMEWORK_FRAME_RECORD_H 17 #define CAMERA_FRAMEWORK_FRAME_RECORD_H 18 #include <condition_variable> 19 #include <cstdint> 20 #include <queue> 21 #include <refbase.h> 22 #include <string> 23 24 #include "camera_log.h" 25 #include "iconsumer_surface.h" 26 #include "native_avbuffer.h" 27 #include "native_avcodec_base.h" 28 #include "output/camera_output_capability.h" 29 #include "sample_info.h" 30 #include "surface_buffer.h" 31 #include "surface_type.h" 32 33 namespace OHOS { 34 namespace CameraStandard { 35 using namespace std; 36 37 class MovingPhotoSurfaceWrapper; 38 class FrameRecord : public RefBase { 39 public: 40 explicit FrameRecord(sptr<SurfaceBuffer> videoBuffer, int64_t timestamp, GraphicTransformType type); 41 ~FrameRecord() override; 42 43 void ReleaseSurfaceBuffer(sptr<MovingPhotoSurfaceWrapper> surfaceWrapper); 44 void ReleaseMetaBuffer(sptr<Surface> surface, bool reuse); 45 void NotifyBufferRelease(); 46 void DeepCopyBuffer(sptr<SurfaceBuffer> newSurfaceBuffer, sptr<SurfaceBuffer> surfaceBuffer) const; 47 SetStatusReadyConvertStatus()48 inline void SetStatusReadyConvertStatus() 49 { 50 status = STATUS_READY_CONVERT; 51 } 52 SetCoverFrame()53 inline void SetCoverFrame() 54 { 55 isCover_ = true; 56 } 57 IsCoverFrame()58 inline bool IsCoverFrame() 59 { 60 return isCover_.load(); 61 } 62 IsIdle()63 inline bool IsIdle() 64 { 65 return status == STATUS_NONE; 66 } 67 IsReadyConvert()68 inline bool IsReadyConvert() 69 { 70 return status == STATUS_READY_CONVERT; 71 } 72 IsFinishCache()73 inline bool IsFinishCache() 74 { 75 return status == STATUS_FINISH_ENCODE; 76 } 77 GetSurfaceBuffer()78 inline sptr<SurfaceBuffer> GetSurfaceBuffer() 79 { 80 std::unique_lock<std::mutex> lock(mutex_); 81 return videoBuffer_; 82 } 83 SetSurfaceBuffer(sptr<SurfaceBuffer> buffer)84 inline void SetSurfaceBuffer(sptr<SurfaceBuffer> buffer) 85 { 86 std::unique_lock<std::mutex> lock(mutex_); 87 videoBuffer_ = buffer; 88 } 89 GetEncodeBuffer()90 inline OH_AVBuffer* GetEncodeBuffer() 91 { 92 return encodedBuffer; 93 } 94 CacheBuffer(OH_AVBuffer * buffer)95 inline void CacheBuffer(OH_AVBuffer* buffer) 96 { 97 MEDIA_DEBUG_LOG("cacheBuffer start"); 98 encodedBuffer = buffer; 99 } 100 SetEncodedResult(bool encodedResult)101 inline void SetEncodedResult(bool encodedResult) 102 { 103 isEncoded_ = encodedResult; 104 } 105 IsEncoded()106 inline bool IsEncoded() 107 { 108 return isEncoded_; 109 } 110 GetFrameId()111 inline const std::string& GetFrameId() const 112 { 113 return frameId_; 114 } 115 GetBufferSize()116 inline uint32_t GetBufferSize() 117 { 118 return bufferSize; 119 } 120 GetTimeStamp()121 inline int64_t GetTimeStamp() 122 { 123 return timestamp_; 124 } 125 GetFrameSize()126 inline shared_ptr<Size> GetFrameSize() 127 { 128 return size; 129 } 130 GetRotation()131 inline int32_t GetRotation() 132 { 133 auto it = transformTypeToValue.find(transformType_); 134 return it == transformTypeToValue.end() ? 0 : it->second; 135 } 136 SetMetaBuffer(sptr<SurfaceBuffer> buffer)137 inline void SetMetaBuffer(sptr<SurfaceBuffer> buffer) 138 { 139 std::unique_lock<std::mutex> lock(metaBufferMutex_); 140 metaBuffer_ = buffer; 141 } 142 GetMetaBuffer()143 inline sptr<SurfaceBuffer> GetMetaBuffer() 144 { 145 metaBufferMutex_.lock(); 146 return metaBuffer_; 147 } 148 UnLockMetaBuffer()149 inline void UnLockMetaBuffer() 150 { 151 metaBufferMutex_.unlock(); 152 } 153 SetIDRProperty(bool isIDRFrame)154 inline void SetIDRProperty(bool isIDRFrame) 155 { 156 isIDRFrame_ = isIDRFrame; 157 } 158 IsIDRFrame()159 inline bool IsIDRFrame() 160 { 161 return isIDRFrame_; 162 } 163 164 struct HashFunction { operatorHashFunction165 std::size_t operator()(const sptr<FrameRecord>& obj) const 166 { 167 return std::hash<std::string>()(obj->GetFrameId()); 168 } 169 }; 170 171 struct EqualFunction { operatorEqualFunction172 bool operator()(const sptr<FrameRecord>& obj1, const sptr<FrameRecord>& obj2) const 173 { 174 return obj1->GetFrameId() == obj2->GetFrameId(); 175 } 176 }; 177 178 const unordered_map<GraphicTransformType, int32_t> transformTypeToValue = { 179 { GRAPHIC_FLIP_H_ROT90, 90 }, 180 { GRAPHIC_FLIP_H_ROT180, 180 }, 181 { GRAPHIC_FLIP_H_ROT270, 270 }, 182 { GRAPHIC_ROTATE_90, 270 }, 183 { GRAPHIC_ROTATE_180, 180 }, 184 { GRAPHIC_ROTATE_270, 90 }, 185 }; 186 187 OH_AVBuffer* encodedBuffer = nullptr; 188 std::string frameId_; 189 190 private: 191 static const int32_t STATUS_NONE = 0; 192 static const int32_t STATUS_READY_CONVERT = 1; 193 static const int32_t STATUS_FINISH_ENCODE = 2; 194 std::atomic<int32_t> status = STATUS_NONE; 195 std::atomic<bool> isEncoded_ { false }; 196 std::atomic<bool> isCover_ { false }; 197 shared_ptr<Size> size; 198 uint32_t bufferSize; 199 sptr<SurfaceBuffer> videoBuffer_; 200 int64_t timestamp_; 201 GraphicTransformType transformType_; 202 std::mutex mutex_; 203 std::condition_variable canReleased_; 204 std::mutex metaBufferMutex_; 205 sptr<SurfaceBuffer> metaBuffer_; 206 bool isIDRFrame_ = false; 207 }; 208 } // namespace CameraStandard 209 } // namespace OHOS 210 #endif // CAMERA_FRAMEWORK_CODEC_BUFFER_INFO_H 211