Skip to content

Commit

Permalink
chore: optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
guoxianzhe committed May 11, 2024
1 parent c844528 commit 06525d7
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 46 deletions.
39 changes: 2 additions & 37 deletions source_code/agora_node_ext/node_iris_event_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ void NodeIrisEventHandler::OnEvent(EventParam *param) {
const char *event = "VideoEncodedFrameObserver_onEncodedVideoFrameReceived";

if (strcmp(event, param->event) == 0) {
onEncodedVideoFrameReceived(param->data, param->buffer[0], param->length);
fireEvent(_callback_with_encoded_video_frame_key, param->event, param->data,
param->buffer, param->length, param->buffer_count);
} else {
fireEvent(_callback_key, param->event, param->data, param->buffer,
param->length, param->buffer_count);
Expand Down Expand Up @@ -134,42 +135,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<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
4 changes: 1 addition & 3 deletions source_code/agora_node_ext/node_iris_event_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -42,6 +39,7 @@ class NodeIrisEventHandler : public iris::IrisEventHandler {
private:
std::unordered_map<std::string, EventCallback *> _callbacks;
const char *_callback_key = "call_back_with_buffer";
const char *_callback_with_encoded_video_frame_key = "call_back_with_encoded_video_frame";
};
}// namespace electron
}// namespace rtc
Expand Down
6 changes: 4 additions & 2 deletions ts/Decoder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export class WebCodecsDecoder {
this._decoder = new VideoDecoder({
// @ts-ignore
output: this._output.bind(this),
error: onError,
error: (e) => {
onError(e);
},
});
}

Expand Down Expand Up @@ -122,7 +124,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);
}
Expand Down
9 changes: 5 additions & 4 deletions ts/Renderer/WebCodecsRendererCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ export class WebCodecsRendererCache extends IRendererCache {
this.draw();
}

onDecoderError() {
logInfo('webCodecsDecoder decode failed, fallback to native decoder');
onDecoderError(e: any) {
logInfo('webCodecsDecoder decode failed, fallback to native decoder', e);
AgoraEnv.AgoraRendererManager?.handleWebCodecsFallback(this.cacheContext);
}

onEncodedVideoFrameReceived(...[data, buffer]: any) {
onEncodedVideoFrameReceived(...[event, data, buffers]: any) {
let _data: any;
try {
_data = JSON.parse(data) ?? {};
Expand Down Expand Up @@ -67,7 +67,7 @@ export class WebCodecsRendererCache extends IRendererCache {
AgoraEnv.AgoraRendererManager?.handleWebCodecsFallback(this.cacheContext);
} else {
this._decoder.decodeFrame(
buffer,
buffers[0],
_data.videoEncodedFrameInfo,
new Date().getTime()
);
Expand Down Expand Up @@ -116,6 +116,7 @@ export class WebCodecsRendererCache extends IRendererCache {
}

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;
Expand Down

0 comments on commit 06525d7

Please sign in to comment.