WebRTC audio and video call - add or modify bitrate limit in SDP

WebRTC audio and video call - add or modify the bitrate limit parameter in SDP

Before setting up the ossrs service, you can check: https://blog.csdn.net/gloryFlow/article/details/132257196
Before implementing iOS to call ossrs audio and video calls, you can check: https://blog.csdn.net/gloryFlow/ article/details/132262724
WebRTC audio and video calls before high resolution does not display the screen problem, you can view: https://blog.csdn.net/gloryFlow/article/details/132240952

Here, modify the bit rate Bitrate in SDP during the WebRTC audio and video call

1. What is SDP?

SDP is the Session Description Protocol (Session Description Protocol)
SDP consists of one or more lines of UTF-8 text, each line begins with a character type, followed by an equal sign (=), and then contains a value or description Structured text, its The format depends on the type. The following is an example of SDP content:

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

The complete SDP data I obtained locally is as follows

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-fragment: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
/ N N NY EPPPP:111,
301 ) Questions Questions , 10: 0 1 , 10-1
307444
192.168.10.100 8000 type host generation 0
\nm=video 9 UDP/TLS/RTP/SAVPF 96 127
\nc=IN IP4 0.0.0.0
\na=ice-subfragment: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

As can be seen from the data format above

common such as

m stands for media, and
m=audio means that this line describes audio information.
m=video means that this line describes video information.

a represents an attribute, such as a=candidate, which means that this row describes candidate information.

And the displayed profile-level-id 640c33 related to the resolution

2. Add or modify the Bitrate limit parameter in SDP

Next, you need to modify the code rate Bitrate in the SDP. If there is no b=AS, add a new one.

The specific code is as follows

+ (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"];
}

renderings

insert image description here

3. Summary

WebRTC audio and video call - add or modify the Bitrate limit parameter in SDP. There are many contents, and the description may be inaccurate, please forgive me.

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

Learning records, keep improving every day.

Guess you like

Origin blog.csdn.net/gloryFlow/article/details/132263021