import Foundation import WebRTC /// libwebrtc, set up once for the life of the app. enum Engine { /// Off (`--no-playout-delay`) only to measure what the extension is worth. static var playoutDelay = true /// H.264 from this app's own low-latency encoder (`LowLatencyH264Encoder`) or from libwebrtc's /// (`--encoder stock` keeps the upstream encoder available for comparisons). static var ownEncoder = true /// Off only for a comparison with the previous sender (`--no-flexfec`). static var flexfec = true /// Where the engine says what the host should know (its log). static var report: (String) -> Void = { _ in } static let factory: RTCPeerConnectionFactory = { // M154 has no way to hand field trials to the factory from Objective-C: its environment is // made with none, which falls back to this global string (environment_factory.cc). Hence the // deprecated call; it has to come before anything else in the library. var trials = Tuning.fieldTrials if !playoutDelay { trials["WebRTC-ForceSendPlayoutDelay"] = nil } if !flexfec { trials["WebRTC-FlexFEC-03-Advertised"] = "Disabled" trials["WebRTC-FlexFEC-03"] = "Disabled" } // `--trial Name=value`, to try one out. for trial in Launch.values(after: "--trial") { let parts = trial.split(separator: "=", maxSplits: 1).map(String.init) if parts.count == 2 { trials[parts[0]] = parts[1] } } initFieldTrials(trials) RTCInitializeSSL() // VideoToolbox for H.264; the software codecs are there for a viewer without it. return RTCPeerConnectionFactory(encoderFactory: ScreenEncoderFactory(), decoderFactory: decoderFactory()) }() /// Codec level must fit this session's eventual rate, without raising old receivers' level. static func screenFactory(maximumFrameRate: Int) -> RTCPeerConnectionFactory { let standard = factory // Initialises SSL and field trials exactly once. guard maximumFrameRate > Tuning.fullFps else { return standard } return RTCPeerConnectionFactory(encoderFactory: ScreenEncoderFactory(maximumFrameRate: maximumFrameRate), decoderFactory: decoderFactory()) } private static func decoderFactory() -> RTCVideoDecoderFactory { HEVCDecoderFactory(MediaStageProbe.enabled ? MediaProbeDecoderFactory() : RTCDefaultVideoDecoderFactory()) } private static func initFieldTrials(_ trials: [String: String]) { // Through a protocol, so that the one deprecated call doesn't warn at every build. (Trials() as FieldTrialSetting).set(trials) } private struct Trials: FieldTrialSetting { @available(*, deprecated) func set(_ trials: [String: String]) { RTCInitFieldTrialDictionary(trials) } } /// libwebrtc's own log, into the host's (`--rtc-log`). private static var logger: RTCCallbackLogger? static func forwardLog(to link: Link) { let logger = RTCCallbackLogger() logger.severity = .info logger.start { message in link.log("rtc: " + message.trimmingCharacters(in: .whitespacesAndNewlines)) } self.logger = logger } } /// The codecs a transceiver offers, with the one asked for first. enum Codecs { /// H.264 high profile before baseline, then whatever else can be sent; the entries that aren't /// pictures (retransmission, FEC) keep their place at the end. static func preferences(_ wanted: String, from capabilities: [RTCRtpCodecCapability]) -> (codecs: [RTCRtpCodecCapability], found: Bool) { let name = wanted == "hevc" ? "H265" : "H264" func rank(_ codec: RTCRtpCodecCapability) -> Int { let upper = codec.name.uppercased() if ["RTX", "RED", "ULPFEC", "FLEXFEC-03"].contains(upper) { return 9 } if upper == name { return (codec.parameters["profile-level-id"] ?? "").lowercased().hasPrefix("64") ? 0 : 1 } if upper == "H264" { return (codec.parameters["profile-level-id"] ?? "").lowercased().hasPrefix("64") ? 2 : 3 } return 4 } let ordered = capabilities.enumerated().sorted { (rank($0.element), $0.offset) < (rank($1.element), $1.offset) }.map(\.element) return (ordered, capabilities.contains { $0.name.uppercased() == name }) } } private protocol FieldTrialSetting { func set(_ trials: [String: String]) } /// libwebrtc's decoders and HEVC (`HEVCDecoder`), for the test receiver (`--loopback`). final class HEVCDecoderFactory: NSObject, RTCVideoDecoderFactory { private let others: RTCVideoDecoderFactory init(_ others: RTCVideoDecoderFactory) { self.others = others } func supportedCodecs() -> [RTCVideoCodecInfo] { (HEVCDecoder.available ? [HEVCDecoder.codecInfo] : []) + others.supportedCodecs() } func createDecoder(_ info: RTCVideoCodecInfo) -> RTCVideoDecoder? { info.name == ScreenEncoderFactory.hevcName ? HEVCDecoder() : others.createDecoder(info) } }