From 36db7cca18e9bc445dba824859e5778f53c5e116 Mon Sep 17 00:00:00 2001 From: zourongchun Date: Sat, 16 Sep 2023 11:48:47 +0800 Subject: [PATCH] add camera status callback Signed-off-by: zourongchun --- media/capture/BUILD.gn | 2 ++ .../video/ohos/ohos_capture_delegate.cc | 2 +- .../ohos/video_capture_device_factory_ohos.cc | 23 +++++++++++- .../ohos/video_capture_device_factory_ohos.h | 2 ++ ...o_capture_status_callback_listener_ohos.cc | 29 +++++++++++++++ ...eo_capture_status_callback_listener_ohos.h | 35 +++++++++++++++++++ ...eo_capture_surface_buffer_listener_ohos.cc | 4 +++ ohos_nweb/src/nweb_impl.cc | 23 ++---------- 8 files changed, 97 insertions(+), 23 deletions(-) create mode 100644 media/capture/video/ohos/video_capture_status_callback_listener_ohos.cc create mode 100644 media/capture/video/ohos/video_capture_status_callback_listener_ohos.h diff --git a/media/capture/BUILD.gn b/media/capture/BUILD.gn index 7f6a9fa1d4..611060816b 100644 --- a/media/capture/BUILD.gn +++ b/media/capture/BUILD.gn @@ -275,6 +275,8 @@ component("capture_lib") { "video/ohos/ohos_capture_delegate.h", "video/ohos/video_capture_common_ohos.cc", "video/ohos/video_capture_common_ohos.h", + "video/ohos/video_capture_status_callback_listener_ohos.cc", + "video/ohos/video_capture_status_callback_listener_ohos.h", ] } diff --git a/media/capture/video/ohos/ohos_capture_delegate.cc b/media/capture/video/ohos/ohos_capture_delegate.cc index 827b2a2d54..4c5de2a6f0 100644 --- a/media/capture/video/ohos/ohos_capture_delegate.cc +++ b/media/capture/video/ohos/ohos_capture_delegate.cc @@ -321,7 +321,7 @@ bool OHOSCaptureDelegate::StartStream() { DCHECK(!is_capturing_); if (OhosAdapterHelper::GetInstance() .GetCameraManagerAdapter() - .GetCameraStatus() != CameraStatus::CLOSED) { + .GetCameraStatus() == CameraStatusAdapter::UNAVAILABLE) { LOG(ERROR) << "camera is not closed"; return false; } diff --git a/media/capture/video/ohos/video_capture_device_factory_ohos.cc b/media/capture/video/ohos/video_capture_device_factory_ohos.cc index 36b50a1d8d..ffc140fe1c 100644 --- a/media/capture/video/ohos/video_capture_device_factory_ohos.cc +++ b/media/capture/video/ohos/video_capture_device_factory_ohos.cc @@ -19,9 +19,11 @@ #include "base/strings/string_util.h" #include "base/strings/stringprintf.h" #include "build/build_config.h" +#include "camera_manager_adapter.h" #include "media/capture/video/ohos/video_capture_device_ohos.h" #include "ohos_adapter_helper.h" #include "video_capture_common_ohos.h" +#include "video_capture_status_callback_listener_ohos.h" namespace media { @@ -36,7 +38,10 @@ bool CompareCaptureDevices(const VideoCaptureDeviceInfo& a, VideoCaptureDeviceFactoryOHOS::VideoCaptureDeviceFactoryOHOS( scoped_refptr ui_task_runner) : ui_task_runner_(ui_task_runner) { - OhosAdapterHelper::GetInstance().GetCameraManagerAdapter().Create(); + auto status_callback = + std::make_shared + (ui_task_runner_, weak_factory_.GetWeakPtr()); + OhosAdapterHelper::GetInstance().GetCameraManagerAdapter().Create(status_callback); } VideoCaptureDeviceFactoryOHOS::~VideoCaptureDeviceFactoryOHOS() { @@ -101,4 +106,20 @@ void VideoCaptureDeviceFactoryOHOS::GetDevicesInfo( std::move(callback).Run(std::move(devices_info)); } +void VideoCaptureDeviceFactoryOHOS::OnCameraStatusChanged( + CameraStatusAdapter camera_status) { + DCHECK(thread_checker_.CalledOnValidThread()); + LOG(INFO) << "camera status changed"; + if (camera_status == CameraStatusAdapter::AVAILABLE) { + OhosAdapterHelper::GetInstance().GetCameraManagerAdapter(). + SetCameraStatus(CameraStatusAdapter::AVAILABLE); + if (!OhosAdapterHelper::GetInstance().GetCameraManagerAdapter(). + IsExistCaptureTask()) { + return; + } + + OhosAdapterHelper::GetInstance().GetCameraManagerAdapter(). + RestartSession(); + } +} } // namespace media diff --git a/media/capture/video/ohos/video_capture_device_factory_ohos.h b/media/capture/video/ohos/video_capture_device_factory_ohos.h index 0580eb1652..611ff874ea 100644 --- a/media/capture/video/ohos/video_capture_device_factory_ohos.h +++ b/media/capture/video/ohos/video_capture_device_factory_ohos.h @@ -37,10 +37,12 @@ class CAPTURE_EXPORT VideoCaptureDeviceFactoryOHOS VideoCaptureErrorOrDevice CreateDevice( const VideoCaptureDeviceDescriptor& device_descriptor) override; void GetDevicesInfo(GetDevicesInfoCallback callback) override; + void OnCameraStatusChanged(CameraStatusAdapter camera_status); private: scoped_refptr ui_task_runner_; std::shared_ptr camera_manager_adapter_; + base::WeakPtrFactory weak_factory_{this}; }; } // namespace media diff --git a/media/capture/video/ohos/video_capture_status_callback_listener_ohos.cc b/media/capture/video/ohos/video_capture_status_callback_listener_ohos.cc new file mode 100644 index 0000000000..ee21e48593 --- /dev/null +++ b/media/capture/video/ohos/video_capture_status_callback_listener_ohos.cc @@ -0,0 +1,29 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "media/capture/video/ohos/video_capture_device_factory_ohos.h" +#include "video_capture_status_callback_listener_ohos.h" +#include "base/logging.h" +#include "ohos_adapter_helper.h" +namespace media { + +VideoCaptureCameraStatusCallbackListenerOHOS::VideoCaptureCameraStatusCallbackListenerOHOS( + const scoped_refptr& task_runner, + base::WeakPtr capture_device_factory) + : task_runner_(task_runner), capture_device_factory_(capture_device_factory) { + DCHECK(task_runner_.get()); + DCHECK(capture_device_factory_); +} + +VideoCaptureCameraStatusCallbackListenerOHOS::~VideoCaptureCameraStatusCallbackListenerOHOS() {} + +void VideoCaptureCameraStatusCallbackListenerOHOS::OnCameraStatusChanged( + CameraStatusAdapter camera_status) { + task_runner_->PostTask( + FROM_HERE, + base::BindOnce(&VideoCaptureDeviceFactoryOHOS::OnCameraStatusChanged, capture_device_factory_, + camera_status)); +} + +} // namespace media diff --git a/media/capture/video/ohos/video_capture_status_callback_listener_ohos.h b/media/capture/video/ohos/video_capture_status_callback_listener_ohos.h new file mode 100644 index 0000000000..b28bd7e3d6 --- /dev/null +++ b/media/capture/video/ohos/video_capture_status_callback_listener_ohos.h @@ -0,0 +1,35 @@ +// Copyright 2023 The Huawei Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef VIDEO_CAPTURE_STATUS_CALLBACK_LISTENER_OHOS_H_ +#define VIDEO_CAPTURE_STATUS_CALLBACK_LISTENER_OHOS_H_ + +#include "base/logging.h" +#include "ohos_capture_delegate.h" +#include "video_capture_device_ohos.h" +namespace media { + +class VideoCaptureCameraStatusCallbackListenerOHOS + : public OHOS::NWeb::CameraStatusCallbackAdapter { + public: + VideoCaptureCameraStatusCallbackListenerOHOS( + const scoped_refptr& task_runner, + base::WeakPtr capture_device_factory); + + VideoCaptureCameraStatusCallbackListenerOHOS( + const VideoCaptureCameraStatusCallbackListenerOHOS&) = delete; + VideoCaptureCameraStatusCallbackListenerOHOS& operator=( + const VideoCaptureCameraStatusCallbackListenerOHOS&) = delete; + + virtual ~VideoCaptureCameraStatusCallbackListenerOHOS(); + void OnCameraStatusChanged(CameraStatusAdapter camera_status) override; + + private: + scoped_refptr task_runner_; + base::WeakPtr capture_device_factory_; +}; + +} // namespace media + +#endif // VIDEO_CAPTURE_STATUS_CALLBACK_LISTENER_OHOS_H_ diff --git a/media/capture/video/ohos/video_capture_surface_buffer_listener_ohos.cc b/media/capture/video/ohos/video_capture_surface_buffer_listener_ohos.cc index 9c16741fd2..ec436e8085 100644 --- a/media/capture/video/ohos/video_capture_surface_buffer_listener_ohos.cc +++ b/media/capture/video/ohos/video_capture_surface_buffer_listener_ohos.cc @@ -1,3 +1,7 @@ +// Copyright (c) 2023 Huawei Device Co., Ltd. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + #include "video_capture_surface_buffer_listener_ohos.h" #include "base/logging.h" #include "ohos_adapter_helper.h" diff --git a/ohos_nweb/src/nweb_impl.cc b/ohos_nweb/src/nweb_impl.cc index b38953a4b1..6cdc4b0a0b 100755 --- a/ohos_nweb/src/nweb_impl.cc +++ b/ohos_nweb/src/nweb_impl.cc @@ -653,35 +653,16 @@ void NWebImpl::OnUnoccluded() const { } void NWebImpl::StopCameraSession() const { - if (!OhosAdapterHelper::GetInstance() - .GetCameraManagerAdapter() - .IsExistCaptureTask()) { - return; - } - if (OhosAdapterHelper::GetInstance() - .GetCameraManagerAdapter() - .GetCameraStatus() == - CameraStatus::CLOSED) { - LOG(ERROR) << "camera is closed, no need to stop session"; - return; - } - WVLOG_I("StopSession"); - OhosAdapterHelper::GetInstance().GetCameraManagerAdapter().StopSession(CameraStopType::TO_BACK); + OhosAdapterHelper::GetInstance().GetCameraManagerAdapter().SetForegroundFlag(false); } void NWebImpl::RestartCameraSession() const { + OhosAdapterHelper::GetInstance().GetCameraManagerAdapter().SetForegroundFlag(true); if (!OhosAdapterHelper::GetInstance() .GetCameraManagerAdapter() .IsExistCaptureTask()) { return; } - if (OhosAdapterHelper::GetInstance() - .GetCameraManagerAdapter() - .GetCameraStatus() != - CameraStatus::CLOSED) { - LOG(ERROR) << "camera is closed, no need to restart session"; - return; - } WVLOG_I("RestartSession"); OhosAdapterHelper::GetInstance().GetCameraManagerAdapter().RestartSession(); } -- Gitee