Skip to content

Instantly share code, notes, and snippets.

@iamyogish
Created February 19, 2018 09:41
Show Gist options
  • Save iamyogish/e2b04caccc361b0433aaf301e8ae34ac to your computer and use it in GitHub Desktop.
Save iamyogish/e2b04caccc361b0433aaf301e8ae34ac to your computer and use it in GitHub Desktop.
Controlling the bandwidth used by WebRTC during calls
//...
func sendOffer() {
let peerConnection = getPeerConnection()
let mediaConstraints = getMediaConstraints()
peerConnection.offer(for: mediaConstraints) { [weak self] (sdp, error) in
guard error == nil else {
self?.delegate?.handleError(error: error)
return
}
var modifiedSDP = sdp
if lowDataModeEnabled {
//If low data mode is enabled modify the SDP
let sdpString = sdp!.sdp
//For example purpose, I have set videoBitrate to 233 & audioBitrate to 100. You can change it as per your needs
let modifiedSDPString = self?.setMediaBitrates(sdp: sdpString, videoBitrate: 233, audioBitrate: 100)
//Create a new SDP using the modified SDP string
modifiedSDP = RTCSessionDescription(type: .offer, sdp: modifiedSDPString!)
}
//Set the modified SDP as local description
self?.peerConnection.setLocalDescription(modifiedSDP!, completionHandler: { (error) in
guard error == nil else {
self?.delegate?.handleError(error: error)
return
}
//Send the modified SDP to other party using your signalling channel
self?.sendLocalSessionDescriptionSignalling(sdp: modifiedSDP!)
})
}
}
func sendAnswer() {
let peerConnection = getPeerConnection()
let mediaConstraints = getMediaConstraints()
peerConnection.answer(for: mediaConstraints) { [weak self] (sdp, error) in
//Handle error if any
guard error == nil else {
self?.delegate?.handleError(error: error)
return
}
var modifiedSDP = sdp
if lowDataModeEnabled {
//If low data mode is enabled modify the SDP
let sdpString = sdp!.sdp
//For example purpose, I have set videoBitrate to 233 & audioBitrate to 100. You can change it as per your needs
let modifiedSDPString = self?.setMediaBitrates(sdp: sdpString, videoBitrate: 233, audioBitrate: 100)
//Create a new SDP using the modified SDP string
modifiedSDP = RTCSessionDescription(type: .answer, sdp: modifiedSDPString!)
}
//Set the modified SDP as local description
self?.peerConnection.setLocalDescription(modifiedSDP!, completionHandler: { (error) in
//Handle error if any
guard error == nil else {
self?.delegate?.handleError(error: error)
return
}
//Send the modified SDP to other party using your signalling channel
self?.sendLocalSessionDescriptionSignalling(sdp: modifiedSDP!)
})
}
}
private func sendLocalSessionDescriptionSignalling(sdp: RTCSessionDescription) {
// Send the sdp to other party using your siganlling channel
}
private func getMediaConstraints() -> RTCMediaConstraints {
//Creates & returns media constraints for call
}
private func getPeerConnection() -> RTCPeerConnection {
//Creates & returns an peer connection
}
func setMediaBitrates(sdp: String, videoBitrate: Int, audioBitrate: Int) -> String {
return setMediaBitrate(sdp: setMediaBitrate(sdp: sdp, mediaType: "video", bitrate: videoBitrate), mediaType: "audio", bitrate: audioBitrate)
}
private func setMediaBitrate(sdp: String, mediaType: String, bitrate: Int) -> String {
var lines = sdp.components(separatedBy: "\n")
var line = -1
for (index, lineString) in lines.enumerated() {
if lineString.hasPrefix("m=\(mediaType)") {
line = index
break
}
}
guard line != -1 else {
//Couldn't find the m (media) line return the original sdp
print("Couldn't find the m line in SDP so returning the original sdp")
return sdp
}
// Go to next line i.e. line after m
line += 1
//Now skip i and c lines
while (lines[line].hasPrefix("i=") || lines[line].hasPrefix("c=")) {
line += 1
}
let newLine = "b=AS:\(bitrate)"
//Check if we're on b (bitrate) line, if so replace it
if lines[line].hasPrefix("b") {
print("Replacing the b line of the SDP")
lines[line] = newLine
} else {
//If there's no b line, add a new b line
lines.insert(newLine, at: line)
}
let modifiedSDP = lines.joined(separator: "\n")
return modifiedSDP
}
//...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment