Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Answer の SDP を書き換える処理を追加する #211

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 21 additions & 4 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 @@ -617,10 +616,28 @@ 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 = "(\\bminptime=\\d+\\b(?!;stereo=1))"
let replacementString = "$1;stereo=1"
let regexp = try! NSRegularExpression(pattern: pattern)
// NSRegularExpression が NSString に基づく API で、NSString は UTF-16 エンコーディングを
// 使用するので、 length を utf16.count で取得している
sdp = regexp.stringByReplacingMatches(in: sdp, range: NSRange(location: 0, length: sdp.utf16.count), withTemplate: replacementString)
}
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 @@ -630,10 +647,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