Skip to content

Commit

Permalink
Merge pull request #178 from shiguredo/feature/fix-camera-video-captu…
Browse files Browse the repository at this point in the history
…rer-format

AVCaptureDevice.Format の選択時にフレームレートを考慮するように修正する
  • Loading branch information
miosakuma authored Oct 20, 2023
2 parents 34c93e9 + 5e47712 commit 876ec8e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

## develop

- [UPDATE] CameraVideoCapturer のログを出力する
- @enm10k
- [FIX] AVCaptureDevice.Format の選択時にフレームレートを考慮するように修正する
- フレームレートに 60 を設定しても、 AVFrameRateRange が 1-30 の AVCaptureDevice.Format が選択されてしまうケースがあった
- 修正前は、カメラから同じ解像度の AVCaptureDevice.Format が複数取得された場合、最初に解像度が一致した AVCaptureDevice.Format を選択しており、フレームレートが考慮されていないという問題があった
- @enm10k

## 2023.3.0

- [CHANGE] `@available(*, unavailable)` は廃止になるため削除する
Expand Down
38 changes: 26 additions & 12 deletions Sora/CameraVideoCapturer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,32 @@ public final class CameraVideoCapturer {
}

/// 指定された設定に最も近い AVCaptureDevice.Format? を返します。
public static func format(width: Int32, height: Int32, for device: AVCaptureDevice) -> AVCaptureDevice.Format? {
let formats = RTCCameraVideoCapturer.supportedFormats(for: device)
var currentFormat: AVCaptureDevice.Format?
var currentDiff = INT_MAX
for format in formats {
public static func format(width: Int32, height: Int32, for device: AVCaptureDevice, frameRate: Int? = nil) -> AVCaptureDevice.Format? {
func calcDiff(_ targetWidth: Int32, _ targetHeight: Int32, _ format: AVCaptureDevice.Format) -> Int32 {
let dimension = CMVideoFormatDescriptionGetDimensions(format.formatDescription)
let diff = abs(width - dimension.width) + abs(height - dimension.height)
if diff < currentDiff {
currentFormat = format
currentDiff = diff
}
return abs(targetWidth - dimension.width) + abs(targetHeight - dimension.height)
}

let supportedFormats = RTCCameraVideoCapturer.supportedFormats(for: device)

// 指定された解像度に近いフォーマットを絞り込む
guard let diff = supportedFormats.map({ calcDiff(width, height, $0) }).min() else {
return nil
}
let formats = supportedFormats.filter { calcDiff(width, height, $0) == diff }
guard !formats.isEmpty else {
return nil
}

// この関数の引数に frameRate が指定された場合、フレームレートも考慮する
guard let frameRate else {
return formats.first
}
return currentFormat
return formats.filter {
$0.videoSupportedFrameRateRanges.contains(where: {
Int($0.minFrameRate) <= frameRate && frameRate <= Int($0.maxFrameRate)
})
}.first ?? formats.first
}

/// 指定された FPS 値をサポートしているレンジが存在すれば、その値を返します。
Expand Down Expand Up @@ -98,7 +111,8 @@ public final class CameraVideoCapturer {
let dimension = CMVideoFormatDescriptionGetDimensions(format.formatDescription)
guard let format = CameraVideoCapturer.format(width: dimension.width,
height: dimension.height,
for: flip.device)
for: flip.device,
frameRate: capturer.frameRate!)
else {
completionHandler(SoraError.cameraError(reason: "CameraVideoCapturer.format failed: suitable format is not found"))
return
Expand Down
3 changes: 2 additions & 1 deletion Sora/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ public final class Logger {
.nativePeerChannel,
.mediaChannel,
.mediaStream,
.dataChannel:
.dataChannel,
.cameraVideoCapturer:
out = true
default:
break
Expand Down
3 changes: 2 additions & 1 deletion Sora/PeerChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,8 @@ class PeerChannel: NSObject, RTCPeerConnectionDelegate {
// デバイスに対応したフォーマットとフレームレートを取得する
guard let format = CameraVideoCapturer.format(width: configuration.cameraSettings.resolution.width,
height: configuration.cameraSettings.resolution.height,
for: capturer.device)
for: capturer.device,
frameRate: configuration.cameraSettings.frameRate)
else {
Logger.error(type: .peerChannel, message: "CameraVideoCapturer.suitableFormat failed: suitable format rate is not found")
return
Expand Down

0 comments on commit 876ec8e

Please sign in to comment.