WebRTC库在Android端后台运行时内存和功耗高的问题修改

1、在房间数超过10个后,so底层库在创建socket时报文件句柄太多导致tombstone的问题

怀疑是libjingle的so被加载多次,导致占用内存过大引起,解决方法:修改PeerconnectionClient中的PeerconnectionFactory对象factory为单实例对象,多个房间共享一个factory,经过验证,可以创建20多个房间都不在产生这个现象。

2、后台运行时,worker_thread持续占用cpu,导致后台功耗居高不下的问题
在CreatePeerConnectionFactoryForJava方法中,创建了3个线程,作为业务的运行线程:

  std::unique_ptr<rtc::Thread> network_thread =
      rtc::Thread::CreateWithSocketServer();
  network_thread->SetName("network_thread", nullptr);
  RTC_CHECK(network_thread->Start()) << "Failed to start thread";

  std::unique_ptr<rtc::Thread> worker_thread = rtc::Thread::Create();
  worker_thread->SetName("worker_thread", nullptr);
  RTC_CHECK(worker_thread->Start()) << "Failed to start thread";

  std::unique_ptr<rtc::Thread> signaling_thread = rtc::Thread::Create();
  signaling_thread->SetName("signaling_thread", NULL);
  RTC_CHECK(signaling_thread->Start()) << "Failed to start thread";

主要修改思路是,延迟线程的运行时间间隔,关闭不需要运行的线程:

android\webrtc\src\modules\congestion_controller\send_side_congestion_controller.cc
int64_t SendSideCongestionController::TimeUntilNextProcess() {
  return 60 * 1000; 
  //return bitrate_controller_->TimeUntilNextProcess();
}


android\webrtc\src\call\call.cc 

      worker_queue_("call_worker_queue") {
  RTC_DCHECK(config.event_log != nullptr);
  transport_send->RegisterTargetTransferRateObserver(this);
  transport_send_ = std::move(transport_send);

  call_stats_->RegisterStatsObserver(&receive_side_cc_);
  call_stats_->RegisterStatsObserver(transport_send_->GetCallStatsObserver());

  module_process_thread_->RegisterModule(
      receive_side_cc_.GetRemoteBitrateEstimator(true), RTC_FROM_HERE);
  module_process_thread_->RegisterModule(call_stats_.get(), RTC_FROM_HERE);//lyz@xdja.com delete for power energy @2018/12/20
  module_process_thread_->RegisterModule(&receive_side_cc_, RTC_FROM_HERE);
  //module_process_thread_->Start();//delete for power energy @2018/12/20
}

Call::~Call() {
  RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);

  RTC_CHECK(audio_send_ssrcs_.empty());
  RTC_CHECK(video_send_ssrcs_.empty());
  RTC_CHECK(video_send_streams_.empty());
  RTC_CHECK(audio_receive_streams_.empty());
  RTC_CHECK(video_receive_streams_.empty());

  module_process_thread_->DeRegisterModule(
      receive_side_cc_.GetRemoteBitrateEstimator(true));
  module_process_thread_->DeRegisterModule(&receive_side_cc_);
  module_process_thread_->DeRegisterModule(call_stats_.get());
  //module_process_thread_->Stop();//delete for power energy @2018/12/20


RtpTransportControllerSend::RtpTransportControllerSend(
    Clock* clock,
    webrtc::RtcEventLog* event_log,
    const BitrateConstraints& bitrate_config)
    : clock_(clock),
      pacer_(clock, &packet_router_, event_log),
      bitrate_configurator_(bitrate_config),
      process_thread_(ProcessThread::Create("SendControllerThread")),
      observer_(nullptr),
      send_side_cc_(CreateController(clock,
                                     event_log,
                                     &pacer_,
                                     bitrate_config,
                                     TaskQueueExperimentEnabled())) {
  send_side_cc_ptr_ = send_side_cc_.get();
  process_thread_->RegisterModule(&pacer_, RTC_FROM_HERE);
  process_thread_->RegisterModule(send_side_cc_.get(), RTC_FROM_HERE);
  //process_thread_->Start();  // delete for energy 2018-12-20
}


RtpTransportControllerSend::~RtpTransportControllerSend() {
  //process_thread_->Stop(); // delete for energy 2018-12-20
  process_thread_->DeRegisterModule(send_side_cc_.get());
  process_thread_->DeRegisterModule(&pacer_);
}



rtc_base.cc

打印出线程发送message被调用的地方,

    if (cmsLoop != kForever) {

      RTC_LOG(LS_INFO) << name_ << ": temp webrtc thread run after:"<< msEnd;

      cmsNext = static_cast<int>(TimeUntil(msEnd));

      if (cmsNext < 0)

        return true;

    }else{

           RTC_LOG(LS_INFO) << name_ << ": temp webrtc thread run kForever:"<< msEnd << ":"<<msg.message_id << ":" << msg.posted_from.ToString();

 

         }

 

找到

void AudioState::SetPlayout(bool enabled) {

  RTC_LOG(INFO) << "SetPlayout(" << enabled << ")";

  RTC_DCHECK(thread_checker_.CalledOnValidThread());

  if (playout_enabled_ != enabled) {

    playout_enabled_ = enabled;

    if (enabled) {

      null_audio_poller_.reset();

      if (!receiving_streams_.empty()) {

                 RTC_LOG(INFO) << "InitPlayout(" << enabled << ")";

             if (config_.audio_device_module->InitPlayout() == 0) {

                 config_.audio_device_module->StartPlayout();

             }

      }

    } else {

      config_.audio_device_module->StopPlayout();

           /*

      null_audio_poller_ =

          rtc::MakeUnique<NullAudioPoller>(&audio_transport_);*///delete for power energy by 20181/2/24.

    }

  }

}

 

12-24 20:22:26.683 10487-10618/? I/libjingle: (thread.cc:503): network_thread: temp webrtc thread run kForever:0:1:UpdateNetworksContinually@../../rtc_base/network.cc:896

12-24 20:22:27.597 10487-10619/? I/libjingle: (thread.cc:503): worker_thread: temp webrtc thread run kForever:0:0:OnPacketReceived@../../pc/channel.cc:541

12-24 20:22:27.597 10487-10619/? I/libjingle: (thread.cc:503): worker_thread: temp webrtc thread run kForever:0:0:OnPacketReceived@../../pc/channel.cc:541

12-24 20:22:28.600 10487-10619/? I/libjingle: (thread.cc:503): worker_thread: temp webrtc thread run kForever:0:0:OnPacketReceived@../../pc/channel.cc:541

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值