Skip to content

Commit a7f6f47

Browse files
committed
Add zoom methods
1 parent b0c10a8 commit a7f6f47

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

WebRTCiOSSDK/api/AntMediaClient.swift

+48
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,54 @@ open class AntMediaClient: NSObject, AntMediaClientProtocol {
524524
open func switchCamera() {
525525
self.webRTCClientMap[(self.publisherStreamId ?? (self.p2pStreamId)) ?? ""]?.switchCamera()
526526
}
527+
528+
/**
529+
Instant zoom
530+
1.0 means no zoom, 2.0 means 2x zoom, and so on.
531+
The method ensures the zoom does not exceed the camera’s limits.
532+
*/
533+
open func setZoomLevel(zoomFactor: CGFloat) {
534+
guard let streamId = publisherStreamId, let camera = webRTCClientMap[streamId]?.captureDevice else { return }
535+
536+
do {
537+
try camera.lockForConfiguration()
538+
camera.videoZoomFactor = max(1.0, min(zoomFactor, camera.activeFormat.videoMaxZoomFactor)) // Keep within limits
539+
camera.unlockForConfiguration()
540+
} catch {
541+
print("Failed to set zoom level: \(error)")
542+
}
543+
}
544+
545+
/**
546+
Smooth zoom
547+
The rate controls how fast the zoom happens.
548+
Lower values (e.g., 1.0) mean slow zoom; higher values (e.g., 5.0) mean faster zoom.
549+
*/
550+
open func smoothZoom(to zoomFactor: CGFloat, rate: Float) {
551+
guard let streamId = publisherStreamId, let camera = webRTCClientMap[streamId]?.captureDevice else { return }
552+
553+
do {
554+
try camera.lockForConfiguration()
555+
camera.ramp(toVideoZoomFactor: max(1.0, min(zoomFactor, camera.activeFormat.videoMaxZoomFactor)), withRate: rate)
556+
camera.unlockForConfiguration()
557+
} catch {
558+
print("Failed to ramp zoom: \(error)")
559+
}
560+
}
561+
/**
562+
If a zoom ramp is in progress, you can cancel it immediately:
563+
*/
564+
open func stopZoomRamp() {
565+
guard let streamId = publisherStreamId, let camera = webRTCClientMap[streamId]?.captureDevice else { return }
566+
567+
do {
568+
try camera.lockForConfiguration()
569+
camera.cancelVideoZoomRamp()
570+
camera.unlockForConfiguration()
571+
} catch {
572+
print("Failed to cancel zoom ramp: \(error)")
573+
}
574+
}
527575

528576
/*
529577
Send data through WebRTC Data channel.

WebRTCiOSSDK/api/AntMediaClientProtocol.swift

+19
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,25 @@ public protocol AntMediaClientProtocol {
162162
*/
163163
func switchCamera()
164164

165+
/**
166+
Instant zoom
167+
1.0 means no zoom, 2.0 means 2x zoom, and so on.
168+
The method ensures the zoom does not exceed the camera’s limits.
169+
*/
170+
func setZoomLevel(zoomFactor: CGFloat)
171+
172+
/**
173+
Smooth zoom
174+
The rate controls how fast the zoom happens.
175+
Lower values (e.g., 1.0) mean slow zoom; higher values (e.g., 5.0) mean faster zoom.
176+
*/
177+
func smoothZoom(to zoomFactor: CGFloat, rate: Float)
178+
179+
/**
180+
If a zoom ramp is in progress, you can cancel it immediately:
181+
*/
182+
func stopZoomRamp()
183+
165184
/**
166185
Sends data via WebRTC's Data Channel.
167186
- Parameters:

WebRTCiOSSDK/api/webrtc/WebRTCClient.swift

+14-5
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ class WebRTCClient: NSObject {
6666

6767
private var degradationPreference: RTCDegradationPreference = .maintainResolution
6868

69+
// this is not an ideal method to get current capture device, we need more legit solution
70+
var captureDevice: AVCaptureDevice? {
71+
if videoEnabled {
72+
return (RTCCameraVideoCapturer.captureDevices().first { $0.position == self.cameraPosition })
73+
}
74+
else {
75+
return nil;
76+
}
77+
}
78+
6979
public init(remoteVideoView: RTCVideoRenderer?, localVideoView: RTCVideoRenderer?, delegate: WebRTCClientDelegate, externalAudio: Bool) {
7080
RTCInitializeSSL()
7181

@@ -388,11 +398,10 @@ class WebRTCClient: NSObject {
388398
}
389399

390400
private func startCapture() -> Bool {
401+
391402

392-
let camera = (RTCCameraVideoCapturer.captureDevices().first { $0.position == self.cameraPosition })
393-
394-
if camera != nil {
395-
let supportedFormats = RTCCameraVideoCapturer.supportedFormats(for: camera!)
403+
if captureDevice != nil {
404+
let supportedFormats = RTCCameraVideoCapturer.supportedFormats(for: captureDevice!)
396405

397406
var currentDiff = INT_MAX
398407

@@ -422,7 +431,7 @@ class WebRTCClient: NSObject {
422431

423432
let cameraVideoCapturer = self.videoCapturer as? RTCCameraVideoCapturer
424433

425-
cameraVideoCapturer?.startCapture(with: camera!,
434+
cameraVideoCapturer?.startCapture(with: captureDevice!,
426435
format: selectedFormat!,
427436
fps: Int(fps))
428437

0 commit comments

Comments
 (0)