Skip to content

Commit

Permalink
WebRTCConfiguration に senderDegradationPreference を追加する
Browse files Browse the repository at this point in the history
  • Loading branch information
enm10k committed Feb 19, 2024
1 parent b4066f1 commit b22bc5a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
- @miosakuma
- [ADD] VideoCodec に H265 を追加する
- @enm10k
- [UPDATE] WebRTCConfiguration に senderDegradationPreference を追加する
- @enm10k
- [UPDATE] 解像度に qHD (960x540) を追加する
- @enm10k
- [UPDATE] CocoaPods を v1.14.2 に更新する
Expand Down
36 changes: 36 additions & 0 deletions Sora/PeerChannel.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
import Foundation
import WebRTC

/// :nodoc:
extension RTCDegradationPreference: CustomStringConvertible {
public var description: String {
switch self {
case .balanced: "balanced"
case .disabled: "disabled"
case .maintainFramerate: "maintain-framerate"
case .maintainResolution: "maintain-resolution"
default: "-"
}
}
}

/// :nodoc:
extension RTCRtpParameters {
override open var description: String {
let degradationPreference = if let unwrapped = self.degradationPreference {
String(describing: RTCDegradationPreference(rawValue: unwrapped.intValue))
} else {
"-"
}

// RTCRtpParameters は他にもプロパティーを持つが、ここでは SDK で利用している値のみ出力する
// encodings もここに追加したい
return "\(transactionId) \(String(describing: degradationPreference))"
}
}

final class PeerChannelInternalHandlers {
/// 接続解除時に呼ばれるクロージャー
var onDisconnect: ((Error?, DisconnectReason) -> Void)?
Expand Down Expand Up @@ -382,6 +410,14 @@ class PeerChannel: NSObject, RTCPeerConnectionDelegate {
if let videoTrack = nativeStream.videoTracks.first {
videoTransceiver.sender.track = videoTrack
}

if let degradationPreference = configuration.webRTCConfiguration.senderDegradationPreference {
let parameters = videoTransceiver.sender.parameters
parameters.degradationPreference = NSNumber.init(value: degradationPreference.nativeValue.rawValue)
videoTransceiver.sender.parameters = parameters
}

Logger.debug(type: .peerChannel, message: "sender.parameters => \(String(describing: videoTransceiver.sender.parameters))")
}
} else {
// mid なしの場合はエラーにする
Expand Down
35 changes: 35 additions & 0 deletions Sora/WebRTCConfigration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,38 @@ public enum SDPSemantics {
}
}

/**
(リソースの逼迫により) 送信する映像の品質が劣化した場合の挙動です。
*/
public enum DegradationPreference {
/// リソースが逼迫しても何もしない
case disabled

/// バランスを取る
case balanced

/// フレームレートを維持する
case maintainFramerate

/// 解像度を維持する
case maintainResolution

// MARK: - ネイティブ

var nativeValue: RTCDegradationPreference {
switch self {
case .balanced:
return RTCDegradationPreference.balanced
case .disabled:
return RTCDegradationPreference.disabled
case .maintainFramerate:
return RTCDegradationPreference.maintainFramerate
case .maintainResolution:
return RTCDegradationPreference.maintainResolution
}
}
}

/**
WebRTC に関する設定です。
*/
Expand All @@ -58,6 +90,9 @@ public struct WebRTCConfiguration {
/// SDP でのマルチストリームの記述方式
public var sdpSemantics: SDPSemantics = .unifiedPlan

/// (リソースの不足により) 送信する映像の品質が劣化した場合の挙動
public var senderDegradationPreference: DegradationPreference?

// MARK: - インスタンスの生成

/**
Expand Down

0 comments on commit b22bc5a

Please sign in to comment.