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

相关文章

Eclipse+ADT与Android Studio开发的区别

下文的EA指Eclipse+ADT,AS就是指Android Studio。 就编写界面布局来说AS可以边开发边预览(所见即所得,以及多个屏幕预览),这个优势比较大。AS运行时占的内存比EA的要小。AS创建项目时要创建gradle项目框架,so,创建项目时AS比较慢。android studio基于gradle构建项目,你无法同时集中管理和维护多个项目的源码,而eclipse ADT可以同时打开

android 免费短信验证功能

没有太复杂的使用的话,功能实现比较简单粗暴。 在www.mob.com网站中可以申请使用免费短信验证功能。 步骤: 1.注册登录。 2.选择“短信验证码SDK” 3.下载对应的sdk包,我这是选studio的。 4.从头像那进入后台并创建短信验证应用,获取到key跟secret 5.根据技术文档操作(initSDK方法写在setContentView上面) 6.关键:在有用到的Mo

android一键分享功能部分实现

为什么叫做部分实现呢,其实是我只实现一部分的分享。如新浪微博,那还有没去实现的是微信分享。还有一部分奇怪的问题:我QQ分享跟QQ空间的分享功能,我都没配置key那些都是原本集成就有的key也可以实现分享,谁清楚的麻烦详解下。 实现分享功能我们可以去www.mob.com这个网站集成。免费的,而且还有短信验证功能。等这分享研究完后就研究下短信验证功能。 开始实现步骤(新浪分享,以下是本人自己实现

Android我的二维码扫描功能发展史(完整)

最近在研究下二维码扫描功能,跟据从网上查阅的资料到自己勉强已实现扫描功能来一一介绍我的二维码扫描功能实现的发展历程: 首页通过网络搜索发现做android二维码扫描功能看去都是基于google的ZXing项目开发。 2、搜索怎么使用ZXing实现自己的二维码扫描:从网上下载ZXing-2.2.zip以及core-2.2-source.jar文件,分别解压两个文件。然后把.jar解压出来的整个c

android 带与不带logo的二维码生成

该代码基于ZXing项目,这个网上能下载得到。 定义的控件以及属性: public static final int SCAN_CODE = 1;private ImageView iv;private EditText et;private Button qr_btn,add_logo;private Bitmap logo,bitmap,bmp; //logo图标private st

Android多线程下载见解

通过for循环开启N个线程,这是多线程,但每次循环都new一个线程肯定很耗内存的。那可以改用线程池来。 就以我个人对多线程下载的理解是开启一个线程后: 1.通过HttpUrlConnection对象获取要下载文件的总长度 2.通过RandomAccessFile流对象在本地创建一个跟远程文件长度一样大小的空文件。 3.通过文件总长度/线程个数=得到每个线程大概要下载的量(线程块大小)。

时间服务器中,适用于国内的 NTP 服务器地址,可用于时间同步或 Android 加速 GPS 定位

NTP 是什么?   NTP 是网络时间协议(Network Time Protocol),它用来同步网络设备【如计算机、手机】的时间的协议。 NTP 实现什么目的?   目的很简单,就是为了提供准确时间。因为我们的手表、设备等,经常会时间跑着跑着就有误差,或快或慢的少几秒,时间长了甚至误差过分钟。 NTP 服务器列表 最常见、熟知的就是 www.pool.ntp.org/zo

高仿精仿愤怒的小鸟android版游戏源码

这是一款很完美的高仿精仿愤怒的小鸟android版游戏源码,大家可以研究一下吧、 为了报复偷走鸟蛋的肥猪们,鸟儿以自己的身体为武器,仿佛炮弹一样去攻击肥猪们的堡垒。游戏是十分卡通的2D画面,看着愤怒的红色小鸟,奋不顾身的往绿色的肥猪的堡垒砸去,那种奇妙的感觉还真是令人感到很欢乐。而游戏的配乐同样充满了欢乐的感觉,轻松的节奏,欢快的风格。 源码下载

百度OCR识别结构结构化处理视频

https://edu.csdn.net/course/detail/10506

Android SurfaceFlinger——图形内存分配器(十一)

前面的文章中的图层合成器(HWC),这里我们接着看一下 SurfaceFlinger 中的另一个重要服务——图形内存分配器。 一、简介         android.hardware.graphics.allocator@2.0 是 Android 系统中硬件抽象层(HAL)的一个组件,专门用于图形内存的分配和管理。它是 SurfaceFlinger 在处理图形数据时所依赖的