Skip to content

Commit

Permalink
answer.sdp を書き換え、その値で local description を設定する
Browse files Browse the repository at this point in the history
  • Loading branch information
zztkm committed Sep 12, 2024
1 parent 8b73c31 commit e18849a
Showing 1 changed file with 32 additions and 33 deletions.
65 changes: 32 additions & 33 deletions Sora/PeerChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -616,10 +616,38 @@ class PeerChannel: NSObject, RTCPeerConnectionDelegate {
return
}

// answer sdp を加工する
var sdp = answer!.sdp
// TODO(zztkm): この実装だと stereo=1 がすでに付与されていても付与してしまうので修正する
// Chrome/Edge 向けのハック stereo=1 が CreateOffer では付与されないので、
// SDP を書き換えて stereo=1 を付与する
// https://github.com/w3c/webrtc-stats/issues/686
// https://github.com/w3c/webrtc-extensions/issues/63
// https://issues.webrtc.org/issues/41481053#comment18
if self.configuration.forceStereoOutput {
Logger.debug(type: .peerChannel, message: "stereo=1 を付与する")
let pattern = "minptime=\\d+"
let regexp = try! NSRegularExpression(pattern: pattern)
let replacementFunc: (String) -> String = { match in
if !match.contains("stereo=1") {
return "\(match);stereo=1"
}
return match
}

let matches = regexp.matches(in: sdp, range: NSRange(sdp.startIndex..., in: sdp))
for match in matches.reversed() {
let range = match.range
let matchedString = (sdp as NSString).substring(with: range)
let replacedString = replacementFunc(matchedString)
sdp = (sdp as NSString).replacingCharacters(in: range, with: replacedString)
}
}
let updatedAnswer = RTCSessionDescription(type: .answer, sdp: sdp)
Logger.debug(type: .peerChannel, message: "did create answer")

Logger.debug(type: .peerChannel, message: "try setting local description")
nativeChannel.setLocalDescription(answer!) { error in
nativeChannel.setLocalDescription(updatedAnswer) { error in
guard error == nil else {
Logger.debug(type: .peerChannel,
message: "failed setting local description")
Expand All @@ -629,10 +657,10 @@ class PeerChannel: NSObject, RTCPeerConnectionDelegate {
Logger.debug(type: .peerChannel,
message: "did set local description")
Logger.debug(type: .peerChannel,
message: "\(answer!.sdpDescription)")
message: "\(updatedAnswer.sdpDescription)")
Logger.debug(type: .peerChannel,
message: "did create answer")
handler(answer!.sdp, nil)
handler(updatedAnswer.sdp, nil)
}
}
}
Expand Down Expand Up @@ -688,36 +716,7 @@ class PeerChannel: NSObject, RTCPeerConnectionDelegate {
return
}

var sdp = sdp!

// Chrome/Edge 向けのハック stereo=1 が CreateOffer では付与されないので、
// SDP を書き換えて stereo=1 を付与する
// https://github.com/w3c/webrtc-stats/issues/686
// https://github.com/w3c/webrtc-extensions/issues/63
// https://issues.webrtc.org/issues/41481053#comment18
if (self.configuration.forceStereoOutput) {
Logger.debug(type: .peerChannel, message: "stereo=1 を付与する")
let pattern = "minptime=\\d+"
let regexp = try! NSRegularExpression(pattern: pattern)
let replacementFunc: (String) -> String = { match in
if !match.contains("stereo=1") {
return "\(match);stereo=1"
}
return match
}

let matches = regexp.matches(in: sdp, range: NSRange(sdp.startIndex..., in: sdp))
for match in matches.reversed() {
print("kensaku: match: \(match)")
let range = match.range
let matchedString = (sdp as NSString).substring(with: range)
let replacedString = replacementFunc(matchedString)
print("kensaku: replacedString: \(replacedString)")
sdp = (sdp as NSString).replacingCharacters(in: range, with: replacedString)
}
}

let answer = SignalingAnswer(sdp: sdp)
let answer = SignalingAnswer(sdp: sdp!)
self.signalingChannel.send(message: Signaling.answer(answer))
self.lock.unlock()
Logger.debug(type: .peerChannel, message: "did send answer")
Expand Down

0 comments on commit e18849a

Please sign in to comment.