diff --git a/entry/src/main/cpp/napi_init.cpp b/entry/src/main/cpp/napi_init.cpp index 6d5b4f350a7ce9f8bab00f05dba493a5ee1e9094..14186102010d69137e9057f2b0b742d3c466b3a3 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 f72774d9d170003c678c92ed06c1742fb46ffda1..e99a0876027a4f302e1759f8b87c3f89311dc576 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,41 +66,72 @@ 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; + return 0; +} + +int32_t ImageReceiverOnManager::DelImageReceiverOn(uint64_t id) { + MutexLock lock(&mapMutex_); + receiverOnMap_.erase(id); + return 0; +} + +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); + return nullptr; } -private: - GlobalOHOSCamera() {}; - OhosCamera* ohosCamera_ {nullptr}; -}; + std::shared_ptr imgRecOnPtr = iter->second.lock(); + return imgRecOnPtr.get(); +} -void 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); +} + +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_; +} + +OH_ImageReceiverNative* OhosImageReveiveOn::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; @@ -105,7 +139,7 @@ void 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; @@ -114,7 +148,7 @@ void 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]; @@ -123,7 +157,7 @@ void 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; @@ -132,7 +166,7 @@ void 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; @@ -143,14 +177,14 @@ void 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; @@ -158,47 +192,27 @@ void 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; } - 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) { OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OH_ImageNative_Release failed with code = %{public}d", ret); - return; + return -1; } + + return 0; } -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 OhosImageReveiveOn::InitImageReceiver() { int32_t returnCode = -1; ImageReceiverOption imageReceiverOption(640, 480, 8); Image_ErrorCode ret = OH_ImageReceiverNative_Create(imageReceiverOption.GetImageReceiverOptions(), &imageReceiverNative_); @@ -207,19 +221,19 @@ int32_t OhosCamera::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); + 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()); - + imageReceiverID_ = GetImageReceiverSurfaceID(imageReceiverNative_); + if (imageReceiverID_ == 0) { + OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "GetImageReceiverSurfaceID failed"); + return returnCode; + } + returnCode = 0; return returnCode; } @@ -294,24 +308,15 @@ 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_); + 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); 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; } @@ -324,15 +329,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; } @@ -369,15 +366,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", @@ -458,7 +446,7 @@ int32_t OhosCamera::StopCamera() { return 0; } -int32_t OhosCamera::ImageReceiverRelease(){ +int32_t OhosImageReveiveOn::ImageReceiverRelease() { if (imageReceiverNative_ == nullptr) { return 0; } @@ -569,48 +557,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; } @@ -621,18 +567,7 @@ void OhosCamera::UnregisterCaptureDataCallback() { OhosCamera::~OhosCamera() { OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "mytest", "OhosCamera::~OhosCamera start"); - - GlobalOHOSCamera::GetInstance().UnsetOhosCamera(); CameraRelease(); - if (img_receive_surfaceId_ != nullptr) { - delete[] img_receive_surfaceId_; - 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 64508f5a2f424f76aa0b2f8961ce5e2bb78d5032..e72eb1dafc111f89686408d957ed465fb82641e2 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 @@ -19,29 +18,64 @@ #include #include #include +#include #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" namespace webrtc{ namespace ohos{ -class OhosCamera { + + +class OhosImageReveiveOn { +public: + virtual int32_t ImageReceiverCallback(OH_ImageReceiverNative *receiver) = 0; + int32_t InitImageReceiver(); + int32_t ImageReceiverRelease(); + static uint64_t GetImageReceiverSurfaceID(OH_ImageReceiverNative *receiver); + uint64_t GetImageReceiverID(); + OH_ImageReceiverNative* GetImageReceiver(); + +private: + uint64_t imageReceiverID_ {0}; + OH_ImageReceiverNative *imageReceiverNative_ {nullptr}; +}; + + +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() {}; + ~ImageReceiverOnManager() {}; + ImageReceiverOnManager(const ImageReceiverOnManager&) {}; + ImageReceiverOnManager& operator=(const ImageReceiverOnManager&) {}; + Mutex mapMutex_; + std::map > receiverOnMap_ RTC_GUARDED_BY(mapMutex_); +}; + + +class OhosCamera: public OhosImageReveiveOn { public: OhosCamera(); - static bool Init(napi_env env, napi_callback_info info); 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(); @@ -55,8 +89,6 @@ private: int32_t CaptureSessionSetting(); int32_t CaptureSessionUnsetting(); - - int32_t ImageReceiverRelease(); int32_t DeleteCameraOutputCapability(); int32_t DeleteCameras(); @@ -65,9 +97,6 @@ private: rtc::VideoSinkInterface *data_callback_ {nullptr}; 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}; uint32_t cameras_size_ {0}; @@ -75,7 +104,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}; 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 23817ffe8168942cacb1ec7843a0bc493fe7f606..6c5626c3c7ff641d1bc50ee1bab57b469ff3c890 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_->GetImageReceiverID(), 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_->GetImageReceiverID()); 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 85ffa0d0c7d8939fbeac88cef87e4bb4ef6c5c23..802e9ffaa501c46dd142407e5124bc0a49a225cb 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_;