From 82b4212e7489d4ab79f52cf3f58214d26fad7131 Mon Sep 17 00:00:00 2001 From: acgist <289547414@qq.com> Date: Wed, 8 May 2024 21:26:34 +0800 Subject: [PATCH] =?UTF-8?q?[*]=20=E8=B5=84=E6=BA=90=E9=87=8A=E6=94=BE?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../media/src/main/cpp/include/Capturer.hpp | 3 ++ .../src/main/cpp/media/AudioCapturer.cpp | 8 ++++ .../src/main/cpp/media/VideoCapturer.cpp | 44 ++++++++++++++----- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/Capturer.hpp b/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/Capturer.hpp index bffb74e..8544b65 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/Capturer.hpp +++ b/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/Capturer.hpp @@ -52,6 +52,9 @@ namespace acgist { template class Capturer { +protected: + bool running = false; + public: // id = rtc::scoped_refptr std::map map; diff --git a/taoyao-client-openharmony/taoyao/media/src/main/cpp/media/AudioCapturer.cpp b/taoyao-client-openharmony/taoyao/media/src/main/cpp/media/AudioCapturer.cpp index 4657030..dc88f5e 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/cpp/media/AudioCapturer.cpp +++ b/taoyao-client-openharmony/taoyao/media/src/main/cpp/media/AudioCapturer.cpp @@ -39,6 +39,10 @@ acgist::AudioCapturer::~AudioCapturer() { } bool acgist::AudioCapturer::start() { + if(this->running) { + return true; + } + this->running = true; // 构造音频采集器 OH_AudioStream_Result ret = OH_AudioStreamBuilder_GenerateCapturer(this->builder, &this->audioCapturer); OH_LOG_DEBUG(LOG_APP, "构造音频采集器:%o", ret); @@ -49,6 +53,10 @@ bool acgist::AudioCapturer::start() { } bool acgist::AudioCapturer::stop() { + if(!this->running) { + return true; + } + this->running = false; if(this->audioCapturer == nullptr) { return true; } diff --git a/taoyao-client-openharmony/taoyao/media/src/main/cpp/media/VideoCapturer.cpp b/taoyao-client-openharmony/taoyao/media/src/main/cpp/media/VideoCapturer.cpp index 9a901ba..0db28e1 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/cpp/media/VideoCapturer.cpp +++ b/taoyao-client-openharmony/taoyao/media/src/main/cpp/media/VideoCapturer.cpp @@ -71,6 +71,10 @@ acgist::VideoCapturer::~VideoCapturer() { } bool acgist::VideoCapturer::start() { + if (this->running) { + return true; + } + this->running = true; OH_CaptureSession_BeginConfig(this->cameraCaptureSession); Camera_ErrorCode ret = OH_CaptureSession_AddVideoOutput(this->cameraCaptureSession, this->cameraVideoOutput); OH_LOG_INFO(LOG_APP, "视频捕获绑定会话:%o", ret); @@ -90,6 +94,10 @@ bool acgist::VideoCapturer::start() { } bool acgist::VideoCapturer::stop() { + if (!this->running) { + return true; + } + this->running = false; OH_NativeImage_DetachContext(this->nativeImage); OH_CaptureSession_BeginConfig(this->cameraCaptureSession); Camera_ErrorCode ret = OH_CaptureSession_RemoveVideoOutput(this->cameraCaptureSession, this->cameraVideoOutput); @@ -106,7 +114,8 @@ bool acgist::VideoCapturer::stop() { void acgist::VideoCapturer::initOpenGLES() { // IMAGE WINDOW glGenTextures(this->textureSize, &this->textureId); - this->nativeImage = OH_NativeImage_Create(this->textureId, GL_TEXTURE_2D); + this->nativeImage = OH_NativeImage_Create(this->textureId, GL_TEXTURE_2D); + // TODO: 验证是否需要window // this->nativeWindow = OH_NativeImage_AcquireNativeWindow(this->nativeImage); // OH_NativeWindow_NativeWindowHandleOpt(this->nativeWindow, SET_BUFFER_GEOMETRY, acgist::width, acgist::height); // OH_NativeWindow_NativeWindowHandleOpt(this->nativeWindow, SET_TIMEOUT, 100'000); @@ -168,15 +177,30 @@ void acgist::VideoCapturer::initOpenGLES() { } void acgist::VideoCapturer::releaseOpenGLES() { - glDeleteTextures(this->textureSize, &this->textureId); - EGLBoolean ret = eglDestroySurface(this->eglDisplay, this->eglSurface); - OH_LOG_INFO(LOG_APP, "销毁EGLSurface:%d", ret); - ret = eglDestroyContext(this->eglDisplay, this->eglContext); - OH_LOG_INFO(LOG_APP, "销毁EGLContext:%d", ret); - OH_NativeImage_Destroy(&this->nativeImage); - this->nativeImage = nullptr; - OH_NativeWindow_DestroyNativeWindow(this->nativeWindow); - this->nativeWindow = nullptr; + if(this->textureId != 0) { + glDeleteTextures(this->textureSize, &this->textureId); + this->textureId = 0; + } + if(this->eglSurface != EGL_NO_SURFACE) { + EGLBoolean ret = eglDestroySurface(this->eglDisplay, this->eglSurface); + this->eglSurface = EGL_NO_SURFACE; + OH_LOG_INFO(LOG_APP, "销毁EGLSurface:%d", ret); + } + if(this->eglContext != EGL_NO_CONTEXT) { + EGLBoolean ret = eglDestroyContext(this->eglDisplay, this->eglContext); + this->eglContext = EGL_NO_CONTEXT; + OH_LOG_INFO(LOG_APP, "销毁EGLContext:%d", ret); + } + if(this->nativeImage != nullptr) { + OH_NativeImage_Destroy(&this->nativeImage); + this->nativeImage = nullptr; + OH_LOG_INFO(LOG_APP, "销毁nativeImage"); + } + if(this->nativeWindow != nullptr) { + OH_NativeWindow_DestroyNativeWindow(this->nativeWindow); + this->nativeWindow = nullptr; + OH_LOG_INFO(LOG_APP, "销毁nativeWindow"); + } } static void onError(Camera_VideoOutput* videoOutput, Camera_ErrorCode errorCode) {