[*] 优化

This commit is contained in:
acgist
2024-05-11 16:09:31 +08:00
parent 70ca1d0146
commit 916f89fb68
3 changed files with 66 additions and 57 deletions

View File

@@ -164,15 +164,14 @@ static napi_value init(napi_env env, napi_callback_info info) {
napi_create_reference(env, args[1], 1, &acgist::pushRef); napi_create_reference(env, args[1], 1, &acgist::pushRef);
napi_create_reference(env, args[2], 1, &acgist::requestRef); napi_create_reference(env, args[2], 1, &acgist::requestRef);
printSupportCodec(); printSupportCodec();
// acgist::clientId = json["clientId"]; acgist::clientId = json["clientId"];
// acgist::name = json["name"]; acgist::name = json["name"];
// OH_LOG_INFO(LOG_APP, "加载libtaoyao"); OH_LOG_INFO(LOG_APP, "加载libtaoyao");
// std::string version = mediasoupclient::Version(); std::string version = mediasoupclient::Version();
// OH_LOG_INFO(LOG_APP, "加载MediasoupClient%s", version.data()); OH_LOG_INFO(LOG_APP, "加载MediasoupClient%s", version.data());
// mediasoupclient::Initialize(); mediasoupclient::Initialize();
// OH_LOG_INFO(LOG_APP, "加载媒体功能"); OH_LOG_INFO(LOG_APP, "加载媒体功能");
// mediaManager = new MediaManager(); mediaManager = new MediaManager();
// mediaManager->init();
napi_create_int32(env, 0, &ret); napi_create_int32(env, 0, &ret);
return ret; return ret;
} }

View File

