Skip to content

Commit

Permalink
Revert "chore: optimize"
Browse files Browse the repository at this point in the history
This reverts commit 29aa87d.
  • Loading branch information
guoxianzhe committed May 11, 2024
1 parent 3240684 commit c844528
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 22 deletions.
25 changes: 25 additions & 0 deletions source_code/agora_node_ext/agora_electron_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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<void **>(&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;
Expand Down
1 change: 1 addition & 0 deletions source_code/agora_node_ext/agora_electron_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
46 changes: 44 additions & 2 deletions source_code/agora_node_ext/node_iris_event_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<unsigned char> 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
3 changes: 3 additions & 0 deletions source_code/agora_node_ext/node_iris_event_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion ts/Decoder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
51 changes: 32 additions & 19 deletions ts/Renderer/WebCodecsRendererCache.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
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';

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;
Expand All @@ -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()
);
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions ts/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ export interface IAgoraElectronBridge {
) => void
): void;

UnEvent(callbackName: string): void;

CallApi(
funcName: string,
params: any,
Expand Down

0 comments on commit c844528

Please sign in to comment.