• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (void *,uint32_t)> callback)26 void StreamCustomer::CamFrame(const std::function<void(void*, uint32_t)> callback)
27 {
28     CAMERA_LOGI("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                 int32_t gotSize = 0;
42                 int32_t frameNum = 0;
43                 int isKey = 0;
44                 int64_t timestamp;
45                 callback(addr, size);
46             }
47             consumer_->ReleaseBuffer(buff, -1);
48         }
49         usleep(delayTime);
50     } while (camFrameExit_ == 0);
51 
52     CAMERA_LOGI("test:Exiting CamFrame thread -- \n");
53 }
54 
CreateProducer()55 OHOS::sptr<OHOS::IBufferProducer> StreamCustomer::CreateProducer()
56 {
57     consumer_ = OHOS::Surface::CreateSurfaceAsConsumer();
58     if (consumer_ == nullptr) {
59         return nullptr;
60     }
61     OHOS::sptr<OHOS::IBufferConsumerListener> listener = new TestBuffersConsumerListener();
62     consumer_->RegisterConsumerListener(listener);
63 
64     auto producer = consumer_->GetProducer();
65     if (producer == nullptr) {
66         return nullptr;
67     }
68 
69     CAMERA_LOGI("test, create a buffer queue producer %{public}p", producer.GetRefPtr());
70     return producer;
71 }
72 
ReceiveFrameOn(const std::function<void (void *,uint32_t)> callback)73 OHOS::Camera::RetCode StreamCustomer::ReceiveFrameOn(const std::function<void(void*, uint32_t)> callback)
74 {
75     CAMERA_LOGI("test:ReceiveFrameOn enter");
76 
77     if (camFrameExit_ == 1) {
78         camFrameExit_ = 0;
79         previewThreadId_ = new (std::nothrow) std::thread(&StreamCustomer::CamFrame, this, callback);
80         if (previewThreadId_ == nullptr) {
81             CAMERA_LOGE("test:ReceiveFrameOn failed\n");
82             return OHOS::Camera::RC_ERROR;
83         }
84     } else {
85         CAMERA_LOGI("test:ReceiveFrameOn loop thread is running\n");
86     }
87     CAMERA_LOGI("test:ReceiveFrameOn exit");
88 
89     return OHOS::Camera::RC_OK;
90 }
91 
ReceiveFrameOff()92 void StreamCustomer::ReceiveFrameOff()
93 {
94     CAMERA_LOGI("test:ReceiveFrameOff enter\n");
95 
96     if (camFrameExit_ == 0) {
97         camFrameExit_ = 1;
98         if (previewThreadId_ != nullptr) {
99             previewThreadId_->join();
100             delete previewThreadId_;
101             previewThreadId_ = nullptr;
102         }
103     }
104 
105     CAMERA_LOGI("test:ReceiveFrameOff exit\n");
106 }