From c844528de5cb301091d9bd39c270db86bd21fa57 Mon Sep 17 00:00:00 2001 From: gxz Date: Sat, 11 May 2024 17:13:57 +0800 Subject: [PATCH] Revert "chore: optimize" This reverts commit 29aa87d61cc7de6de1b0b40d65555f5e64e3c889. --- .../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, 108 insertions(+), 22 deletions(-) diff --git a/source_code/agora_node_ext/agora_electron_bridge.cpp b/source_code/agora_node_ext/agora_electron_bridge.cpp index 440e0966a..b06b508b4 100644 --- a/source_code/agora_node_ext/agora_electron_bridge.cpp +++ b/source_code/agora_node_ext/agora_electron_bridge.cpp @@ -40,6 +40,7 @@ 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), @@ -250,6 +251,30 @@ 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 bd8672002..dcdafcfee 100644 --- a/source_code/agora_node_ext/agora_electron_bridge.h +++ b/source_code/agora_node_ext/agora_electron_bridge.h @@ -34,6 +34,7 @@ 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 706ef51ea..33a5dee9c 100644 --- a/source_code/agora_node_ext/node_iris_event_handler.cpp +++ b/source_code/agora_node_ext/node_iris_event_handler.cpp @@ -53,8 +53,14 @@ void NodeIrisEventHandler::removeEvent(const std::string &eventName) { } void NodeIrisEventHandler::OnEvent(EventParam *param) { - fireEvent(_callback_key, param->event, param->data, param->buffer, - param->length, param->buffer_count); + 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); + } } void NodeIrisEventHandler::fireEvent(const char *callback_name, @@ -128,6 +134,42 @@ 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 7b65bf043..863674ebd 100644 --- a/source_code/agora_node_ext/node_iris_event_handler.h +++ b/source_code/agora_node_ext/node_iris_event_handler.h @@ -31,6 +31,9 @@ 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 85deb0007..7c6cec465 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('frameInfo has changed, reconfigure decoder'); + logInfo('codecType is changed, reconfigure decoder'); this._decoder.reset(); this.decoderConfigure(frameInfo); } diff --git a/ts/Renderer/WebCodecsRendererCache.ts b/ts/Renderer/WebCodecsRendererCache.ts index 912a11255..ef9a9471a 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,10 +10,7 @@ import { AgoraEnv, logInfo } from '../Utils'; import { IRendererCache } from './IRendererCache'; import { WebCodecsRenderer } from './WebCodecsRenderer/index'; -export class WebCodecsRendererCache - extends IRendererCache - implements IVideoEncodedFrameObserver -{ +export class WebCodecsRendererCache extends IRendererCache { private _decoder?: WebCodecsDecoder | null; private _engine?: IRtcEngineEx; private _firstFrame = true; @@ -34,38 +31,44 @@ export class WebCodecsRendererCache AgoraEnv.AgoraRendererManager?.handleWebCodecsFallback(this.cacheContext); } - onEncodedVideoFrameReceived( - uid: number, - imageBuffer: Uint8Array, - length: number, - videoEncodedFrameInfo: EncodedVideoFrameInfo - ) { - if (!this._decoder || this.cacheContext.uid !== uid) return; + 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; if (this._firstFrame) { for (let renderer of this.renderers) { if (renderer.rendererType !== RendererType.WEBCODECSRENDERER) { continue; } renderer.bind(renderer.context.view, { - width: videoEncodedFrameInfo.width!, - height: videoEncodedFrameInfo.height!, + width: _data.videoEncodedFrameInfo.width!, + height: _data.videoEncodedFrameInfo.height!, }); } try { - this._decoder.decoderConfigure(videoEncodedFrameInfo); + this._decoder.decoderConfigure(_data.videoEncodedFrameInfo); } catch (error: any) { logInfo(error); return; } this._firstFrame = false; } - if (this.shouldFallback(videoEncodedFrameInfo)) { + if (this.shouldFallback(_data.videoEncodedFrameInfo)) { AgoraEnv.AgoraRendererManager?.handleWebCodecsFallback(this.cacheContext); } else { this._decoder.decodeFrame( - imageBuffer, - videoEncodedFrameInfo, + buffer, + _data.videoEncodedFrameInfo, new Date().getTime() ); } @@ -76,6 +79,16 @@ export class WebCodecsRendererCache 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 { @@ -103,7 +116,7 @@ export class WebCodecsRendererCache } public release(): void { - logInfo('call_back_with_encoded_video_frame release'); + AgoraElectronBridge.UnEvent('call_back_with_encoded_video_frame'); this._decoder?.release(); this._decoder = null; super.release(); diff --git a/ts/Types.ts b/ts/Types.ts index cfe4c05cc..2752ccc3f 100644 --- a/ts/Types.ts +++ b/ts/Types.ts @@ -117,6 +117,8 @@ export interface IAgoraElectronBridge { ) => void ): void; + UnEvent(callbackName: string): void; + CallApi( funcName: string, params: any,