@@ -20,6 +20,9 @@
namespace acgist { namespace acgist {
/**
* 媒体管理器
*/
class MediaManager { class MediaManager {
public: public:
@@ -27,24 +30,34 @@ public:
virtual ~MediaManager(); virtual ~MediaManager();
public: public:
// 本地终端数量
int localClientRef = 0; int localClientRef = 0;
#if __TAOYAO_AUDIO_LOCAL__ #if __TAOYAO_AUDIO_LOCAL__
// 音频来源:本地创建
acgist::TaoyaoAudioTrackSource* audioTrackSource = nullptr; acgist::TaoyaoAudioTrackSource* audioTrackSource = nullptr;
#else #else
// 音频来源:设备管理
rtc::scoped_refptr<webrtc::AudioSourceInterface> audioTrackSource = nullptr; rtc::scoped_refptr<webrtc::AudioSourceInterface> audioTrackSource = nullptr;
#endif #endif
// 视频来源
acgist::TaoyaoVideoTrackSource* videoTrackSource = nullptr; acgist::TaoyaoVideoTrackSource* videoTrackSource = nullptr;
// 音频采集
acgist::AudioCapturer* audioCapturer = nullptr; acgist::AudioCapturer* audioCapturer = nullptr;
// 视频采集
acgist::VideoCapturer* videoCapturer = nullptr; acgist::VideoCapturer* videoCapturer = nullptr;
// 网络线程
std::unique_ptr<rtc::Thread> networkThread = nullptr; std::unique_ptr<rtc::Thread> networkThread = nullptr;
// 信令线程
std::unique_ptr<rtc::Thread> signalingThread = nullptr; std::unique_ptr<rtc::Thread> signalingThread = nullptr;
// 工作线程
std::unique_ptr<rtc::Thread> workerThread = nullptr; std::unique_ptr<rtc::Thread> workerThread = nullptr;
// 连接工厂
rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> peerConnectionFactory = nullptr; rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> peerConnectionFactory = nullptr;
protected: protected:
// 加载PC工厂 // 加载连接工厂
bool newPeerConnectionFactory(); bool newPeerConnectionFactory();
// 释放PC工厂 // 释放连接工厂
bool releasePeerConnectionFactory(); bool releasePeerConnectionFactory();
// 开始采集 // 开始采集
bool startCapture(); bool startCapture();
@@ -60,8 +73,6 @@ protected:
bool stopVideoCapture(); bool stopVideoCapture();
public: public:
// 加载媒体
bool init();
// 新增本地终端 // 新增本地终端
int newLocalClient(); int newLocalClient();
// 释放本地终端 // 释放本地终端

View File

@@ -20,11 +20,6 @@ acgist::MediaManager::MediaManager() {
} }
acgist::MediaManager::~MediaManager() { acgist::MediaManager::~MediaManager() {
// TODO验证是否需要释放线程和工厂
}
bool acgist::MediaManager::init() {
return true;
} }
bool acgist::MediaManager::newPeerConnectionFactory() { bool acgist::MediaManager::newPeerConnectionFactory() {
@@ -33,22 +28,30 @@ bool acgist::MediaManager::newPeerConnectionFactory() {
} }
OH_LOG_INFO(LOG_APP, "加载PeerConnectionFactory"); OH_LOG_INFO(LOG_APP, "加载PeerConnectionFactory");
this->networkThread = rtc::Thread::CreateWithSocketServer(); this->networkThread = rtc::Thread::CreateWithSocketServer();
this->signalingThread = rtc::Thread::Create();
this->workerThread = rtc::Thread::Create(); this->workerThread = rtc::Thread::Create();
this->signalingThread = rtc::Thread::Create();
this->networkThread->SetName("network_thread", nullptr); this->networkThread->SetName("network_thread", nullptr);
this->signalingThread->SetName("signaling_thread", nullptr);
this->workerThread->SetName("worker_thread", nullptr); this->workerThread->SetName("worker_thread", nullptr);
if (!this->networkThread->Start() || !this->signalingThread->Start() || !this->workerThread->Start()) { this->signalingThread->SetName("signaling_thread", nullptr);
if (
!this->networkThread->Start() ||
!this->signalingThread->Start() ||
!this->workerThread->Start()
) {
OH_LOG_WARN(LOG_APP, "WebRTC线程启动失败"); OH_LOG_WARN(LOG_APP, "WebRTC线程启动失败");
// TODO: 释放线程 this->networkThread = nullptr;
this->workerThread = nullptr;
this->signalingThread = nullptr;
return false; return false;
} }
this->peerConnectionFactory = webrtc::CreatePeerConnectionFactory( this->peerConnectionFactory = webrtc::CreatePeerConnectionFactory(
// 网络线程
this->networkThread.get(), this->networkThread.get(),
// 工作线程:可以使用信令线程
this->workerThread.get(), this->workerThread.get(),
// this->signalingThread.get(), // 信令线程
this->signalingThread.get(), this->signalingThread.get(),
// 音频设备 // 音频设备:为空自动创建
nullptr, nullptr,
// 音频编码 // 音频编码
webrtc::CreateBuiltinAudioEncoderFactory(), webrtc::CreateBuiltinAudioEncoderFactory(),
@@ -58,12 +61,12 @@ bool acgist::MediaManager::newPeerConnectionFactory() {
webrtc::CreateBuiltinVideoEncoderFactory(), webrtc::CreateBuiltinVideoEncoderFactory(),
// 视频解码 // 视频解码
webrtc::CreateBuiltinVideoDecoderFactory(), webrtc::CreateBuiltinVideoDecoderFactory(),
// 混音 // 混音处理
nullptr, nullptr,
// 音频处理 // 音频处理
nullptr nullptr
); );
return this->peerConnectionFactory != nullptr; return true;
} }
bool acgist::MediaManager::releasePeerConnectionFactory() { bool acgist::MediaManager::releasePeerConnectionFactory() {
@@ -71,11 +74,9 @@ bool acgist::MediaManager::releasePeerConnectionFactory() {
return true; return true;
} }
OH_LOG_INFO(LOG_APP, "释放PeerConnectionFactory"); OH_LOG_INFO(LOG_APP, "释放PeerConnectionFactory");
if(this->peerConnectionFactory != nullptr) { this->peerConnectionFactory->Release();
this->peerConnectionFactory->Release(); // delete this->peerConnectionFactory;
// delete this->peerConnectionFactory; this->peerConnectionFactory = nullptr;
this->peerConnectionFactory = nullptr;
}
return true; return true;
} }
@@ -83,12 +84,10 @@ int acgist::MediaManager::newLocalClient() {
{ {
std::lock_guard<std::recursive_mutex> mediaLock(mediaMutex); std::lock_guard<std::recursive_mutex> mediaLock(mediaMutex);
this->localClientRef++; this->localClientRef++;
if(this->localClientRef > 0) { this->newPeerConnectionFactory();
this->newPeerConnectionFactory(); this->startCapture();
this->startCapture();
}
} }
OH_LOG_WARN(LOG_APP, "打开本地终端:%d", this->localClientRef); OH_LOG_INFO(LOG_APP, "打开本地终端:%d", this->localClientRef);
return this->localClientRef; return this->localClientRef;
} }
@@ -105,7 +104,7 @@ int acgist::MediaManager::releaseLocalClient() {
} }
} }
} }
OH_LOG_WARN(LOG_APP, "关闭本地终端:%d", this->localClientRef); OH_LOG_INFO(LOG_APP, "关闭本地终端:%d", this->localClientRef);
return this->localClientRef; return this->localClientRef;
} }
@@ -118,18 +117,18 @@ bool acgist::MediaManager::startCapture() {
bool acgist::MediaManager::startAudioCapture() { bool acgist::MediaManager::startAudioCapture() {
#if __TAOYAO_AUDIO_LOCAL__ #if __TAOYAO_AUDIO_LOCAL__
if (this->audioCapturer == nullptr) { if (this->audioCapturer == nullptr) {
OH_LOG_WARN(LOG_APP, "开始音频采集"); OH_LOG_INFO(LOG_APP, "开始音频采集");
this->audioCapturer = new acgist::AudioCapturer(); this->audioCapturer = new acgist::AudioCapturer();
this->audioCapturer->start(); this->audioCapturer->start();
} }
if(this->audioTrackSource == nullptr) { if(this->audioTrackSource == nullptr) {
OH_LOG_WARN(LOG_APP, "设置音频来源"); OH_LOG_INFO(LOG_APP, "设置音频来源");
this->audioTrackSource = new rtc::RefCountedObject<acgist::TaoyaoAudioTrackSource>(); this->audioTrackSource = new rtc::RefCountedObject<acgist::TaoyaoAudioTrackSource>();
this->audioCapturer->source = this->audioTrackSource; this->audioCapturer->source = this->audioTrackSource;
} }
#else #else
if(this->audioTrackSource == nullptr) { if(this->audioTrackSource == nullptr) {
OH_LOG_WARN(LOG_APP, "设置音频来源"); OH_LOG_INFO(LOG_APP, "设置音频来源");
cricket::AudioOptions options; cricket::AudioOptions options;
options.highpass_filter = true; options.highpass_filter = true;
options.auto_gain_control = true; options.auto_gain_control = true;
@@ -143,12 +142,12 @@ bool acgist::MediaManager::startAudioCapture() {
bool acgist::MediaManager::startVideoCapture() { bool acgist::MediaManager::startVideoCapture() {
if(this->videoCapturer == nullptr) { if(this->videoCapturer == nullptr) {
OH_LOG_WARN(LOG_APP, "开始视频采集"); OH_LOG_INFO(LOG_APP, "开始视频采集");
this->videoCapturer = new acgist::VideoCapturer(); this->videoCapturer = new acgist::VideoCapturer();
this->videoCapturer->start(); this->videoCapturer->start();
} }
if(this->videoTrackSource == nullptr) { if(this->videoTrackSource == nullptr) {
OH_LOG_WARN(LOG_APP, "设置视频来源"); OH_LOG_INFO(LOG_APP, "设置视频来源");
this->videoTrackSource = new rtc::RefCountedObject<acgist::TaoyaoVideoTrackSource>(); this->videoTrackSource = new rtc::RefCountedObject<acgist::TaoyaoVideoTrackSource>();
this->videoCapturer->source = this->videoTrackSource; this->videoCapturer->source = this->videoTrackSource;
} }
@@ -163,21 +162,21 @@ bool acgist::MediaManager::stopCapture() {
bool acgist::MediaManager::stopAudioCapture() { bool acgist::MediaManager::stopAudioCapture() {
#if __TAOYAO_AUDIO_LOCAL__ #if __TAOYAO_AUDIO_LOCAL__
if(this->audioCapturer != nullptr) { if(this->audioCapturer != nullptr) {
OH_LOG_WARN(LOG_APP, "停止音频采集"); OH_LOG_INFO(LOG_APP, "停止音频采集");
this->audioCapturer->stop(); this->audioCapturer->stop();
delete this->audioCapturer; delete this->audioCapturer;
this->audioCapturer = nullptr; this->audioCapturer = nullptr;
} }
if(this->audioTrackSource != nullptr) { if(this->audioTrackSource != nullptr) {
OH_LOG_WARN(LOG_APP, "释放音频来源"); OH_LOG_INFO(LOG_APP, "释放音频来源");
this->audioTrackSource->Release(); this->audioTrackSource->Release();
// delete this->AudioTrackSource; // delete this->AudioTrackSource;
this->audioTrackSource = nullptr; this->audioTrackSource = nullptr;
} }
#else #else
if(this->audioTrackSource != nullptr) { if(this->audioTrackSource != nullptr) {
OH_LOG_WARN(LOG_APP, "释放音频来源"); OH_LOG_INFO(LOG_APP, "释放音频来源");
this->audioTrackSource->Release(); this->audioTrackSource->Release();
// delete this->audioTrackSource; // delete this->audioTrackSource;
this->audioTrackSource = nullptr; this->audioTrackSource = nullptr;
@@ -188,13 +187,13 @@ bool acgist::MediaManager::stopAudioCapture() {
bool acgist::MediaManager::stopVideoCapture() { bool acgist::MediaManager::stopVideoCapture() {
if(this->videoCapturer != nullptr) { if(this->videoCapturer != nullptr) {
OH_LOG_WARN(LOG_APP, "停止视频采集"); OH_LOG_INFO(LOG_APP, "停止视频采集");
this->videoCapturer->stop(); this->videoCapturer->stop();
delete this->videoCapturer; delete this->videoCapturer;
this->videoCapturer = nullptr; this->videoCapturer = nullptr;
} }
if(this->videoTrackSource != nullptr) { if(this->videoTrackSource != nullptr) {
OH_LOG_WARN(LOG_APP, "释放视频来源"); OH_LOG_INFO(LOG_APP, "释放视频来源");
this->videoTrackSource->Release(); this->videoTrackSource->Release();
// delete this->videoTrackSource; // delete this->videoTrackSource;
this->videoTrackSource = nullptr; this->videoTrackSource = nullptr;