iOS - 如何在webRTC中更改视频分辨率?

时间:2016-06-29 02:27:09

标签: ios video-streaming webrtc video-capture

我正在尝试更改webRTC中的本地视频分辨率。我使用以下方法创建本地视频跟踪器:

-(RTCVideoTrack *)createLocalVideoTrack {
   RTCVideoTrack *localVideoTrack = nil;
   RTCMediaConstraints *mediaConstraints = [[RTCMediaConstraints alloc] initWithMandatoryConstraints:nil                                                                        optionalConstraints:nil];
   RTCAVFoundationVideoSource *source =
   [self.factory avFoundationVideoSourceWithConstraints:mediaConstraints];
   localVideoTrack =
   [self.factory videoTrackWithSource:source
                           trackId:@"ARDAMSv0"];
   return localVideoTrack;
}

我将强制约束设置如下,但它不起作用:

@{@"minFrameRate":@"20",@"maxFrameRate":@"30",@"maxWidth":@"320",@"minWidth":@"240",@"maxHeight":@"320",@"minHeight":@"240"};

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:3)

最新的SDK构建不再提供工厂方法来构建具有约束的捕获器。解决方案应基于AVCaptureSession,而WebRTC将关注CPU和带宽利用率。

为此,您需要保留对传递给捕获器的RTCVideoSource的引用。它具有方法:

- (void)adaptOutputFormatToWidth:(int)width height:(int)height fps:(int)fps;
  

调用此功能将导致帧缩小到要求的分辨率。同样,将裁切帧以匹配请求的宽高比,并丢弃帧以匹配请求的fps。所请求的宽高比与方向无关,并且将进行调整以保持输入方向,因此例如请求1280x720或720x1280。

var localVideoSource: RTCVideoSource?

您可以通过以下方式创建视频轨道:

func createVideoTrack() -> RTCVideoTrack? {
    var source: RTCVideoSource
    if let localSource = self.localVideoSource {
        source = localSource
    } else {
        source = self.factory.videoSource()
        self.localVideoSource = source
    }
    let devices = RTCCameraVideoCapturer.captureDevices()
    if let camera = devices.first, 
        // here you can decide to use front or back camera

        let format = RTCCameraVideoCapturer.supportedFormats(for: camera).last, 
        // here you have a bunch of formats from tiny to up to 4k, find 1st that conforms your needs, i.e. if you usemax 1280x720, then no need to pick 4k

        let fps = format.videoSupportedFrameRateRanges.first?.maxFrameRate 
        // or take smth in between min..max, i.e. 24 fps and not 30, to reduce gpu/cpu use {

        let intFps = Int(fps)
        let capturer = RTCCameraVideoCapturer(delegate: source)
        capturer.startCapture(with: camera, format: format, fps: intFps)
        let videoTrack = self.factory.videoTrack(with: source, trackId: WebRTCClient.trackIdVideo)
        return videoTrack
    }
    retun nil
}

当需要更改分辨率时,可以告诉此视频源进行“缩放”。

func changeResolution(w: Int32, h: Int32) -> Bool {
    guard let videoSource = self.localVideoSource else {
        return false
    }
    // TODO: decide fps
    videoSource.adaptOutputFormat(toWidth: w, height: h, fps: 30)
    return true
}

相机仍将捕获formatstartCapture中提供的分辨率的帧。而且,如果您关心资源利用,那么还可以在adaptOutputFormat之前使用下一种方法。

// Stops the capture session asynchronously and notifies callback on completion.
- (void)stopCaptureWithCompletionHandler:(nullable void (^)(void))completionHandler;

// Starts the capture session asynchronously.
- (void)startCaptureWithDevice:(AVCaptureDevice *)device format:(AVCaptureDeviceFormat *)format fps:(NSInteger)fps;