10000 Fix switching camera · CodeStrings/flutter-webrtc@2111a3f · GitHub
[go: up one dir, main page]

Skip to content

Commit 2111a3f

Browse files
committed
Fix switching camera
1 parent ac916f9 commit 2111a3f

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

ios/Classes/FlutterRTCMediaStream.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
result:(FlutterResult)result;
88

99
-(void)getSources:(FlutterResult)result;
10+
11+
-(void)mediaStreamTrackSwitchCamera:(RTCMediaStreamTrack *)track;
1012
@end
1113

1214

ios/Classes/FlutterRTCMediaStream.m

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,10 @@ - (void)getUserVideo:(NSDictionary *)constraints
233233
if (facingMode && [facingMode isKindOfClass:[NSString class]]) {
234234
AVCaptureDevicePosition position;
235235
if ([facingMode isEqualToString:@"environment"]) {
236+
self._usingFrontCamera = NO;
236237
position = AVCaptureDevicePositionBack;
237238
} else if ([facingMode isEqualToString:@"user"]) {
239+
self._usingFrontCamera = YES;
238240
position = AVCaptureDevicePositionFront;
239241
} else {
240242
// If the specified facingMode value is not supported, fall back to
@@ -433,9 +435,26 @@ -(void)mediaStreamTrackSetEnabled:(RTCMediaStreamTrack *)track : (BOOL)enabled
433435

434436
-(void)mediaStreamTrackSwitchCamera:(RTCMediaStreamTrack *)track
435437
{
436-
if (track) {
437-
//TODO(rostopira): I will handle it, if will work at least
438+
if (!self.videoCapturer) {
439+
NSLog(@"Video capturer is null. Can't switch camera");
440+
return;
441+
}
442+
self._usingFrontCamera = !self._usingFrontCamera;
443+
AVCaptureDevicePosition position = self._usingFrontCamera ? AVCaptureDevicePositionFront : AVCaptureDevicePositionBack;
444+
AVCaptureDevice *videoDevice;
445+
if (AVCaptureDevicePositionUnspecified != position) {
446+
for (AVCaptureDevice *aVideoDevice in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
447+
if (aVideoDevice.position == position) {
448+
videoDevice = aVideoDevice;
449+
break;
450+
}
451+
}
452+
}
453+
if (!videoDevice) {
454+
videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
438455
}
456+
AVCaptureDeviceFormat *selectedFormat = [RTCCameraVideoCapturer supportedFormatsForDevice:videoDevice].firstObject;
457+
[self.videoCapturer startCaptureWithDevice:videoDevice format:selectedFormat fps:30];
439458
}
440459

441460
-(void)mediaStreamTrackStop:(RTCMediaStreamTrack *)track

ios/Classes/FlutterWebRTCPlugin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
@property (nonatomic, retain) UIViewController *viewController;/*for broadcast or ReplayKit */
2121
@property (nonatomic, strong) NSObject<FlutterBinaryMessenger>* messenger;
2222
@property (nonatomic, strong) RTCCameraVideoCapturer *videoCapturer;
23+
@property (nonatomic) BOOL _usingFrontCamera;
2324

2425
- (RTCMediaStream*)streamForId:(NSString*)streamId;
2526

ios/Classes/FlutterWebRTCPlugin.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult) result
340340
RTCMediaStreamTrack *track = self.localTracks[trackId];
341341
if (track != nil && [track isKindOfClass:[RTCVideoTrack class]]) {
342342
RTCVideoTrack *videoTrack = (RTCVideoTrack *)track;
343-
//TODO(rostopira)
343+
[self mediaStreamTrackSwitchCamera:videoTrack];
344344
} else {
345345
if (track == nil) {
346346
NSLog(@"Track is nil");

0 commit comments

Comments
 (0)
0