Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] MediaStreamTrack ApplyConstraints Zoom #1

Draft
wants to merge 4 commits into
base: lldc-main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ public void onCameraSwitchError(String s) {
}
}

public void applyZoomFactor(float zoomFactor) {
if (!(videoCapturer instanceof CameraVideoCapturer)) {
return;
}

CameraVideoCapturer capturer = (CameraVideoCapturer) videoCapturer;
}

@Override
protected VideoCapturer createVideoCapturer() {
String deviceId = ReactBridgeUtil.getMapStrValue(this.constraints, "deviceId");
Expand Down
18 changes: 18 additions & 0 deletions android/src/main/java/com/oney/WebRTCModule/WebRTCModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,24 @@ public void mediaStreamTrackSetVolume(int pcId, String id, double volume) {
});
}

@ReactMethod
public void mediaStreamTrackApplyConstraints(int pcId, String id, ReadableMap constraints) {
ThreadUtils.runOnExecutor(() -> {
MediaStreamTrack track = getTrack(pcId, id);
if (track == null) {
Log.d(TAG, "mediaStreamTrackApplyConstraints() could not find track " + id);
return;
}

if (!(track instanceof VideoTrack)) {
Log.d(TAG, "mediaStreamTrackApplyConstraints() track is not a VideoTrack!");
return;
}

VideoTrack videoTrack = (VideoTrack)track;
});
}

