iOS开发-WebRTC本地直播高分辨率不显示画面问题

iOS开发-WebRTC本地直播高分辨率不显示画面问题

在之前使用WebRTC结合ossrs进行推流时候,ossrs的播放端无法看到高分辨率画面问题。根据这个问题,找到了解决方案。

一、WebRTC是什么

WebRTC是什么呢?

WebRTC (Web Real-Time Communications) 是一项实时通讯技术,它允许网络应用或者站点,在不借助中间媒介的情况下,建立浏览器之间点对点(Peer-to-Peer)的连接,实现视频流和(或)音频流或者其他任意数据的传输。

二、ossrs是什么?

ossrs是什么呢?
SRS(Simple Realtime Server)是一个简单高效的实时视频服务器,支持RTMP、WebRTC、HLS、HTTP-FLV、SRT等多种实时流媒体协议。

官网地址:https://ossrs.net/lts/zh-cn/

这里暂时不写iOS Google WebRTC与ossrs实现RTC直播了。暂时值记录一下本地WebRTC直播高分辨率不显示画面问题。

三、高分辨率不显示画面问题解决方案

本地WebRTC直播高分辨率不显示画面问题,这个问题和SDP中的profile-level-id有关。

profile-level-id正好是SPS中的第二至四个字节的base16编码。这三个字节的具体含义是

sps[1] AVCProfileIndication
sps[2] profile_compatibility
sps[3] AVCLevlIndication

http://en.wikipedia.org/wiki/H.264/MPEG-4_AVC#Levels
实际设置时,就是level值乘以10,例如level 1.0,设置值就是0x0A。level 3.0,设置值就是0x1E。比较例外的是level 1b,设置值是0x09

图片来源网络(抱歉忘记地址了,如果引用了您的博客图片,请留言)
在这里插入图片描述

第三个代表level,比如1f值为31,从图中可以看出3.1,分辨率在720480 720576 1280*720

如果需要高分辨率 19201080 25601920 3840*2160 ,需要设置的level 5.1 16进制值为33,这里设置的值是42e033。

下面是一个SDP示例

“code”: 0,
“server”: “vid-415v5lz”,
“sdp”: “v=0\r\no=SRS/4.0.268(Leo) 94003279212192 2 IN IP4 0.0.0.0\r\ns=SRSPublishSession\r\nt=0 0\r\na=ice-lite\r\na=group:BUNDLE 0 1\r\na=msid-semantic: WMS live/livestream\r\nm=audio 9 UDP/TLS/RTP/SAVPF 111\r\nc=IN IP4 0.0.0.0\r\na=ice-ufrag:q01184s8\r\na=ice-pwd:o25158210twbb093o342910094v0wo5k\r\na=fingerprint:sha-256 6A:66:81:7C:68:91:79:18:05:2C:EE:5F:BF:1B:4B:F4:78:C4:01:06:CC:CC:9E:F0:32:5B:72:21:4A:C2:A1:AA\r\na=setup:passive\r\na=mid:0\r\na=recvonly\r\na=rtcp-mux\r\na=rtcp-rsize\r\na=rtpmap:111 opus/48000/2\r\na=fmtp:111 minptime=10;useinbandfec=1\r\na=candidate:0 1 udp 2130706431 10.0.80.128 8000 typ host generation 0\r\na=candidate:1 1 udp 2130706431 112.124.157.141 8000 typ host generation 0\r\nm=video 9 UDP/TLS/RTP/SAVPF 96 127\r\nc=IN IP4 0.0.0.0\r\na=ice-ufrag:q01184s8\r\na=ice-pwd:o25158210twbb093o342910094v0wo5k\r\na=fingerprint:sha-256 6A:66:81:7C:68:91:79:18:05:2C:EE:5F:BF:1B:4B:F4:78:C4:01:06:CC:CC:9E:F0:32:5B:72:21:4A:C2:A1:AA\r\na=setup:passive\r\na=mid:1\r\na=extmap:5 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01\r\na=recvonly\r\na=rtcp-mux\r\na=rtcp-rsize\r\na=rtpmap:96 H264/90000\r\na=rtcp-fb:96 transport-cc\r\na=rtcp-fb:96 nack\r\na=rtcp-fb:96 nack pli\r\na=fmtp:96 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f\r\na=rtpmap:127 red/90000\r\na=candidate:0 1 udp 2130706431 10.0.80.128 8000 typ host generation 0\r\na=candidate:1 1 udp 2130706431 112.124.157.141 8000 typ host generation 0\r\n”,
“sessionid”: “q01184s8:oPvh”
}

