• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 namespace OHOS::Camera {
StreamCustomer()20 StreamCustomer::StreamCustomer() {}
~StreamCustomer()21 StreamCustomer::~StreamCustomer() {}
22 
CamFrame(const std::function<void (void *,uint32_t)> callback)23 void StreamCustomer::CamFrame(const std::function<void(void*, uint32_t)> callback)
24 {
25     CAMERA_LOGD("demo test:enter CamFrame thread ++ \n");
26 #ifdef CAMERA_BUILT_ON_OHOS_LITE
27     do {
28         OHOS::SurfaceBuffer* buffer = consumer_->AcquireBuffer();
29         if (buffer != nullptr) {
30             if (callback != nullptr) {
31                 void* addr = buffer->GetVirAddr();
32                 int32_t size = 0;
33                 buffer->GetInt32(VIDEO_KEY_INFO_DATA_SIZE, size);
34                 callback(addr, size);
35             }
36             consumer_->ReleaseBuffer(buffer);
37         }
38     } while (camFrameExit_ == 0);
39 #else
40     OHOS::Rect damage;
41     int32_t flushFence = 0;
42     int64_t timestamp = 0;
43     constexpr uint32_t delayTime = 12000;
44 
45     do {
46         OHOS::sptr<OHOS::SurfaceBuffer> buff = nullptr;
47         consumer_->AcquireBuffer(buff, flushFence, timestamp, damage);
48         if (buff != nullptr) {
49             void* addr = buff->GetVirAddr();
50             if (callback != nullptr) {
51                 int32_t gotSize = 0;
52                 int32_t frameNum = 0;
53                 int isKey = 0;
54                 timestamp = 0;
55                 buff->GetExtraData()->ExtraGet(OHOS::Camera::dataSize, gotSize);
56                 buff->GetExtraData()->ExtraGet(OHOS::Camera::isKeyFrame, isKey);
57                 buff->GetExtraData()->ExtraGet(OHOS::Camera::timeStamp, timestamp);
58                 CAMERA_LOGE("demo test:CamFrame callback +++++++ Size == %d frameNum = %d timestamp == %lld\n",
59                     gotSize, frameNum, timestamp);
60                 callback(addr, gotSize);
61             }
62             consumer_->ReleaseBuffer(buff, -1);
63         }
64         usleep(delayTime);
65     } while (camFrameExit_ == 0);
66 #endif
67 
68     CAMERA_LOGD("demo test:Exiting CamFrame thread -- \n");
69 }
70 
71 #ifdef CAMERA_BUILT_ON_OHOS_LITE
CreateProducer()72 std::shared_ptr<OHOS::Surface> StreamCustomer::CreateProducer()
73 #else
74 sptr<OHOS::IBufferProducer> StreamCustomer::CreateProducer()
75 #endif
76 {
77 #ifdef CAMERA_BUILT_ON_OHOS_LITE
78     consumer_ = std::shared_ptr<OHOS::Surface>(OHOS::Surface::CreateSurface());
79     if (consumer_ == nullptr) {
80         return nullptr;
81     }
82     return consumer_;
83 #else
84     consumer_ = OHOS::IConsumerSurface::Create();
85     if (consumer_ == nullptr) {
86         return nullptr;
87     }
88     sptr<IBufferConsumerListener> listener = new(std::nothrow) TestBuffersConsumerListener();
89     consumer_->RegisterConsumerListener(listener);
90 
91     auto producer = consumer_->GetProducer();
92     if (producer == nullptr) {
93         return nullptr;
94     }
95 
96     CAMERA_LOGI("demo test, create a buffer queue producer");
97     return producer;
98 #endif
99 }
100 
ReceiveFrameOn(const std::function<void (void *,uint32_t)> callback)101 RetCode StreamCustomer::ReceiveFrameOn(const std::function<void(void*, uint32_t)> callback)
102 {
103     CAMERA_LOGD("demo test:ReceiveFrameOn enter");
104 
105     if (camFrameExit_ == 1) {
106         camFrameExit_ = 0;
107         previewThreadId_ = new (std::nothrow) std::thread(&StreamCustomer::CamFrame, this, callback);
108         if (previewThreadId_ == nullptr) {
109             CAMERA_LOGE("demo test:ReceiveFrameOn failed\n");
110             return OHOS::Camera::RC_ERROR;
111         }
112     } else {
113         CAMERA_LOGD("demo test:ReceiveFrameOn loop thread is running\n");
114     }
115     CAMERA_LOGD("demo test:ReceiveFrameOn exit");
116 
117     return RC_OK;
118 }
119 
ReceiveFrameOff()120 void StreamCustomer::ReceiveFrameOff()
121 {
122     CAMERA_LOGD("demo test:ReceiveFrameOff enter\n");
123 
124     if (camFrameExit_ == 0) {
125         camFrameExit_ = 1;
126         if (previewThreadId_ != nullptr) {
127             previewThreadId_->join();
128             delete previewThreadId_;
129             previewThreadId_ = nullptr;
130         }
131     }
132 
133     CAMERA_LOGD("demo test:ReceiveFrameOff exit\n");
134 }
135 }
136