From c271d61b5507017521ee1dffaba86f95d25a52e9 Mon Sep 17 00:00:00 2001 From: Veeresh Kadasani Date: Thu, 9 Jun 2022 23:46:13 +0530 Subject: [PATCH] Implement power manager requirement events Signed-off-by: Veeresh Kadasani --- frameworks/native/camera/BUILD.gn | 3 + .../native/camera/src/input/camera_input.cpp | 11 +++- .../camera/src/input/camera_manager.cpp | 29 +++++++++ .../camera/src/session/capture_session.cpp | 60 ++++++++++++++++++- hisysevent.yaml | 29 +++++++++ .../camera/include/session/capture_session.h | 2 +- services/camera_service/include/camera_log.h | 41 +++++++++++++ 7 files changed, 172 insertions(+), 3 deletions(-) diff --git a/frameworks/native/camera/BUILD.gn b/frameworks/native/camera/BUILD.gn index e035856..d414fc9 100644 --- a/frameworks/native/camera/BUILD.gn +++ b/frameworks/native/camera/BUILD.gn @@ -60,6 +60,9 @@ ohos_shared_library("camera_framework") { ] external_deps = [ + "ability_base:want", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", "hisysevent_native:libhisysevent", "hitrace_native:hitrace_meter", "hiviewdfx_hilog_native:libhilog", diff --git a/frameworks/native/camera/src/input/camera_input.cpp b/frameworks/native/camera/src/input/camera_input.cpp index b82d3fa..1662583 100644 --- a/frameworks/native/camera/src/input/camera_input.cpp +++ b/frameworks/native/camera/src/input/camera_input.cpp @@ -21,6 +21,7 @@ #include "camera_device_ability_items.h" #include "camera_util.h" #include "hcamera_device_callback_stub.h" +#include "ipc_skeleton.h" #include "camera_log.h" #include "metadata_utils.h" @@ -70,6 +71,8 @@ public: item.data.u8[0]); CAMERA_SYSEVENT_BEHAVIOR(CreateMsg("FlashStateChanged! current OHOS_CONTROL_FLASH_STATE is %d", item.data.u8[0])); + POWERMGR_SYSEVENT_TORCH_STATE(IPCSkeleton::GetCallingPid(), + IPCSkeleton::GetCallingUid(), item.data.u8[0]); } ret = Camera::FindCameraMetadataItem(result->get(), OHOS_CONTROL_FLASH_MODE, &item); if (ret == 0) { @@ -829,8 +832,14 @@ void CameraInput::SetFlashMode(camera_flash_mode_enum_t flashMode) if (!status) { MEDIA_ERR_LOG("CameraInput::SetFlashMode Failed to set flash mode"); + return; + } + + if (flashMode == OHOS_CAMERA_FLASH_MODE_CLOSE) { + POWERMGR_SYSEVENT_FLASH_OFF(); + } else { + POWERMGR_SYSEVENT_FLASH_ON(); } - return; } void CameraInput::SetErrorCallback(std::shared_ptr errorCallback) diff --git a/frameworks/native/camera/src/input/camera_manager.cpp b/frameworks/native/camera/src/input/camera_manager.cpp index b05dc4d..73af0f5 100644 --- a/frameworks/native/camera/src/input/camera_manager.cpp +++ b/frameworks/native/camera/src/input/camera_manager.cpp @@ -105,6 +105,8 @@ public: CAMERA_SYSEVENT_BEHAVIOR(CreateMsg("OnFlashlightStatusChanged! for cameraId:%s, current Flash Status:%d", cameraId.c_str(), status)); + POWERMGR_SYSEVENT_TORCH_STATE(IPCSkeleton::GetCallingPid(), + IPCSkeleton::GetCallingUid(), status); MEDIA_INFO_LOG("OnFlashlightStatusChanged: cameraId: %{public}s, status: %{public}d", cameraId.c_str(), status); if (camMngr_ != nullptr && camMngr_->GetApplicationCallback() != nullptr) { @@ -173,6 +175,9 @@ sptr CameraManager::CreatePhotoOutput(sptr &surface) result = new(std::nothrow) PhotoOutput(streamCapture); if (result == nullptr) { MEDIA_ERR_LOG("Failed to new PhotoOutput "); + } else { + POWERMGR_SYSEVENT_CAMERA_CONFIG(PHOTO, surface->GetDefaultWidth(), + surface->GetDefaultHeight()); } } else { MEDIA_ERR_LOG("Failed to get stream capture object from hcamera service!, %{public}d", retCode); @@ -196,6 +201,10 @@ sptr CameraManager::CreatePhotoOutput(const sptrGetDefaultWidth(), + producer->GetDefaultHeight()); } } else { MEDIA_ERR_LOG("Failed to get stream capture object from hcamera service!, %{public}d", retCode); @@ -220,6 +229,10 @@ sptr CameraManager::CreatePreviewOutput(sptr surface) result = new(std::nothrow) PreviewOutput(streamRepeat); if (result == nullptr) { MEDIA_ERR_LOG("Failed to new PreviewOutput"); + } else { + POWERMGR_SYSEVENT_CAMERA_CONFIG(PREVIEW, + surface->GetDefaultWidth(), + surface->GetDefaultHeight()); } } else { MEDIA_ERR_LOG("PreviewOutput: Failed to get stream repeat object from hcamera service!, %{public}d", retCode); @@ -243,6 +256,10 @@ sptr CameraManager::CreatePreviewOutput(const sptrGetDefaultWidth(), + producer->GetDefaultHeight()); } } else { MEDIA_ERR_LOG("PreviewOutput: Failed to get stream repeat object from hcamera service!, %{public}d", retCode); @@ -268,6 +285,8 @@ sptr CameraManager::CreateCustomPreviewOutput(sptr surfa result = new(std::nothrow) PreviewOutput(streamRepeat); if (result == nullptr) { MEDIA_ERR_LOG("Failed to new PreviewOutput"); + } else { + POWERMGR_SYSEVENT_CAMERA_CONFIG(PREVIEW, width, height); } } else { MEDIA_ERR_LOG("PreviewOutput: Failed to get stream repeat object from hcamera service!, %{public}d", retCode); @@ -292,6 +311,8 @@ sptr CameraManager::CreateCustomPreviewOutput(const sptr CameraManager::CreateVideoOutput(sptr &surface) result = new(std::nothrow) VideoOutput(streamRepeat); if (result == nullptr) { MEDIA_ERR_LOG("Failed to new VideoOutput"); + } else { + POWERMGR_SYSEVENT_CAMERA_CONFIG(VIDEO, + surface->GetDefaultWidth(), + surface->GetDefaultHeight()); } } else { MEDIA_ERR_LOG("VideoOutpout: Failed to get stream repeat object from hcamera service! %{public}d", retCode); @@ -339,6 +364,10 @@ sptr CameraManager::CreateVideoOutput(const sptrGetDefaultWidth(), + producer->GetDefaultHeight()); } } else { MEDIA_ERR_LOG("VideoOutpout: Failed to get stream repeat object from hcamera service! %{public}d", retCode); diff --git a/frameworks/native/camera/src/session/capture_session.cpp b/frameworks/native/camera/src/session/capture_session.cpp index a07ec09..c7249d4 100644 --- a/frameworks/native/camera/src/session/capture_session.cpp +++ b/frameworks/native/camera/src/session/capture_session.cpp @@ -17,12 +17,18 @@ #include "camera_util.h" #include "hcapture_session_callback_stub.h" #include "input/camera_input.h" +#include "ipc_skeleton.h" #include "camera_log.h" #include "output/photo_output.h" #include "output/preview_output.h" #include "output/video_output.h" +#include "bundle_mgr_interface.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" using namespace std; +using namespace OHOS::AppExecFwk; +using namespace OHOS::AAFwk; namespace OHOS { namespace CameraStandard { @@ -54,12 +60,50 @@ public: } }; +static std::string GetClientBundle(int uid) +{ + std::string bundleName = ""; + auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgr == nullptr) { + MEDIA_ERR_LOG("Get ability manager failed"); + return bundleName; + } + + sptr object = samgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (object == nullptr) { + MEDIA_DEBUG_LOG("object is NULL."); + return bundleName; + } + + sptr bms = iface_cast(object); + if (bms == nullptr) { + MEDIA_DEBUG_LOG("bundle manager service is NULL."); + return bundleName; + } + + auto result = bms->GetBundleNameForUid(uid, bundleName); + if (!result) { + MEDIA_ERR_LOG("GetBundleNameForUid fail"); + return ""; + } + MEDIA_INFO_LOG("bundle name is %{public}s ", bundleName.c_str()); + + return bundleName; +} + CaptureSession::CaptureSession(sptr &captureSession) { captureSession_ = captureSession; inputDevice_ = nullptr; } +CaptureSession::~CaptureSession() +{ + if (inputDevice_ != nullptr) { + inputDevice_ = nullptr; + } +} + int32_t CaptureSession::BeginConfig() { CAMERA_SYNC_TRACE; @@ -69,6 +113,14 @@ int32_t CaptureSession::BeginConfig() int32_t CaptureSession::CommitConfig() { CAMERA_SYNC_TRACE; + if (inputDevice_ != nullptr) { + int32_t pid = IPCSkeleton::GetCallingPid(); + int32_t uid = IPCSkeleton::GetCallingUid(); + POWERMGR_SYSEVENT_CAMERA_CONNECT(pid, uid, + inputDevice_->GetCameraDeviceInfo()->GetID().c_str(), + GetClientBundle(uid)); + } + return captureSession_->CommitConfig(); } @@ -104,6 +156,9 @@ int32_t CaptureSession::RemoveInput(sptr &input) return CAMERA_INVALID_ARG; } CAMERA_SYSEVENT_STATISTIC(CreateMsg("CaptureSession::RemoveInput")); + if (inputDevice_ != nullptr) { + inputDevice_ = nullptr; + } return captureSession_->RemoveInput(((sptr &)input)->GetCameraDevice()); } @@ -161,11 +216,14 @@ std::shared_ptr CaptureSession::GetApplicationCallback() void CaptureSession::Release() { CAMERA_SYNC_TRACE; + if (inputDevice_ != nullptr) { + POWERMGR_SYSEVENT_CAMERA_DISCONNECT(inputDevice_->GetCameraDeviceInfo()->GetID().c_str()); + inputDevice_ = nullptr; + } int32_t errCode = captureSession_->Release(0); if (errCode != CAMERA_OK) { MEDIA_ERR_LOG("Failed to Release capture session!, %{public}d", errCode); } - return; } } // CameraStandard } // OHOS diff --git a/hisysevent.yaml b/hisysevent.yaml index b692d5e..b7b0f7d 100644 --- a/hisysevent.yaml +++ b/hisysevent.yaml @@ -24,3 +24,32 @@ CAMERA_STATE: CAMERA_STATISTIC: __BASE: {type: STATISTIC, level: MINOR, desc: camera stats} MSG: {type: STRING, desc: message} + +CAMERA_CONNECT: + __BASE: {type: STATISTIC, level: MINOR, tag: PowerStats, desc: camera connect} + PID: {type: INT32, desc: pid} + UID: {type: INT32, desc: uid} + ID: {type: STRING, desc: camera id} + NAME: {type: STRING, desc: package name} + +CAMERA_DISCONNECT: + __BASE: {type: STATISTIC, level: MINOR, tag: PowerStats, desc: camera disconnect} + ID: {type: STRING, desc: camera id} + +TORCH_STATE: + __BASE: {type: STATISTIC, level: MINOR, tag: PowerStats, desc: torch state} + PID: {type: INT32, desc: pid} + UID: {type: INT32, desc: uid} + STATE: {type: INT32, desc: status} + +CAMERA_CONFIG: + __BASE: {type: STATISTIC, level: MINOR, tag: PowerStats, desc: camera config} + TYPE: {type: STRING, desc: stream type} + WIDTH: {type: INT32, desc: image width} + HEIGHT: {type: INT32, desc: image height} + +FLASHLIGHT_ON: + __BASE: {type: STATISTIC, level: MINOR, tag: PowerStats, desc: flashlight on} + +FLASHLIGHT_OFF: + __BASE: {type: STATISTIC, level: MINOR, tag: PowerStats, desc: flashlight off} diff --git a/interfaces/inner_api/native/camera/include/session/capture_session.h b/interfaces/inner_api/native/camera/include/session/capture_session.h index 0256e9f..7c2b3a8 100644 --- a/interfaces/inner_api/native/camera/include/session/capture_session.h +++ b/interfaces/inner_api/native/camera/include/session/capture_session.h @@ -41,7 +41,7 @@ class CaptureSession : public RefBase { public: sptr inputDevice_; explicit CaptureSession(sptr &captureSession); - ~CaptureSession() {} + ~CaptureSession(); /** * @brief Begin the capture session config. diff --git a/services/camera_service/include/camera_log.h b/services/camera_service/include/camera_log.h index f168b1e..1481db1 100644 --- a/services/camera_service/include/camera_log.h +++ b/services/camera_service/include/camera_log.h @@ -102,6 +102,47 @@ "MSG", str); \ } while (0) +#define POWERMGR_SYSEVENT_CAMERA_CONNECT(pid, uid, camid, name) \ + do { \ + HiviewDFX::HiSysEvent::Write("CAMERA", "CAMERA_CONNECT", \ + HiviewDFX::HiSysEvent::EventType::STATISTIC, \ + "PID", pid, "UID", uid, "ID", camid, \ + "NAME", name); \ + } while (0) + +#define POWERMGR_SYSEVENT_CAMERA_DISCONNECT(camid) \ + do { \ + HiviewDFX::HiSysEvent::Write("CAMERA", "CAMERA_DISCONNECT", \ + HiviewDFX::HiSysEvent::EventType::STATISTIC, \ + "ID", camid); \ + } while (0) + +#define POWERMGR_SYSEVENT_TORCH_STATE(pid, uid, status) \ + do { \ + HiviewDFX::HiSysEvent::Write("CAMERA", "TORCH_STATE", \ + HiviewDFX::HiSysEvent::EventType::STATISTIC, \ + "PID", pid, "UID", uid, "STATE", status); \ + } while (0) + +#define POWERMGR_SYSEVENT_CAMERA_CONFIG(type, width, height) \ + do { \ + HiviewDFX::HiSysEvent::Write("CAMERA", "CAMERA_CONFIG", \ + HiviewDFX::HiSysEvent::EventType::STATISTIC, \ + "TYPE", #type, "WIDTH", width, "HEIGHT", height); \ + } while (0) + +#define POWERMGR_SYSEVENT_FLASH_ON() \ + do { \ + HiviewDFX::HiSysEvent::Write("CAMERA", "FLASHLIGHT_ON", \ + HiviewDFX::HiSysEvent::EventType::STATISTIC); \ + } while (0) + +#define POWERMGR_SYSEVENT_FLASH_OFF() \ + do { \ + HiviewDFX::HiSysEvent::Write("CAMERA", "FLASHLIGHT_OFF", \ + HiviewDFX::HiSysEvent::EventType::STATISTIC); \ + } while (0) + #define CAMERA_START_ASYNC_TRACE(str, taskId) \ do { \ StartAsyncTrace(HITRACE_TAG_ZCAMERA, str, taskId, -1); \