From be94fe2d37b33c839b8a8d6f3f648336c08c6bf0 Mon Sep 17 00:00:00 2001 From: zhuxu Date: Mon, 11 Apr 2022 11:42:33 +0800 Subject: [PATCH] fix distributed camera operator bug Signed-off-by: zhuxu --- .../client/include/dcamera_client.h | 4 +- .../client/src/dcamera_client.cpp | 43 ++++++++++++++++--- .../client/src/dcamera_client_common.cpp | 32 ++++++++++++-- .../dcamera_photo_surface_listener.cpp | 22 +++++----- .../dcamera_photo_surface_listener_common.cpp | 22 +++++----- .../dcamera_video_surface_listener.cpp | 26 +++++------ .../dcamera_video_surface_listener_common.cpp | 22 +++++----- .../handler/include/dcamera_handler.h | 2 +- .../handler/src/dcamera_handler.cpp | 6 +-- .../handler/src/dcamera_handler_common.cpp | 12 +++--- 10 files changed, 123 insertions(+), 68 deletions(-) diff --git a/services/cameraservice/cameraoperator/client/include/dcamera_client.h b/services/cameraservice/cameraoperator/client/include/dcamera_client.h index f97d2ee4..1eb348ab 100644 --- a/services/cameraservice/cameraoperator/client/include/dcamera_client.h +++ b/services/cameraservice/cameraoperator/client/include/dcamera_client.h @@ -69,10 +69,10 @@ private: sptr photoOutput_; sptr previewOutput_; sptr videoOutput_; + sptr photoListener_; + sptr videoListener_; std::shared_ptr stateCallback_; std::shared_ptr resultCallback_; - std::shared_ptr photoListener_; - std::shared_ptr videoListener_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/cameraservice/cameraoperator/client/src/dcamera_client.cpp b/services/cameraservice/cameraoperator/client/src/dcamera_client.cpp index 5f50dc95..9458c7f3 100644 --- a/services/cameraservice/cameraoperator/client/src/dcamera_client.cpp +++ b/services/cameraservice/cameraoperator/client/src/dcamera_client.cpp @@ -153,6 +153,39 @@ int32_t DCameraClient::StopCapture() DHLOGE("DCameraClient::StopCapture videoOutput stop failed, cameraId: %s, ret: %d", GetAnonyString(cameraId_).c_str(), ret); } + DHLOGI("DCameraClient::StopCapture %s release videoOutput", GetAnonyString(cameraId_).c_str()); + videoOutput_->Release(); + videoOutput_ = nullptr; + } + + if (videoSurface_ != nullptr) { + DHLOGI("DCameraClient::StopCapture %s video surface unregister consumer listener", + GetAnonyString(cameraId_).c_str()); + int32_t ret = videoSurface_->UnregisterConsumerListener(); + if (ret != DCAMERA_OK) { + DHLOGE("DCameraClient::StopCapture %s video surface unregister consumer listener failed, ret: %d", + GetAnonyString(cameraId_).c_str(), ret); + } + videoListener_ = nullptr; + videoSurface_ = nullptr; + } + + if (photoOutput_ != nullptr) { + DHLOGI("DCameraClient::StopCapture %s release photoOutput", GetAnonyString(cameraId_).c_str()); + photoOutput_->Release(); + photoOutput_ = nullptr; + } + + if (photoSurface_ != nullptr) { + DHLOGI("DCameraClient::StopCapture %s photo surface unregister consumer listener", + GetAnonyString(cameraId_).c_str()); + int32_t ret = photoSurface_->UnregisterConsumerListener(); + if (ret != DCAMERA_OK) { + DHLOGE("DCameraClient::StopCapture %s photo surface unregister consumer listener failed, ret: %d", + GetAnonyString(cameraId_).c_str(), ret); + } + photoListener_ = nullptr; + photoSurface_ = nullptr; } if (captureSession_ != nullptr) { @@ -164,17 +197,15 @@ int32_t DCameraClient::StopCapture() } DHLOGI("DCameraClient::StopCapture %s release captureSession", GetAnonyString(cameraId_).c_str()); captureSession_->Release(); + captureSession_ = nullptr; } if (cameraInput_ != nullptr) { DHLOGI("DCameraClient::StopCapture %s release cameraInput", GetAnonyString(cameraId_).c_str()); cameraInput_->Release(); + cameraInput_ = nullptr; } - photoOutput_ = nullptr; - videoOutput_ = nullptr; - cameraInput_ = nullptr; - captureSession_ = nullptr; DHLOGI("DCameraClient::StopCapture %s success", GetAnonyString(cameraId_).c_str()); return DCAMERA_OK; } @@ -317,7 +348,7 @@ int32_t DCameraClient::CreatePhotoOutput(std::shared_ptr& in photoSurface_ = Surface::CreateSurfaceAsConsumer(); photoSurface_->SetDefaultWidthAndHeight(info->width_, info->height_); photoSurface_->SetUserData(CAMERA_SURFACE_FORMAT, std::to_string(info->format_)); - photoListener_ = std::make_shared(photoSurface_, resultCallback_); + photoListener_ = new DCameraPhotoSurfaceListener(photoSurface_, resultCallback_); photoSurface_->RegisterConsumerListener((sptr &)photoListener_); photoOutput_ = cameraManager_->CreatePhotoOutput(photoSurface_); if (photoOutput_ == nullptr) { @@ -338,7 +369,7 @@ int32_t DCameraClient::CreateVideoOutput(std::shared_ptr& in videoSurface_ = Surface::CreateSurfaceAsConsumer(); videoSurface_->SetDefaultWidthAndHeight(info->width_, info->height_); videoSurface_->SetUserData(CAMERA_SURFACE_FORMAT, std::to_string(info->format_)); - videoListener_ = std::make_shared(videoSurface_, resultCallback_); + videoListener_ = new DCameraVideoSurfaceListener(videoSurface_, resultCallback_); videoSurface_->RegisterConsumerListener((sptr &)videoListener_); videoOutput_ = cameraManager_->CreateVideoOutput(videoSurface_); if (videoOutput_ == nullptr) { diff --git a/services/cameraservice/cameraoperator/client/src/dcamera_client_common.cpp b/services/cameraservice/cameraoperator/client/src/dcamera_client_common.cpp index 2a026d4f..194e14f5 100644 --- a/services/cameraservice/cameraoperator/client/src/dcamera_client_common.cpp +++ b/services/cameraservice/cameraoperator/client/src/dcamera_client_common.cpp @@ -146,6 +146,30 @@ int32_t DCameraClient::StartCapture(std::vectorUnregisterConsumerListener(); + if (ret != DCAMERA_OK) { + DHLOGE("DCameraClientCommon::StopCapture %s video surface unregister consumer listener failed, ret: %d", + GetAnonyString(cameraId_).c_str(), ret); + } + videoListener_ = nullptr; + videoSurface_ = nullptr; + } + + if (photoSurface_ != nullptr) { + DHLOGI("DCameraClientCommon::StopCapture %s photo surface unregister consumer listener", + GetAnonyString(cameraId_).c_str()); + int32_t ret = photoSurface_->UnregisterConsumerListener(); + if (ret != DCAMERA_OK) { + DHLOGE("DCameraClientCommon::StopCapture %s photo surface unregister consumer listener failed, ret: %d", + GetAnonyString(cameraId_).c_str(), ret); + } + photoListener_ = nullptr; + photoSurface_ = nullptr; + } + if (captureSession_ != nullptr) { DHLOGI("DCameraClientCommon::StopCapture %s stop captureSession", GetAnonyString(cameraId_).c_str()); int32_t ret = captureSession_->Stop(); @@ -155,17 +179,17 @@ int32_t DCameraClient::StopCapture() } DHLOGI("DCameraClientCommon::StopCapture %s release captureSession", GetAnonyString(cameraId_).c_str()); captureSession_->Release(); + captureSession_ = nullptr; } if (cameraInput_ != nullptr) { DHLOGI("DCameraClientCommon::StopCapture %s release cameraInput", GetAnonyString(cameraId_).c_str()); cameraInput_->Release(); + cameraInput_ = nullptr; } photoOutput_ = nullptr; previewOutput_ = nullptr; - cameraInput_ = nullptr; - captureSession_ = nullptr; DHLOGI("DCameraClientCommon::StopCapture %s success", GetAnonyString(cameraId_).c_str()); return DCAMERA_OK; } @@ -312,7 +336,7 @@ int32_t DCameraClient::CreatePhotoOutput(std::shared_ptr& in photoSurface_ = Surface::CreateSurfaceAsConsumer(); photoSurface_->SetDefaultWidthAndHeight(info->width_, info->height_); photoSurface_->SetUserData(CAMERA_SURFACE_FORMAT, std::to_string(camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888)); - photoListener_ = std::make_shared(photoSurface_, resultCallback_); + photoListener_ = new DCameraPhotoSurfaceListener(photoSurface_, resultCallback_); photoSurface_->RegisterConsumerListener((sptr &)photoListener_); photoOutput_ = cameraManager_->CreatePhotoOutput(photoSurface_); if (photoOutput_ == nullptr) { @@ -334,7 +358,7 @@ int32_t DCameraClient::CreateVideoOutput(std::shared_ptr& in videoSurface_ = Surface::CreateSurfaceAsConsumer(); videoSurface_->SetDefaultWidthAndHeight(info->width_, info->height_); videoSurface_->SetUserData(CAMERA_SURFACE_FORMAT, std::to_string(camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888)); - videoListener_ = std::make_shared(videoSurface_, resultCallback_); + videoListener_ = new DCameraVideoSurfaceListener(videoSurface_, resultCallback_); videoSurface_->RegisterConsumerListener((sptr &)videoListener_); previewOutput_ = cameraManager_->CreateCustomPreviewOutput(videoSurface_, info->width_, info->height_); if (previewOutput_ == nullptr) { diff --git a/services/cameraservice/cameraoperator/client/src/listener/dcamera_photo_surface_listener.cpp b/services/cameraservice/cameraoperator/client/src/listener/dcamera_photo_surface_listener.cpp index 29cbed9d..e3dd5e56 100644 --- a/services/cameraservice/cameraoperator/client/src/listener/dcamera_photo_surface_listener.cpp +++ b/services/cameraservice/cameraoperator/client/src/listener/dcamera_photo_surface_listener.cpp @@ -31,18 +31,22 @@ DCameraPhotoSurfaceListener::DCameraPhotoSurfaceListener(const sptr& su void DCameraPhotoSurfaceListener::OnBufferAvailable() { DHLOGI("DCameraPhotoSurfaceListener::OnBufferAvailable"); - OHOS::sptr buffer = nullptr; + if (callback_ == nullptr || surface_ == nullptr) { + DHLOGE("DCameraPhotoSurfaceListener ResultCallback or Surface is null"); + return; + } + int32_t flushFence = 0; int64_t timestamp = 0; OHOS::Rect damage; + OHOS::sptr buffer = nullptr; + surface_->AcquireBuffer(buffer, flushFence, timestamp, damage); + if (buffer == nullptr) { + DHLOGE("DCameraPhotoSurfaceListener AcquireBuffer failed"); + return; + } do { - surface_->AcquireBuffer(buffer, flushFence, timestamp, damage); - if (buffer == nullptr) { - DHLOGE("DCameraPhotoSurfaceListener AcquireBuffer failed"); - break; - } - char *address = static_cast(buffer->GetVirAddr()); int32_t size = static_cast(buffer->GetSize()); if ((address == nullptr) || (size <= 0)) { @@ -58,10 +62,6 @@ void DCameraPhotoSurfaceListener::OnBufferAvailable() break; } - if (callback_ == nullptr) { - DHLOGE("DCameraPhotoSurfaceListener ResultCallback is null"); - break; - } callback_->OnPhotoResult(dataBuffer); } while (0); surface_->ReleaseBuffer(buffer, -1); diff --git a/services/cameraservice/cameraoperator/client/src/listener/dcamera_photo_surface_listener_common.cpp b/services/cameraservice/cameraoperator/client/src/listener/dcamera_photo_surface_listener_common.cpp index 0fbb10ae..6e63d1b3 100644 --- a/services/cameraservice/cameraoperator/client/src/listener/dcamera_photo_surface_listener_common.cpp +++ b/services/cameraservice/cameraoperator/client/src/listener/dcamera_photo_surface_listener_common.cpp @@ -31,18 +31,22 @@ DCameraPhotoSurfaceListener::DCameraPhotoSurfaceListener(const sptr& su void DCameraPhotoSurfaceListener::OnBufferAvailable() { DHLOGI("DCameraPhotoSurfaceListenerCommon::OnBufferAvailable"); - OHOS::sptr buffer = nullptr; + if (callback_ == nullptr || surface_ == nullptr) { + DHLOGE("DCameraPhotoSurfaceListenerCommon ResultCallback or Surface is null"); + return; + } + int32_t flushFence = 0; int64_t timestamp = 0; OHOS::Rect damage; + OHOS::sptr buffer = nullptr; + surface_->AcquireBuffer(buffer, flushFence, timestamp, damage); + if (buffer == nullptr) { + DHLOGE("DCameraPhotoSurfaceListenerCommon AcquireBuffer failed"); + return; + } do { - surface_->AcquireBuffer(buffer, flushFence, timestamp, damage); - if (buffer == nullptr) { - DHLOGE("DCameraPhotoSurfaceListenerCommon AcquireBuffer failed"); - break; - } - int32_t size; buffer->ExtraGet("dataSize", size); if (size <= 0) { @@ -63,10 +67,6 @@ void DCameraPhotoSurfaceListener::OnBufferAvailable() break; } - if (callback_ == nullptr) { - DHLOGE("DCameraPhotoSurfaceListenerCommon ResultCallback is null"); - break; - } callback_->OnPhotoResult(dataBuffer); } while (0); surface_->ReleaseBuffer(buffer, -1); diff --git a/services/cameraservice/cameraoperator/client/src/listener/dcamera_video_surface_listener.cpp b/services/cameraservice/cameraoperator/client/src/listener/dcamera_video_surface_listener.cpp index b2c2f811..f5baa340 100644 --- a/services/cameraservice/cameraoperator/client/src/listener/dcamera_video_surface_listener.cpp +++ b/services/cameraservice/cameraoperator/client/src/listener/dcamera_video_surface_listener.cpp @@ -31,22 +31,26 @@ DCameraVideoSurfaceListener::DCameraVideoSurfaceListener(const sptr& su void DCameraVideoSurfaceListener::OnBufferAvailable() { DHLOGI("DCameraVideoSurfaceListener::OnBufferAvailable"); - OHOS::sptr buffer = nullptr; + if (callback_ == nullptr || surface_ == nullptr) { + DHLOGE("DCameraVideoSurfaceListener ResultCallback or Surface is null"); + return; + } + int32_t flushFence = 0; int64_t timestamp = 0; OHOS::Rect damage; + OHOS::sptr buffer = nullptr; + surface_->AcquireBuffer(buffer, flushFence, timestamp, damage); + if (buffer == nullptr) { + DHLOGE("DCameraVideoSurfaceListener AcquireBuffer failed"); + return; + } do { - surface_->AcquireBuffer(buffer, flushFence, timestamp, damage); - if (buffer == nullptr) { - DHLOGE("DCameraVideoSurfaceListener AcquireBuffer failed"); - break; - } - - char *address = static_cast(buffer->GetVirAddr()); - int32_t size = buffer->GetSize(); int32_t width = buffer->GetWidth(); int32_t height = buffer->GetHeight(); + int32_t size = buffer->GetSize(); + char *address = static_cast(buffer->GetVirAddr()); if ((address == nullptr) || (size <= 0) || (width <= 0) || (height <= 0)) { DHLOGE("DCameraVideoSurfaceListener invalid params, width: %d, height: %d, size: %d", width, height, size); break; @@ -63,10 +67,6 @@ void DCameraVideoSurfaceListener::OnBufferAvailable() break; } - if (callback_ == nullptr) { - DHLOGE("DCameraVideoSurfaceListener ResultCallback is null"); - break; - } callback_->OnVideoResult(dataBuffer); } while (0); surface_->ReleaseBuffer(buffer, -1); diff --git a/services/cameraservice/cameraoperator/client/src/listener/dcamera_video_surface_listener_common.cpp b/services/cameraservice/cameraoperator/client/src/listener/dcamera_video_surface_listener_common.cpp index b372140f..c793813a 100644 --- a/services/cameraservice/cameraoperator/client/src/listener/dcamera_video_surface_listener_common.cpp +++ b/services/cameraservice/cameraoperator/client/src/listener/dcamera_video_surface_listener_common.cpp @@ -31,18 +31,22 @@ DCameraVideoSurfaceListener::DCameraVideoSurfaceListener(const sptr& su void DCameraVideoSurfaceListener::OnBufferAvailable() { DHLOGI("DCameraVideoSurfaceListenerCommon::OnBufferAvailable"); - OHOS::sptr buffer = nullptr; + if (callback_ == nullptr || surface_ == nullptr) { + DHLOGE("DCameraVideoSurfaceListenerCommon ResultCallback or Surface is null"); + return; + } + int32_t flushFence = 0; int64_t timestamp = 0; OHOS::Rect damage; + OHOS::sptr buffer = nullptr; + surface_->AcquireBuffer(buffer, flushFence, timestamp, damage); + if (buffer == nullptr) { + DHLOGE("DCameraVideoSurfaceListenerCommon AcquireBuffer failed"); + return; + } do { - surface_->AcquireBuffer(buffer, flushFence, timestamp, damage); - if (buffer == nullptr) { - DHLOGE("DCameraVideoSurfaceListenerCommon AcquireBuffer failed"); - break; - } - int32_t width = buffer->GetWidth(); int32_t height = buffer->GetHeight(); int32_t size = width * height * 4; @@ -60,10 +64,6 @@ void DCameraVideoSurfaceListener::OnBufferAvailable() break; } - if (callback_ == nullptr) { - DHLOGE("DCameraVideoSurfaceListenerCommon ResultCallback is null"); - break; - } callback_->OnVideoResult(dataBuffer); } while (0); surface_->ReleaseBuffer(buffer, -1); diff --git a/services/cameraservice/cameraoperator/handler/include/dcamera_handler.h b/services/cameraservice/cameraoperator/handler/include/dcamera_handler.h index 0276319e..9faebc46 100644 --- a/services/cameraservice/cameraoperator/handler/include/dcamera_handler.h +++ b/services/cameraservice/cameraoperator/handler/include/dcamera_handler.h @@ -56,7 +56,7 @@ private: std::string GetCameraPosition(camera_position_enum_t position); void ConfigFormatAndResolution(ConfigInfo& info, Json::Value& outputFormat, Json::Value& resolution, std::vector& formatList, std::set& formatSet); - bool IsValid(DCStreamType type, CameraStandard::CameraPicSize& size); + bool IsValid(const DCStreamType type, const CameraStandard::CameraPicSize& size); sptr cameraManager_; std::shared_ptr pluginListener_; diff --git a/services/cameraservice/cameraoperator/handler/src/dcamera_handler.cpp b/services/cameraservice/cameraoperator/handler/src/dcamera_handler.cpp index 495323b9..742034f9 100644 --- a/services/cameraservice/cameraoperator/handler/src/dcamera_handler.cpp +++ b/services/cameraservice/cameraoperator/handler/src/dcamera_handler.cpp @@ -67,7 +67,7 @@ std::vector DCameraHandler::Query() (info->GetPosition() == OHOS_CAMERA_POSITION_BACK && info->GetCameraType() == OHOS_CAMERA_TYPE_LOGICAL)) { DHItem item = CreateDHItem(info); - itemList.push_back(item); + itemList.emplace_back(item); } } DHLOGI("DCameraHandler::Query success, get %d items", itemList.size()); @@ -121,7 +121,7 @@ std::vector DCameraHandler::GetCameras() (info->GetPosition() == OHOS_CAMERA_POSITION_BACK && info->GetCameraType() == OHOS_CAMERA_TYPE_LOGICAL)) { std::string dhId = CAMERA_ID_PREFIX + info->GetID(); - cameras.push_back(dhId); + cameras.emplace_back(dhId); } } DHLOGI("DCameraHandler::GetCameras success, get %d items", cameras.size()); @@ -231,7 +231,7 @@ void DCameraHandler::ConfigFormatAndResolution(ConfigInfo& info, Json::Value& ou } } -bool DCameraHandler::IsValid(DCStreamType type, CameraStandard::CameraPicSize& size) +bool DCameraHandler::IsValid(const DCStreamType type, const CameraStandard::CameraPicSize& size) { bool ret = false; switch (type) { diff --git a/services/cameraservice/cameraoperator/handler/src/dcamera_handler_common.cpp b/services/cameraservice/cameraoperator/handler/src/dcamera_handler_common.cpp index 009be60b..0c5c29ab 100644 --- a/services/cameraservice/cameraoperator/handler/src/dcamera_handler_common.cpp +++ b/services/cameraservice/cameraoperator/handler/src/dcamera_handler_common.cpp @@ -67,7 +67,7 @@ std::vector DCameraHandler::Query() (info->GetPosition() == OHOS_CAMERA_POSITION_BACK && info->GetCameraType() == OHOS_CAMERA_TYPE_LOGICAL)) { DHItem item = CreateDHItem(info); - itemList.push_back(item); + itemList.emplace_back(item); } } DHLOGI("DCameraHandlerCommon::Query success, get %d items", itemList.size()); @@ -121,7 +121,7 @@ std::vector DCameraHandler::GetCameras() (info->GetPosition() == OHOS_CAMERA_POSITION_BACK && info->GetCameraType() == OHOS_CAMERA_TYPE_LOGICAL)) { std::string dhId = CAMERA_ID_PREFIX + info->GetID(); - cameras.push_back(dhId); + cameras.emplace_back(dhId); } } DHLOGI("DCameraHandlerCommon::GetCameras success, get %d items", cameras.size()); @@ -151,17 +151,17 @@ DHItem DCameraHandler::CreateDHItem(sptr& info) std::set formatSet; std::vector videoFormats; - videoFormats.push_back(camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888); + videoFormats.emplace_back(camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888); ConfigInfo videoConfig = {CONTINUOUS_FRAME, CAMERA_FORMAT_VIDEO, cameraInput}; ConfigFormatAndResolution(videoConfig, outputFormat, resolution, videoFormats, formatSet); std::vector previewFormats; - previewFormats.push_back(camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888); + previewFormats.emplace_back(camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888); ConfigInfo previewInfo = {CONTINUOUS_FRAME, CAMERA_FORMAT_PREVIEW, cameraInput}; ConfigFormatAndResolution(previewInfo, outputFormat, resolution, previewFormats, formatSet); std::vector photoFormats; - photoFormats.push_back(camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888); + photoFormats.emplace_back(camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888); ConfigInfo photoConfig = {SNAPSHOT_FRAME, CAMERA_FORMAT_PHOTO, cameraInput}; ConfigFormatAndResolution(photoConfig, outputFormat, resolution, photoFormats, formatSet); @@ -218,7 +218,7 @@ void DCameraHandler::ConfigFormatAndResolution(ConfigInfo& info, Json::Value& ou } } -bool DCameraHandler::IsValid(DCStreamType type, CameraStandard::CameraPicSize& size) +bool DCameraHandler::IsValid(const DCStreamType type, const CameraStandard::CameraPicSize& size) { bool ret = false; switch (type) { -- Gitee