webrtc之Android视频质量提升:保帧率降码率

2024-03-16 00:08

本文主要是介绍webrtc之Android视频质量提升:保帧率降码率,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言:

我们的产品是在一款跑着Android系统的特定芯片上使用webrtc开发的一个视频通话业务,当前的情况是在网络正常的情况下帧率也比较低,弱网环境下适应能力较差。基于此,我了解了webrtc Android平台的视频采集编码流程,并编写了相应的文章《webrtc之Android视频采集编码》。在深入细节之后,定位问题所在,并做了相应优化,在此记录相关过程,以后日后复习,以及和大家共同学习。
 

帧率低下问题定位与优化

问题定位:

在采集到编码的这条链路上进行帧率的统计,看一下那个地方导致的帧率下降。通过如下的方法进行帧率的计算:

// TODO
static int captureFrameCount = 0;
static double nextCaptureStatisticsTime = -1;
static double UNIT_TIME_INTERVAL = 1000;static int capturePreprocessingFrameCount = 0;
static double nextCapturePreprocessingStatisticsTime = -1;captureFrameCount++;
long currentTime = clock_->TimeInMicroseconds()/rtc::kNumMicrosecsPerMillisec;
if(nextCaptureStatisticsTime == -1) {nextCaptureStatisticsTime = currentTime + UNIT_TIME_INTERVAL;
}
if(currentTime > nextCaptureStatisticsTime) {RTC_LOG(LS_INFO) << "statistics VideoStreamEncoder capture frame count:" << captureFrameCount;nextCaptureStatisticsTime = currentTime + UNIT_TIME_INTERVAL;captureFrameCount = 0;
}

通过上述方法跟踪到帧率降低在类VideoSender的AddVideoFrame函数中,经过如下代码后,帧率发生的明显降低。

if (_mediaOpt.DropFrame()) {RTC_LOG(LS_INFO) << "statistics bitrate track Drop Frame "<< " rtt " << encoder_params.rtt<< " input frame rate " << encoder_params.input_frame_rate<< " loss rate " << encoder_params.loss_rate<< " target bitrate " << encoder_params.target_bitrate.get_sum_bps();post_encode_callback_->OnDroppedFrame(EncodedImageCallback::DropReason::kDroppedByMediaOptimizations);return VCM_OK;
}

通过深入代码后,了解到这是webrtc支持弱网环境策略中的一个模块,根据目标码率丢帧。算法简单的理解就是:1.统计每个从采集模块过来的图像,然后计算帧率。2.根据编码后的图像码率和目标码率(webrtc估算出来当前网络最合适传输码率)以及统计得到的帧率等信息更新丢帧比率。3.根据计算的丢帧比率去实现均匀的丢帧。算法的具体细节已有道友做文章进行了刨析,各位可以结合代码进行深入理解,我就不重复造轮子了。《webrtc视频帧率控制算法机制(一)--目标码率丢帧》。
 

优化帧率:

已经定位到导致帧率地下的地方,接下来要做的就是如何进行优化。根据业务场景:画面质量可以下降,但是帧率一定要稳定的要求我简单粗暴的将该策略禁掉。这样一来就引申出了一系列的问题,当网络条件不好的时候如何保证视频质量,当然可以通过降码率,这部分内容在后面将会详细介绍。另一个问题是采集到来的图像帧率始终特别高,这将导致码率一直飙高,还有因为设备性能的不同或者一些其他问题(采集端帧率设置接口并不好使,或者是我没找到好使的接口)导致不同设备到来的帧率不一样。解决这个问题我通过webrtc的另一个丢帧策略来实现:根据目标帧率丢帧。该算法在我使用的版本已经被移除,我有手动添加回来。具体实现如下:

