fix: Use bitRate multiplier instead of setting it to an absolute value (#2216)

* fix: Use `bitRate` multiplier instead of setting it to an absolute value

* Pass override

* Format

* Rename

* feat: Also implement Android

* fix: Log Mbps properly

* fix: Up-/Down-scale bit-rate if different options

* fix: Parse in Manager

* Update RecordingSession+getRecommendedBitRate.kt
This commit is contained in:
Marc Rousavy
2023-11-27 17:20:26 +01:00
committed by GitHub
parent d78798ff84
commit d7f7095d1a
10 changed files with 231 additions and 84 deletions

View File

@@ -24,12 +24,31 @@ extension AVCaptureVideoDataOutput {
throw CameraError.capture(.createRecorderError(message: "Failed to get video settings!"))
}
if let bitRate = options.bitRate {
if let bitRateOverride = options.bitRateOverride {
// Convert from Mbps -> bps
let bitsPerSecond = bitRate * 1_000_000
settings[AVVideoCompressionPropertiesKey] = [
AVVideoAverageBitRateKey: NSNumber(value: bitsPerSecond),
]
let bitsPerSecond = bitRateOverride * 1_000_000
if settings[AVVideoCompressionPropertiesKey] == nil {
settings[AVVideoCompressionPropertiesKey] = [:]
}
var compressionSettings = settings[AVVideoCompressionPropertiesKey] as? [String: Any] ?? [:]
let currentBitRate = compressionSettings[AVVideoAverageBitRateKey] as? NSNumber
ReactLogger.log(level: .info, message: "Setting Video Bit-Rate from \(currentBitRate?.doubleValue.description ?? "nil") bps to \(bitsPerSecond) bps...")
compressionSettings[AVVideoAverageBitRateKey] = NSNumber(value: bitsPerSecond)
settings[AVVideoCompressionPropertiesKey] = compressionSettings
}
if let bitRateMultiplier = options.bitRateMultiplier {
// Check if the bit-rate even exists in the settings
if var compressionSettings = settings[AVVideoCompressionPropertiesKey] as? [String: Any],
let currentBitRate = compressionSettings[AVVideoAverageBitRateKey] as? NSNumber {
// Multiply the current value by the given multiplier
let newBitRate = Int(currentBitRate.doubleValue * bitRateMultiplier)
ReactLogger.log(level: .info, message: "Setting Video Bit-Rate from \(currentBitRate) bps to \(newBitRate) bps...")
compressionSettings[AVVideoAverageBitRateKey] = NSNumber(value: newBitRate)
settings[AVVideoCompressionPropertiesKey] = compressionSettings
}
}
return settings

View File

@@ -14,9 +14,14 @@ struct RecordVideoOptions {
var flash: Torch = .off
var codec: AVVideoCodecType?
/**
Bit-Rate of the Video, in Megabits per second (Mbps)
* Full Bit-Rate override for the Video Encoder, in Megabits per second (Mbps)
*/
var bitRate: Double?
var bitRateOverride: Double?
/**
* A multiplier applied to whatever the currently set bit-rate is, whether it's automatically computed by the OS Encoder,
* or set via bitRate, in Megabits per second (Mbps)
*/
var bitRateMultiplier: Double?
init(fromJSValue dictionary: NSDictionary) throws {
// File Type (.mov or .mp4)
@@ -31,9 +36,13 @@ struct RecordVideoOptions {
if let codecOption = dictionary["videoCodec"] as? String {
codec = try AVVideoCodecType(withString: codecOption)
}
// BitRate
if let parsed = dictionary["videoBitRate"] as? Double {
bitRate = parsed
// BitRate Override
if let parsed = dictionary["videoBitRateOverride"] as? Double {
bitRateOverride = parsed
}
// BitRate Multiplier
if let parsed = dictionary["videoBitRateMultiplier"] as? Double {
bitRateMultiplier = parsed
}
}
}