From 29aa87d61cc7de6de1b0b40d65555f5e64e3c889 Mon Sep 17 00:00:00 2001 From: gxz Date: Sat, 11 May 2024 17:02:28 +0800 Subject: [PATCH] chore: optimize --- .../agora_node_ext/agora_electron_bridge.cpp | 25 --------- .../agora_node_ext/agora_electron_bridge.h | 1 - .../node_iris_event_handler.cpp | 46 +---------------- .../agora_node_ext/node_iris_event_handler.h | 3 -- ts/Decoder/index.ts | 2 +- ts/Renderer/WebCodecsRendererCache.ts | 51 +++++++------------ ts/Types.ts | 2 - 7 files changed, 22 insertions(+), 108 deletions(-) diff --git a/source_code/agora_node_ext/agora_electron_bridge.cpp b/source_code/agora_node_ext/agora_electron_bridge.cpp index b06b508b4..440e0966a 100644 --- a/source_code/agora_node_ext/agora_electron_bridge.cpp +++ b/source_code/agora_node_ext/agora_electron_bridge.cpp @@ -40,7 +40,6 @@ napi_value AgoraElectronBridge::Init(napi_env env, napi_value exports) { napi_property_descriptor properties[] = { DECLARE_NAPI_METHOD("CallApi", CallApi), DECLARE_NAPI_METHOD("OnEvent", OnEvent), - DECLARE_NAPI_METHOD("UnEvent", UnEvent), DECLARE_NAPI_METHOD("GetBuffer", GetBuffer), DECLARE_NAPI_METHOD("EnableVideoFrameCache", EnableVideoFrameCache), DECLARE_NAPI_METHOD("DisableVideoFrameCache", DisableVideoFrameCache), @@ -251,30 +250,6 @@ napi_value AgoraElectronBridge::OnEvent(napi_env env, napi_callback_info info) { RETURE_NAPI_OBJ(); } -napi_value AgoraElectronBridge::UnEvent(napi_env env, napi_callback_info info) { - napi_status status; - size_t argc = 2; - napi_value args[2]; - napi_value jsthis; - int ret = ERR_FAILED; - status = napi_get_cb_info(env, info, &argc, args, &jsthis, nullptr); - assert(status == napi_ok); - - AgoraElectronBridge *agoraElectronBridge; - status = - napi_unwrap(env, jsthis, reinterpret_cast(&agoraElectronBridge)); - assert(status == napi_ok); - - std::string eventName = ""; - status = napi_get_value_utf8string(env, args[0], eventName); - assert(status == napi_ok); - - agoraElectronBridge->_iris_rtc_event_handler->removeEvent(eventName); - ret = ERR_OK; - - RETURE_NAPI_OBJ(); -} - napi_value AgoraElectronBridge::SetAddonLogFile(napi_env env, napi_callback_info info) { napi_status status; diff --git a/source_code/agora_node_ext/agora_electron_bridge.h b/source_code/agora_node_ext/agora_electron_bridge.h index dcdafcfee..bd8672002 100644 --- a/source_code/agora_node_ext/agora_electron_bridge.h +++ b/source_code/agora_node_ext/agora_electron_bridge.h @@ -34,7 +34,6 @@ class AgoraElectronBridge { static napi_value CallApi(napi_env env, napi_callback_info info); static napi_value GetBuffer(napi_env env, napi_callback_info info); static napi_value OnEvent(napi_env env, napi_callback_info info); - static napi_value UnEvent(napi_env env, napi_callback_info info); static napi_value EnableVideoFrameCache(napi_env env, napi_callback_info info); static napi_value DisableVideoFrameCache(napi_env env, diff --git a/source_code/agora_node_ext/node_iris_event_handler.cpp b/source_code/agora_node_ext/node_iris_event_handler.cpp index 33a5dee9c..706ef51ea 100644 --- a/source_code/agora_node_ext/node_iris_event_handler.cpp +++ b/source_code/agora_node_ext/node_iris_event_handler.cpp @@ -53,14 +53,8 @@ void NodeIrisEventHandler::removeEvent(const std::string &eventName) { } void NodeIrisEventHandler::OnEvent(EventParam *param) { - const char *event = "VideoEncodedFrameObserver_onEncodedVideoFrameReceived"; - - if (strcmp(event, param->event) == 0) { - onEncodedVideoFrameReceived(param->data, param->buffer[0], param->length); - } else { - fireEvent(_callback_key, param->event, param->data, param->buffer, - param->length, param->buffer_count); - } + fireEvent(_callback_key, param->event, param->data, param->buffer, + param->length, param->buffer_count); } void NodeIrisEventHandler::fireEvent(const char *callback_name, @@ -134,42 +128,6 @@ void NodeIrisEventHandler::fireEvent(const char *callback_name, }); } -void NodeIrisEventHandler::onEncodedVideoFrameReceived(const char *data, - void *buffer, - unsigned int *length) { - std::string eventData = ""; - if (data) { eventData = data; } - std::vector buffer_data(length[0]); - memcpy(buffer_data.data(), buffer, length[0]); - - unsigned int buffer_length = length[0]; - - node_async_call::async_call([this, eventData, buffer_data, buffer_length] { - auto it = _callbacks.find("call_back_with_encoded_video_frame"); - if (it != _callbacks.end()) { - size_t argc = 2; - napi_value args[2]; - napi_value result; - napi_status status; - status = napi_create_string_utf8(it->second->env, eventData.c_str(), - eventData.length(), &args[0]); - - napi_create_buffer_copy(it->second->env, buffer_length, - buffer_data.data(), nullptr, &args[1]); - - napi_value call_back_value; - status = napi_get_reference_value( - it->second->env, it->second->call_back_ref, &call_back_value); - - napi_value recv_value; - status = napi_get_undefined(it->second->env, &recv_value); - - status = napi_call_function(it->second->env, recv_value, call_back_value, - argc, args, &result); - } - }); -} - }// namespace electron }// namespace rtc }// namespace agora diff --git a/source_code/agora_node_ext/node_iris_event_handler.h b/source_code/agora_node_ext/node_iris_event_handler.h index 863674ebd..7b65bf043 100644 --- a/source_code/agora_node_ext/node_iris_event_handler.h +++ b/source_code/agora_node_ext/node_iris_event_handler.h @@ -31,9 +31,6 @@ class NodeIrisEventHandler : public iris::IrisEventHandler { void **buffer, unsigned int *length, unsigned int buffer_count); - void onEncodedVideoFrameReceived(const char *data, void *buffer, - unsigned int *length); - void addEvent(const std::string &eventName, napi_env &env, napi_value &call_bcak, napi_value &global); diff --git a/ts/Decoder/index.ts b/ts/Decoder/index.ts index 7c6cec465..85deb0007 100644 --- a/ts/Decoder/index.ts +++ b/ts/Decoder/index.ts @@ -122,7 +122,7 @@ export class WebCodecsDecoder { this._currentCodecConfig?.codedWidth !== frameInfo.width || this._currentCodecConfig?.codedHeight !== frameInfo.height ) { - logInfo('codecType is changed, reconfigure decoder'); + logInfo('frameInfo has changed, reconfigure decoder'); this._decoder.reset(); this.decoderConfigure(frameInfo); } diff --git a/ts/Renderer/WebCodecsRendererCache.ts b/ts/Renderer/WebCodecsRendererCache.ts index ef9a9471a..912a11255 100644 --- a/ts/Renderer/WebCodecsRendererCache.ts +++ b/ts/Renderer/WebCodecsRendererCache.ts @@ -1,8 +1,8 @@ import createAgoraRtcEngine from '../AgoraSdk'; import { WebCodecsDecoder } from '../Decoder/index'; import { EncodedVideoFrameInfo, VideoStreamType } from '../Private/AgoraBase'; +import { IVideoEncodedFrameObserver } from '../Private/AgoraMediaBase'; import { IRtcEngineEx } from '../Private/IAgoraRtcEngineEx'; -import { AgoraElectronBridge } from '../Private/internal/IrisApiEngine'; import { RendererContext, RendererType } from '../Types'; import { AgoraEnv, logInfo } from '../Utils'; @@ -10,7 +10,10 @@ import { AgoraEnv, logInfo } from '../Utils'; import { IRendererCache } from './IRendererCache'; import { WebCodecsRenderer } from './WebCodecsRenderer/index'; -export class WebCodecsRendererCache extends IRendererCache { +export class WebCodecsRendererCache + extends IRendererCache + implements IVideoEncodedFrameObserver +{ private _decoder?: WebCodecsDecoder | null; private _engine?: IRtcEngineEx; private _firstFrame = true; @@ -31,44 +34,38 @@ export class WebCodecsRendererCache extends IRendererCache { AgoraEnv.AgoraRendererManager?.handleWebCodecsFallback(this.cacheContext); } - onEncodedVideoFrameReceived(...[data, buffer]: any) { - let _data: any; - try { - _data = JSON.parse(data) ?? {}; - } catch (e) { - _data = {}; - } - if ( - Object.keys(_data).length === 0 || - !this._decoder || - this.cacheContext.uid !== _data.uid - ) - return; + onEncodedVideoFrameReceived( + uid: number, + imageBuffer: Uint8Array, + length: number, + videoEncodedFrameInfo: EncodedVideoFrameInfo + ) { + if (!this._decoder || this.cacheContext.uid !== uid) return; if (this._firstFrame) { for (let renderer of this.renderers) { if (renderer.rendererType !== RendererType.WEBCODECSRENDERER) { continue; } renderer.bind(renderer.context.view, { - width: _data.videoEncodedFrameInfo.width!, - height: _data.videoEncodedFrameInfo.height!, + width: videoEncodedFrameInfo.width!, + height: videoEncodedFrameInfo.height!, }); } try { - this._decoder.decoderConfigure(_data.videoEncodedFrameInfo); + this._decoder.decoderConfigure(videoEncodedFrameInfo); } catch (error: any) { logInfo(error); return; } this._firstFrame = false; } - if (this.shouldFallback(_data.videoEncodedFrameInfo)) { + if (this.shouldFallback(videoEncodedFrameInfo)) { AgoraEnv.AgoraRendererManager?.handleWebCodecsFallback(this.cacheContext); } else { this._decoder.decodeFrame( - buffer, - _data.videoEncodedFrameInfo, + imageBuffer, + videoEncodedFrameInfo, new Date().getTime() ); } @@ -79,16 +76,6 @@ export class WebCodecsRendererCache extends IRendererCache { type: VideoStreamType.VideoStreamHigh, encodedFrameOnly: true, }); - AgoraElectronBridge.OnEvent( - 'call_back_with_encoded_video_frame', - (...params: any) => { - try { - this.onEncodedVideoFrameReceived(...params); - } catch (e) { - console.error(e); - } - } - ); } public shouldFallback(frameInfo: EncodedVideoFrameInfo): boolean { @@ -116,7 +103,7 @@ export class WebCodecsRendererCache extends IRendererCache { } public release(): void { - AgoraElectronBridge.UnEvent('call_back_with_encoded_video_frame'); + logInfo('call_back_with_encoded_video_frame release'); this._decoder?.release(); this._decoder = null; super.release(); diff --git a/ts/Types.ts b/ts/Types.ts index 2752ccc3f..cfe4c05cc 100644 --- a/ts/Types.ts +++ b/ts/Types.ts @@ -117,8 +117,6 @@ export interface IAgoraElectronBridge { ) => void ): void; - UnEvent(callbackName: string): void; - CallApi( funcName: string, params: any,