1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License. 5094332d3Sopenharmony_ci * You may obtain a copy of the License at 6094332d3Sopenharmony_ci * 7094332d3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8094332d3Sopenharmony_ci * 9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and 13094332d3Sopenharmony_ci * limitations under the License. 14094332d3Sopenharmony_ci */ 15094332d3Sopenharmony_ci 16094332d3Sopenharmony_ci#include "stream_customer.h" 17094332d3Sopenharmony_ci#include "video_key_info.h" 18094332d3Sopenharmony_ci 19094332d3Sopenharmony_ciStreamCustomer::StreamCustomer() {} 20094332d3Sopenharmony_ciStreamCustomer::~StreamCustomer() {} 21094332d3Sopenharmony_ci 22094332d3Sopenharmony_civoid StreamCustomer::CamFrame(const std::function<void(const unsigned char *, uint32_t)> callback) 23094332d3Sopenharmony_ci{ 24094332d3Sopenharmony_ci CAMERA_LOGD("test:enter CamFrame thread ++ "); 25094332d3Sopenharmony_ci OHOS::Rect damage; 26094332d3Sopenharmony_ci int32_t flushFence = 0; 27094332d3Sopenharmony_ci int64_t timestamp = 0; 28094332d3Sopenharmony_ci constexpr uint32_t delayTime = 12000; 29094332d3Sopenharmony_ci 30094332d3Sopenharmony_ci do { 31094332d3Sopenharmony_ci OHOS::sptr<OHOS::SurfaceBuffer> buff = nullptr; 32094332d3Sopenharmony_ci consumer_->AcquireBuffer(buff, flushFence, timestamp, damage); 33094332d3Sopenharmony_ci if (buff != nullptr) { 34094332d3Sopenharmony_ci void* addr = buff->GetVirAddr(); 35094332d3Sopenharmony_ci int32_t size = buff->GetSize(); 36094332d3Sopenharmony_ci if (callback != nullptr) { 37094332d3Sopenharmony_ci callback(static_cast<const unsigned char*>(addr), size); 38094332d3Sopenharmony_ci } 39094332d3Sopenharmony_ci consumer_->ReleaseBuffer(buff, -1); 40094332d3Sopenharmony_ci } 41094332d3Sopenharmony_ci usleep(delayTime); 42094332d3Sopenharmony_ci } while (camFrameExit_ == 0); 43094332d3Sopenharmony_ci 44094332d3Sopenharmony_ci CAMERA_LOGD("test:Exiting CamFrame thread -- "); 45094332d3Sopenharmony_ci} 46094332d3Sopenharmony_ci 47094332d3Sopenharmony_ciOHOS::sptr<OHOS::IBufferProducer> StreamCustomer::CreateProducer() 48094332d3Sopenharmony_ci{ 49094332d3Sopenharmony_ci consumer_ = OHOS::IConsumerSurface::Create(); 50094332d3Sopenharmony_ci if (consumer_ == nullptr) { 51094332d3Sopenharmony_ci return nullptr; 52094332d3Sopenharmony_ci } 53094332d3Sopenharmony_ci OHOS::sptr<OHOS::IBufferConsumerListener> listener = new TestBuffersConsumerListener(); 54094332d3Sopenharmony_ci CHECK_IF_PTR_NULL_RETURN_VALUE(listener, nullptr); 55094332d3Sopenharmony_ci consumer_->RegisterConsumerListener(listener); 56094332d3Sopenharmony_ci 57094332d3Sopenharmony_ci auto producer = consumer_->GetProducer(); 58094332d3Sopenharmony_ci if (producer == nullptr) { 59094332d3Sopenharmony_ci return nullptr; 60094332d3Sopenharmony_ci } 61094332d3Sopenharmony_ci 62094332d3Sopenharmony_ci CAMERA_LOGI("test, create a buffer queue producer"); 63094332d3Sopenharmony_ci return producer; 64094332d3Sopenharmony_ci} 65094332d3Sopenharmony_ci 66094332d3Sopenharmony_ciOHOS::Camera::RetCode StreamCustomer::ReceiveFrameOn( 67094332d3Sopenharmony_ci const std::function<void(const unsigned char *, uint32_t)> callback) 68094332d3Sopenharmony_ci{ 69094332d3Sopenharmony_ci CAMERA_LOGD("test:ReceiveFrameOn enter"); 70094332d3Sopenharmony_ci 71094332d3Sopenharmony_ci if (camFrameExit_ == 1) { 72094332d3Sopenharmony_ci camFrameExit_ = 0; 73094332d3Sopenharmony_ci previewThreadId_ = new (std::nothrow) std::thread(&StreamCustomer::CamFrame, this, callback); 74094332d3Sopenharmony_ci if (previewThreadId_ == nullptr) { 75094332d3Sopenharmony_ci CAMERA_LOGE("test:ReceiveFrameOn failed"); 76094332d3Sopenharmony_ci return OHOS::Camera::RC_ERROR; 77094332d3Sopenharmony_ci } 78094332d3Sopenharmony_ci } else { 79094332d3Sopenharmony_ci CAMERA_LOGI("test:ReceiveFrameOn loop thread is running"); 80094332d3Sopenharmony_ci } 81094332d3Sopenharmony_ci CAMERA_LOGD("test:ReceiveFrameOn exit"); 82094332d3Sopenharmony_ci 83094332d3Sopenharmony_ci return OHOS::Camera::RC_OK; 84094332d3Sopenharmony_ci} 85094332d3Sopenharmony_ci 86094332d3Sopenharmony_civoid StreamCustomer::ReceiveFrameOff() 87094332d3Sopenharmony_ci{ 88094332d3Sopenharmony_ci CAMERA_LOGD("test:ReceiveFrameOff enter"); 89094332d3Sopenharmony_ci 90094332d3Sopenharmony_ci if (camFrameExit_ == 0) { 91094332d3Sopenharmony_ci camFrameExit_ = 1; 92094332d3Sopenharmony_ci if (previewThreadId_ != nullptr) { 93094332d3Sopenharmony_ci previewThreadId_->join(); 94094332d3Sopenharmony_ci delete previewThreadId_; 95094332d3Sopenharmony_ci previewThreadId_ = nullptr; 96094332d3Sopenharmony_ci } 97094332d3Sopenharmony_ci } 98094332d3Sopenharmony_ci 99094332d3Sopenharmony_ci CAMERA_LOGD("test:ReceiveFrameOff exit"); 100094332d3Sopenharmony_ci} 101