avfvideosrc: dispatch AVFoundation calls synchronously in the main queue

This commit is contained in:
Matthieu Bouron 2013-10-30 14:51:50 +00:00 committed by Andoni Morales Alastruey
parent 0d74dc802a
commit 19844fab47

View file

@ -133,10 +133,6 @@ G_DEFINE_TYPE (GstAVFVideoSrc, gst_avf_video_src, GST_TYPE_PUSH_SRC);
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection; fromConnection:(AVCaptureConnection *)connection;
- (void)waitForMainQueueToDrain;
- (void)waitForWorkerQueueToDrain;
- (void)waitForQueueToDrain:(dispatch_queue_t)dispatchQueue;
@end @end
@implementation GstAVFVideoSrcImpl @implementation GstAVFVideoSrcImpl
@ -181,7 +177,7 @@ didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
{ {
BOOL success = NO, *successPtr = &success; BOOL success = NO, *successPtr = &success;
dispatch_async (mainQueue, ^{ dispatch_sync (mainQueue, ^{
NSString *mediaType = AVMediaTypeVideo; NSString *mediaType = AVMediaTypeVideo;
NSError *err; NSError *err;
@ -231,14 +227,13 @@ didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
*successPtr = YES; *successPtr = YES;
}); });
[self waitForMainQueueToDrain];
return success; return success;
} }
- (void)closeDevice - (void)closeDevice
{ {
dispatch_async (mainQueue, ^{ dispatch_sync (mainQueue, ^{
g_assert (![session isRunning]); g_assert (![session isRunning]);
[session removeInput:input]; [session removeInput:input];
@ -256,7 +251,6 @@ didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
[device release]; [device release];
device = nil; device = nil;
}); });
[self waitForMainQueueToDrain];
} }
#define GST_AVF_CAPS_NEW(format, w, h) \ #define GST_AVF_CAPS_NEW(format, w, h) \
@ -328,7 +322,7 @@ didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
height = info.height; height = info.height;
format = info.finfo->format; format = info.finfo->format;
dispatch_async (mainQueue, ^{ dispatch_sync (mainQueue, ^{
int newformat; int newformat;
g_assert (![session isRunning]); g_assert (![session isRunning]);
@ -380,12 +374,10 @@ didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
width, height, width, height,
GST_FOURCC_ARGS (gst_video_format_to_fourcc (format))); GST_FOURCC_ARGS (gst_video_format_to_fourcc (format)));
output.videoSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:newformat] forKey:(NSString*)kCVPixelBufferPixelFormatTypeKey]; output.videoSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:newformat] forKey:(NSString*)kCVPixelBufferPixelFormatTypeKey];
[session startRunning]; [session startRunning];
}); });
[self waitForMainQueueToDrain];
return YES; return YES;
} }
@ -408,9 +400,8 @@ didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
- (BOOL)stop - (BOOL)stop
{ {
dispatch_async (mainQueue, ^{ [session stopRunning]; }); dispatch_sync (mainQueue, ^{ [session stopRunning]; });
[self waitForMainQueueToDrain]; dispatch_sync (workerQueue, ^{});
[self waitForWorkerQueueToDrain];
[bufQueueLock release]; [bufQueueLock release];
bufQueueLock = nil; bufQueueLock = nil;
@ -590,22 +581,6 @@ didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
} }
} }
- (void)waitForMainQueueToDrain
{
[self waitForQueueToDrain:mainQueue];
}
- (void)waitForWorkerQueueToDrain
{
[self waitForQueueToDrain:workerQueue];
}
- (void)waitForQueueToDrain:(dispatch_queue_t)dispatchQueue
{
if (dispatchQueue != dispatch_get_current_queue())
dispatch_sync (dispatchQueue, ^{});
}
@end @end
/* /*