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