Skip to content

Commit

Permalink
fix: RendererManager memory leak (#952)
Browse files Browse the repository at this point in the history
  • Loading branch information
LichKing-2234 authored Mar 14, 2023
1 parent 27897f2 commit 421f802
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
5 changes: 4 additions & 1 deletion ts/Private/internal/RtcEngineExInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import { MusicContentCenterInternal } from './MusicContentCenterInternal';

import { callIrisApi, DeviceEventEmitter, EVENT_TYPE } from './IrisApiEngine';
import { EmitterSubscription } from './emitter/EventEmitter';
import { RendererManager } from '../../Renderer/RendererManager';

const checkers = createCheckers(
AgoraBaseTI,
Expand Down Expand Up @@ -105,7 +106,8 @@ export class RtcEngineExInternal extends IRtcEngineExImpl {
>();

initialize(context: RtcEngineContext): number {
AgoraEnv.AgoraRendererManager?.enableRender();
AgoraEnv.AgoraRendererManager = new RendererManager();
AgoraEnv.AgoraRendererManager.enableRender();
const ret = super.initialize(context);
callIrisApi.call(this, 'RtcEngine_setAppType', {
appType: 3,
Expand All @@ -115,6 +117,7 @@ export class RtcEngineExInternal extends IRtcEngineExImpl {

release(sync: boolean = false) {
AgoraEnv.AgoraRendererManager?.enableRender(false);
AgoraEnv.AgoraRendererManager = undefined;
this._audio_device_manager.release();
this._video_device_manager.release();
this._media_engine.release();
Expand Down
15 changes: 10 additions & 5 deletions ts/Renderer/AgoraView.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { VideoSourceType } from '../Private/AgoraBase';
import { RenderModeType } from '../Private/AgoraMediaBase';
import AgoraRendererManager from './RendererManager';
import { AgoraEnv } from '../Utils';

const VIDEO_SOURCE_TYPE_STRING = 'video-source-type';
const UID_STRING = 'uid';
Expand Down Expand Up @@ -95,6 +95,7 @@ export default class AgoraView extends HTMLElement {
this.removeAttribute(UID_STRING);
}
}

get channelId(): string {
return this.getAttribute(CHANNEL_ID_STRING) || '';
}
Expand Down Expand Up @@ -122,6 +123,7 @@ export default class AgoraView extends HTMLElement {
this.removeAttribute(RENDERER_CONTENT_MODE_STRING);
}
}

get renderMirror(): boolean {
return this.getAttribute(RENDERER_MIRROR_STRING) === 'true';
}
Expand All @@ -137,9 +139,10 @@ export default class AgoraView extends HTMLElement {
constructor() {
super();
}

initializeRender = () => {
AgoraRendererManager.destroyRendererByView(this);
AgoraRendererManager.setupVideo({
AgoraEnv.AgoraRendererManager?.destroyRendererByView(this);
AgoraEnv.AgoraRendererManager?.setupVideo({
videoSourceType: this.videoSourceType,
view: this,
uid: this.uid,
Expand All @@ -150,8 +153,9 @@ export default class AgoraView extends HTMLElement {
},
});
};

destroyRender = () => {
AgoraRendererManager.destroyRendererByView(this);
AgoraEnv.AgoraRendererManager?.destroyRendererByView(this);
};

connectedCallback() {
Expand All @@ -169,7 +173,7 @@ export default class AgoraView extends HTMLElement {
].includes(attrName);

if (isSetRenderOption) {
AgoraRendererManager.setRenderOption(
AgoraEnv.AgoraRendererManager?.setRenderOption(
this,
this.renderContentMode,
this.renderMirror
Expand All @@ -182,6 +186,7 @@ export default class AgoraView extends HTMLElement {
}
this.initializeRender();
}

disconnectedCallback() {
this.isConnectedCallback = false;
this.destroyRender();
Expand Down
25 changes: 5 additions & 20 deletions ts/Renderer/RendererManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { YUVCanvasRenderer } from './YUVCanvasRenderer';
/**
* @ignore
*/
class RendererManager {
export class RendererManager {
/**
* @ignore
*/
Expand Down Expand Up @@ -89,7 +89,7 @@ class RendererManager {
/**
* @ignore
*/
setFPS(fps: number) {
public setFPS(fps: number) {
this.renderFps = fps;
this.restartRender();
}
Expand Down Expand Up @@ -139,7 +139,7 @@ class RendererManager {

public checkWebglEnv(): boolean {
let gl;
const canvas = document.createElement('canvas');
let canvas: HTMLCanvasElement = document.createElement('canvas');

try {
gl =
Expand All @@ -150,11 +150,7 @@ class RendererManager {
return false;
}

if (gl) {
return true;
} else {
return false;
}
return !!gl;
}

public setupVideo(rendererVideoConfig: RendererVideoConfig): number {
Expand All @@ -165,11 +161,7 @@ class RendererManager {

if (!formatConfig.view) {
logWarn('setupVideo->destroyRenderersByConfig, because of view is null');
AgoraRendererManager.destroyRenderersByConfig(
videoSourceType,
channelId,
uid
);
this.destroyRenderersByConfig(videoSourceType, channelId, uid);
return -ErrorCodeType.ErrInvalidArgument;
}

Expand Down Expand Up @@ -551,10 +543,3 @@ class RendererManager {
);
}
}

const AgoraRendererManager = new RendererManager();

AgoraEnv.AgoraRendererManager = AgoraRendererManager;

export default AgoraRendererManager;
export { RendererManager };

0 comments on commit 421f802

Please sign in to comment.