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 "stream_customer.h"
17
18 namespace OHOS::Camera {
StreamCustomer()19 StreamCustomer::StreamCustomer() {}
~StreamCustomer()20 StreamCustomer::~StreamCustomer() {}
21
CamFrame(const std::function<void (void *,uint32_t)> callback)22 void StreamCustomer::CamFrame(const std::function<void(void*, uint32_t)> callback)
23 {
24 OHOS::Rect damage;
25 int32_t flushFence = 0;
26 int64_t timestamp = 0;
27 constexpr uint32_t delayTime = 12000;
28
29 CAMERA_LOGD("demo test:enter CamFrame thread ++ \n");
30
31 do {
32 OHOS::sptr<OHOS::SurfaceBuffer> buff = nullptr;
33 consumer_->AcquireBuffer(buff, flushFence, timestamp, damage);
34 if (buff != nullptr) {
35 void* addr = buff->GetVirAddr();
36 int32_t size = buff->GetSize();
37 if (callback != nullptr) {
38 int32_t gotSize = 0;
39 int32_t frameNum = 0;
40 int isKey = 0;
41 int64_t timestamp;
42 buff->ExtraGet("dataSize", gotSize);
43 buff->ExtraGet("isKeyFrame", isKey);
44 buff->ExtraGet("timeStamp", timestamp);
45 buff->ExtraGet("frameNum", frameNum);
46 CAMERA_LOGE("demo test:CamFrame callback +++++++ Size == %d frameNum = %d timestamp == %lld\n",
47 gotSize, frameNum, timestamp);
48 callback(addr, gotSize);
49 }
50 consumer_->ReleaseBuffer(buff, -1);
51 }
52 usleep(delayTime);
53 } while (camFrameExit_ == 0);
54
55 CAMERA_LOGD("demo test:Exiting CamFrame thread -- \n");
56 }
57
CreateProducer()58 sptr<OHOS::IBufferProducer> StreamCustomer::CreateProducer()
59 {
60 consumer_ = OHOS::Surface::CreateSurfaceAsConsumer();
61 if (consumer_ == nullptr) {
62 return nullptr;
63 }
64 sptr<IBufferConsumerListener> listener = new TestBuffersConsumerListener();
65 consumer_->RegisterConsumerListener(listener);
66
67 auto producer = consumer_->GetProducer();
68 if (producer == nullptr) {
69 return nullptr;
70 }
71
72 CAMERA_LOGI("demo test, create a buffer queue producer %{public}p", producer.GetRefPtr());
73 return producer;
74 }
75
ReceiveFrameOn(const std::function<void (void *,uint32_t)> callback)76 RetCode StreamCustomer::ReceiveFrameOn(const std::function<void(void*, uint32_t)> callback)
77 {
78 CAMERA_LOGD("demo test:ReceiveFrameOn enter");
79
80 if (camFrameExit_ == 1) {
81 camFrameExit_ = 0;
82 previewThreadId_ = new (std::nothrow) std::thread(&StreamCustomer::CamFrame, this, callback);
83 if (previewThreadId_ == nullptr) {
84 CAMERA_LOGE("demo test:ReceiveFrameOn failed\n");
85 return OHOS::Camera::RC_ERROR;
86 }
87 } else {
88 CAMERA_LOGD("demo test:ReceiveFrameOn loop thread is running\n");
89 }
90 CAMERA_LOGD("demo test:ReceiveFrameOn exit");
91
92 return RC_OK;
93 }
94
ReceiveFrameOff()95 void StreamCustomer::ReceiveFrameOff()
96 {
97 CAMERA_LOGD("demo test:ReceiveFrameOff enter\n");
98
99 if (camFrameExit_ == 0) {
100 camFrameExit_ = 1;
101 if (previewThreadId_ != nullptr) {
102 previewThreadId_->join();
103 delete previewThreadId_;
104 previewThreadId_ = nullptr;
105 }
106 }
107
108 CAMERA_LOGD("demo test:ReceiveFrameOff exit\n");
109 }
110 }
111