Updated iOS video capturer to take device orientation into consideration.

BUG=4122
R=tkchin@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/48769004

Patch from Jonas Martinsson <jonas.d.martinsson@gmail.com>.

Cr-Commit-Position: refs/heads/master@{#8953}
This commit is contained in:
Jonas Martinsson 2015-04-08 11:12:46 -07:00 committed by Zeke Chin
parent 1064679bba
commit 036b420db6

View File

@ -31,6 +31,7 @@ using namespace webrtc::videocapturemodule;
webrtc::VideoCaptureCapability _capability;
AVCaptureSession* _captureSession;
int _captureId;
BOOL _orientationHasChanged;
AVCaptureConnection* _connection;
BOOL _captureChanging; // Guarded by _captureChangingCondition.
NSCondition* _captureChangingCondition;
@ -80,14 +81,16 @@ using namespace webrtc::videocapturemodule;
__LINE__);
}
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
NSNotificationCenter* notify = [NSNotificationCenter defaultCenter];
[notify addObserver:self
selector:@selector(onVideoError:)
name:AVCaptureSessionRuntimeErrorNotification
object:_captureSession];
[notify addObserver:self
selector:@selector(statusBarOrientationDidChange:)
name:@"StatusBarOrientationDidChange"
selector:@selector(deviceOrientationDidChange:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
@ -105,7 +108,8 @@ using namespace webrtc::videocapturemodule;
[[self currentOutput] setSampleBufferDelegate:nil queue:NULL];
}
- (void)statusBarOrientationDidChange:(NSNotification*)notification {
- (void)deviceOrientationDidChange:(NSNotification*)notification {
_orientationHasChanged = YES;
[self setRelativeVideoOrientation];
}
@ -171,6 +175,7 @@ using namespace webrtc::videocapturemodule;
[self directOutputToSelf];
_orientationHasChanged = NO;
_captureChanging = YES;
dispatch_async(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
@ -238,24 +243,33 @@ using namespace webrtc::videocapturemodule;
}
- (void)setRelativeVideoOrientation {
if (!_connection.supportsVideoOrientation)
if (!_connection.supportsVideoOrientation) {
return;
switch ([UIApplication sharedApplication].statusBarOrientation) {
case UIInterfaceOrientationPortrait:
#if defined(__IPHONE_8_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
case UIInterfaceOrientationUnknown:
#endif
_connection.videoOrientation = AVCaptureVideoOrientationPortrait;
break;
case UIInterfaceOrientationPortraitUpsideDown:
}
switch ([UIDevice currentDevice].orientation) {
case UIDeviceOrientationPortrait:
_connection.videoOrientation =
AVCaptureVideoOrientationPortraitUpsideDown;
case UIDeviceOrientationPortraitUpsideDown:
_connection.videoOrientation =
AVCaptureVideoOrientationPortrait;
break;
case UIInterfaceOrientationLandscapeLeft:
_connection.videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
case UIDeviceOrientationLandscapeLeft:
_connection.videoOrientation =
AVCaptureVideoOrientationLandscapeRight;
break;
case UIInterfaceOrientationLandscapeRight:
_connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
case UIDeviceOrientationLandscapeRight:
_connection.videoOrientation =
AVCaptureVideoOrientationLandscapeLeft;
break;
case UIDeviceOrientationFaceUp:
case UIDeviceOrientationFaceDown:
case UIDeviceOrientationUnknown:
if (!_orientationHasChanged) {
_connection.videoOrientation =
AVCaptureVideoOrientationPortrait;
}
break;
}
}
@ -273,6 +287,8 @@ using namespace webrtc::videocapturemodule;
}
- (BOOL)stopCapture {
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
_orientationHasChanged = NO;
[self waitForCaptureChangeToFinish];
[self directOutputToNil];