• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "avcodec/common/frame_record.h"
17 
18 #include "moving_photo/moving_photo_surface_wrapper.h"
19 
20 namespace OHOS {
21 namespace CameraStandard {
22 
FrameRecord(sptr<SurfaceBuffer> videoBuffer,int64_t timestamp,GraphicTransformType type)23 FrameRecord::FrameRecord(sptr<SurfaceBuffer> videoBuffer, int64_t timestamp, GraphicTransformType type)
24     : videoBuffer_(videoBuffer), timestamp_(timestamp), transformType_(type)
25 {
26     frameId_ = std::to_string(timestamp);
27     size = make_shared<Size>();
28     size->width = static_cast<uint32_t>(videoBuffer->GetSurfaceBufferWidth());
29     size->height = static_cast<uint32_t>(videoBuffer->GetSurfaceBufferHeight());
30     bufferSize = videoBuffer->GetSize();
31     format = videoBuffer->GetFormat();
32     usage = videoBuffer->GetUsage();
33 }
34 
~FrameRecord()35 FrameRecord::~FrameRecord()
36 {
37     MEDIA_DEBUG_LOG("FrameRecord::~FrameRecord");
38     encodedBuffer = nullptr;
39 }
40 
ReleaseSurfaceBuffer(sptr<MovingPhotoSurfaceWrapper> surfaceWrapper)41 void FrameRecord::ReleaseSurfaceBuffer(sptr<MovingPhotoSurfaceWrapper> surfaceWrapper)
42 {
43     std::unique_lock<std::mutex> lock(mutex_);
44     if (IsReadyConvert()) {
45         MEDIA_DEBUG_LOG("FrameRecord::ReleaseSurfaceBuffer isReadyConvert");
46         auto thisPtr = sptr<FrameRecord>(this);
47         canReleased_.wait_for(lock, std::chrono::milliseconds(BUFFER_RELEASE_EXPIREATION_TIME),
48             [thisPtr] { return thisPtr->IsFinishCache(); });
49         MEDIA_DEBUG_LOG("FrameRecord::ReleaseSurfaceBuffer wait end");
50     }
51     if (videoBuffer_) {
52         CHECK_EXECUTE(surfaceWrapper != nullptr, surfaceWrapper->RecycleBuffer(videoBuffer_));
53         videoBuffer_ = nullptr;
54         MEDIA_DEBUG_LOG("release buffer end %{public}s", frameId_.c_str());
55     }
56 }
57 
ReleaseMetaBuffer(sptr<Surface> surface,bool reuse)58 void FrameRecord::ReleaseMetaBuffer(sptr<Surface> surface, bool reuse)
59 {
60     std::unique_lock<std::mutex> lock(metaBufferMutex_);
61     sptr<SurfaceBuffer> buffer = nullptr;
62     if (status != STATUS_NONE && metaBuffer_) {
63         buffer = SurfaceBuffer::Create();
64         DeepCopyBuffer(buffer, metaBuffer_);
65     }
66     if (metaBuffer_) {
67         if (reuse) {
68             SurfaceError surfaceRet = surface->AttachBufferToQueue(metaBuffer_);
69             CHECK_ERROR_RETURN_LOG(surfaceRet != SURFACE_ERROR_OK,
70                 "Failed to attach meta buffer %{public}d", surfaceRet);
71             surfaceRet = surface->ReleaseBuffer(metaBuffer_, -1);
72             CHECK_ERROR_RETURN_LOG(surfaceRet != SURFACE_ERROR_OK,
73                 "Failed to Release meta Buffer %{public}d", surfaceRet);
74         }
75         metaBuffer_ = buffer;
76         MEDIA_DEBUG_LOG("release meta buffer end %{public}s", frameId_.c_str());
77     }
78 }
79 
NotifyBufferRelease()80 void FrameRecord::NotifyBufferRelease()
81 {
82     MEDIA_DEBUG_LOG("notifyBufferRelease");
83     status = STATUS_FINISH_ENCODE;
84     canReleased_.notify_one();
85 }
86 
DeepCopyBuffer(sptr<SurfaceBuffer> newSurfaceBuffer,sptr<SurfaceBuffer> surfaceBuffer) const87 void FrameRecord::DeepCopyBuffer(sptr<SurfaceBuffer> newSurfaceBuffer, sptr<SurfaceBuffer> surfaceBuffer) const
88 {
89     BufferRequestConfig requestConfig = {
90         .width = surfaceBuffer->GetWidth(),
91         .height = surfaceBuffer->GetHeight(),
92         .strideAlignment = 0x8, // default stride is 8 Bytes.
93         .format = surfaceBuffer->GetFormat(),
94         .usage = surfaceBuffer->GetUsage(),
95         .timeout = 0,
96         .colorGamut = surfaceBuffer->GetSurfaceBufferColorGamut(),
97         .transform = surfaceBuffer->GetSurfaceBufferTransform(),
98     };
99     auto allocErrorCode = newSurfaceBuffer->Alloc(requestConfig);
100     CHECK_ERROR_RETURN_LOG(allocErrorCode != GSERROR_OK, "SurfaceBuffer alloc ret: %d", allocErrorCode);
101     CHECK_ERROR_PRINT_LOG(memcpy_s(newSurfaceBuffer->GetVirAddr(), newSurfaceBuffer->GetSize(),
102         surfaceBuffer->GetVirAddr(), surfaceBuffer->GetSize()) != EOK, "SurfaceBuffer memcpy_s failed");
103 }
104 } // namespace CameraStandard
105 } // namespace OHOS