-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathRTC.swift
More file actions
204 lines (161 loc) · 8.32 KB
/
Copy pathRTC.swift
File metadata and controls
204 lines (161 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright 2026 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Foundation
internal import LiveKitWebRTC
private extension Array where Element: LKRTCVideoCodecInfo {
func rewriteCodecsIfNeeded() -> [LKRTCVideoCodecInfo] {
// rewrite H264's profileLevelId to 42e032
map { $0.name == kLKRTCVideoCodecH264Name ? RTC.h264BaselineLevel5CodecInfo : $0 }
// logger.log("supportedCodecs: \(codecs.map({ "\($0.name) - \($0.parameters)" }).joined(separator: ", "))", type: RTC.self)
}
}
private class VideoEncoderFactory: LKRTCDefaultVideoEncoderFactory, @unchecked Sendable {
override func supportedCodecs() -> [LKRTCVideoCodecInfo] {
super.supportedCodecs().rewriteCodecsIfNeeded()
}
}
private class VideoDecoderFactory: LKRTCDefaultVideoDecoderFactory, @unchecked Sendable {
override func supportedCodecs() -> [LKRTCVideoCodecInfo] {
super.supportedCodecs().rewriteCodecsIfNeeded()
}
}
private class VideoEncoderFactorySimulcast: LKRTCVideoEncoderFactorySimulcast, @unchecked Sendable {
override func supportedCodecs() -> [LKRTCVideoCodecInfo] {
super.supportedCodecs().rewriteCodecsIfNeeded()
}
}
actor RTC {
struct PeerConnectionFactoryState {
var isInitialized: Bool = false
var admType: AudioDeviceModuleType = .audioEngine
var bypassVoiceProcessing: Bool = false
}
static let pcFactoryState = StateSync(PeerConnectionFactoryState())
static let h264BaselineLevel5CodecInfo: LKRTCVideoCodecInfo = {
// this should never happen
guard let profileLevelId = LKRTCH264ProfileLevelId(profile: .constrainedBaseline, level: .level5) else {
Room.log("failed to generate profileLevelId", .error)
fatalError("failed to generate profileLevelId")
}
// create a new H264 codec with new profileLevelId
return LKRTCVideoCodecInfo(name: kLKRTCH264CodecName,
parameters: ["profile-level-id": profileLevelId.hexString,
"level-asymmetry-allowed": "1",
"packetization-mode": "1"])
}()
// global properties are already lazy
static let encoderFactory: LKRTCVideoEncoderFactory & Sendable = {
let encoderFactory = VideoEncoderFactory()
return VideoEncoderFactorySimulcast(primary: encoderFactory,
fallback: encoderFactory)
}()
static let decoderFactory: LKRTCVideoDecoderFactory & Sendable = VideoDecoderFactory()
static let audioProcessingModule: LKRTCDefaultAudioProcessingModule = .init()
static let videoSenderCapabilities = peerConnectionFactory.rtpSenderCapabilities(forKind: kLKRTCMediaStreamTrackKindVideo)
static let audioSenderCapabilities = peerConnectionFactory.rtpSenderCapabilities(forKind: kLKRTCMediaStreamTrackKindAudio)
static let peerConnectionFactory: LKRTCPeerConnectionFactory = {
// Update pc init lock
let (admType, bypassVoiceProcessing) = pcFactoryState.mutate {
$0.isInitialized = true
return ($0.admType, $0.bypassVoiceProcessing)
}
Room.log("Initializing SSL...")
LKRTCInitializeSSL()
Room.log("Initializing PeerConnectionFactory...")
// Enable DTLS during ICE handshake for faster connection setup.
LKRTCPeerConnectionFactory.configureFieldTrials("WebRTC-IceHandshakeDtls/Enabled/")
return LKRTCPeerConnectionFactory(audioDeviceModuleType: admType.toRTCType(),
bypassVoiceProcessing: bypassVoiceProcessing,
encoderFactory: encoderFactory,
decoderFactory: decoderFactory,
audioProcessingModule: audioProcessingModule)
}()
// forbid direct access
static var audioDeviceModule: LKRTCAudioDeviceModule {
peerConnectionFactory.audioDeviceModule
}
static func createPeerConnection(_ configuration: LKRTCConfiguration,
constraints: LKRTCMediaConstraints) -> LKRTCPeerConnection?
{
DispatchQueue.liveKitWebRTC.sync { peerConnectionFactory.peerConnection(with: configuration,
constraints: constraints,
delegate: nil) }
}
static func createVideoSource(forScreenShare: Bool) -> LKRTCVideoSource {
DispatchQueue.liveKitWebRTC.sync { peerConnectionFactory.videoSource(forScreenCast: forScreenShare) }
}
static func createVideoTrack(source: LKRTCVideoSource) -> LKRTCVideoTrack {
DispatchQueue.liveKitWebRTC.sync { peerConnectionFactory.videoTrack(with: source,
trackId: UUID().uuidString) }
}
static func createAudioSource(_ constraints: LKRTCMediaConstraints?) -> LKRTCAudioSource {
DispatchQueue.liveKitWebRTC.sync { peerConnectionFactory.audioSource(with: constraints) }
}
static func createAudioTrack(source: LKRTCAudioSource) -> LKRTCAudioTrack {
DispatchQueue.liveKitWebRTC.sync { peerConnectionFactory.audioTrack(with: source,
trackId: UUID().uuidString) }
}
static func createDataChannelConfiguration(ordered: Bool = true,
maxRetransmits: Int32 = -1) -> LKRTCDataChannelConfiguration
{
let result = DispatchQueue.liveKitWebRTC.sync { LKRTCDataChannelConfiguration() }
result.isOrdered = ordered
result.maxRetransmits = maxRetransmits
return result
}
static func createDataBuffer(data: Data) -> LKRTCDataBuffer {
DispatchQueue.liveKitWebRTC.sync { LKRTCDataBuffer(data: data, isBinary: true) }
}
static func createIceCandidate(fromJsonString: String) throws -> LKRTCIceCandidate {
try DispatchQueue.liveKitWebRTC.sync { try LKRTCIceCandidate(fromJsonString: fromJsonString) }
}
static func createSessionDescription(type: LKRTCSdpType, sdp: String) -> LKRTCSessionDescription {
DispatchQueue.liveKitWebRTC.sync { LKRTCSessionDescription(type: type, sdp: sdp) }
}
static func createVideoCapturer() -> LKRTCVideoCapturer {
DispatchQueue.liveKitWebRTC.sync { LKRTCVideoCapturer() }
}
static func createRtpEncodingParameters(rid: String? = nil,
encoding: MediaEncoding? = nil,
scaleDownBy: Double? = nil,
active: Bool = true,
scalabilityMode: ScalabilityMode? = nil) -> LKRTCRtpEncodingParameters
{
let result = DispatchQueue.liveKitWebRTC.sync { LKRTCRtpEncodingParameters() }
result.isActive = active
result.rid = rid
if let scaleDownBy {
result.scaleResolutionDownBy = NSNumber(value: scaleDownBy)
}
if let encoding {
result.maxBitrateBps = NSNumber(value: encoding.maxBitrate)
// VideoEncoding specific
if let videoEncoding = encoding as? VideoEncoding {
result.maxFramerate = NSNumber(value: videoEncoding.maxFps)
}
}
if let scalabilityMode {
result.scalabilityMode = scalabilityMode.rawStringValue
}
if let bitratePriority = encoding?.bitratePriority {
result.bitratePriority = bitratePriority.toBitratePriority()
}
if let networkPriority = encoding?.networkPriority {
result.networkPriority = networkPriority.toRTCPriority()
}
return result
}
}