From 324d67518d52ed9554ffdec012df3d5b69ab8bd3 Mon Sep 17 00:00:00 2001 From: pengyueqing Date: Thu, 23 May 2024 17:19:06 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E7=9B=B8=E6=9C=BA=E6=96=B0=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=BF=AE=E6=94=B9=EF=BC=8C=E5=88=A0=E9=99=A4xcomponen?= =?UTF-8?q?t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/cpp/napi_init.cpp | 2 -- .../main/cpp/webRTC_adapter/ohos_camera.cpp | 36 ------------------- .../src/main/cpp/webRTC_adapter/ohos_camera.h | 2 -- 3 files changed, 40 deletions(-) diff --git a/entry/src/main/cpp/napi_init.cpp b/entry/src/main/cpp/napi_init.cpp index 6d5b4f35..14186102 100644 --- a/entry/src/main/cpp/napi_init.cpp +++ b/entry/src/main/cpp/napi_init.cpp @@ -31,7 +31,6 @@ rtc::scoped_refptr ohos_cts; static napi_value InitCamera(napi_env env, napi_callback_info info){ - webrtc::ohos::OhosCamera::Init(env, info); ohos_cts = webrtc::ohos::CapturerTrackSource::Create(); OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "had been try create"); @@ -105,7 +104,6 @@ static napi_value PeerClientDisConnectServer(napi_env env, napi_callback_info in static napi_value PeerClientConnectPeer(napi_env env, napi_callback_info info) { OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "PeerClientConnectPeer"); - webrtc::ohos::OhosCamera::Init(env, info); if (!GetPeerConnect()) { PeerSamplePostEvent(PEER_EVENT_CONNECT_PEER); diff --git a/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp b/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp index f72774d9..98e59c27 100644 --- a/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp +++ b/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp @@ -171,33 +171,12 @@ void ImageReceiverCallback(OH_ImageReceiverNative* receiver) { } } -char* OhosCamera::x_component_surfaceId_ = nullptr; OhosCamera::OhosCamera() { InitImageReceiver(); InitCamera(); GlobalOHOSCamera::GetInstance().SetOhosCamera(this); } -bool OhosCamera::Init(napi_env env, napi_callback_info info){ - size_t argc = 2; - // 声明参数数组 - napi_value args[2] = {nullptr}; - - // 获取传入的参数并依次放入参数数组中 - napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); - - size_t result; - napi_get_value_string_utf8(env, args[1], nullptr, 0, &result); - if (result > 0) { - x_component_surfaceId_ = new char[result + 1]; - napi_get_value_string_utf8(env, args[1], x_component_surfaceId_, result + 1, &result); - OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "x_component_surfaceId_ = %{public}s", - x_component_surfaceId_); - } - - return true; -} - int32_t OhosCamera::InitImageReceiver() { int32_t returnCode = -1; ImageReceiverOption imageReceiverOption(640, 480, 8); @@ -302,16 +281,6 @@ int32_t OhosCamera::PreviewOutputCreate() { return -1; } - if (x_component_surfaceId_ != nullptr) { - ret = OH_CameraManager_CreatePreviewOutput(camera_manager_, preview_profile_, x_component_surfaceId_, - &x_component_preview_); - if (preview_output_ == nullptr || ret != CAMERA_OK) { - OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", - "OH_CameraManager_CreatePreviewOutput failed, ret = %{public}d", ret); - return -1; - } - } - return 0; } @@ -629,11 +598,6 @@ OhosCamera::~OhosCamera() { img_receive_surfaceId_ = nullptr; } - if (x_component_preview_ != nullptr) { - delete[] x_component_surfaceId_; - x_component_surfaceId_ = nullptr; - } - camera_dev_index_ = 0; profile_index_ = 0; } diff --git a/entry/src/main/cpp/webRTC_adapter/ohos_camera.h b/entry/src/main/cpp/webRTC_adapter/ohos_camera.h index 64508f5a..44e50d7d 100644 --- a/entry/src/main/cpp/webRTC_adapter/ohos_camera.h +++ b/entry/src/main/cpp/webRTC_adapter/ohos_camera.h @@ -31,7 +31,6 @@ namespace ohos{ class OhosCamera { public: OhosCamera(); - static bool Init(napi_env env, napi_callback_info info); int32_t InitCamera(); int32_t InitImageReceiver(); int32_t StartCamera(); @@ -66,7 +65,6 @@ private: webrtc::VideoCaptureCapability configured_capability_; bool is_camera_started_ {false}; char* img_receive_surfaceId_ {nullptr}; - static char* x_component_surfaceId_; OH_ImageReceiverNative *imageReceiverNative_ {nullptr}; Camera_Manager* camera_manager_ {nullptr}; Camera_Device* cameras_ {nullptr}; -- Gitee From 7854c9dc442a46be5b6780bfbe4d80c9a419a645 Mon Sep 17 00:00:00 2001 From: pengyueqing Date: Thu, 23 May 2024 18:30:48 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E7=9B=B8=E6=9C=BA=E6=96=B0=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=BF=AE=E6=94=B9=EF=BC=8C=E6=B7=BB=E5=8A=A0OhosImage?= =?UTF-8?q?ReveiveOn=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/cpp/webRTC_adapter/ohos_camera.cpp | 38 ++++++------------- .../src/main/cpp/webRTC_adapter/ohos_camera.h | 20 ++++++---- 2 files changed, 24 insertions(+), 34 deletions(-) diff --git a/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp b/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp index 98e59c27..5a6a83d4 100644 --- a/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp +++ b/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp @@ -86,7 +86,7 @@ private: OhosCamera* ohosCamera_ {nullptr}; }; -void ImageReceiverCallback(OH_ImageReceiverNative* receiver) { +int32_t OhosCamera::ImageReceiverCallback(OH_ImageReceiverNative* receiver) { OhosCamera* ohosCamera = GlobalOHOSCamera::GetInstance().GetOhosCamera(); if (ohosCamera == nullptr) { return; @@ -177,7 +177,7 @@ OhosCamera::OhosCamera() { GlobalOHOSCamera::GetInstance().SetOhosCamera(this); } -int32_t OhosCamera::InitImageReceiver() { +int32_t OhosImageReveiveOn::InitImageReceiver() { int32_t returnCode = -1; ImageReceiverOption imageReceiverOption(640, 480, 8); Image_ErrorCode ret = OH_ImageReceiverNative_Create(imageReceiverOption.GetImageReceiverOptions(), &imageReceiverNative_); @@ -188,17 +188,18 @@ int32_t OhosCamera::InitImageReceiver() { ret = OH_ImageReceiverNative_On(imageReceiverNative_, ImageReceiverCallback); if (ret != IMAGE_SUCCESS) { - OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OH_ImageReceiverNative_On failed with code = %{public}d", - ret); + OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", + "OH_ImageReceiverNative_On failed with code = %{public}d", ret); return returnCode; } - uint64_t id; - ret = OH_ImageReceiverNative_GetReceivingSurfaceId(imageReceiverNative_, &id); - std::string idStr = std::to_string(id); - img_receive_surfaceId_ = new char[idStr.length() + 1]; - std::strcpy(img_receive_surfaceId_, idStr.c_str()); - + ret = OH_ImageReceiverNative_GetReceivingSurfaceId(imageReceiverNative_, &imageReceiverID_); + if (ret != IMAGE_SUCCESS) { + OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", + "OH_ImageReceiverNative_On failed with code = %{public}d", ret); + return returnCode; + } + returnCode = 0; return returnCode; } @@ -293,15 +294,7 @@ int32_t OhosCamera::PreviewOutputRelease() { } } - if (x_component_preview_ != nullptr) { - ret = OH_PreviewOutput_Release(x_component_preview_); - if (ret != CAMERA_OK) { - OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OH_PreviewOutput_Release failed, ret = %{public}d", ret); - } - } - preview_output_ = nullptr; - x_component_preview_ = nullptr; return 0; } @@ -338,15 +331,6 @@ int32_t OhosCamera::CaptureSessionSetting() { } } - if (x_component_preview_) { - ret = OH_CaptureSession_AddPreviewOutput(capture_session_, x_component_preview_); - if (ret != CAMERA_OK) { - OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", - "OH_CaptureSession_AddPreviewOutput failed, ret = %{public}d", ret); - return -1; - } - } - ret = OH_CaptureSession_CommitConfig(capture_session_); if (ret != CAMERA_OK) { OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OH_CaptureSession_CommitConfig failed, ret = %{public}d", diff --git a/entry/src/main/cpp/webRTC_adapter/ohos_camera.h b/entry/src/main/cpp/webRTC_adapter/ohos_camera.h index 44e50d7d..cf0c40ae 100644 --- a/entry/src/main/cpp/webRTC_adapter/ohos_camera.h +++ b/entry/src/main/cpp/webRTC_adapter/ohos_camera.h @@ -9,7 +9,6 @@ #define OH_WEB_RTC_OHOS_CAMERA_H #include "hilog/log.h" -#include "napi/native_api.h" #include #include #include @@ -28,19 +27,29 @@ namespace webrtc{ namespace ohos{ -class OhosCamera { + +class OhosImageReveiveOn { +public: + virtual int32_t ImageReceiverCallback(OH_ImageReceiverNative *receiver) = 0; + int32_t InitImageReceiver(); + +private: + uint64_t imageReceiverID_{0}; + OH_ImageReceiverNative *imageReceiverNative_{nullptr}; +}; + +class OhosCamera: public OhosImageReveiveOn { public: OhosCamera(); int32_t InitCamera(); - int32_t InitImageReceiver(); int32_t StartCamera(); int32_t StopCamera(); int32_t CameraRelease(); uint32_t GetCameraIndex(); int32_t SetCameraIndex(uint32_t camera_index); - napi_value GetImageData(napi_env env, ImageReceiverNative *image_receiver_c); bool ImageReceiverOn(uint8_t *buffer, int32_t width, int32_t height, int32_t stride, size_t bufferSize); + int32_t ImageReceiverCallback(OH_ImageReceiverNative *receiver) override ; void RegisterCaptureDataCallback(rtc::VideoSinkInterface *dataCallback); void UnregisterCaptureDataCallback(); ~OhosCamera(); @@ -64,8 +73,6 @@ private: rtc::VideoSinkInterface *data_callback_ {nullptr}; webrtc::VideoCaptureCapability configured_capability_; bool is_camera_started_ {false}; - char* img_receive_surfaceId_ {nullptr}; - OH_ImageReceiverNative *imageReceiverNative_ {nullptr}; Camera_Manager* camera_manager_ {nullptr}; Camera_Device* cameras_ {nullptr}; uint32_t cameras_size_ {0}; @@ -73,7 +80,6 @@ private: Camera_OutputCapability* camera_output_capability_ {nullptr}; const Camera_Profile* preview_profile_ {nullptr}; Camera_PreviewOutput* preview_output_ {nullptr}; - Camera_PreviewOutput* x_component_preview_ {nullptr}; Camera_Input* camera_input_ {nullptr}; uint32_t camera_dev_index_ {0}; uint32_t profile_index_ {0}; -- Gitee From 0fb8f30cc96413624237efd029bcab75b64309a3 Mon Sep 17 00:00:00 2001 From: pengyueqing Date: Fri, 24 May 2024 11:27:04 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E7=9B=B8=E6=9C=BA=E6=96=B0=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=BF=AE=E6=94=B9=EF=BC=8C=E6=B7=BB=E5=8A=A0ImageReve?= =?UTF-8?q?iveOnManage=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/cpp/webRTC_adapter/ohos_camera.cpp | 113 +++++++----------- .../src/main/cpp/webRTC_adapter/ohos_camera.h | 20 +++- 2 files changed, 59 insertions(+), 74 deletions(-) diff --git a/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp b/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp index 5a6a83d4..8eb00222 100644 --- a/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp +++ b/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp @@ -7,6 +7,8 @@ #include "api/video/i420_buffer.h" #include "api/video/video_rotation.h" #include "libyuv/convert.h" +#include "rtc_base/synchronization/mutex.h" +#include "rtc_base/thread_annotations.h" #include "rtc_base/time_utils.h" #include "hilog/log.h" #include @@ -17,6 +19,7 @@ #include #include #include +#include #include @@ -63,35 +66,45 @@ private: OH_ImageReceiverOptions *options_ {nullptr}; }; -class GlobalOHOSCamera { -public: - static GlobalOHOSCamera& GetInstance() { - static GlobalOHOSCamera globalOHOSCamera; - return globalOHOSCamera; - } - - void SetOhosCamera(OhosCamera* ohosCamera) { - ohosCamera_ = ohosCamera; - } - - void UnsetOhosCamera() { - ohosCamera_ = nullptr; - } - - OhosCamera* GetOhosCamera() { - return ohosCamera_; + +ImageReceiverOnManager& ImageReceiverOnManager::GetInstance() { + static ImageReceiverOnManager manager; + return manager; +} + +int32_t ImageReceiverOnManager::AddImageReceiverOn(uint64_t id, std::shared_ptr imgRecOn) { + MutexLock lock(&mapMutex_); + receiverOnMap_[id] = imgRecOn; +} + +int32_t ImageReceiverOnManager::DelImageReceiverOn(uint64_t id) { + MutexLock lock(&mapMutex_); + receiverOnMap_.erase(id); +} + +OhosImageReveiveOn& ImageReceiverOnManager::GetReceiverOnCb(const uint64_t imageReceiverID) { + std::map >::iterator iter; + iter = receiverOnMap_.find(imageReceiverID); + if (iter == receiverOnMap_.end()) { + OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", + "GetReceiverOnCb not find key: %{public}d", imageReceiverID); + throw EXIT_FAILURE; } -private: - GlobalOHOSCamera() {}; - OhosCamera* ohosCamera_ {nullptr}; -}; + std::shared_ptr imgRecOnPtr = iter->second.lock(); + return imgRecOnPtr.release(); +} -int32_t OhosCamera::ImageReceiverCallback(OH_ImageReceiverNative* receiver) { - OhosCamera* ohosCamera = GlobalOHOSCamera::GetInstance().GetOhosCamera(); - if (ohosCamera == nullptr) { +void ImageReceiverOnManager::ImageReceiverCallback(OH_ImageReceiverNative* receiver) { + uint64_t imageReceiverID = OhosImageReveiveOn::GetImageReceiverSurfaceID(receiver); + if (imageReceiverID == 0) { return; } + OhosImageReveiveOn& ohosCamera = ImageReceiverOnManager::GetInstance().GetReceiverOnCb(imageReceiverID); + ohosCamera.ImageReceiverCallback(receiver); +} + +int32_t OhosCamera::ImageReceiverCallback(OH_ImageReceiverNative* receiver) { OH_ImageNative *image; Image_ErrorCode ret = OH_ImageReceiverNative_ReadLatestImage(receiver, &image); if (ret != IMAGE_SUCCESS) { @@ -161,7 +174,7 @@ int32_t OhosCamera::ImageReceiverCallback(OH_ImageReceiverNative* receiver) { return; } - ohosCamera->ImageReceiverOn(addr, imageSize.width, imageSize.height, rowStride, bufferSize); + ImageReceiverOn(addr, imageSize.width, imageSize.height, rowStride, bufferSize); ret = OH_ImageNative_Release(image); if (ret != IMAGE_SUCCESS) { @@ -186,7 +199,7 @@ int32_t OhosImageReveiveOn::InitImageReceiver() { return returnCode; } - ret = OH_ImageReceiverNative_On(imageReceiverNative_, ImageReceiverCallback); + ret = OH_ImageReceiverNative_On(imageReceiverNative_, ImageReceiverOnManager::ImageReceiverCallback); if (ret != IMAGE_SUCCESS) { OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OH_ImageReceiverNative_On failed with code = %{public}d", ret); @@ -275,7 +288,7 @@ int32_t OhosCamera::PreviewOutputCreate() { preview_profile_ = camera_output_capability_->previewProfiles[profile_index_]; ret = - OH_CameraManager_CreatePreviewOutput(camera_manager_, preview_profile_, img_receive_surfaceId_, &preview_output_); + OH_CameraManager_CreatePreviewOutput(camera_manager_, preview_profile_, imageReceiverID_, &preview_output_); if (preview_output_ == nullptr || ret != CAMERA_OK) { OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OH_CameraManager_CreatePreviewOutput failed, ret = %{public}d", ret); @@ -522,48 +535,6 @@ bool OhosCamera::ImageReceiverOn(uint8_t *buffer, int32_t width, int32_t height, return true; } -napi_value OhosCamera::GetImageData(napi_env env, ImageReceiverNative* image_receiver_c) { - int32_t ret; - napi_value next_image; - // 或调用 OH_Image_Receiver_ReadNextImage(imgReceiver_c, &nextImage); - ret = OH_Image_Receiver_ReadLatestImage(image_receiver_c, &next_image); - if (ret != IMAGE_RESULT_SUCCESS) { - OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OH_Image_Receiver_ReadLatestImage failed, ret = %{public}d", ret); - return nullptr; - } - - ImageNative *next_image_native = OH_Image_InitImageNative(env, next_image); - - OhosImageSize img_size; - ret = OH_Image_Size(next_image_native, &img_size); - OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OH_Image_Size width: %{public}d, height:%{public}d", - img_size.width, img_size.height); - if (ret != IMAGE_RESULT_SUCCESS) { - OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OH_Image_Size failed, ret = %{public}d", ret); - return nullptr; - } - - OhosImageComponent imgComponent; - ret = OH_Image_GetComponent(next_image_native, 4, &imgComponent); // 4=jpeg - if (ret != IMAGE_RESULT_SUCCESS) { - OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OH_Image_GetComponent failed, ret = %{public}d", ret); - return nullptr; - } - -// uint8_t *img_buffer = imgComponent.byteBuffer; -// if (ImageReceiverOn(img_buffer, img_size.width, img_size.height) == false) { -// OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "ImageReceiverOn failed"); -// return nullptr; -// } - - ret = OH_Image_Release(next_image_native); - if (ret != IMAGE_RESULT_SUCCESS) { - OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OH_Image_Release failed, ret = %{public}d", ret); - return nullptr; - } - return next_image; -} - void OhosCamera::RegisterCaptureDataCallback(rtc::VideoSinkInterface *data_callback) { data_callback_ = data_callback; } @@ -577,10 +548,6 @@ OhosCamera::~OhosCamera() { GlobalOHOSCamera::GetInstance().UnsetOhosCamera(); CameraRelease(); - if (img_receive_surfaceId_ != nullptr) { - delete[] img_receive_surfaceId_; - img_receive_surfaceId_ = nullptr; - } camera_dev_index_ = 0; profile_index_ = 0; diff --git a/entry/src/main/cpp/webRTC_adapter/ohos_camera.h b/entry/src/main/cpp/webRTC_adapter/ohos_camera.h index cf0c40ae..702934f3 100644 --- a/entry/src/main/cpp/webRTC_adapter/ohos_camera.h +++ b/entry/src/main/cpp/webRTC_adapter/ohos_camera.h @@ -28,16 +28,34 @@ namespace webrtc{ namespace ohos{ + +class ImageReceiverOnManager { +public: + static ImageReceiverOnManager& GetInstance(); + int32_t AddImageReceiverOn(uint64_t id, std::shared_ptr imgRecOn); + int32_t DelImageReceiverOn(uint64_t id); + OhosImageReveiveOn& GetReceiverOnCb(const uint64_t imageReceiverID); + static void ImageReceiverCallback(OH_ImageReceiverNative* receiver) ; + +private: + ImageReceiverOnManager() {}; + std::mutex mapMutex_; + std::map > receiverOnMap_ RTC_GUARDED_BY(mapMutex_); +}; + + class OhosImageReveiveOn { public: virtual int32_t ImageReceiverCallback(OH_ImageReceiverNative *receiver) = 0; int32_t InitImageReceiver(); + static uint64_t GetImageReceiverSurfaceID(OH_ImageReceiverNative *receiver); private: uint64_t imageReceiverID_{0}; OH_ImageReceiverNative *imageReceiverNative_{nullptr}; }; + class OhosCamera: public OhosImageReveiveOn { public: OhosCamera(); @@ -49,7 +67,7 @@ public: uint32_t GetCameraIndex(); int32_t SetCameraIndex(uint32_t camera_index); bool ImageReceiverOn(uint8_t *buffer, int32_t width, int32_t height, int32_t stride, size_t bufferSize); - int32_t ImageReceiverCallback(OH_ImageReceiverNative *receiver) override ; + int32_t ImageReceiverCallback(OH_ImageReceiverNative *receiver) override; void RegisterCaptureDataCallback(rtc::VideoSinkInterface *dataCallback); void UnregisterCaptureDataCallback(); ~OhosCamera(); -- Gitee From d73ef368e9305ee6be5389d99e5038737e70af78 Mon Sep 17 00:00:00 2001 From: pengyueqing Date: Fri, 24 May 2024 11:43:11 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E7=9B=B8=E6=9C=BA=E6=96=B0=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=BF=AE=E6=94=B9=20camera=5Fcapture=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/cpp/webRTC_adapter/ohos_camera_capture.cpp | 5 +++-- entry/src/main/cpp/webRTC_adapter/ohos_camera_capture.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/entry/src/main/cpp/webRTC_adapter/ohos_camera_capture.cpp b/entry/src/main/cpp/webRTC_adapter/ohos_camera_capture.cpp index 23817ffe..5c5dd49b 100644 --- a/entry/src/main/cpp/webRTC_adapter/ohos_camera_capture.cpp +++ b/entry/src/main/cpp/webRTC_adapter/ohos_camera_capture.cpp @@ -15,7 +15,8 @@ namespace ohos { OhosCameraCapture::OhosCameraCapture() {} bool OhosCameraCapture::Init(){ - ohosCamera_ = new OhosCamera(); + ohosCamera_ = std::make_shared(); + ImageReceiverOnManager::GetInstance().AddImageReceiverOn(ohosCamera_->imageReceiverID_, ohosCamera_); ohosCamera_->RegisterCaptureDataCallback(this); ohosCamera_->StartCamera(); @@ -36,7 +37,7 @@ OhosCameraCapture* OhosCameraCapture::Create() { void OhosCameraCapture::Destroy() { ohosCamera_->StopCamera(); ohosCamera_->UnregisterCaptureDataCallback(); - delete ohosCamera_; + ImageReceiverOnManager::GetInstance().DelImageReceiverOn(ohosCamera_->imageReceiverID_); ohosCamera_ = nullptr; } diff --git a/entry/src/main/cpp/webRTC_adapter/ohos_camera_capture.h b/entry/src/main/cpp/webRTC_adapter/ohos_camera_capture.h index 85ffa0d0..802e9ffa 100644 --- a/entry/src/main/cpp/webRTC_adapter/ohos_camera_capture.h +++ b/entry/src/main/cpp/webRTC_adapter/ohos_camera_capture.h @@ -45,7 +45,7 @@ private: void UpdateVideoAdapter(); VideoFrame MaybePreprocess(const VideoFrame &frame); - OhosCamera* ohosCamera_ {nullptr}; + std::shared_ptr ohosCamera_ {nullptr}; Mutex lock_; rtc::VideoBroadcaster broadcaster_; cricket::VideoAdapter video_adapter_; -- Gitee From 7bbc18870b87808d96bb5725bd90591d553d223a Mon Sep 17 00:00:00 2001 From: pengyueqing Date: Fri, 24 May 2024 15:06:44 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E7=9B=B8=E6=9C=BA=E6=96=B0=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=BF=AE=E6=94=B9=20=E4=BB=A3=E7=A0=81=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/cpp/webRTC_adapter/ohos_camera.cpp | 40 +++++++++++-------- .../src/main/cpp/webRTC_adapter/ohos_camera.h | 27 +++++++------ 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp b/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp index 8eb00222..f1aa0c5a 100644 --- a/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp +++ b/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp @@ -87,11 +87,11 @@ OhosImageReveiveOn& ImageReceiverOnManager::GetReceiverOnCb(const uint64_t image iter = receiverOnMap_.find(imageReceiverID); if (iter == receiverOnMap_.end()) { OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", - "GetReceiverOnCb not find key: %{public}d", imageReceiverID); + "GetReceiverOnCb not find key: %{public}llu", imageReceiverID); throw EXIT_FAILURE; } std::shared_ptr imgRecOnPtr = iter->second.lock(); - return imgRecOnPtr.release(); + return imgRecOnPtr.get(); } void ImageReceiverOnManager::ImageReceiverCallback(OH_ImageReceiverNative* receiver) { @@ -104,13 +104,21 @@ void ImageReceiverOnManager::ImageReceiverCallback(OH_ImageReceiverNative* recei ohosCamera.ImageReceiverCallback(receiver); } +uint64_t ImageReceiverOnManager::GetImageReceiverID() { + return imageReceiverID_; +} + +OH_ImageReceiverNative* ImageReceiverOnManager::GetImageReceiver() { + return imageReceiverNative_; +} + int32_t OhosCamera::ImageReceiverCallback(OH_ImageReceiverNative* receiver) { OH_ImageNative *image; Image_ErrorCode ret = OH_ImageReceiverNative_ReadLatestImage(receiver, &image); if (ret != IMAGE_SUCCESS) { OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OH_ImageReceiverNative_ReadLatestImage failed with code = %{public}d", ret); - return; + return -1; } Image_Size imageSize; @@ -118,7 +126,7 @@ int32_t OhosCamera::ImageReceiverCallback(OH_ImageReceiverNative* receiver) { if (ret != IMAGE_SUCCESS) { OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OH_ImageNative_GetImageSize failed with code = %{public}d", ret); - return; + return -1; } uint32_t* types; @@ -127,7 +135,7 @@ int32_t OhosCamera::ImageReceiverCallback(OH_ImageReceiverNative* receiver) { if (ret != IMAGE_SUCCESS || typeSize <= 0) { OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OH_ImageNative_GetComponentTypes failed with code = %{public}d", ret); - return; + return -1; } types = new uint32_t[typeSize]; @@ -136,7 +144,7 @@ int32_t OhosCamera::ImageReceiverCallback(OH_ImageReceiverNative* receiver) { OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OH_ImageNative_GetComponentTypes failed with code = %{public}d", ret); delete [] types; - return; + return -1; } int32_t rowStride; @@ -145,7 +153,7 @@ int32_t OhosCamera::ImageReceiverCallback(OH_ImageReceiverNative* receiver) { OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OH_ImageNative_GetRowStride failed with code = %{public}d", ret); delete[] types; - return; + return -1; } uint8_t *addr = nullptr; @@ -156,14 +164,14 @@ int32_t OhosCamera::ImageReceiverCallback(OH_ImageReceiverNative* receiver) { OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OH_ImageNative_GetByteBuffer failed with code = %{public}d", ret); delete[] types; - return; + return -1; } ret = OH_ImageNative_GetBufferSize(image, types[0], &bufferSize); if (ret != IMAGE_SUCCESS) { OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OH_ImageNative_GetBufferSize failed with code = %{public}d", ret); delete[] types; - return; + return -1; } delete[] types; @@ -171,7 +179,7 @@ int32_t OhosCamera::ImageReceiverCallback(OH_ImageReceiverNative* receiver) { if (retInt != 0) { OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OH_NativeBuffer_Map failed with code = %{public}d", retInt); - return; + return -1; } ImageReceiverOn(addr, imageSize.width, imageSize.height, rowStride, bufferSize); @@ -180,14 +188,15 @@ int32_t OhosCamera::ImageReceiverCallback(OH_ImageReceiverNative* receiver) { if (ret != IMAGE_SUCCESS) { OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OH_ImageNative_Release failed with code = %{public}d", ret); - return; + return -1; } + + return 0; } OhosCamera::OhosCamera() { InitImageReceiver(); InitCamera(); - GlobalOHOSCamera::GetInstance().SetOhosCamera(this); } int32_t OhosImageReveiveOn::InitImageReceiver() { @@ -287,8 +296,9 @@ int32_t OhosCamera::PreviewOutputCreate() { } preview_profile_ = camera_output_capability_->previewProfiles[profile_index_]; - ret = - OH_CameraManager_CreatePreviewOutput(camera_manager_, preview_profile_, imageReceiverID_, &preview_output_); + char surfaceID[24]; + snprintf(surfaceID, 24, "%llu", GetImageReceiverID()); + ret = OH_CameraManager_CreatePreviewOutput(camera_manager_, preview_profile_, surfaceID, &preview_output_); if (preview_output_ == nullptr || ret != CAMERA_OK) { OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OH_CameraManager_CreatePreviewOutput failed, ret = %{public}d", ret); @@ -545,8 +555,6 @@ void OhosCamera::UnregisterCaptureDataCallback() { OhosCamera::~OhosCamera() { OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OhosCamera::~OhosCamera start"); - - GlobalOHOSCamera::GetInstance().UnsetOhosCamera(); CameraRelease(); camera_dev_index_ = 0; diff --git a/entry/src/main/cpp/webRTC_adapter/ohos_camera.h b/entry/src/main/cpp/webRTC_adapter/ohos_camera.h index 702934f3..fc9ca965 100644 --- a/entry/src/main/cpp/webRTC_adapter/ohos_camera.h +++ b/entry/src/main/cpp/webRTC_adapter/ohos_camera.h @@ -22,6 +22,7 @@ #include "rtc_base/ref_counter.h" #include "api/video/video_source_interface.h" #include "api/video/video_frame.h" +#include "rtc_base/synchronization/mutex.h" #include "modules/video_capture/video_capture.h" @@ -29,6 +30,20 @@ namespace webrtc{ namespace ohos{ +class OhosImageReveiveOn { +public: + virtual int32_t ImageReceiverCallback(OH_ImageReceiverNative *receiver) = 0; + int32_t InitImageReceiver(); + static uint64_t GetImageReceiverSurfaceID(OH_ImageReceiverNative *receiver); +protected: + uint64_t GetImageReceiverID(); + OH_ImageReceiverNative* GetImageReceiver(); +private: + uint64_t imageReceiverID_ {0}; + OH_ImageReceiverNative *imageReceiverNative_ {nullptr}; +}; + + class ImageReceiverOnManager { public: static ImageReceiverOnManager& GetInstance(); @@ -44,18 +59,6 @@ private: }; -class OhosImageReveiveOn { -public: - virtual int32_t ImageReceiverCallback(OH_ImageReceiverNative *receiver) = 0; - int32_t InitImageReceiver(); - static uint64_t GetImageReceiverSurfaceID(OH_ImageReceiverNative *receiver); - -private: - uint64_t imageReceiverID_{0}; - OH_ImageReceiverNative *imageReceiverNative_{nullptr}; -}; - - class OhosCamera: public OhosImageReveiveOn { public: OhosCamera(); -- Gitee From d4bb54f2a8dd636ee774cbd9e6f839dbc4d33a79 Mon Sep 17 00:00:00 2001 From: pengyueqing Date: Fri, 24 May 2024 15:52:59 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E7=9B=B8=E6=9C=BA=E6=96=B0=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=BF=AE=E6=94=B9=20=E4=BB=A3=E7=A0=81=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/cpp/webRTC_adapter/ohos_camera.cpp | 16 +++++++++------- entry/src/main/cpp/webRTC_adapter/ohos_camera.h | 13 ++++++++----- .../cpp/webRTC_adapter/ohos_camera_capture.cpp | 4 ++-- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp b/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp index f1aa0c5a..df4a9395 100644 --- a/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp +++ b/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp @@ -75,20 +75,22 @@ ImageReceiverOnManager& ImageReceiverOnManager::GetInstance() { int32_t ImageReceiverOnManager::AddImageReceiverOn(uint64_t id, std::shared_ptr imgRecOn) { MutexLock lock(&mapMutex_); receiverOnMap_[id] = imgRecOn; + return 0; } int32_t ImageReceiverOnManager::DelImageReceiverOn(uint64_t id) { MutexLock lock(&mapMutex_); receiverOnMap_.erase(id); + return 0; } -OhosImageReveiveOn& ImageReceiverOnManager::GetReceiverOnCb(const uint64_t imageReceiverID) { +OhosImageReveiveOn* ImageReceiverOnManager::GetReceiverOnCb(const uint64_t imageReceiverID) { std::map >::iterator iter; iter = receiverOnMap_.find(imageReceiverID); if (iter == receiverOnMap_.end()) { OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "GetReceiverOnCb not find key: %{public}llu", imageReceiverID); - throw EXIT_FAILURE; + return nullptr; } std::shared_ptr imgRecOnPtr = iter->second.lock(); return imgRecOnPtr.get(); @@ -100,15 +102,15 @@ void ImageReceiverOnManager::ImageReceiverCallback(OH_ImageReceiverNative* recei return; } - OhosImageReveiveOn& ohosCamera = ImageReceiverOnManager::GetInstance().GetReceiverOnCb(imageReceiverID); - ohosCamera.ImageReceiverCallback(receiver); + OhosImageReveiveOn* ohosCamera = ImageReceiverOnManager::GetInstance().GetReceiverOnCb(imageReceiverID); + ohosCamera->ImageReceiverCallback(receiver); } -uint64_t ImageReceiverOnManager::GetImageReceiverID() { +uint64_t OhosImageReveiveOn::GetImageReceiverID() { return imageReceiverID_; } -OH_ImageReceiverNative* ImageReceiverOnManager::GetImageReceiver() { +OH_ImageReceiverNative* OhosImageReveiveOn::GetImageReceiver() { return imageReceiverNative_; } @@ -434,7 +436,7 @@ int32_t OhosCamera::StopCamera() { return 0; } -int32_t OhosCamera::ImageReceiverRelease(){ +int32_t OhosImageReveiveOn::ImageReceiverRelease() { if (imageReceiverNative_ == nullptr) { return 0; } diff --git a/entry/src/main/cpp/webRTC_adapter/ohos_camera.h b/entry/src/main/cpp/webRTC_adapter/ohos_camera.h index fc9ca965..ed7c0ab4 100644 --- a/entry/src/main/cpp/webRTC_adapter/ohos_camera.h +++ b/entry/src/main/cpp/webRTC_adapter/ohos_camera.h @@ -18,6 +18,7 @@ #include #include #include +#include #include "rtc_base/ref_counter.h" #include "api/video/video_source_interface.h" @@ -34,10 +35,11 @@ class OhosImageReveiveOn { public: virtual int32_t ImageReceiverCallback(OH_ImageReceiverNative *receiver) = 0; int32_t InitImageReceiver(); + int32_t ImageReceiverRelease(); static uint64_t GetImageReceiverSurfaceID(OH_ImageReceiverNative *receiver); -protected: uint64_t GetImageReceiverID(); OH_ImageReceiverNative* GetImageReceiver(); + private: uint64_t imageReceiverID_ {0}; OH_ImageReceiverNative *imageReceiverNative_ {nullptr}; @@ -49,12 +51,15 @@ public: static ImageReceiverOnManager& GetInstance(); int32_t AddImageReceiverOn(uint64_t id, std::shared_ptr imgRecOn); int32_t DelImageReceiverOn(uint64_t id); - OhosImageReveiveOn& GetReceiverOnCb(const uint64_t imageReceiverID); + OhosImageReveiveOn* GetReceiverOnCb(const uint64_t imageReceiverID); static void ImageReceiverCallback(OH_ImageReceiverNative* receiver) ; private: ImageReceiverOnManager() {}; - std::mutex mapMutex_; + ~ImageReceiverOnManager() {}; + ImageReceiverOnManager(const ImageReceiverOnManager&) {}; + ImageReceiverOnManager& operator=(const ImageReceiverOnManager&) {}; + Mutex mapMutex_; std::map > receiverOnMap_ RTC_GUARDED_BY(mapMutex_); }; @@ -84,8 +89,6 @@ private: int32_t CaptureSessionSetting(); int32_t CaptureSessionUnsetting(); - - int32_t ImageReceiverRelease(); int32_t DeleteCameraOutputCapability(); int32_t DeleteCameras(); diff --git a/entry/src/main/cpp/webRTC_adapter/ohos_camera_capture.cpp b/entry/src/main/cpp/webRTC_adapter/ohos_camera_capture.cpp index 5c5dd49b..6c5626c3 100644 --- a/entry/src/main/cpp/webRTC_adapter/ohos_camera_capture.cpp +++ b/entry/src/main/cpp/webRTC_adapter/ohos_camera_capture.cpp @@ -16,7 +16,7 @@ OhosCameraCapture::OhosCameraCapture() {} bool OhosCameraCapture::Init(){ ohosCamera_ = std::make_shared(); - ImageReceiverOnManager::GetInstance().AddImageReceiverOn(ohosCamera_->imageReceiverID_, ohosCamera_); + ImageReceiverOnManager::GetInstance().AddImageReceiverOn(ohosCamera_->GetImageReceiverID(), ohosCamera_); ohosCamera_->RegisterCaptureDataCallback(this); ohosCamera_->StartCamera(); @@ -37,7 +37,7 @@ OhosCameraCapture* OhosCameraCapture::Create() { void OhosCameraCapture::Destroy() { ohosCamera_->StopCamera(); ohosCamera_->UnregisterCaptureDataCallback(); - ImageReceiverOnManager::GetInstance().DelImageReceiverOn(ohosCamera_->imageReceiverID_); + ImageReceiverOnManager::GetInstance().DelImageReceiverOn(ohosCamera_->GetImageReceiverID()); ohosCamera_ = nullptr; } -- Gitee From f9e8ae345dd80939e11a75f69d8dd42c47c3eef3 Mon Sep 17 00:00:00 2001 From: pengyueqing Date: Fri, 24 May 2024 16:18:16 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E7=9B=B8=E6=9C=BA=E6=96=B0=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=BF=AE=E6=94=B9=20=E4=BB=A3=E7=A0=81=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/cpp/webRTC_adapter/ohos_camera.cpp | 18 ++++++++++++++---- .../src/main/cpp/webRTC_adapter/ohos_camera.h | 2 +- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp b/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp index df4a9395..e99a0876 100644 --- a/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp +++ b/entry/src/main/cpp/webRTC_adapter/ohos_camera.cpp @@ -106,6 +106,17 @@ void ImageReceiverOnManager::ImageReceiverCallback(OH_ImageReceiverNative* recei ohosCamera->ImageReceiverCallback(receiver); } +uint64_t OhosImageReveiveOn::GetImageReceiverSurfaceID(OH_ImageReceiverNative *receiver) { + uint64_t id; + Image_ErrorCode ret = OH_ImageReceiverNative_GetReceivingSurfaceId(receiver, &id); + if (ret != IMAGE_SUCCESS) { + OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OH_ImageReceiverNative_On failed with code = %{public}d", + ret); + return 0; + } + return id; +} + uint64_t OhosImageReveiveOn::GetImageReceiverID() { return imageReceiverID_; } @@ -217,10 +228,9 @@ int32_t OhosImageReveiveOn::InitImageReceiver() { return returnCode; } - ret = OH_ImageReceiverNative_GetReceivingSurfaceId(imageReceiverNative_, &imageReceiverID_); - if (ret != IMAGE_SUCCESS) { - OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", - "OH_ImageReceiverNative_On failed with code = %{public}d", ret); + imageReceiverID_ = GetImageReceiverSurfaceID(imageReceiverNative_); + if (imageReceiverID_ == 0) { + OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "GetImageReceiverSurfaceID failed"); return returnCode; } diff --git a/entry/src/main/cpp/webRTC_adapter/ohos_camera.h b/entry/src/main/cpp/webRTC_adapter/ohos_camera.h index ed7c0ab4..e72eb1da 100644 --- a/entry/src/main/cpp/webRTC_adapter/ohos_camera.h +++ b/entry/src/main/cpp/webRTC_adapter/ohos_camera.h @@ -52,7 +52,7 @@ public: int32_t AddImageReceiverOn(uint64_t id, std::shared_ptr imgRecOn); int32_t DelImageReceiverOn(uint64_t id); OhosImageReveiveOn* GetReceiverOnCb(const uint64_t imageReceiverID); - static void ImageReceiverCallback(OH_ImageReceiverNative* receiver) ; + static void ImageReceiverCallback(OH_ImageReceiverNative* receiver); private: ImageReceiverOnManager() {}; -- Gitee