iOS -- 如何在 webRTC 中更改视频分辨率? [英] iOS -- How to change video resolution in webRTC?

查看:118
本文介绍了iOS -- 如何在 webRTC 中更改视频分辨率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

-(RTCVideoTrack *)createLocalVideoTrack {RTCVideoTrack *localVideoTrack = nil;RTCMediaConstraints *mediaConstraints = [[RTCMediaConstraints alloc] initWithMandatoryConstraints:nil optionalConstraints:nil];RTCAVFoundationVideoSource *source =[self.factory avFoundationVideoSourceWithConstraints:mediaConstraints];本地视频轨道 =[self.factory videoTrackWithSource:sourcetrackId:@"ARDAMSv0"];返回本地视频轨道;}

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

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

有人可以帮我吗?

解决方案

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

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

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

<块引用>

调用此函数将导致帧缩小到请求的分辨率.此外,帧将被裁剪以匹配请求的纵横比,帧将被丢弃以匹配请求的 fps.请求的纵横比与方向无关,将进行调整以保持输入方向,因此例如要求 1280x720 或 720x1280.

var localVideoSource: RTCVideoSource?

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

func createVideoTrack() ->RTCVideoTrack?{无功来源:RTCVideoSource如果让 localSource = self.localVideoSource {来源 = 本地来源} 别的 {源 = self.factory.videoSource()self.localVideoSource = 源}让设备 = RTCCameraVideoCapturer.captureDevices()如果让相机 = devices.first,//在这里你可以决定使用前置或后置摄像头让格式 = RTCCameraVideoCapturer.supportedFormats(for: camera).last,//这里你有一堆格式,从小到 4k,找到第一个符合你的需求,即如果你使用最大 1280x720,那么不需要选择 4k让 fps = format.videoSupportedFrameRateRanges.first?.maxFrameRate//或在 min..max 之间取 smth,即 24 fps 而不是 30,以减少 gpu/cpu 使用 {让 intFps = Int(fps)让捕获器 = RTCCameraVideoCapturer(委托:源)capturer.startCapture(with: camera, format: format, fps: intFps)让 videoTrack = self.factory.videoTrack(with: source, trackId: WebRTCClient.trackIdVideo)返回视频轨道}归零}

当您需要更改分辨率时,您可以告诉此视频源进行缩放".

func changeResolution(w: Int32, h: Int32) ->布尔{守卫让 videoSource = self.localVideoSource else {返回假}//TODO: 决定fpsvideoSource.adaptOutputFormat(toWidth: w, height: h, fps: 30)返回真}

相机仍会以format 中提供的分辨率捕获帧到startCapture.如果你关心资源利用率,那么你也可以在 adaptOutputFormat 之前使用 next 方法.

//异步停止捕获会话并在完成时通知回调.- (void)stopCaptureWithCompletionHandler:(nullable void (^)(void))completionHandler;//异步启动捕获会话.- (void)startCaptureWithDevice:(AVCaptureDevice *)device format:(AVCaptureDeviceFormat *)format fps:(NSInteger)fps;

I am trying to change local video resolution in webRTC. I used following method to create local video tracker:

-(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;
}

I set the mandatory constraint as follow, but it doesn't work:

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

Could anyone help me?

解决方案

Latest SDK builds don't provide factory method to build capturer with constraints any more. Solution should be based on AVCaptureSession instead and WebRTC will take care about CPU and bandwidth utilization.

For this you need to keep reference to your RTCVideoSource that was passed to capturer. It has method:

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

Calling this function will cause frames to be scaled down to the requested resolution. Also, frames will be cropped to match the requested aspect ratio, and frames will be dropped to match the requested fps. The requested aspect ratio is orientation agnostic and will be adjusted to maintain the input orientation, so it doesn't matter if e.g. 1280x720 or 720x1280 is requested.

var localVideoSource: RTCVideoSource?

You may create your video track this way:

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
}

And when you need to change resolution, you can tell this video source to do "scaling".

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
}

Camera will still capture frames with resolution providd in format to startCapture. And if you care about resource utilization, then you can also use next methods prior to 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;

这篇关于iOS -- 如何在 webRTC 中更改视频分辨率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