Skip to content

Commit

Permalink
implement PluginRTCVideoCaptureController DEFAULT_HEIGHT, DEFAULT_WID…
Browse files Browse the repository at this point in the history
…TH, DEFAULT_FPS to limit RTCCameraVideoCapturer ramp up when not with and height constraints is provided causing to many resize and leading to freeze on chrome
  • Loading branch information
hthetiot committed Oct 25, 2019
1 parent 5a047f0 commit 0a90699
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/PluginRTCVideoCaptureController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ extension RTCMediaStreamTrack {

class PluginRTCVideoCaptureController : NSObject {

private let DEFAULT_HEIGHT : Int = 480
private let DEFAULT_WIDTH : Int = 640
private let DEFAULT_FPS : Int = 15

var capturer: RTCCameraVideoCapturer

// Default to the front camera.
var device: AVCaptureDevice?
var deviceFormat: AVCaptureDevice.Format?
var deviceFrameRate: Int = 30
var deviceFrameRate: Int?

var constraints: NSDictionary = [:]

Expand Down Expand Up @@ -141,7 +145,7 @@ class PluginRTCVideoCaptureController : NSObject {
self.capturer.startCapture(
with: device!,
format: deviceFormat!,
fps: deviceFrameRate
fps: deviceFrameRate!
)

NSLog("PluginRTCVideoCaptureController#startCapture Capture started, device:%@, format:%@", device!, deviceFormat!);
Expand Down Expand Up @@ -216,7 +220,7 @@ class PluginRTCVideoCaptureController : NSObject {
return nil;
}

let frameRateRange: NSDictionary = getConstrainRangeValues(constraint: "frameRate"),
let frameRateRange: NSDictionary = getConstrainRangeValues(constraint: "frameRate", defaultValue: self.DEFAULT_FPS),
minFrameRate = frameRateRange.object(forKey: "min") as! Float64,
maxFrameRate = frameRateRange.object(forKey: "max") as! Float64

Expand All @@ -234,11 +238,11 @@ class PluginRTCVideoCaptureController : NSObject {

deviceFrameRate = Int(maxFrameRate > 0 ? maxFrameRate : minFrameRate)

NSLog("PluginRTCVideoCaptureController#findDevice deviceFrameRate:%i", deviceFrameRate);
NSLog("PluginRTCVideoCaptureController#findDevice deviceFrameRate:%i", deviceFrameRate!);

// Apply default deviceFrameRate
} else {
deviceFrameRate = 30;
deviceFrameRate = DEFAULT_FPS;
}

return device;
Expand All @@ -257,20 +261,22 @@ class PluginRTCVideoCaptureController : NSObject {
return captureDevices[0] as? AVCaptureDevice
}



fileprivate func findFormatForDevice(device: AVCaptureDevice) -> AVCaptureDevice.Format? {

var selectedFormat: AVCaptureDevice.Format? = nil
let formats: NSArray = RTCCameraVideoCapturer.supportedFormats(for: device) as NSArray

let widthRange: NSDictionary = getConstrainRangeValues(constraint: "width"),
let widthRange: NSDictionary = getConstrainRangeValues(constraint: "width", defaultValue: self.DEFAULT_WIDTH),
minWidth = widthRange.object(forKey: "min") as! Int32,
maxWidth = widthRange.object(forKey: "max") as! Int32

let heightRange: NSDictionary = getConstrainRangeValues(constraint: "height"),
let heightRange: NSDictionary = getConstrainRangeValues(constraint: "height", defaultValue: self.DEFAULT_HEIGHT),
minHeight = heightRange.object(forKey: "min") as! Int32,
maxHeight = heightRange.object(forKey: "max") as! Int32

let frameRateRange: NSDictionary = getConstrainRangeValues(constraint: "frameRate"),
let frameRateRange: NSDictionary = getConstrainRangeValues(constraint: "frameRate", defaultValue: self.DEFAULT_FPS),
minFrameRate = frameRateRange.object(forKey: "min") as! Float64,
maxFrameRate = frameRateRange.object(forKey: "max") as! Float64

Expand Down Expand Up @@ -431,11 +437,11 @@ class PluginRTCVideoCaptureController : NSObject {
return isRequired;
}

fileprivate func getConstrainRangeValues(constraint: String) -> NSDictionary {
fileprivate func getConstrainRangeValues(constraint: String, defaultValue: Int = 0) -> NSDictionary {
let constraints = self.constraints;
let finalValue: NSMutableDictionary = [:];
finalValue.setValue(0, forKey: "min")
finalValue.setValue(0, forKey: "max")
finalValue.setValue(defaultValue, forKey: "min")
finalValue.setValue(defaultValue, forKey: "max")

let value = constraints.object(forKey: constraint);

Expand Down

0 comments on commit 0a90699

Please sign in to comment.