可以看到这里的profile-level-id=42e01f

如果在WebRTC中将本地的SDP的profile-level-id替换成42e033

[weakSelf.webRTCClient offer:^(RTCSessionDescription *sdp) {DebugLog(@"changeSDP2Server offer sdp:%@", sdp);NSString *offerSDPString = [SDWebRTCSDPUtil setMediaBitrate:sdp.sdp media:@"video" bitrate:(6*1024*1024)];DebugLog(@"changeSDP2Server offerSDPString:%@", offerSDPString);[weakSelf changeSDP2Server:offerSDPString];}];

将调用rtc/v1/publish/接口获得的remoteSDPString中的profile-level-id更改42e033之后再调用

- (void)setRemoteDescription:(RTC_OBJC_TYPE(RTCSessionDescription) *)sdpcompletionHandler:(nullable void (^)(NSError *_Nullable error))completionHandler;

经过测试,切换高分辨率画面可以正常显示。

NSString *resultRemoteSDPString = [SDWebRTCSDPUtil setMediaBitrate:remoteSDPString media:@"video" bitrate:(6*1024*1024)];DebugLog(@"changeSDP2Server resultRemoteSDPString:%@", resultRemoteSDPString);RTCSessionDescription *remoteSDP = [[RTCSessionDescription alloc] initWithType:RTCSdpTypeAnswer sdp:resultRemoteSDPString];[weakSelf.webRTCClient setRemoteSdp:remoteSDP completion:^(NSError * error) {DebugLog(@"changeSDP2Server setRemoteDescription error:%@", error);}];

四、通过RTCVideoEncoderFactory解决高分辨率不显示画面问题解决方案

通过将SDP中的profile-level-id进行更改并不是我的最终解决方法,我最后使用的是通过指定RTCVideoEncoder的RTCVideoCodecInfo中constrainedHighParams的profile-level-id值为42e033

设置codecs的constrainedHighInfo和constrainedBaselineInfo元素。

NSDictionary<NSString *, NSString *> *constrainedHighParams = @{@"profile-level-id" : kLevelHighConstrainedHigh,@"level-asymmetry-allowed" : @"1",@"packetization-mode" : @"1",};RTCVideoCodecInfo *constrainedHighInfo =[[RTCVideoCodecInfo alloc] initWithName:kRTCVideoCodecH264Name parameters:constrainedHighParams];[codecs addObject:constrainedHighInfo];NSDictionary<NSString *, NSString *> *constrainedBaselineParams = @{@"profile-level-id" : kLevelHighConstrainedBaseline,@"level-asymmetry-allowed" : @"1",@"packetization-mode" : @"1",};RTCVideoCodecInfo *constrainedBaselineInfo =[[RTCVideoCodecInfo alloc] initWithName:kRTCVideoCodecH264Name parameters:constrainedBaselineParams];[codecs addObject:constrainedBaselineInfo];

完整代码如下

SDRTCVideoEncoderFactory.h

