[*] 资源释放优化

This commit is contained in:
acgist
2024-05-08 21:26:34 +08:00
parent cb77abef2e
commit 82b4212e74
3 changed files with 45 additions and 10 deletions

View File

@@ -52,6 +52,9 @@ namespace acgist {
template <typename Sink> template <typename Sink>
class Capturer { class Capturer {
protected:
bool running = false;
public: public:
// id = rtc::scoped_refptr // id = rtc::scoped_refptr
std::map<std::string, Sink*> map; std::map<std::string, Sink*> map;

View File

@@ -39,6 +39,10 @@ acgist::AudioCapturer::~AudioCapturer() {
} }
bool acgist::AudioCapturer::start() { bool acgist::AudioCapturer::start() {
if(this->running) {
return true;
}
this->running = true;
// 构造音频采集器 // 构造音频采集器
OH_AudioStream_Result ret = OH_AudioStreamBuilder_GenerateCapturer(this->builder, &this->audioCapturer); OH_AudioStream_Result ret = OH_AudioStreamBuilder_GenerateCapturer(this->builder, &this->audioCapturer);
OH_LOG_DEBUG(LOG_APP, "构造音频采集器:%o", ret); OH_LOG_DEBUG(LOG_APP, "构造音频采集器:%o", ret);
@@ -49,6 +53,10 @@ bool acgist::AudioCapturer::start() {
} }
bool acgist::AudioCapturer::stop() { bool acgist::AudioCapturer::stop() {
if(!this->running) {
return true;
}
this->running = false;
if(this->audioCapturer == nullptr) { if(this->audioCapturer == nullptr) {
return true; return true;
} }

View File

@@ -71,6 +71,10 @@ acgist::VideoCapturer::~VideoCapturer() {
} }
bool acgist::VideoCapturer::start() { bool acgist::VideoCapturer::start() {
if (this->running) {
return true;
}
this->running = true;
OH_CaptureSession_BeginConfig(this->cameraCaptureSession); OH_CaptureSession_BeginConfig(this->cameraCaptureSession);
Camera_ErrorCode ret = OH_CaptureSession_AddVideoOutput(this->cameraCaptureSession, this->cameraVideoOutput); Camera_ErrorCode ret = OH_CaptureSession_AddVideoOutput(this->cameraCaptureSession, this->cameraVideoOutput);
OH_LOG_INFO(LOG_APP, "视频捕获绑定会话:%o", ret); OH_LOG_INFO(LOG_APP, "视频捕获绑定会话:%o", ret);
@@ -90,6 +94,10 @@ bool acgist::VideoCapturer::start() {
} }
bool acgist::VideoCapturer::stop() { bool acgist::VideoCapturer::stop() {
if (!this->running) {
return true;
}
this->running = false;
OH_NativeImage_DetachContext(this->nativeImage); OH_NativeImage_DetachContext(this->nativeImage);
OH_CaptureSession_BeginConfig(this->cameraCaptureSession); OH_CaptureSession_BeginConfig(this->cameraCaptureSession);
Camera_ErrorCode ret = OH_CaptureSession_RemoveVideoOutput(this->cameraCaptureSession, this->cameraVideoOutput); Camera_ErrorCode ret = OH_CaptureSession_RemoveVideoOutput(this->cameraCaptureSession, this->cameraVideoOutput);
@@ -107,6 +115,7 @@ void acgist::VideoCapturer::initOpenGLES() {
// IMAGE WINDOW // IMAGE WINDOW
glGenTextures(this->textureSize, &this->textureId); 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); // 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_BUFFER_GEOMETRY, acgist::width, acgist::height);
// OH_NativeWindow_NativeWindowHandleOpt(this->nativeWindow, SET_TIMEOUT, 100'000); // OH_NativeWindow_NativeWindowHandleOpt(this->nativeWindow, SET_TIMEOUT, 100'000);
@@ -168,15 +177,30 @@ void acgist::VideoCapturer::initOpenGLES() {
} }
void acgist::VideoCapturer::releaseOpenGLES() { void acgist::VideoCapturer::releaseOpenGLES() {
if(this->textureId != 0) {
glDeleteTextures(this->textureSize, &this->textureId); glDeleteTextures(this->textureSize, &this->textureId);
this->textureId = 0;
}
if(this->eglSurface != EGL_NO_SURFACE) {
EGLBoolean ret = eglDestroySurface(this->eglDisplay, this->eglSurface); EGLBoolean ret = eglDestroySurface(this->eglDisplay, this->eglSurface);
this->eglSurface = EGL_NO_SURFACE;
OH_LOG_INFO(LOG_APP, "销毁EGLSurface%d", ret); OH_LOG_INFO(LOG_APP, "销毁EGLSurface%d", ret);
ret = eglDestroyContext(this->eglDisplay, this->eglContext); }
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); OH_LOG_INFO(LOG_APP, "销毁EGLContext%d", ret);
}
if(this->nativeImage != nullptr) {
OH_NativeImage_Destroy(&this->nativeImage); OH_NativeImage_Destroy(&this->nativeImage);
this->nativeImage = nullptr; this->nativeImage = nullptr;
OH_LOG_INFO(LOG_APP, "销毁nativeImage");
}
if(this->nativeWindow != nullptr) {
OH_NativeWindow_DestroyNativeWindow(this->nativeWindow); OH_NativeWindow_DestroyNativeWindow(this->nativeWindow);
this->nativeWindow = nullptr; this->nativeWindow = nullptr;
OH_LOG_INFO(LOG_APP, "销毁nativeWindow");
}
} }
static void onError(Camera_VideoOutput* videoOutput, Camera_ErrorCode errorCode) { static void onError(Camera_VideoOutput* videoOutput, Camera_ErrorCode errorCode) {