优化RTMP播放器重连和稳定性
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user