#import <Foundation/Foundation.h>
#import <WebRTC/WebRTC.h>/**+ (NSArray<RTCVideoCodecInfo *> *)supportedCodecs {NSDictionary<NSString *, NSString *> *constrainedHighParams = @{@"profile-level-id" : kRTCMaxSupportedH264ProfileLevelConstrainedHigh,@"level-asymmetry-allowed" : @"1",@"packetization-mode" : @"1",};RTCVideoCodecInfo *constrainedHighInfo =[[RTCVideoCodecInfo alloc] initWithName:kRTCVideoCodecH264Nameparameters:constrainedHighParams];NSDictionary<NSString *, NSString *> *constrainedBaselineParams = @{@"profile-level-id" : kRTCMaxSupportedH264ProfileLevelConstrainedBaseline,@"level-asymmetry-allowed" : @"1",@"packetization-mode" : @"1",};RTCVideoCodecInfo *constrainedBaselineInfo =[[RTCVideoCodecInfo alloc] initWithName:kRTCVideoCodecH264Nameparameters:constrainedBaselineParams];RTCVideoCodecInfo *vp8Info = [[RTCVideoCodecInfo alloc] initWithName:kRTCVideoCodecVp8Name];#if defined(RTC_ENABLE_VP9)RTCVideoCodecInfo *vp9Info = [[RTCVideoCodecInfo alloc] initWithName:kRTCVideoCodecVp9Name];#endifreturn @[constrainedHighInfo,constrainedBaselineInfo,vp8Info,#if defined(RTC_ENABLE_VP9)vp9Info,#endif];}*/@interface SDRTCVideoEncoderFactory : NSObject<RTCVideoEncoderFactory>@end

SDRTCVideoEncoderFactory.m

#import "SDRTCVideoEncoderFactory.h"static NSString *kLevelHighConstrainedHigh = @"640c33";
static NSString *kLevelHighConstrainedBaseline = @"42e033";@implementation SDRTCVideoEncoderFactory- (id<RTCVideoEncoder>)createEncoder:(RTCVideoCodecInfo *)info {if ([info.name isEqualToString:kRTCVideoCodecH264Name]) {return [[RTCVideoEncoderH264 alloc] initWithCodecInfo:info];} else if ([info.name isEqualToString:kRTCVideoCodecVp8Name]) {return [RTCVideoEncoderVP8 vp8Encoder];} else if ([info.name isEqualToString:kRTCVideoCodecVp9Name]) {return [RTCVideoEncoderVP9 vp9Encoder];}return nil;
}- (NSArray<RTCVideoCodecInfo *> *)supportedCodecs {NSMutableArray<RTCVideoCodecInfo *> *codecs = [NSMutableArray array];NSDictionary<NSString *, NSString *> *constrainedHighParams = @{@"profile-level-id" : kLevelHighConstrainedHigh,@"level-asymmetry-allowed" : @"1",@"packetization-mode" : @"1",};RTCVideoCodecInfo *constrainedHighInfo =[[RTCVideoCodecInfo alloc] initWithName:kRTCVideoCodecH264Name parameters:constrainedHighParams];[codecs addObject:constrainedHighInfo];NSDictionary<NSString *, NSString *> *constrainedBaselineParams = @{@"profile-level-id" : kLevelHighConstrainedBaseline,@"level-asymmetry-allowed" : @"1",@"packetization-mode" : @"1",};RTCVideoCodecInfo *constrainedBaselineInfo =[[RTCVideoCodecInfo alloc] initWithName:kRTCVideoCodecH264Name parameters:constrainedBaselineParams];[codecs addObject:constrainedBaselineInfo];RTCVideoCodecInfo *vp8Info = [[RTCVideoCodecInfo alloc] initWithName:kRTCVideoCodecVp8Name parameters:nil];[codecs addObject:vp8Info];#if defined(RTC_ENABLE_VP9)RTCVideoCodecInfo *vp9Info = [[RTCVideoCodecInfo alloc] initWithName:kRTCVideoCodecVp9Name];[codecs addObject:vp9Info];
#endifreturn [codecs copy];
}@end

最后在RTCPeerConnectionFactory初始化的时候做一下设置

