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 SetFinishStatus()53 inline void SetFinishStatus() 54 { 55 status = STATUS_FINISH_ENCODE; 56 } 57 SetCoverFrame()58 inline void SetCoverFrame() 59 { 60 isCover_ = true; 61 } 62 IsCoverFrame()63 inline bool IsCoverFrame() 64 { 65 return isCover_.load(); 66 } 67 IsIdle()68 inline bool IsIdle() 69 { 70 return status == STATUS_NONE; 71 } 72 IsReadyConvert()73 inline bool IsReadyConvert() 74 { 75 return status == STATUS_READY_CONVERT; 76 } 77 IsFinishCache()78 inline bool IsFinishCache() 79 { 80 return status == STATUS_FINISH_ENCODE; 81 } 82 GetSurfaceBuffer()83 inline sptr<SurfaceBuffer> GetSurfaceBuffer() 84 { 85 std::unique_lock<std::mutex> lock(mutex_); 86 return videoBuffer_; 87 } 88 SetSurfaceBuffer(sptr<SurfaceBuffer> buffer)89 inline void SetSurfaceBuffer(sptr<SurfaceBuffer> buffer) 90 { 91 std::unique_lock<std::mutex> lock(mutex_); 92 videoBuffer_ = buffer; 93 } 94 GetEncodeBuffer()95 inline OH_AVBuffer* GetEncodeBuffer() 96 { 97 return encodedBuffer; 98 } 99 CacheBuffer(OH_AVBuffer * buffer)100 inline void CacheBuffer(OH_AVBuffer* buffer) 101 { 102 MEDIA_DEBUG_LOG("cacheBuffer start"); 103 encodedBuffer = buffer; 104 } 105 SetEncodedResult(bool encodedResult)106 inline void SetEncodedResult(bool encodedResult) 107 { 108 isEncoded_ = encodedResult; 109 } 110 IsEncoded()111 inline bool IsEncoded() 112 { 113 return isEncoded_; 114 } 115 GetFormat()116 inline int32_t GetFormat() 117 { 118 auto surfaceBuffer = GetSurfaceBuffer(); 119 if (surfaceBuffer) { 120 return surfaceBuffer->GetFormat(); 121 } 122 return -1; 123 } 124 GetUsage()125 inline uint64_t GetUsage() 126 { 127 auto surfaceBuffer = GetSurfaceBuffer(); 128 if (surfaceBuffer) { 129 return surfaceBuffer->GetUsage(); 130 } 131 return std::numeric_limits<uint64_t>::max(); 132 } 133 GetFrameId()134 inline const std::string& GetFrameId() const 135 { 136 return frameId_; 137 } 138 GetBufferSize()139 inline uint32_t GetBufferSize() 140 { 141 return bufferSize; 142 } 143 GetTimeStamp()144 inline int64_t GetTimeStamp() 145 { 146 return timestamp_; 147 } 148 GetFrameSize()149 inline shared_ptr<Size> GetFrameSize() 150 { 151 return size; 152 } 153 GetRotation()154 inline int32_t GetRotation() 155 { 156 auto it = transformTypeToValue.find(transformType_); 157 return it == transformTypeToValue.end() ? 0 : it->second; 158 } 159 SetMetaBuffer(sptr<SurfaceBuffer> buffer)160 inline void SetMetaBuffer(sptr<SurfaceBuffer> buffer) 161 { 162 std::unique_lock<std::mutex> lock(metaBufferMutex_); 163 metaBuffer_ = buffer; 164 } 165 GetMetaBuffer()166 inline sptr<SurfaceBuffer> GetMetaBuffer() 167 { 168 metaBufferMutex_.lock(); 169 return metaBuffer_; 170 } 171 UnLockMetaBuffer()172 inline void UnLockMetaBuffer() 173 { 174 metaBufferMutex_.unlock(); 175 } 176 SetIDRProperty(bool isIDRFrame)177 inline void SetIDRProperty(bool isIDRFrame) 178 { 179 isIDRFrame_ = isIDRFrame; 180 } 181 IsIDRFrame()182 inline bool IsIDRFrame() 183 { 184 return isIDRFrame_; 185 } 186 187 struct HashFunction { operatorHashFunction188 std::size_t operator()(const sptr<FrameRecord>& obj) const 189 { 190 return std::hash<std::string>()(obj->GetFrameId()); 191 } 192 }; 193 194 struct EqualFunction { operatorEqualFunction195 bool operator()(const sptr<FrameRecord>& obj1, const sptr<FrameRecord>& obj2) const 196 { 197 return obj1->GetFrameId() == obj2->GetFrameId(); 198 } 199 }; 200 201 const unordered_map<GraphicTransformType, int32_t> transformTypeToValue = { 202 { GRAPHIC_FLIP_H_ROT90, 90 }, 203 { GRAPHIC_FLIP_H_ROT180, 180 }, 204 { GRAPHIC_FLIP_H_ROT270, 270 }, 205 { GRAPHIC_ROTATE_90, 270 }, 206 { GRAPHIC_ROTATE_180, 180 }, 207 { GRAPHIC_ROTATE_270, 90 }, 208 }; 209 210 OH_AVBuffer* encodedBuffer = nullptr; 211 std::string frameId_; 212 std::mutex bufferMutex_; 213 214 private: 215 static const int32_t STATUS_NONE = 0; 216 static const int32_t STATUS_READY_CONVERT = 1; 217 static const int32_t STATUS_FINISH_ENCODE = 2; 218 std::atomic<int32_t> status = STATUS_NONE; 219 std::atomic<bool> isEncoded_ { false }; 220 std::atomic<bool> isCover_ { false }; 221 shared_ptr<Size> size; 222 uint32_t bufferSize; 223 int32_t format; 224 uint64_t usage; 225 sptr<SurfaceBuffer> videoBuffer_; 226 int64_t timestamp_; 227 GraphicTransformType transformType_; 228 std::mutex mutex_; 229 std::condition_variable canReleased_; 230 std::mutex metaBufferMutex_; 231 sptr<SurfaceBuffer> metaBuffer_; 232 bool isIDRFrame_ = false; 233 }; 234 } // namespace CameraStandard 235 } // namespace OHOS 236 #endif // CAMERA_FRAMEWORK_CODEC_BUFFER_INFO_H 237