Skip to content

Commit

Permalink
style: better safety
Browse files Browse the repository at this point in the history
  • Loading branch information
gaowanlu committed May 4, 2024
1 parent cbef139 commit b8eb699
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
32 changes: 21 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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
3 changes: 3 additions & 0 deletions src/encode_thread/encode_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -71,3 +73,4 @@ void *EncodeThread::func(void *ptr)
m_encoder->run();
return nullptr;
}

12 changes: 10 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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});
}
};

Expand Down

0 comments on commit b8eb699

Please sign in to comment.