-
-
Notifications
You must be signed in to change notification settings - Fork 617
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
87 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,73 @@ | ||
import Foundation | ||
|
||
/// A type with a network bitrate strategy representation. | ||
public protocol HKStreamBitRateStrategy { | ||
public protocol HKStreamBitRateStrategy: Sendable { | ||
/// The mamimum video bitRate. | ||
var mamimumVideoBitRate: Int { get } | ||
/// The mamimum audio bitRate. | ||
var mamimumAudioBitRate: Int { get } | ||
|
||
/// Adjust a bitRate. | ||
func adjustBitrate(_ event: NetworkMonitorEvent, stream: some HKStream) | ||
func adjustBitrate(_ event: NetworkMonitorEvent, stream: some HKStream) async | ||
} | ||
|
||
/// An actor provides an algorithm that focuses on video bitrate control. | ||
public final actor HKStreamVideoAdaptiveBitRateStrategy: HKStreamBitRateStrategy { | ||
/// The status counts threshold for restoring the status | ||
public static let statusCountsThreshold: Int = 15 | ||
|
||
public let mamimumVideoBitRate: Int | ||
public let mamimumAudioBitRate: Int = 0 | ||
private var sufficientBWCounts: Int = 0 | ||
private var zeroBytesOutPerSecondCounts: Int = 0 | ||
|
||
/// Creates a new instance. | ||
public init(mamimumVideoBitrate: Int) { | ||
self.mamimumVideoBitRate = mamimumVideoBitrate | ||
} | ||
|
||
public func adjustBitrate(_ event: NetworkMonitorEvent, stream: some HKStream) async { | ||
switch event { | ||
case .status: | ||
var videoSettings = await stream.videoSettings | ||
if videoSettings.bitRate == mamimumVideoBitRate { | ||
return | ||
} | ||
if Self.statusCountsThreshold <= sufficientBWCounts { | ||
let incremental = mamimumVideoBitRate / 10 | ||
videoSettings.bitRate = min(videoSettings.bitRate + incremental, mamimumVideoBitRate) | ||
await stream.setVideoSettings(videoSettings) | ||
sufficientBWCounts = 0 | ||
} else { | ||
sufficientBWCounts += 1 | ||
} | ||
case .publishInsufficientBWOccured(let report): | ||
sufficientBWCounts = 0 | ||
var videoSettings = await stream.videoSettings | ||
let audioSettings = await stream.audioSettings | ||
if 0 < report.currentBytesOutPerSecond { | ||
let bitRate = Int(report.currentBytesOutPerSecond * 8) / (zeroBytesOutPerSecondCounts + 1) | ||
videoSettings.bitRate = max(bitRate - audioSettings.bitRate, mamimumVideoBitRate / 10) | ||
videoSettings.frameInterval = 0.0 | ||
sufficientBWCounts = 0 | ||
zeroBytesOutPerSecondCounts = 0 | ||
} else { | ||
switch zeroBytesOutPerSecondCounts { | ||
case 2: | ||
videoSettings.frameInterval = VideoCodecSettings.frameInterval10 | ||
case 4: | ||
videoSettings.frameInterval = VideoCodecSettings.frameInterval05 | ||
default: | ||
break | ||
} | ||
await stream.setVideoSettings(videoSettings) | ||
zeroBytesOutPerSecondCounts += 1 | ||
} | ||
case .reset: | ||
var videoSettings = await stream.videoSettings | ||
zeroBytesOutPerSecondCounts = 0 | ||
videoSettings.bitRate = mamimumVideoBitRate | ||
await stream.setVideoSettings(videoSettings) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
import Foundation | ||
|
||
/// An enumeration that indicate the network monitor event. | ||
public enum NetworkMonitorEvent: Sendable { | ||
/// To update statistics. | ||
case status(report: NetworkMonitorReport) | ||
/// To publish sufficient bandwidth occured. | ||
case publishInsufficientBWOccured(report: NetworkMonitorReport) | ||
case publishSufficientBWOccured(report: NetworkMonitorReport) | ||
/// To reset statistics. | ||
case reset | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
import Foundation | ||
|
||
/// The struct represents a network statistics. | ||
public struct NetworkMonitorReport: Sendable { | ||
/// The statistics of total incoming bytes. | ||
public let totalBytesIn: Int | ||
/// The statistics of total outgoing bytes. | ||
public let totalBytesOut: Int | ||
/// The statistics of outgoing queue bytes per second. | ||
public let currentQueueBytesOut: Int | ||
/// The statistics of incoming bytes per second. | ||
public let currentBytesInPerSecond: Int | ||
/// The statistics of outgoing bytes per second. | ||
public let currentBytesOutPerSecond: Int | ||
public let totalBytesIn: Int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters