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 #include "dcamera_photo_surface_listener.h"
17 
18 #include <securec.h>
19 
20 #include "data_buffer.h"
21 #include "dcamera_hidumper.h"
22 #include "dcamera_utils_tools.h"
23 #include "distributed_camera_errno.h"
24 #include "distributed_hardware_log.h"
25 
26 namespace OHOS {
27 namespace DistributedHardware {
DCameraPhotoSurfaceListener(const sptr<IConsumerSurface> & surface,const std::shared_ptr<ResultCallback> & callback)28 DCameraPhotoSurfaceListener::DCameraPhotoSurfaceListener(const sptr<IConsumerSurface>& surface,
29     const std::shared_ptr<ResultCallback>& callback) : surface_(surface), callback_(callback)
30 {
31     photoCount_ = COUNT_INIT_NUM;
32 }
33 
OnBufferAvailable()34 void DCameraPhotoSurfaceListener::OnBufferAvailable()
35 {
36     DHLOGI("enter");
37     if (callback_ == nullptr || surface_ == nullptr) {
38         DHLOGE("DCameraPhotoSurfaceListener ResultCallback or Surface is null");
39         return;
40     }
41 
42     int32_t flushFence = 0;
43     int64_t timestamp = 0;
44     OHOS::Rect damage;
45     OHOS::sptr<OHOS::SurfaceBuffer> buffer = nullptr;
46     surface_->AcquireBuffer(buffer, flushFence, timestamp, damage);
47     if (buffer == nullptr) {
48         DHLOGE("DCameraPhotoSurfaceListener AcquireBuffer failed");
49         return;
50     }
51 
52     do {
53         char *address = static_cast<char *>(buffer->GetVirAddr());
54         int32_t size = -1;
55         buffer->GetExtraData()->ExtraGet("dataSize", size);
56         if (size <= 0) {
57             size = static_cast<int32_t>(buffer->GetSize());
58         }
59         if ((address == nullptr) || (size <= 0) || (size > SURFACE_BUFFER_MAX_SIZE)) {
60             DHLOGE("DCameraPhotoSurfaceListener invalid params, size: %{public}d", size);
61             break;
62         }
63         DHLOGI("DCameraPhotoSurfaceListener size: %{public}d", size);
64         std::shared_ptr<DataBuffer> dataBuffer = std::make_shared<DataBuffer>(size);
65         int32_t ret = memcpy_s(dataBuffer->Data(), dataBuffer->Capacity(), address, size);
66         if (ret != EOK) {
67             DHLOGE("DCameraPhotoSurfaceListener Memory Copy failed, ret: %{public}d", ret);
68             break;
69         }
70 #ifdef DUMP_DCAMERA_FILE
71         std::string fileName = DUMP_PHOTO_PATH + std::to_string(photoCount_++) + SINK_PHOTO;
72         if (DcameraHidumper::GetInstance().GetDumpFlag() && (IsUnderDumpMaxSize(fileName) == DCAMERA_OK)) {
73             DumpBufferToFile(fileName, dataBuffer->Data(), dataBuffer->Size());
74         }
75 #endif
76         callback_->OnPhotoResult(dataBuffer);
77     } while (0);
78     surface_->ReleaseBuffer(buffer, -1);
79 }
80 } // namespace DistributedHardware
81 } // namespace OHOS