/**
* This serializes the transceivers current direction and mid and returns them
* for update when an sdp negotiation/renegotiation happens
Expand Down
2 changes: 2 additions & 0 deletions ios/RCTWebRTC/VideoCaptureController.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@
- (void)startCapture;
- (void)stopCapture;
- (void)switchCamera;
- (void)applyZoomFactor:(CGFloat)zoomFactor;
- (NSDictionary *)getZoomCapabilities;

@end
39 changes: 37 additions & 2 deletions ios/RCTWebRTC/VideoCaptureController.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ - (void)startCapture {

return;
}

AVCaptureDeviceFormat *format = [self selectFormatForDevice:self.device
withTargetWidth:self.width
withTargetHeight:self.height];
Expand All @@ -84,7 +84,7 @@ - (void)startCapture {
}

self.selectedFormat = format;

RCTLog(@"[VideoCaptureController] Capture will start");

// Starting the capture happens on another thread. Wait for it.
Expand Down Expand Up @@ -135,6 +135,41 @@ - (void)switchCamera {
[self startCapture];
}

- (void)applyZoomFactor:(CGFloat)zoomFactor {
if (!self.device) {
RCTLogWarn(@"[VideoCaptureController] No capture devices found!");
return;
}

CGFloat minZoomFactor = self.device.minAvailableVideoZoomFactor;
CGFloat maxZoomFactor = self.device.maxAvailableVideoZoomFactor;
CGFloat clampedZoomFactor = MAX(minZoomFactor, MIN(zoomFactor, maxZoomFactor));

NSError *error = nil;
[self.device lockForConfiguration:&error];
if (error) {
RCTLog(@"[VideoCaptureController] Could not lock device for configuration: %@", error);
return;
}

[self.device setVideoZoomFactor:clampedZoomFactor];

[self.device unlockForConfiguration];
}

- (NSDictionary *)getZoomCapabilities {
if (!self.device) {
RCTLogWarn(@"[VideoCaptureController] No capture devices found!");
return @{};
}

return @{
@"min" : @(self.device.minAvailableVideoZoomFactor),
@"max" : @(self.device.maxAvailableVideoZoomFactor),
@"step" : @(0.1)
};
}

#pragma mark NSKeyValueObserving

- (void)observeValueForKeyPath:(NSString *)keyPath
Expand Down
34 changes: 31 additions & 3 deletions ios/RCTWebRTC/WebRTCModule+RTCMediaStream.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import <objc/runtime.h>
#import <React/RCTLog.h>

#import <WebRTC/RTCCameraVideoCapturer.h>
#import <WebRTC/RTCMediaConstraints.h>
Expand Down Expand Up @@ -56,7 +57,7 @@ - (NSArray *)createMediaStream:(NSArray<RTCMediaStreamTrack *> *)tracks {
NSString *mediaStreamId = [[NSUUID UUID] UUIDString];
RTCMediaStream *mediaStream = [self.peerConnectionFactory mediaStreamWithStreamId:mediaStreamId];
NSMutableArray<NSDictionary *> *trackInfos = [NSMutableArray array];

for (RTCMediaStreamTrack *track in tracks) {
if ([track.kind isEqualToString:@"audio"]) {
[mediaStream addAudioTrack:(RTCAudioTrack *)track];
Expand Down Expand Up @@ -182,7 +183,7 @@ - (RTCVideoTrack *)createScreenCaptureVideoTrack {
if (constraints[@"video"]) {
videoTrack = [self createVideoTrack:constraints];
}

if (audioTrack == nil && videoTrack == nil) {
// Fail with DOMException with name AbortError as per:
// https://www.w3.org/TR/mediacapture-streams/#dom-mediadevices-getusermedia
Expand All @@ -205,7 +206,7 @@ - (RTCVideoTrack *)createScreenCaptureVideoTrack {
} else if ([track.kind isEqualToString:@"video"]) {
[mediaStream addVideoTrack:(RTCVideoTrack *)track];
}

NSString *trackId = track.trackId;

self.localTracks[trackId] = track;
Expand Down Expand Up @@ -374,6 +375,33 @@ - (RTCVideoTrack *)createScreenCaptureVideoTrack {
}
}

RCT_EXPORT_METHOD(mediaStreamTrackApplyConstraints: (nonnull NSNumber *)pcId : (nonnull NSString *)trackID : (NSDictionary *)constraints) {
RTCMediaStreamTrack *track = [self trackForId:trackID pcId:pcId];
if (track && [track.kind isEqualToString:@"video"]) {
CGFloat zoomFactor = [constraints[@"zoom"] floatValue];
RTCVideoTrack *videoTrack = (RTCVideoTrack *)track;
[(VideoCaptureController *)videoTrack.captureController applyZoomFactor:zoomFactor];
}
}

RCT_EXPORT_METHOD(mediaStreamTrackGetCapabilities: (nonnull NSNumber *)pcId : (nonnull NSString *)trackID :
(RCTResponseSenderBlock)callback) {
RTCMediaStreamTrack *track = [self trackForId:trackID pcId:pcId];

if (track && [track.kind isEqualToString:@"video"]) {
RTCVideoTrack *videoTrack = (RTCVideoTrack*)track;
NSDictionary *zoomCapabilities = [(VideoCaptureController *)videoTrack.captureController getZoomCapabilities];

callback(@[@{
@"zoom" : zoomCapabilities
}]);
return;
}

RCTLogWarn(@"getCapabilities only supported for video track");
callback(@[]);
}

#pragma mark - Helpers

- (RTCMediaStreamTrack *)trackForId:(nonnull NSString *)trackId pcId:(nonnull NSNumber *)pcId {
Expand Down
21 changes: 17 additions & 4 deletions src/MediaStreamTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,29 @@ class MediaStreamTrack extends defineCustomEventTarget(...MEDIA_STREAM_TRACK_EVE
WebRTCModule.mediaStreamTrackSetVolume(this.remote ? this._peerConnectionId : -1, this.id, volume);
}

applyConstraints(): never {
throw new Error('Not implemented.');
applyConstraints(constraints: object) {
if (this.kind !== 'video') {
throw new Error('Only implemented for video tracks');
}

if (!('zoom' in constraints)) {
log.warn('Only zoom is supported for applyConstraints on video tracks');
}

this._constraints = Object.assign(this._constraints, constraints);
WebRTCModule.mediaStreamTrackApplyConstraints(this.remote ? this._peerConnectionId : -1, this.id, constraints);
}

clone(): never {
throw new Error('Not implemented.');
}

getCapabilities(): never {
throw new Error('Not implemented.');
getCapabilities(): Promise<object> {
if (this.kind !== 'video') {
throw new Error('Only implemented for video tracks');
}

return new Promise((resolve) => WebRTCModule.mediaStreamTrackGetCapabilities(this.remote ? this._peerConnectionId : -1, this.id, resolve));
}

getConstraints() {
Expand Down