yin*_*huo 5 video-capture video-streaming ios webrtc
我正在尝试在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;
}
Run Code Online (Sandbox Code Playgroud)
我将强制性约束设置如下,但是它不起作用:
@{@"minFrameRate":@"20",@"maxFrameRate":@"30",@"maxWidth":@"320",@"minWidth":@"240",@"maxHeight":@"320",@"minHeight":@"240"};
Run Code Online (Sandbox Code Playgroud)
有人可以帮我吗?
最新的SDK构建不再提供工厂方法来构建具有约束的捕获器。解决方案应该基于,AVCaptureSession而WebRTC将关注CPU和带宽利用率。
为此,您需要保留对RTCVideoSource传递给捕获器的引用。它具有方法:
- (void)adaptOutputFormatToWidth:(int)width height:(int)height fps:(int)fps;
Run Code Online (Sandbox Code Playgroud)
调用此函数将导致帧缩小到要求的分辨率。同样,将裁切帧以匹配请求的宽高比,并丢弃帧以匹配请求的fps。所请求的宽高比与方向无关,并且将进行调整以保持输入方向,因此,例如请求1280x720或720x1280都无关紧要。
var localVideoSource: RTCVideoSource?
Run Code Online (Sandbox Code Playgroud)
您可以通过以下方式创建视频轨道:
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
}
Run Code Online (Sandbox Code Playgroud)
而且,当您需要更改分辨率时,可以告诉此视频源进行“缩放”。
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
}
Run Code Online (Sandbox Code Playgroud)
相机仍将捕获分辨率format为的帧startCapture。而且,如果您关心资源利用,那么还可以在之前使用下一个方法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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2111 次 |
| 最近记录: |