#pragma mark - Lazy
- (RTCPeerConnectionFactory *)factory {if (!_factory) {RTCInitializeSSL();SDRTCVideoDecoderFactory *decoderFactory = [[SDRTCVideoDecoderFactory alloc] init];SDRTCVideoEncoderFactory *encoderFactory = [[SDRTCVideoEncoderFactory alloc] init];//        for (RTCVideoCodecInfo *codec in encoderFactory.supportedCodecs) {
//            DebugLog(@"RTCVideoCodecInfo codec.parameters:%@", codec.parameters);
//        }_factory = [[RTCPeerConnectionFactory alloc] initWithEncoderFactory:encoderFactory decoderFactory:decoderFactory];}return _factory;
}

经过测试,解决了高分辨率不显示画面的问题

五、小结

iOS开发-WebRTC本地直播高分辨率不显示画面问题,使用WebRTC结合ossrs进行推流时候,ossrs的播放端无法看到高分辨率画面问题。通过设置RTCPeerConnectionFactory的initWithEncoderFactory最后解决了该问题。

https://blog.csdn.net/gloryFlow/article/details/132240952

学习记录,每天不停进步。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/34592.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【ES】笔记-函数参数默认值

函数参数默认值 ES6 允许给函数参数赋值初始值 1. 形参初始值 具有默认值的参数&#xff0c;一般放到最后 function add(a,b,c10){return abc}let resultadd(1,2);console.log(result);2. 与解构赋值结合 function connect({host"127.0.0.1",username,password,port…

FlinkCDC能读取到快照,但是无法输出更新数据

FlinkCDC能读取到快照&#xff0c;但是无法输出更新数据 发现是并行度问题&#xff0c;当我的并行度超过1的时候就无法捕获更新。 public static void main(String[] args) throws Exception {System.setProperty("HADOOP_USER_NAME", "lcq");Configurati…

python selenium如何保存网站的cookie用于下次自动登录

## 一、python selenium如何保存网站的cookie 使用Selenium保存网站的Cookie非常简单。下面是一个示例&#xff0c;展示了如何使用Selenium打开网站&#xff0c;然后保存获取到的Cookie&#xff1a; from selenium import webdriver# 初始化浏览器 browser webdriver.Chrome…

数据结构与算法-数组(附阿里面试题)

一 面试经典&#xff1a; 给你一个文件里面包含全国人民&#xff08;14亿&#xff09;的年龄数据&#xff08;0~180&#xff09;&#xff0c;现在要你统计每一个年龄 有多少人&#xff1f; 给定机器为 单台2CPU2G内存。不得使用现成的容器&#xff0c;比如map等。&am…

多个 Github 账户访问 Github

文章目录 多个 Github 账户访问 Github背景步骤 参考 多个 Github 账户访问 Github 背景 如果我想在这台电脑上同时使用两个 Github 账号怎么办呢&#xff1f; 你主机上的 SSH 公钥只能标识出一个账号。如果需要使用另外一个git账号&#xff0c;访问仓库&#xff0c;你需要创…

Java基础篇--String 类

Java中的String类是用于处理字符串的核心类之一。它属于Java的标准库&#xff0c;并提供了许多操作字符串的方法。 String类是不可变的&#xff0c;这意味着一旦创建了一个String对象&#xff0c;它的值就不能被改变。当对字符串进行操作时&#xff0c;实际上是创建了一个新的…

【博客695】k8s subPathExpr作用

k8s subPathExpr作用 场景&#xff1a; 对于一个deployment或者job拉起的服务&#xff0c;所有pod都是一样的配置&#xff0c;如果都挂载了宿主机的同一个目录&#xff0c;那么就会互相干扰&#xff0c;我们希望挂载相同目录&#xff0c;且在这个目录下&#xff0c;每个pod建立…

“深入理解JVM:Java虚拟机的工作原理揭秘“

标题&#xff1a;深入理解JVM&#xff1a;Java虚拟机的工作原理揭秘 摘要&#xff1a;本文将深入解析Java虚拟机&#xff08;JVM&#xff09;的工作原理&#xff0c;包括JVM的组成部分、类加载过程、运行时数据区域、垃圾回收机制等。通过详细的代码示例&#xff0c;帮助读者更…

考虑分布式电源的配电网无功优化问题研究(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

React Router 6

1.概述 React Router 以三个不同的包发布到 npm 上&#xff0c;它们分别为&#xff1a; react-router: 路由的核心库&#xff0c;提供了很多的&#xff1a;组件、钩子。 react-router-dom: 包含react-router所有内容&#xff0c;并添加一些专门用于 DOM 的组件&#xff0c;例如…

算法练习Day43|● 518. 零钱兑换 II ● 377. 组合总和 Ⅳ

LeetCode:518. 零钱兑换 II 518. 零钱兑换 II - 力扣&#xff08;LeetCode&#xff09; 1.思路 求组合数&#xff0c;先遍历物品再遍历背包&#xff0c;dp[]数组累加即可。 2.代码实现 1class Solution {2 public int change(int amount, int[] coins) {34 int[…

Shell编程之条件测试、if语句、case语句

条件语句 一、条件测试1.1 测试命令1.1 文件测试1.2 整数比较1.3 字符串比较1.4 逻辑测试1.4.1 逻辑与 &&1.4.2 逻辑或 || 1.4.3 组合应用1.5 多个命令组合执行 ( ) { } 二、if语句2.1单分支结构2.2 多分支结构2.4 if语句练习2.4.1 单分支2.4.2 简单的交互式分数反馈 三…

Qt 编译程序打包依赖库

windows环境 使用windeployqt.exe 打包 # 进入exe目录&#xff0c;执行windeployqt命令&#xff0c;注意Qt的安装目录 D:\Qt\Qt5.9.6\5.9.6\mingw53_32\bin\windeployqt.exe my.exelinux环境 使用ldd命令打包 #!/bin/bash LibDir$PWD"/lib" Tag$1 lib_array($(ld…

使用 Flask 部署 Next.js

原文 使用 Flask 部署 Next.js Flask 和 Next.js 是两个独特的开源 Web 框架&#xff0c;分别构建在 Python 和 JavaScript 编程语言之上。 您可以在没有 Next.js 的情况下构建 Flask 应用程序&#xff0c;也可以在没有 Flask 的情况下构建 Next.js 应用程序。但是&#xff0…

ubuntu切换python版本

在没有安装类似anoconda的管理工具的时候&#xff0c;我们常常会被Ubuntu下的Python版本切换问题所头疼。 可以使用update-alternatives工具进行python版本的任意切换 当使用update-alternatives工具来切换Ubuntu系统上的Python版本时&#xff0c;您实际上是在系统范围内选择…

PyTorch翻译官网教程-NLP FROM SCRATCH: CLASSIFYING NAMES WITH A CHARACTER-LEVEL RNN

官网链接 NLP From Scratch: Classifying Names with a Character-Level RNN — PyTorch Tutorials 2.0.1cu117 documentation 使用CHARACTER-LEVEL RNN 对名字分类 我们将建立和训练一个基本的字符级递归神经网络(RNN)来分类单词。本教程以及另外两个“from scratch”的自然…

【国赛清单】2023全国大学生电赛综合测试【总结】

综合测评简介 &#xff08;1&#xff09;综合测评是全国大学生电子设计竞赛评审工作中非常重要的一个环节&#xff0c;是“一次竞赛二级评审”工作中全国专家组评审工作的一部分。 &#xff08;2&#xff09;测试对象为赛区推荐上报全国评奖的优秀参赛队全体队员&#xff0c;…

轻松转换TS视频为MP4,实现优质视频剪辑体验

如果你是一个视频剪辑爱好者&#xff0c;你一定会遇到各种视频格式之间的转换问题&#xff0c;特别是将TS视频转换为MP4格式。别担心&#xff0c;我们的视频剪辑软件将为你提供最简单、高效的解决方案&#xff01; 首先第一步&#xff0c;我们要进入媒体梦工厂主页面&#xff…

Elasticsearch同时使用should和must

问题及解决方法 must和should组合查询&#xff0c;should失效。使用must嵌套查询&#xff0c;将should组成的bool查询包含在其中一个must查询中。 SearchRequest request new SearchRequest(); request.indices("function_log");SearchSourceBuilder sourceBuilde…

XLua案例学习

下载 xlua 之后把 asset 文件中的全部文件粘贴到项目文件Asset文件下&#xff0c;将tool粘贴到 asset 同级目录下 然后把 HOTFIX_ENABLE 宏打开 之后 编辑 lua 脚本 更改源代码之后先 Generate Code 然后 HotFix inject in Editor 开发过程&#xff1a; 首先开发业务…