Skip to content

Commit

Permalink
Answer の SDP を書き換える処理を追加する
Browse files Browse the repository at this point in the history
  • Loading branch information
zztkm committed Sep 12, 2024
1 parent 905635c commit 8b73c31
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Sora/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ public struct Configuration {
/// 通常、指定する必要はありません。
public var publisherAudioTrackId: String = defaultPublisherAudioTrackId

/// ステレオ出力を許可するための設定です。
public var forceStereoOutput: Bool = false

/**
初期化します。

Expand Down
32 changes: 30 additions & 2 deletions Sora/PeerChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,6 @@ class PeerChannel: NSObject, RTCPeerConnectionDelegate {

Logger.debug(type: .peerChannel, message: "try create answer")
Logger.debug(type: .peerChannel, message: offer)

Logger.debug(type: .peerChannel, message: "try setting remote description")
let offer = RTCSessionDescription(type: .offer, sdp: offer)
nativeChannel.setRemoteDescription(offer) { error in
Expand Down Expand Up @@ -689,7 +688,36 @@ class PeerChannel: NSObject, RTCPeerConnectionDelegate {
return
}

let answer = SignalingAnswer(sdp: sdp!)
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)
self.signalingChannel.send(message: Signaling.answer(answer))
self.lock.unlock()
Logger.debug(type: .peerChannel, message: "did send answer")
Expand Down

0 comments on commit 8b73c31

Please sign in to comment.