Commit 4126a32c authored by Felix Paul Kühne's avatar Felix Paul Kühne

avcapture: drop legacy NSAutoreleasePool pattern

parent 7b2b08ec
......@@ -70,7 +70,6 @@ vlc_module_end ()
demux_t *p_avcapture;
CVImageBufferRef currentImageBuffer;
CMVideoDimensions videoDimensions;
mtime_t currentPts;
mtime_t previousPts;
......@@ -167,26 +166,26 @@ vlc_module_end ()
return currentPts;
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CVImageBufferRef imageBufferToRelease;
CMTime presentationtimestamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
CVImageBufferRef videoFrame = CMSampleBufferGetImageBuffer(sampleBuffer);
CVBufferRetain(videoFrame);
[self getVideoDimensions:sampleBuffer];
@synchronized (self)
{
imageBufferToRelease = currentImageBuffer;
currentImageBuffer = videoFrame;
currentPts = (mtime_t)presentationtimestamp.value;
timeScale = (long)presentationtimestamp.timescale;
@autoreleasepool {
CVImageBufferRef imageBufferToRelease;
CMTime presentationtimestamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
CVImageBufferRef videoFrame = CMSampleBufferGetImageBuffer(sampleBuffer);
CVBufferRetain(videoFrame);
[self getVideoDimensions:sampleBuffer];
@synchronized (self) {
imageBufferToRelease = currentImageBuffer;
currentImageBuffer = videoFrame;
currentPts = (mtime_t)presentationtimestamp.value;
timeScale = (long)presentationtimestamp.timescale;
}
CVBufferRelease(imageBufferToRelease);
}
CVBufferRelease(imageBufferToRelease);
[pool release];
}
- (mtime_t)copyCurrentFrameToBuffer:(void *)buffer
......@@ -257,114 +256,113 @@ static int Open(vlc_object_t *p_this)
char *psz_uid = NULL;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
/* Only when selected */
if ( *p_demux->psz_access == '\0' )
return VLC_EGENERIC;
if ( p_demux->psz_location && *p_demux->psz_location )
psz_uid = strdup(p_demux->psz_location);
@autoreleasepool {
if (p_demux->psz_location && *p_demux->psz_location)
psz_uid = strdup(p_demux->psz_location);
msg_Dbg(p_demux, "avcapture uid = %s", psz_uid);
avf_currdevice_uid = [[NSString alloc] initWithFormat:@"%s", psz_uid];
msg_Dbg(p_demux, "avcapture uid = %s", psz_uid);
avf_currdevice_uid = [[NSString alloc] initWithFormat:@"%s", psz_uid];
/* Set up p_demux */
p_demux->pf_demux = Demux;
p_demux->pf_control = Control;
p_demux->info.i_update = 0;
p_demux->info.i_title = 0;
p_demux->info.i_seekpoint = 0;
/* Set up p_demux */
p_demux->pf_demux = Demux;
p_demux->pf_control = Control;
p_demux->info.i_update = 0;
p_demux->info.i_title = 0;
p_demux->info.i_seekpoint = 0;
p_demux->p_sys = p_sys = calloc(1, sizeof(demux_sys_t));
if ( !p_sys )
return VLC_ENOMEM;
p_demux->p_sys = p_sys = calloc(1, sizeof(demux_sys_t));
if ( !p_sys )
return VLC_ENOMEM;
myVideoDevices = [[[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo] arrayByAddingObjectsFromArray:[AVCaptureDevice devicesWithMediaType:AVMediaTypeMuxed]] retain];
if ( [myVideoDevices count] == 0 )
{
dialog_FatalWait(p_demux, _("No video devices found"),
_("Your Mac does not seem to be equipped with a suitable video input device. "
"Please check your connectors and drivers."));
msg_Err(p_demux, "Can't find any suitable video device");
goto error;
}
deviceCount = [myVideoDevices count];
for ( ivideo = 0; ivideo < deviceCount; ivideo++ )
{
AVCaptureDevice *avf_device;
avf_device = [myVideoDevices objectAtIndex:ivideo];
msg_Dbg(p_demux, "avcapture %i/%i %s %s", ivideo, deviceCount, [[avf_device modelID] UTF8String], [[avf_device uniqueID] UTF8String]);
if ([[[avf_device uniqueID]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualToString:avf_currdevice_uid]) {
break;
myVideoDevices = [[[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]
arrayByAddingObjectsFromArray:[AVCaptureDevice devicesWithMediaType:AVMediaTypeMuxed]] retain];
if ( [myVideoDevices count] == 0 )
{
dialog_FatalWait(p_demux, _("No video devices found"),
_("Your Mac does not seem to be equipped with a suitable video input device. "
"Please check your connectors and drivers."));
msg_Err(p_demux, "Can't find any suitable video device");
goto error;
}
}
if ( ivideo < [myVideoDevices count] )
{
p_sys->device = [myVideoDevices objectAtIndex:ivideo];
}
else
{
msg_Dbg(p_demux, "Cannot find designated device as %s, falling back to default.", [avf_currdevice_uid UTF8String]);
p_sys->device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}
if ( !p_sys->device )
{
dialog_FatalWait(p_demux, _("No video devices found"),
_("Your Mac does not seem to be equipped with a suitable input device. "
"Please check your connectors and drivers."));
msg_Err(p_demux, "Can't find any suitable video device");
goto error;
}
if ( [p_sys->device isInUseByAnotherApplication] == YES )
{
msg_Err(p_demux, "default capture device is exclusively in use by another application");
goto error;
}
input = [AVCaptureDeviceInput deviceInputWithDevice:p_sys->device error:&o_returnedError];
if ( !input )
{
msg_Err(p_demux, "can't create a valid capture input facility (%ld)", [o_returnedError code]);
goto error;
}
int chroma = VLC_CODEC_RGB32;
memset(&p_sys->fmt, 0, sizeof(es_format_t));
es_format_Init(&p_sys->fmt, VIDEO_ES, chroma);
p_sys->session = [[AVCaptureSession alloc] init];
[p_sys->session addInput:input];
p_sys->output = [[VLCAVDecompressedVideoOutput alloc] initWithDemux:p_demux];
[p_sys->session addOutput:p_sys->output];
dispatch_queue_t queue = dispatch_queue_create("avCaptureQueue", NULL);
[p_sys->output setSampleBufferDelegate:(id)p_sys->output queue:queue];
dispatch_release(queue);
p_sys->output.videoSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
[p_sys->session startRunning];
deviceCount = [myVideoDevices count];
for ( ivideo = 0; ivideo < deviceCount; ivideo++ )
{
AVCaptureDevice *avf_device;
avf_device = [myVideoDevices objectAtIndex:ivideo];
msg_Dbg(p_demux, "avcapture %i/%i %s %s", ivideo, deviceCount, [[avf_device modelID] UTF8String], [[avf_device uniqueID] UTF8String]);
if ([[[avf_device uniqueID]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualToString:avf_currdevice_uid]) {
break;
}
}
[input release];
if ( ivideo < [myVideoDevices count] )
{
p_sys->device = [myVideoDevices objectAtIndex:ivideo];
}
else
{
msg_Dbg(p_demux, "Cannot find designated device as %s, falling back to default.", [avf_currdevice_uid UTF8String]);
p_sys->device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}
if ( !p_sys->device )
{
dialog_FatalWait(p_demux, _("No video devices found"),
_("Your Mac does not seem to be equipped with a suitable input device. "
"Please check your connectors and drivers."));
msg_Err(p_demux, "Can't find any suitable video device");
goto error;
}
msg_Dbg(p_demux, "AVCapture: Video device ready!");
if ( [p_sys->device isInUseByAnotherApplication] == YES )
{
msg_Err(p_demux, "default capture device is exclusively in use by another application");
goto error;
}
[pool release];
return VLC_SUCCESS;
error:
msg_Err(p_demux, "Error");
[input release];
input = [AVCaptureDeviceInput deviceInputWithDevice:p_sys->device error:&o_returnedError];
free(p_sys);
if ( !input )
{
msg_Err(p_demux, "can't create a valid capture input facility (%ld)", [o_returnedError code]);
goto error;
}
[pool release];
return VLC_EGENERIC;
int chroma = VLC_CODEC_RGB32;
memset(&p_sys->fmt, 0, sizeof(es_format_t));
es_format_Init(&p_sys->fmt, VIDEO_ES, chroma);
p_sys->session = [[AVCaptureSession alloc] init];
[p_sys->session addInput:input];
p_sys->output = [[VLCAVDecompressedVideoOutput alloc] initWithDemux:p_demux];
[p_sys->session addOutput:p_sys->output];
dispatch_queue_t queue = dispatch_queue_create("avCaptureQueue", NULL);
[p_sys->output setSampleBufferDelegate:(id)p_sys->output queue:queue];
dispatch_release(queue);
p_sys->output.videoSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
[p_sys->session startRunning];
[input release];
msg_Dbg(p_demux, "AVCapture: Video device ready!");
return VLC_SUCCESS;
error:
msg_Err(p_demux, "Error");
[input release];
free(p_sys);
return VLC_EGENERIC;
}
}
/*****************************************************************************
......@@ -372,21 +370,20 @@ error:
*****************************************************************************/
static void Close(vlc_object_t *p_this)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
demux_t *p_demux = (demux_t*)p_this;
demux_sys_t *p_sys = p_demux->p_sys;
msg_Dbg(p_demux,"Close AVCapture");
// Perform this on main thread, as the framework itself will sometimes try to synchronously
// work on main thread. And this will create a dead lock.
[p_sys->session performSelectorOnMainThread:@selector(stopRunning) withObject:nil waitUntilDone:NO];
[p_sys->output performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
[p_sys->session performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
free(p_sys);
@autoreleasepool {
msg_Dbg(p_demux,"Close AVCapture");
[pool release];
// Perform this on main thread, as the framework itself will sometimes try to synchronously
// work on main thread. And this will create a dead lock.
[p_sys->session performSelectorOnMainThread:@selector(stopRunning) withObject:nil waitUntilDone:NO];
[p_sys->output performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
[p_sys->session performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
free(p_sys);
}
}
/*****************************************************************************
......@@ -397,44 +394,42 @@ static int Demux(demux_t *p_demux)
demux_sys_t *p_sys = p_demux->p_sys;
block_t *p_block;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@synchronized ( p_sys->output )
{
p_block = block_Alloc([p_sys->output width] * [p_sys->output bytesPerRow]);
if ( !p_block )
@autoreleasepool {
@synchronized ( p_sys->output )
{
msg_Err(p_demux, "cannot get block");
return 0;
}
p_block = block_Alloc([p_sys->output width] * [p_sys->output bytesPerRow]);
p_block->i_pts = [p_sys->output copyCurrentFrameToBuffer: p_block->p_buffer];
if ( !p_block )
{
msg_Err(p_demux, "cannot get block");
return 0;
}
if ( !p_block->i_pts )
{
/* Nothing to display yet, just forget */
block_Release(p_block);
[pool release];
msleep(10000);
return 1;
}
else if ( !p_sys->b_es_setup )
{
p_sys->fmt.video.i_frame_rate_base = [p_sys->output timeScale];
msg_Dbg(p_demux, "using frame rate base: %i", p_sys->fmt.video.i_frame_rate_base);
p_sys->width = p_sys->fmt.video.i_width = [p_sys->output width];
p_sys->height = p_sys->fmt.video.i_height = [p_sys->output height];
p_sys->p_es_video = es_out_Add(p_demux->out, &p_sys->fmt);
msg_Dbg(p_demux, "added new video es %4.4s %dx%d", (char*)&p_sys->fmt.i_codec, p_sys->width, p_sys->height);
p_sys->b_es_setup = YES;
p_block->i_pts = [p_sys->output copyCurrentFrameToBuffer: p_block->p_buffer];
if ( !p_block->i_pts )
{
/* Nothing to display yet, just forget */
block_Release(p_block);
msleep(10000);
return 1;
}
else if ( !p_sys->b_es_setup )
{
p_sys->fmt.video.i_frame_rate_base = [p_sys->output timeScale];
msg_Dbg(p_demux, "using frame rate base: %i", p_sys->fmt.video.i_frame_rate_base);
p_sys->width = p_sys->fmt.video.i_width = [p_sys->output width];
p_sys->height = p_sys->fmt.video.i_height = [p_sys->output height];
p_sys->p_es_video = es_out_Add(p_demux->out, &p_sys->fmt);
msg_Dbg(p_demux, "added new video es %4.4s %dx%d", (char*)&p_sys->fmt.i_codec, p_sys->width, p_sys->height);
p_sys->b_es_setup = YES;
}
}
es_out_Control(p_demux->out, ES_OUT_SET_PCR, p_block->i_pts);
es_out_Send(p_demux->out, p_sys->p_es_video, p_block);
}
es_out_Control(p_demux->out, ES_OUT_SET_PCR, p_block->i_pts);
es_out_Send(p_demux->out, p_sys->p_es_video, p_block);
[pool release];
return 1;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment