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实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

基于Python和MoviePy实现照片管理和视频合成工具

《基于Python和MoviePy实现照片管理和视频合成工具》在这篇博客中,我们将详细剖析一个基于Python的图形界面应用程序,该程序使用wxPython构建用户界面,并结合MoviePy、Pill... 目录引言项目概述代码结构分析1. 导入和依赖2. 主类:PhotoManager初始化方法:__in

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(