WebRTC音视频通话-新增或修改SDP中的码率Bitrate限制参数
之前搭建ossrs服务,可以查看:https://blog.csdn.net/gloryFlow/article/details/132257196
之前实现iOS端调用ossrs音视频通话,可以查看:https://blog.csdn.net/gloryFlow/article/details/132262724
之前WebRTC音视频通话高分辨率不显示画面问题,可以查看:https://blog.csdn.net/gloryFlow/article/details/132240952
这里WebRTC音视频通话过程中修改SDP中的码率Bitrate
一、SDP是什么?
SDP即Session Description Protocol(会话描述协议)
SDP由一行或多行UTF-8文本组成,每行以一个字符的类型开头,后跟等号(=),然后是包含值或描述的结构化文本,其格式取决于类型。如下为一个SDP内容示例:
v=0
o=alice 2890844526 2890844526 IN IP4
s=
c=IN IP4
t=0 0
m=audio 49170 RTP/AVP 0
a=rtpmap:0 PCMU/8000
m=video 51372 RTP/AVP 31
a=rtpmap:31 H261/90000
m=video 53000 RTP/AVP 32
a=rtpmap:32 MPV/90000
我这里本地获取的SDP完整数据如下
v=0
\no=SRS/6.0.64(Bee) 107408568903808 2 IN IP4 0.0.0.0
\ns=SRSPublishSession
\nt=0 0
\na=ice-lite
\na=group:BUNDLE 0 1
\na=msid-semantic: WMS live/livestream
\nm=audio 9 UDP/TLS/RTP/SAVPF 111
\nc=IN IP4 0.0.0.0
\na=ice-ufrag:4ahia260
\na=ice-pwd:11777k546394014cto09595g5em82339
\na=fingerprint:sha-256 26:AF:1F:AA:18:C0:4F:69:E3:19:B4:EF:9C:43:98:A9:E6:56:9A:2D:D4:2E:A8:31:D7:B1:C9:A1:08:CA:B2:13
\na=setup:passive
\na=mid:0
\na=extmap:3 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01
\na=recvonly
\na=rtcp-mux
\na=rtcp-rsize
\na=rtpmap:111 opus/48000/2
\na=rtcp-fb:111 transport-cc
\na=fmtp:111 minptime=10;useinbandfec=1
\na=candidate:0 1 udp 2130706431 169.254.136.162 8000 typ host generation 0
\na=candidate:1 1 udp 2130706431 192.168.10.100 8000 typ host generation 0
\nm=video 9 UDP/TLS/RTP/SAVPF 96 127
\nc=IN IP4 0.0.0.0
\na=ice-ufrag:4ahia260
\na=ice-pwd:11777k546394014cto09595g5em82339
\na=fingerprint:sha-256 26:AF:1F:AA:18:C0:4F:69:E3:19:B4:EF:9C:43:98:A9:E6:56:9A:2D:D4:2E:A8:31:D7:B1:C9:A1:08:CA:B2:13
\na=setup:passive
\na=mid:1
\na=extmap:3 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01
\na=recvonly
\na=rtcp-mux
\na=rtcp-rsize
\na=rtpmap:96 H264/90000
\na=rtcp-fb:96 transport-cc
\na=rtcp-fb:96 nack
\na=rtcp-fb:96 nack pli
\na=fmtp:96 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=640c33
\na=rtpmap:127 red/90000
\na=candidate:0 1 udp 2130706431 169.254.136.162 8000 typ host generation 0
\na=candidate:1 1 udp 2130706431 192.168.10.100 8000 typ host generation 0
\n
从上面的数据格式中可以看到
常见的比如
m代表media,
m=audio表示此行描述的是音频信息相关。
m=video代表此行描述的是视频信息相关。
a代表属性,比如a=candidate,表示这一行描述的是candidate信息。
以及涉及到分辨率的显示的profile-level-id 640c33
二、新增或修改SDP中的码率Bitrate限制参数
下面需要修改一下修改SDP中的码率Bitrate,如果没有b=AS,则新增一条。
具体代码如下
+ (NSString *)setMediaBitrate:(NSString *)sdp media:(NSString *)media bitrate:(int)bitrate {if (!(sdp && [sdp isKindOfClass:[NSString class]] && sdp.length > 0)) {return sdp;}NSMutableArray *lines = [NSMutableArray arrayWithArray:[sdp componentsSeparatedByString:@"\n"]];int line = -1;for (int i = 0; i < lines.count; i++) {NSString *start = [NSString stringWithFormat:@"m=%@",media];if ([lines[i] hasPrefix:start]) {line = i;break;}}if (line == -1) {NSLog(@"Could not find the m line for %@", media);return sdp;}NSLog(@"Found the m line for %@", media);line++;while ([lines[line] hasPrefix:@"i="] || [lines[line] hasPrefix:@"c="]) {line++;}if ([lines[line] hasPrefix:@"b"]) {NSLog(@"Replaced b line at line:%d", line);lines[line] = [NSString stringWithFormat:@"b=AS:%d", bitrate];return [lines componentsJoinedByString:@"\n"];}NSLog(@"Adding new b line before line:%d", line);NSMutableArray *newLines = [NSMutableArray arrayWithArray:[lines subarrayWithRange:NSMakeRange(0, line)]];NSMutableArray *aLeftLines = [NSMutableArray arrayWithArray:[lines subarrayWithRange:NSMakeRange(line, lines.count - line)]];NSString *aLineStr = [NSString stringWithFormat:@"b=AS:%d", bitrate];[newLines addObject:aLineStr];NSMutableArray *resultLines = [NSMutableArray arrayWithCapacity:0];[resultLines addObjectsFromArray:newLines];[resultLines addObjectsFromArray:aLeftLines];return [resultLines componentsJoinedByString:@"\n"];
}
效果图
三、小结
WebRTC音视频通话-新增或修改SDP中的码率Bitrate限制参数。内容较多,描述可能不准确,请见谅。
https://blog.csdn.net/gloryFlow/article/details/132263021
学习记录,每天不停进步。