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_cinamespace OHOS::Camera { 20094332d3Sopenharmony_ciStreamCustomer::StreamCustomer() {} 21094332d3Sopenharmony_ciStreamCustomer::~StreamCustomer() {} 22094332d3Sopenharmony_ci 23094332d3Sopenharmony_civoid StreamCustomer::CamFrame(const std::function<void(void*, uint32_t)> callback) 24094332d3Sopenharmony_ci{ 25094332d3Sopenharmony_ci CAMERA_LOGD("demo test:enter CamFrame thread ++ "); 26094332d3Sopenharmony_ci#ifdef CAMERA_BUILT_ON_OHOS_LITE 27094332d3Sopenharmony_ci do { 28094332d3Sopenharmony_ci OHOS::SurfaceBuffer* buffer = consumer_->AcquireBuffer(); 29094332d3Sopenharmony_ci if (buffer != nullptr) { 30094332d3Sopenharmony_ci if (callback != nullptr) { 31094332d3Sopenharmony_ci void* addr = buffer->GetVirAddr(); 32094332d3Sopenharmony_ci int32_t size = 0; 33094332d3Sopenharmony_ci buffer->GetInt32(VIDEO_KEY_INFO_DATA_SIZE, size); 34094332d3Sopenharmony_ci callback(addr, size); 35094332d3Sopenharmony_ci } 36094332d3Sopenharmony_ci consumer_->ReleaseBuffer(buffer); 37094332d3Sopenharmony_ci } 38094332d3Sopenharmony_ci } while (camFrameExit_ == 0); 39094332d3Sopenharmony_ci#else 40094332d3Sopenharmony_ci OHOS::Rect damage; 41094332d3Sopenharmony_ci int32_t flushFence = 0; 42094332d3Sopenharmony_ci int64_t timestamp = 0; 43094332d3Sopenharmony_ci constexpr uint32_t delayTime = 12000; 44094332d3Sopenharmony_ci 45094332d3Sopenharmony_ci do { 46094332d3Sopenharmony_ci OHOS::sptr<OHOS::SurfaceBuffer> buff = nullptr; 47094332d3Sopenharmony_ci consumer_->AcquireBuffer(buff, flushFence, timestamp, damage); 48094332d3Sopenharmony_ci if (buff != nullptr) { 49094332d3Sopenharmony_ci void* addr = buff->GetVirAddr(); 50094332d3Sopenharmony_ci if (callback != nullptr) { 51094332d3Sopenharmony_ci int32_t gotSize = 0; 52094332d3Sopenharmony_ci int32_t frameNum = 0; 53094332d3Sopenharmony_ci int isKey = 0; 54094332d3Sopenharmony_ci timestamp = 0; 55094332d3Sopenharmony_ci buff->GetExtraData()->ExtraGet(OHOS::Camera::dataSize, gotSize); 56094332d3Sopenharmony_ci buff->GetExtraData()->ExtraGet(OHOS::Camera::isKeyFrame, isKey); 57094332d3Sopenharmony_ci buff->GetExtraData()->ExtraGet(OHOS::Camera::timeStamp, timestamp); 58094332d3Sopenharmony_ci CAMERA_LOGE("demo test:CamFrame callback +++++++ Size == %d frameNum = %d timestamp == %lld\n", 59094332d3Sopenharmony_ci gotSize, frameNum, timestamp); 60094332d3Sopenharmony_ci callback(addr, gotSize); 61094332d3Sopenharmony_ci } 62094332d3Sopenharmony_ci consumer_->ReleaseBuffer(buff, -1); 63094332d3Sopenharmony_ci } 64094332d3Sopenharmony_ci usleep(delayTime); 65094332d3Sopenharmony_ci } while (camFrameExit_ == 0); 66094332d3Sopenharmony_ci#endif 67094332d3Sopenharmony_ci 68094332d3Sopenharmony_ci CAMERA_LOGD("demo test:Exiting CamFrame thread -- "); 69094332d3Sopenharmony_ci} 70094332d3Sopenharmony_ci 71094332d3Sopenharmony_ci#ifdef CAMERA_BUILT_ON_OHOS_LITE 72094332d3Sopenharmony_cistd::shared_ptr<OHOS::Surface> StreamCustomer::CreateProducer() 73094332d3Sopenharmony_ci#else 74094332d3Sopenharmony_cisptr<OHOS::IBufferProducer> StreamCustomer::CreateProducer() 75094332d3Sopenharmony_ci#endif 76094332d3Sopenharmony_ci{ 77094332d3Sopenharmony_ci#ifdef CAMERA_BUILT_ON_OHOS_LITE 78094332d3Sopenharmony_ci consumer_ = std::shared_ptr<OHOS::Surface>(OHOS::Surface::CreateSurface()); 79094332d3Sopenharmony_ci if (consumer_ == nullptr) { 80094332d3Sopenharmony_ci return nullptr; 81094332d3Sopenharmony_ci } 82094332d3Sopenharmony_ci return consumer_; 83094332d3Sopenharmony_ci#else 84094332d3Sopenharmony_ci consumer_ = OHOS::IConsumerSurface::Create(); 85094332d3Sopenharmony_ci if (consumer_ == nullptr) { 86094332d3Sopenharmony_ci return nullptr; 87094332d3Sopenharmony_ci } 88094332d3Sopenharmony_ci sptr<IBufferConsumerListener> listener = new(std::nothrow) TestBuffersConsumerListener(); 89094332d3Sopenharmony_ci consumer_->RegisterConsumerListener(listener); 90094332d3Sopenharmony_ci 91094332d3Sopenharmony_ci auto producer = consumer_->GetProducer(); 92094332d3Sopenharmony_ci if (producer == nullptr) { 93094332d3Sopenharmony_ci return nullptr; 94094332d3Sopenharmony_ci } 95094332d3Sopenharmony_ci 96094332d3Sopenharmony_ci CAMERA_LOGI("demo test, create a buffer queue producer"); 97094332d3Sopenharmony_ci return producer; 98094332d3Sopenharmony_ci#endif 99094332d3Sopenharmony_ci} 100094332d3Sopenharmony_ci 101094332d3Sopenharmony_ciRetCode StreamCustomer::ReceiveFrameOn(const std::function<void(void*, uint32_t)> callback) 102094332d3Sopenharmony_ci{ 103094332d3Sopenharmony_ci CAMERA_LOGD("demo test:ReceiveFrameOn enter"); 104094332d3Sopenharmony_ci 105094332d3Sopenharmony_ci if (camFrameExit_ == 1) { 106094332d3Sopenharmony_ci camFrameExit_ = 0; 107094332d3Sopenharmony_ci previewThreadId_ = new (std::nothrow) std::thread(&StreamCustomer::CamFrame, this, callback); 108094332d3Sopenharmony_ci if (previewThreadId_ == nullptr) { 109094332d3Sopenharmony_ci CAMERA_LOGE("demo test:ReceiveFrameOn failed"); 110094332d3Sopenharmony_ci return OHOS::Camera::RC_ERROR; 111094332d3Sopenharmony_ci } 112094332d3Sopenharmony_ci } else { 113094332d3Sopenharmony_ci CAMERA_LOGD("demo test:ReceiveFrameOn loop thread is running"); 114094332d3Sopenharmony_ci } 115094332d3Sopenharmony_ci CAMERA_LOGD("demo test:ReceiveFrameOn exit"); 116094332d3Sopenharmony_ci 117094332d3Sopenharmony_ci return RC_OK; 118094332d3Sopenharmony_ci} 119094332d3Sopenharmony_ci 120094332d3Sopenharmony_civoid StreamCustomer::ReceiveFrameOff() 121094332d3Sopenharmony_ci{ 122094332d3Sopenharmony_ci CAMERA_LOGD("demo test:ReceiveFrameOff enter"); 123094332d3Sopenharmony_ci 124094332d3Sopenharmony_ci if (camFrameExit_ == 0) { 125094332d3Sopenharmony_ci camFrameExit_ = 1; 126094332d3Sopenharmony_ci if (previewThreadId_ != nullptr) { 127094332d3Sopenharmony_ci previewThreadId_->join(); 128094332d3Sopenharmony_ci delete previewThreadId_; 129094332d3Sopenharmony_ci previewThreadId_ = nullptr; 130094332d3Sopenharmony_ci } 131094332d3Sopenharmony_ci } 132094332d3Sopenharmony_ci 133094332d3Sopenharmony_ci CAMERA_LOGD("demo test:ReceiveFrameOff exit"); 134094332d3Sopenharmony_ci} 135094332d3Sopenharmony_ci} 136