From b8eb69958f5086f859ccc241caaf9948e57940da Mon Sep 17 00:00:00 2001 From: gaowanlu <2209120827@qq.com> Date: Sun, 5 May 2024 02:58:37 +0800 Subject: [PATCH] style: better safety --- index.js | 32 +++++++++++++++++++---------- src/encode_thread/encode_thread.cpp | 3 +++ src/main.cpp | 12 +++++++++-- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index d1134b2..31f919f 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,13 @@ -const openlive = require('./build/Release/openlive'); -const express = require('express'); -const app = express(); -const server = require('http').Server(app); -const io = require('socket.io')(server); +const openlive = require('./build/Release/openlive'); // import openlive cpp moudle +const express = require('express'); // static web server with express -app.use(express.static('app')); +const app = express(); // create express instance +const server = require('http').Server(app); // http server +const io = require('socket.io')(server); // socket.io ws server for broadcast h264 stream +app.use(express.static('app')); // init static resource path + +// ws init io.on('connection', (socket) => { console.log('a user connected'); socket.on('chat message', (msg) => { @@ -16,27 +18,35 @@ io.on('connection', (socket) => { }); }); +// static http server init server.listen(20002, "0.0.0.0", () => { console.log('listening on *:20002'); }); -//C++ execute +// openlive config openlive.setConf({ "path": "0", "encodeBufferLen": 5, "captureBufferLen": 5 }); +// loop stream reader callback const getInfo = () => { openlive.getMat((res) => { - io.emit('chat message', res); - setTimeout(getInfo, 1); + if(res && res.length > 0) + { + io.emit('chat message', res); + } + else + { + console.error("res null or res.length <= 0"); + } + setTimeout(getInfo, 1); }); } +// start reader loop if (openlive.start()) { getInfo(); } -//netsh interface portproxy add v4tov4 listenport=20002 listenaddress=10.34.119.245 connectport=20002 connectaddress=127.0.0.1 -//netsh interface portproxy delete v4tov4 listenport=20002 listenaddress=0.0.0.0 diff --git a/src/encode_thread/encode_thread.cpp b/src/encode_thread/encode_thread.cpp index 255282b..a2f0984 100644 --- a/src/encode_thread/encode_thread.cpp +++ b/src/encode_thread/encode_thread.cpp @@ -40,10 +40,12 @@ void EncodeThread::run() // 转为base64 std::string base64 = base64_encode(buf, ret_size); pthread_mutex_lock(&mat_buf_mutex); + // 缓冲满了则剔除第一个 if (mat_buf.size() > Conf::Conf::getEncodeBufferLen()) { mat_buf.erase(mat_buf.begin()); } + // 加入到缓冲的末尾 mat_buf.push_back(base64); pthread_mutex_unlock(&mat_buf_mutex); pthread_cond_broadcast(&mat_buf_cond); @@ -71,3 +73,4 @@ void *EncodeThread::func(void *ptr) m_encoder->run(); return nullptr; } + diff --git a/src/main.cpp b/src/main.cpp index b52e8fd..5e813e1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,8 +24,9 @@ namespace OpenLive MyAsyncWorker(Napi::Function &callback) : Napi::AsyncWorker(callback) { } - ~MyAsyncWorker() // will be call,because using Napi::HandleScope in OnOK + ~MyAsyncWorker() { + // std::cout << "~MyAsyncWorker()" << std::endl; } void Execute() override { @@ -34,7 +35,14 @@ namespace OpenLive void OnOK() override { Napi::HandleScope scope(Napi::AsyncWorker::Env()); - Callback().Call({Napi::String::New(Napi::AsyncWorker::Env(), mat.c_str())}); + Napi::String str2js = Napi::String::New(Napi::AsyncWorker::Env(), mat.c_str(), mat.size()); + Callback().Call({str2js}); + } + void OnError(const Napi::Error& e) override + { + Napi::HandleScope scope(Napi::AsyncWorker::Env()); + Napi::String str2js = Napi::String::New(Napi::AsyncWorker::Env(), "", 0); + Callback().Call({str2js}); } };