iOS video_capture: start camera in the background.

Camera start is a blocking operation so never a good idea to do on a main
thread, but worse than that is that the guts of WebView appear to be
interacting with capture start in a bad way causing startup to pause for 10s
while a timeout expires.  This change eliminates that 10s delay.

R=noahric@google.com

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

git-svn-id: http://webrtc.googlecode.com/svn/trunk@5772 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
fischman@webrtc.org
2014-03-25 05:23:32 +00:00
parent 385a722646
commit b64d52c292
2 changed files with 29 additions and 7 deletions

View File

@@ -28,6 +28,8 @@
AVCaptureSession* _captureSession;
int _captureId;
AVCaptureConnection* _connection;
BOOL _captureStarting; // Guarded by _captureStartingCondition.
NSCondition* _captureStartingCondition;
}
@property webrtc::VideoCaptureRotation frameRotation;
@@ -37,7 +39,7 @@
// default init methods have been overridden to return nil.
- (id)initWithOwner:(webrtc::videocapturemodule::VideoCaptureIos*)owner
captureId:(int)captureId;
- (BOOL)setCaptureDeviceByUniqueId:(NSString*)uniequeId;
- (BOOL)setCaptureDeviceByUniqueId:(NSString*)uniqueId;
- (BOOL)startCaptureWithCapability:
(const webrtc::VideoCaptureCapability&)capability;
- (BOOL)stopCapture;

View File

@@ -24,7 +24,6 @@ using namespace webrtc::videocapturemodule;
@interface RTCVideoCaptureIosObjC (hidden)
- (int)changeCaptureInputWithName:(NSString*)captureDeviceName;
@end
@implementation RTCVideoCaptureIosObjC
@@ -36,8 +35,10 @@ using namespace webrtc::videocapturemodule;
_owner = owner;
_captureId = captureId;
_captureSession = [[AVCaptureSession alloc] init];
_captureStarting = NO;
_captureStartingCondition = [[NSCondition alloc] init];
if (!_captureSession) {
if (!_captureSession || !_captureStartingCondition) {
return nil;
}
@@ -144,6 +145,17 @@ using namespace webrtc::videocapturemodule;
return NO;
}
AVCaptureVideoDataOutput* currentOutput =
(AVCaptureVideoDataOutput*)[currentOutputs objectAtIndex:0];
dispatch_async(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^(void) { [self startCaptureInBackgroundWithOutput:currentOutput]; });
return YES;
}
- (void)startCaptureInBackgroundWithOutput:
(AVCaptureVideoDataOutput*)currentOutput {
NSString* captureQuality =
[NSString stringWithString:AVCaptureSessionPresetLow];
if (_capability.width >= 1920 || _capability.height >= 1080) {
@@ -157,9 +169,6 @@ using namespace webrtc::videocapturemodule;
captureQuality = [NSString stringWithString:AVCaptureSessionPreset352x288];
}
AVCaptureVideoDataOutput* currentOutput =
(AVCaptureVideoDataOutput*)[currentOutputs objectAtIndex:0];
// begin configuration for the AVCaptureSession
[_captureSession beginConfiguration];
@@ -179,7 +188,10 @@ using namespace webrtc::videocapturemodule;
[_captureSession startRunning];
return YES;
[_captureStartingCondition lock];
_captureStarting = NO;
[_captureStartingCondition signal];
[_captureStartingCondition unlock];
}
- (void)setRelativeVideoOrientation {
@@ -215,6 +227,7 @@ using namespace webrtc::videocapturemodule;
}
- (BOOL)stopCapture {
[self waitForCaptureStartToFinish];
if (!_captureSession) {
return NO;
}
@@ -320,4 +333,11 @@ using namespace webrtc::videocapturemodule;
CVPixelBufferUnlockBaseAddress(videoFrame, kFlags);
}
- (void)waitForCaptureStartToFinish {
[_captureStartingCondition lock];
while (_captureStarting) {
[_captureStartingCondition wait];
}
[_captureStartingCondition unlock];
}
@end