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