diff --git a/services/cameraservice/cameraoperator/client/include/dcamera_client.h b/services/cameraservice/cameraoperator/client/include/dcamera_client.h index f97d2ee431b14afdca13bc0b3b61887a6e0355e6..1eb348ababa422df60eb2596693af7776ec3af0b 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 5f50dc95a392a9660a534d60777f9d621bb58fa1..9458c7f387bcc67f816aa6aab084bc37a3be3167 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 2a026d4f32b5e31e54a45f662cd4a427eafe726e..194e14f5f1d216f1fc2e9be34aa44c4b9a486a5d 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 29cbed9dabf567a51cbbb6f3267e3bf7f974bac7..e3dd5e565fa7256baa364561be1d10d0e153c69c 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 0fbb10aede44447066f852027635419127c5bb57..6e63d1b3f21b90f02c215311c0b3bc66a287aaeb 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 b2c2f8113ad6103d7abf5c008a476a73d7ffbb6c..f5baa34036c008c435ddea764d459b55b1fbf301 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 b372140ffff91cad8947a8370e7a03cbcb44e766..c793813a6c33ee34fab575edec4d80684d362f8a 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 0276319eafdee27f7f8862f6b2dd85273d028e95..9faebc4629d46425ff9c21ff4a3c3d856aebd626 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 495323b98f02050454b7699e75148886fd14efb0..742034f9540aac74c8e47363c6d10619fbd31041 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 009be60b8e838e748f3d77d0f9605c6712f51d3d..0c5c29ab2e87a3b30131a208874ac35dbe1e0a08 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) {