/**  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.**  Use of this source code is governed by a BSD-style license*  that can be found in the LICENSE file in the root of the source*  tree. An additional intellectual property rights grant can be found*  in the file PATENTS.  All contributing project authors may*  be found in the AUTHORS file in the root of the source tree.*/#include "webrtc/modules/video_processing/main/interface/video_processing.h"
#include "webrtc/modules/video_processing/main/source/video_decimator.h"
#include "webrtc/system_wrappers/interface/tick_util.h"#define VD_MIN(a, b) ((a) < (b)) ? (a) : (b)namespace webrtc {VPMVideoDecimator::VPMVideoDecimator() {Reset();
}VPMVideoDecimator::~VPMVideoDecimator() {}void VPMVideoDecimator::Reset()  {overshoot_modifier_ = 0;drop_count_ = 0;keep_count_ = 0;target_frame_rate_ = 30;incoming_frame_rate_ = 0.0f;memset(incoming_frame_times_, 0, sizeof(incoming_frame_times_));enable_temporal_decimation_ = true;
}void VPMVideoDecimator::EnableTemporalDecimation(bool enable) {enable_temporal_decimation_ = enable;
}int32_t VPMVideoDecimator::SetTargetFramerate(uint32_t frame_rate) {if (frame_rate == 0) return VPM_PARAMETER_ERROR;target_frame_rate_ = frame_rate;return VPM_OK;
}bool VPMVideoDecimator::DropFrame() {if (!enable_temporal_decimation_) return false;if (incoming_frame_rate_ <= 0) return false;const uint32_t incomingframe_rate =static_cast<uint32_t>(incoming_frame_rate_ + 0.5f);if (target_frame_rate_ == 0) return true;bool drop = false;if (incomingframe_rate > target_frame_rate_) {int32_t overshoot =overshoot_modifier_ + (incomingframe_rate - target_frame_rate_);if (overshoot < 0) {overshoot = 0;overshoot_modifier_ = 0;}if (overshoot && 2 * overshoot < (int32_t) incomingframe_rate) {if (drop_count_) {  // Just got here so drop to be sure.drop_count_ = 0;return true;}const uint32_t dropVar = incomingframe_rate / overshoot;if (keep_count_ >= dropVar) {drop = true;overshoot_modifier_ = -((int32_t) incomingframe_rate % overshoot) / 3;keep_count_ = 1;} else {keep_count_++;}} else {keep_count_ = 0;const uint32_t dropVar = overshoot / target_frame_rate_;if (drop_count_ < dropVar) {drop = true;drop_count_++;} else {overshoot_modifier_ = overshoot % target_frame_rate_;drop = false;drop_count_ = 0;}}}return drop;
}uint32_t VPMVideoDecimator::Decimatedframe_rate() {
ProcessIncomingframe_rate(TickTime::MillisecondTimestamp());if (!enable_temporal_decimation_) {return static_cast<uint32_t>(incoming_frame_rate_ + 0.5f);}return VD_MIN(target_frame_rate_,static_cast<uint32_t>(incoming_frame_rate_ + 0.5f));
}uint32_t VPMVideoDecimator::Inputframe_rate() {ProcessIncomingframe_rate(TickTime::MillisecondTimestamp());return static_cast<uint32_t>(incoming_frame_rate_ + 0.5f);
}void VPMVideoDecimator::UpdateIncomingframe_rate() {int64_t now = TickTime::MillisecondTimestamp();if (incoming_frame_times_[0] == 0) {// First no shift.} else {// Shift.for (int i = kFrameCountHistory_size - 2; i >= 0; i--) {incoming_frame_times_[i+1] = incoming_frame_times_[i];}}incoming_frame_times_[0] = now;ProcessIncomingframe_rate(now);
}void VPMVideoDecimator::ProcessIncomingframe_rate(int64_t now) {int32_t num = 0;int32_t nrOfFrames = 0;for (num = 1; num < (kFrameCountHistory_size - 1); num++) {// Don't use data older than 2sec.if (incoming_frame_times_[num] <= 0 ||now - incoming_frame_times_[num] > kFrameHistoryWindowMs) {break;} else {nrOfFrames++;}}if (num > 1) {int64_t diff = now - incoming_frame_times_[num-1];incoming_frame_rate_ = 1.0;if (diff > 0) {incoming_frame_rate_ = nrOfFrames * 1000.0f / static_cast<float>(diff);}} else {incoming_frame_rate_ = static_cast<float>(nrOfFrames);}
}}  // namespace webrtc

该算法和根据目标码率丢帧类似,相对来说更简单。先统计当前帧率,然后根据目标帧率和当前帧率计算不同的比率,最后实现均匀的丢帧。通过根据目标码率丢帧策略能够有效的抑制帧率在一个合适的数值。

动态调节码率

webrtc有一套码率自适应策略来应对弱网环境,我们要做的不是修改人家的算法,而是针对不同的业务调整不同的策略。
webrtc会通过丢包率和rtt等信息来判断当前的网络状况,进而通过调节编码器的码率来适应当前的网络状况。这个必须要求编码器支持动态调节码率。
我们的优化方案首先是设置码率的最大值和最小值,在弱网的情况下尽可能的传输的 流畅,而在网络条件好的情况下又要让视频达到一个很好的画质。
通过修改给sdp添加"x-google-start-bitrate"; x-google-max-bitrate"; "x-google-min-bitrate";等参数未能达到目的。

通过跟进编码器的初始化流程发现最小码率和最大码率的设置在VideoStreamEncoder类的ReconfigureEncoder函数:

void VideoStreamEncoder::ReconfigureEncoder() {//最小码率和最大码率在这里被设置。std::vector<VideoStream> streams =encoder_config_.video_stream_factory->CreateEncoderStreams(last_frame_info_->width, last_frame_info_->height, encoder_config_);VideoCodec codec;if (!VideoCodecInitializer::SetupCodec(encoder_config_, settings_, streams,nack_enabled_, &codec,&rate_allocator_)) {RTC_LOG(LS_ERROR) << "Failed to create encoder configuration.";}codec.startBitrate =std::max(encoder_start_bitrate_bps_ / 1000, codec.minBitrate);codec.startBitrate = std::min(codec.startBitrate, codec.maxBitrate);codec.expect_encode_from_texture = last_frame_info_->is_texture;max_framerate_ = codec.maxFramerate;RTC_DCHECK_LE(max_framerate_, kMaxFramerateFps);//codec被传递下去,进行编码器初始化。bool success = video_sender_.RegisterSendCodec(&codec, number_of_cores_,static_cast<uint32_t>(max_data_payload_length_)) == VCM_OK;

webrtcvideoengine.cc文件中类EncoderStreamFactory的CreateEncoderStreams函数:

std::vector<webrtc::VideoStream> EncoderStreamFactory::CreateEncoderStreams(int width, int height,const webrtc::VideoEncoderConfig& encoder_config) {// For unset max bitrates set default bitrate for non-simulcast.int max_bitrate_bps =(encoder_config.max_bitrate_bps > 0)? encoder_config.max_bitrate_bps: GetMaxDefaultVideoBitrateKbps(width, height) * 1000;webrtc::VideoStream stream;stream.width = width;stream.height = height;stream.max_framerate = max_framerate_;stream.max_framerate= GetMinVideoBitrateBps();stream.target_bitrate_bps = stream.max_bitrate_bps = max_bitrate_bps;

在这个函数中设置了max_framerate,和max_framerate。如果追溯更上层的设置,较为复杂繁琐。因为我们使用的平台统一,参数一致,所以直接在这里设置了。当然这种设置方式比较粗放,有待进一步细致柔性的设置。

stream.min_bitrate_bps = 200*1000;
stream.target_bitrate_bps = stream.max_bitrate_bps = 1800 *1000;

设置了这些参数后,通过测试发现,webrtc在网络条件不好的情况下逐渐减小编码码率,达到最小值;而当网络条件好的话,逐渐增加编码码率达到最大值。

webrtc在调节码率的时候还要依据一个参数,那就是帧率。帧率设置的大小,决定了某一网络条件下每一帧图像的数据量,直接决定了图像的清晰程度。最终码率 = 帧率*每一帧图像的数据量 。我在这里粗放的设置了某个值。

EncoderStreamFactory::EncoderStreamFactory(std::string codec_name,int max_qp,int max_framerate,bool is_screencast,bool conference_mode): codec_name_(codec_name),max_qp_(max_qp),//max_framerate_(max_framerate),max_framerate_(18),

 

后记

webrtc的码率自适应包括动态调节码率、帧率、分辨率来达到既定的码率,这里只介绍了调节帧率和码率,关于动态调节分辨率,有待进一步的研究。
 

这篇关于webrtc之Android视频质量提升:保帧率降码率的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/813713

相关文章

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

深度剖析SpringBoot日志性能提升的原因与解决

《深度剖析SpringBoot日志性能提升的原因与解决》日志记录本该是辅助工具,却为何成了性能瓶颈,SpringBoot如何用代码彻底破解日志导致的高延迟问题,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言第一章:日志性能陷阱的底层原理1.1 日志级别的“双刃剑”效应1.2 同步日志的“吞吐量杀手”

Java利用@SneakyThrows注解提升异常处理效率详解

《Java利用@SneakyThrows注解提升异常处理效率详解》这篇文章将深度剖析@SneakyThrows的原理,用法,适用场景以及隐藏的陷阱,看看它如何让Java异常处理效率飙升50%,感兴趣的... 目录前言一、检查型异常的“诅咒”:为什么Java开发者讨厌它1.1 检查型异常的痛点1.2 为什么说

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

PowerShell中15个提升运维效率关键命令实战指南

《PowerShell中15个提升运维效率关键命令实战指南》作为网络安全专业人员的必备技能,PowerShell在系统管理、日志分析、威胁检测和自动化响应方面展现出强大能力,下面我们就来看看15个提升... 目录一、PowerShell在网络安全中的战略价值二、网络安全关键场景命令实战1. 系统安全基线核查

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局