优化RTMP播放器重连和稳定性

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
caleb
2026-04-16 14:42:54 +08:00
parent f78ca66860
commit 72bfbb5cdf
81 changed files with 4869 additions and 5461 deletions

View File

@@ -1,6 +1,6 @@
// Software License Agreement (BSD License)
//
// Copyright (c) 2010-2021, Deusty, LLC
// Copyright (c) 2010-2026, Deusty, LLC
// All rights reserved.
//
// Redistribution and use of this software in source and binary forms,
@@ -13,71 +13,164 @@
// to endorse or promote products derived from this software without specific
// prior written permission of Deusty, LLC.
@_exported import CocoaLumberjack
@_exported public import CocoaLumberjack
#if SWIFT_PACKAGE
import CocoaLumberjackSwiftSupport
public import CocoaLumberjackSwiftSupport
#endif
extension DDLogFlag {
public static func from(_ logLevel: DDLogLevel) -> DDLogFlag {
return DDLogFlag(rawValue: logLevel.rawValue)
}
public init(_ logLevel: DDLogLevel) {
self = DDLogFlag(rawValue: logLevel.rawValue)
}
/// Returns the log level, or the lowest equivalent.
public func toLogLevel() -> DDLogLevel {
if let ourValid = DDLogLevel(rawValue: rawValue) {
return ourValid
} else {
if contains(.verbose) {
return .verbose
} else if contains(.debug) {
return .debug
} else if contains(.info) {
return .info
} else if contains(.warning) {
return .warning
} else if contains(.error) {
return .error
} else {
return .off
}
}
}
}
/// The log level that can dynamically limit log messages (vs. the static DDDefaultLogLevel). This log level will only be checked, if the message passes the `DDDefaultLogLevel`.
public var dynamicLogLevel = DDLogLevel.all
/// Resets the `dynamicLogLevel` to `.all`.
/// - SeeAlso: `dynamicLogLevel`
@inlinable
public func resetDynamicLogLevel() {
dynamicLogLevel = .all
}
@available(*, deprecated, message: "Please use dynamicLogLevel", renamed: "dynamicLogLevel")
public var defaultDebugLevel: DDLogLevel {
get {
return dynamicLogLevel
public func _DDLogMessage(_ messageFormat: @autoclosure () -> DDLogMessageFormat,
level: DDLogLevel,
flag: DDLogFlag,
context: Int,
file: StaticString,
function: StaticString,
line: UInt,
tag: Any?,
asynchronous: Bool?,
ddlog: DDLog) {
// The `dynamicLogLevel` will always be checked here (instead of being passed in).
// We cannot "mix" it with the `DDDefaultLogLevel`, because otherwise the compiler won't strip strings that are not logged.
#if compiler(>=6.2)
if unsafe level.rawValue & flag.rawValue != 0 && dynamicLogLevel.rawValue & flag.rawValue != 0 {
let logMessage = DDLogMessage(messageFormat(),
level: level,
flag: flag,
context: context,
file: file,
function: function,
line: line,
tag: tag)
unsafe ddlog.log(asynchronous: asynchronous ?? asyncLoggingEnabled, message: logMessage)
}
set {
dynamicLogLevel = newValue
#else
if level.rawValue & flag.rawValue != 0 && dynamicLogLevel.rawValue & flag.rawValue != 0 {
let logMessage = DDLogMessage(messageFormat(),
level: level,
flag: flag,
context: context,
file: file,
function: function,
line: line,
tag: tag)
ddlog.log(asynchronous: asynchronous ?? asyncLoggingEnabled, message: logMessage)
}
#endif
}
@available(*, deprecated, message: "Please use resetDynamicLogLevel", renamed: "resetDynamicLogLevel")
public func resetDefaultDebugLevel() {
resetDynamicLogLevel()
}
/// If `true`, all logs (except errors) are logged asynchronously by default.
public var asyncLoggingEnabled = true
@inlinable
public func DDLogDebug(_ message: @autoclosure () -> DDLogMessageFormat,
level: DDLogLevel = DDDefaultLogLevel,
context: Int = 0,
file: StaticString = #file,
function: StaticString = #function,
line: UInt = #line,
tag: Any? = nil,
asynchronous: Bool? = nil,
ddlog: DDLog = .sharedInstance) {
_DDLogMessage(message(),
level: level,
flag: .debug,
context: context,
file: file,
function: function,
line: line,
tag: tag,
asynchronous: asynchronous,
ddlog: ddlog)
}
@inlinable
public func DDLogInfo(_ message: @autoclosure () -> DDLogMessageFormat,
level: DDLogLevel = DDDefaultLogLevel,
context: Int = 0,
file: StaticString = #file,
function: StaticString = #function,
line: UInt = #line,
tag: Any? = nil,
asynchronous: Bool? = nil,
ddlog: DDLog = .sharedInstance) {
_DDLogMessage(message(),
level: level,
flag: .info,
context: context,
file: file,
function: function,
line: line,
tag: tag,
asynchronous: asynchronous,
ddlog: ddlog)
}
@inlinable
public func DDLogWarn(_ message: @autoclosure () -> DDLogMessageFormat,
level: DDLogLevel = DDDefaultLogLevel,
context: Int = 0,
file: StaticString = #file,
function: StaticString = #function,
line: UInt = #line,
tag: Any? = nil,
asynchronous: Bool? = nil,
ddlog: DDLog = .sharedInstance) {
_DDLogMessage(message(),
level: level,
flag: .warning,
context: context,
file: file,
function: function,
line: line,
tag: tag,
asynchronous: asynchronous,
ddlog: ddlog)
}
@inlinable
public func DDLogVerbose(_ message: @autoclosure () -> DDLogMessageFormat,
level: DDLogLevel = DDDefaultLogLevel,
context: Int = 0,
file: StaticString = #file,
function: StaticString = #function,
line: UInt = #line,
tag: Any? = nil,
asynchronous: Bool? = nil,
ddlog: DDLog = .sharedInstance) {
_DDLogMessage(message(),
level: level,
flag: .verbose,
context: context,
file: file,
function: function,
line: line,
tag: tag,
asynchronous: asynchronous,
ddlog: ddlog)
}
@inlinable
public func DDLogError(_ message: @autoclosure () -> DDLogMessageFormat,
level: DDLogLevel = DDDefaultLogLevel,
context: Int = 0,
file: StaticString = #file,
function: StaticString = #function,
line: UInt = #line,
tag: Any? = nil,
asynchronous: Bool? = nil,
ddlog: DDLog = .sharedInstance) {
_DDLogMessage(message(),
level: level,
flag: .error,
context: context,
file: file,
function: function,
line: line,
tag: tag,
asynchronous: asynchronous ?? false,
ddlog: ddlog)
}
@available(*, deprecated, message: "Use an interpolated DDLogMessageFormat instead")
@inlinable
@_disfavoredOverload
public func _DDLogMessage(_ message: @autoclosure () -> Any,
level: DDLogLevel,
flag: DDLogFlag,
@@ -86,27 +179,25 @@ public func _DDLogMessage(_ message: @autoclosure () -> Any,
function: StaticString,
line: UInt,
tag: Any?,
asynchronous: Bool,
asynchronous: Bool?,
ddlog: DDLog) {
// The `dynamicLogLevel` will always be checked here (instead of being passed in).
// We cannot "mix" it with the `DDDefaultLogLevel`, because otherwise the compiler won't strip strings that are not logged.
if level.rawValue & flag.rawValue != 0 && dynamicLogLevel.rawValue & flag.rawValue != 0 {
// Tell the DDLogMessage constructor to copy the C strings that get passed to it.
let logMessage = DDLogMessage(message: String(describing: message()),
level: level,
flag: flag,
context: context,
file: String(describing: file),
function: String(describing: function),
line: line,
tag: tag,
options: [.copyFile, .copyFunction],
timestamp: nil)
ddlog.log(asynchronous: asynchronous, message: logMessage)
}
// This will lead to `messageFormat` and `message` being equal on DDLogMessage,
// which is what the legacy initializer of DDLogMessage does as well.
_DDLogMessage(.init(_formattedMessage: String(describing: message())),
level: level,
flag: flag,
context: context,
file: file,
function: function,
line: line,
tag: tag,
asynchronous: asynchronous,
ddlog: ddlog)
}
@available(*, deprecated, message: "Use an interpolated DDLogMessageFormat instead")
@inlinable
@_disfavoredOverload
public func DDLogDebug(_ message: @autoclosure () -> Any,
level: DDLogLevel = DDDefaultLogLevel,
context: Int = 0,
@@ -114,12 +205,23 @@ public func DDLogDebug(_ message: @autoclosure () -> Any,
function: StaticString = #function,
line: UInt = #line,
tag: Any? = nil,
asynchronous async: Bool = asyncLoggingEnabled,
asynchronous async: Bool? = nil,
ddlog: DDLog = .sharedInstance) {
_DDLogMessage(message(), level: level, flag: .debug, context: context, file: file, function: function, line: line, tag: tag, asynchronous: async, ddlog: ddlog)
_DDLogMessage(message(),
level: level,
flag: .debug,
context: context,
file: file,
function: function,
line: line,
tag: tag,
asynchronous: async,
ddlog: ddlog)
}
@available(*, deprecated, message: "Use an interpolated DDLogMessageFormat instead")
@inlinable
@_disfavoredOverload
public func DDLogInfo(_ message: @autoclosure () -> Any,
level: DDLogLevel = DDDefaultLogLevel,
context: Int = 0,
@@ -127,12 +229,23 @@ public func DDLogInfo(_ message: @autoclosure () -> Any,
function: StaticString = #function,
line: UInt = #line,
tag: Any? = nil,
asynchronous async: Bool = asyncLoggingEnabled,
asynchronous async: Bool? = nil,
ddlog: DDLog = .sharedInstance) {
_DDLogMessage(message(), level: level, flag: .info, context: context, file: file, function: function, line: line, tag: tag, asynchronous: async, ddlog: ddlog)
_DDLogMessage(message(),
level: level,
flag: .info,
context: context,
file: file,
function: function,
line: line,
tag: tag,
asynchronous: async,
ddlog: ddlog)
}
@available(*, deprecated, message: "Use an interpolated DDLogMessageFormat instead")
@inlinable
@_disfavoredOverload
public func DDLogWarn(_ message: @autoclosure () -> Any,
level: DDLogLevel = DDDefaultLogLevel,
context: Int = 0,
@@ -140,12 +253,23 @@ public func DDLogWarn(_ message: @autoclosure () -> Any,
function: StaticString = #function,
line: UInt = #line,
tag: Any? = nil,
asynchronous async: Bool = asyncLoggingEnabled,
asynchronous async: Bool? = nil,
ddlog: DDLog = .sharedInstance) {
_DDLogMessage(message(), level: level, flag: .warning, context: context, file: file, function: function, line: line, tag: tag, asynchronous: async, ddlog: ddlog)
_DDLogMessage(message(),
level: level,
flag: .warning,
context: context,
file: file,
function: function,
line: line,
tag: tag,
asynchronous: async,
ddlog: ddlog)
}
@available(*, deprecated, message: "Use an interpolated DDLogMessageFormat instead")
@inlinable
@_disfavoredOverload
public func DDLogVerbose(_ message: @autoclosure () -> Any,
level: DDLogLevel = DDDefaultLogLevel,
context: Int = 0,
@@ -153,12 +277,23 @@ public func DDLogVerbose(_ message: @autoclosure () -> Any,
function: StaticString = #function,
line: UInt = #line,
tag: Any? = nil,
asynchronous async: Bool = asyncLoggingEnabled,
asynchronous async: Bool? = nil,
ddlog: DDLog = .sharedInstance) {
_DDLogMessage(message(), level: level, flag: .verbose, context: context, file: file, function: function, line: line, tag: tag, asynchronous: async, ddlog: ddlog)
_DDLogMessage(message(),
level: level,
flag: .verbose,
context: context,
file: file,
function: function,
line: line,
tag: tag,
asynchronous: async,
ddlog: ddlog)
}
@available(*, deprecated, message: "Use an interpolated DDLogMessageFormat instead")
@inlinable
@_disfavoredOverload
public func DDLogError(_ message: @autoclosure () -> Any,
level: DDLogLevel = DDDefaultLogLevel,
context: Int = 0,
@@ -166,28 +301,16 @@ public func DDLogError(_ message: @autoclosure () -> Any,
function: StaticString = #function,
line: UInt = #line,
tag: Any? = nil,
asynchronous async: Bool = false,
asynchronous async: Bool? = nil,
ddlog: DDLog = .sharedInstance) {
_DDLogMessage(message(), level: level, flag: .error, context: context, file: file, function: function, line: line, tag: tag, asynchronous: async, ddlog: ddlog)
}
/// Returns a String of the current filename, without full path or extension.
///
/// Analogous to the C preprocessor macro `THIS_FILE`.
public func currentFileName(_ fileName: StaticString = #file) -> String {
var str = String(describing: fileName)
if let idx = str.range(of: "/", options: .backwards)?.upperBound {
str = String(str[idx...])
}
if let idx = str.range(of: ".", options: .backwards)?.lowerBound {
str = String(str[..<idx])
}
return str
}
// swiftlint:disable identifier_name
// swiftlint doesn't like func names that begin with a capital letter - deprecated
@available(*, deprecated, message: "Please use currentFileName", renamed: "currentFileName")
public func CurrentFileName(_ fileName: StaticString = #file) -> String {
return currentFileName(fileName)
_DDLogMessage(message(),
level: level,
flag: .error,
context: context,
file: file,
function: function,
line: line,
tag: tag,
asynchronous: async ?? false,
ddlog: ddlog)
}

View File

@@ -1,6 +1,6 @@
// Software License Agreement (BSD License)
//
// Copyright (c) 2010-2021, Deusty, LLC
// Copyright (c) 2010-2026, Deusty, LLC
// All rights reserved.
//
// Redistribution and use of this software in source and binary forms,
@@ -14,8 +14,8 @@
// prior written permission of Deusty, LLC.
#if SWIFT_PACKAGE
import CocoaLumberjack
import CocoaLumberjackSwiftSupport
public import CocoaLumberjack
public import CocoaLumberjackSwiftSupport
#endif
/**
@@ -29,9 +29,91 @@ import CocoaLumberjackSwiftSupport
* The default is an empty string.
*/
@inlinable
public func DDAssert(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String = "", level: DDLogLevel = DDDefaultLogLevel, context: Int = 0, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, tag: Any? = nil, asynchronous async: Bool = false, ddlog: DDLog = DDLog.sharedInstance) {
public func DDAssert(_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> DDLogMessageFormat = "",
level: DDLogLevel = DDDefaultLogLevel,
context: Int = 0,
file: StaticString = #file,
function: StaticString = #function,
line: UInt = #line,
tag: Any? = nil,
asynchronous async: Bool? = nil,
ddlog: DDLog = DDLog.sharedInstance) {
if !condition() {
DDLogError(message(), level: level, context: context, file: file, function: function, line: line, tag: tag, asynchronous: async, ddlog: ddlog)
DDLogError(message(),
level: level,
context: context,
file: file,
function: function,
line: line,
tag: tag,
asynchronous: async,
ddlog: ddlog)
Swift.assertionFailure(message().formatted, file: file, line: line)
}
}
/**
* Replacement for Swift's `assertionFailure` function that will output a log message even
* when assertions are disabled.
*
* - Parameters:
* - message: A string to log (using `DDLogError`). The default is an empty string.
*/
@inlinable
public func DDAssertionFailure(_ message: @autoclosure () -> DDLogMessageFormat = "",
level: DDLogLevel = DDDefaultLogLevel,
context: Int = 0,
file: StaticString = #file,
function: StaticString = #function,
line: UInt = #line,
tag: Any? = nil,
asynchronous async: Bool? = nil,
ddlog: DDLog = DDLog.sharedInstance) {
DDLogError(message(),
level: level,
context: context,
file: file,
function: function,
line: line,
tag: tag,
asynchronous: async,
ddlog: ddlog)
Swift.assertionFailure(message().formatted, file: file, line: line)
}
/**
* Replacement for Swift's `assert` function that will output a log message even when assertions
* are disabled.
*
* - Parameters:
* - condition: The condition to test. Unlike `Swift.assert`, `condition` is always evaluated,
* even when assertions are disabled.
* - message: A string to log (using `DDLogError`) if `condition` evaluates to `false`.
* The default is an empty string.
*/
@inlinable
@available(*, deprecated, message: "Use an interpolated message.")
public func DDAssert(_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String = "",
level: DDLogLevel = DDDefaultLogLevel,
context: Int = 0,
file: StaticString = #file,
function: StaticString = #function,
line: UInt = #line,
tag: Any? = nil,
asynchronous async: Bool? = nil,
ddlog: DDLog = DDLog.sharedInstance) {
if !condition() {
DDLogError(message(),
level: level,
context: context,
file: file,
function: function,
line: line,
tag: tag,
asynchronous: async,
ddlog: ddlog)
Swift.assertionFailure(message(), file: file, line: line)
}
}
@@ -44,7 +126,24 @@ public func DDAssert(_ condition: @autoclosure () -> Bool, _ message: @autoclosu
* - message: A string to log (using `DDLogError`). The default is an empty string.
*/
@inlinable
public func DDAssertionFailure(_ message: @autoclosure () -> String = "", level: DDLogLevel = DDDefaultLogLevel, context: Int = 0, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, tag: Any? = nil, asynchronous async: Bool = false, ddlog: DDLog = DDLog.sharedInstance) {
DDLogError(message(), level: level, context: context, file: file, function: function, line: line, tag: tag, asynchronous: async, ddlog: ddlog)
@available(*, deprecated, message: "Use an interpolated message.")
public func DDAssertionFailure(_ message: @autoclosure () -> String = "",
level: DDLogLevel = DDDefaultLogLevel,
context: Int = 0,
file: StaticString = #file,
function: StaticString = #function,
line: UInt = #line,
tag: Any? = nil,
asynchronous async: Bool? = nil,
ddlog: DDLog = DDLog.sharedInstance) {
DDLogError(message(),
level: level,
context: context,
file: file,
function: function,
line: line,
tag: tag,
asynchronous: async,
ddlog: ddlog)
Swift.assertionFailure(message(), file: file, line: line)
}

View File

@@ -1,6 +1,6 @@
// Software License Agreement (BSD License)
//
// Copyright (c) 2010-2021, Deusty, LLC
// Copyright (c) 2010-2026, Deusty, LLC
// All rights reserved.
//
// Redistribution and use of this software in source and binary forms,
@@ -15,15 +15,76 @@
#if arch(arm64) || arch(x86_64)
#if canImport(Combine)
import Combine
public import Combine
#if SWIFT_PACKAGE
import CocoaLumberjack
import CocoaLumberjackSwiftSupport
public import CocoaLumberjack
#endif
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension DDLog {
// MARK: - Subscription
private final class Subscription<S: Subscriber>: NSObject, DDLogger, Combine.Subscription
where S.Input == DDLogMessage
{ // swiftlint:disable:this opening_brace
private var subscriber: S?
private weak var log: DDLog?
/// Not used but ``DDLogger`` requires it.
/// The preferred way to achieve this is to use the `map` Combine operator of the publisher.
/// Example:
/// ```
/// DDLog.sharedInstance.messagePublisher()
/// .map { message in /* format message */ }
/// .sink(receiveValue: { formattedMessage in /* process formattedMessage */ })
/// ```
var logFormatter: (any DDLogFormatter)?
init(log: DDLog, with logLevel: DDLogLevel, subscriber: S) {
self.subscriber = subscriber
self.log = log
super.init()
log.add(self, with: logLevel)
}
func request(_ demand: Subscribers.Demand) {
// The log messages are endless until canceled, so we won't do any demand management.
// Combine operators can be used to deal with it as needed.
}
func cancel() {
log?.remove(self)
subscriber = nil
}
func log(message logMessage: DDLogMessage) {
_ = subscriber?.receive(logMessage)
}
}
// MARK: - Publisher
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
public struct MessagePublisher: Combine.Publisher {
public typealias Output = DDLogMessage
public typealias Failure = Never
private let log: DDLog
private let logLevel: DDLogLevel
public init(log: DDLog, with logLevel: DDLogLevel) {
self.log = log
self.logLevel = logLevel
}
public func receive<S>(subscriber: S)
where S: Subscriber, S.Failure == Failure, S.Input == Output
{ // swiftlint:disable:this opening_brace
let subscription = Subscription(log: log, with: logLevel, subscriber: subscriber)
subscriber.receive(subscription: subscription)
}
}
/**
* Creates a message publisher.
*
@@ -40,73 +101,15 @@ extension DDLog {
* - Returns: A MessagePublisher that emits LogMessages filtered by the specified logLevel
**/
public func messagePublisher(with logLevel: DDLogLevel = .all) -> MessagePublisher {
return MessagePublisher(log: self, with: logLevel)
}
// MARK: - MessagePublisher
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
public struct MessagePublisher: Combine.Publisher {
public typealias Output = DDLogMessage
public typealias Failure = Never
private let log: DDLog
private let logLevel: DDLogLevel
public init(log: DDLog, with logLevel: DDLogLevel) {
self.log = log
self.logLevel = logLevel
}
public func receive<S>(subscriber: S) where S: Subscriber, S.Failure == Failure, S.Input == Output {
let subscription = Subscription(log: log, with: logLevel, subscriber: subscriber)
subscriber.receive(subscription: subscription)
}
}
// MARK: - Subscription
private final class Subscription<S: Subscriber>: NSObject, DDLogger, Combine.Subscription where S.Input == DDLogMessage {
private var subscriber: S?
private weak var log: DDLog?
//Not used but DDLogger requires it. The preferred way to achieve this is to use the `map()` Combine operator of the publisher.
//ie:
// DDLog.sharedInstance.messagePublisher()
// .map { [format log message] }
// .sink(receiveValue: { [process log message] })
//
var logFormatter: DDLogFormatter?
init(log: DDLog, with logLevel: DDLogLevel, subscriber: S) {
self.subscriber = subscriber
self.log = log
super.init()
log.add(self, with: logLevel)
}
func request(_ demand: Subscribers.Demand) {
//The log messages are endless until canceled, so we won't do any demand management.
//Combine operators can be used to deal with it as needed.
}
func cancel() {
log?.remove(self)
subscriber = nil
}
func log(message logMessage: DDLogMessage) {
_ = subscriber?.receive(logMessage)
}
MessagePublisher(log: self, with: logLevel)
}
}
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Publisher where Output == DDLogMessage {
public func formatted(with formatter: DDLogFormatter) -> Publishers.CompactMap<Self, String> {
return compactMap { formatter.format(message: $0) }
public func formatted(with formatter: any DDLogFormatter) -> Publishers.CompactMap<Self, String> {
compactMap { formatter.format(message: $0) }
}
}
#endif
#endif