From c36ce49706a557932988e001a464e81ec239d9b7 Mon Sep 17 00:00:00 2001 From: Palana Date: Mon, 5 May 2014 20:19:33 +0200 Subject: [PATCH] Minor cleanup in mac-avcapture --- plugins/mac-avcapture/av-capture.m | 46 +++++++++++++++++------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/plugins/mac-avcapture/av-capture.m b/plugins/mac-avcapture/av-capture.m index 9ac9ec9c6..d78a3c06b 100644 --- a/plugins/mac-avcapture/av-capture.m +++ b/plugins/mac-avcapture/av-capture.m @@ -42,6 +42,23 @@ struct av_capture { struct source_frame frame; }; +static inline void update_frame_size(struct source_frame *frame, + uint32_t width, uint32_t height) +{ + if (width != frame->width) { + blog(LOG_DEBUG, "Changed width from %d to %d", + frame->width, width); + frame->width = width; + frame->linesize[0] = width * 2; + } + + if (height != frame->height) { + blog(LOG_DEBUG, "Changed height from %d to %d", + frame->height, height); + frame->height = height; + } +} + @implementation OBSAVCaptureDelegate - (void)captureOutput:(AVCaptureOutput *)out didDropSampleBuffer:(CMSampleBufferRef)sampleBuffer @@ -65,37 +82,26 @@ struct av_capture { struct source_frame *frame = &capture->frame; + CVImageBufferRef img = CMSampleBufferGetImageBuffer(sampleBuffer); + + update_frame_size(frame, CVPixelBufferGetWidth(img), + CVPixelBufferGetHeight(img)); + CMSampleTimingInfo info; CMSampleBufferGetSampleTimingInfo(sampleBuffer, 0, &info); - CVImageBufferRef img = CMSampleBufferGetImageBuffer(sampleBuffer); - CVPixelBufferLockBaseAddress(img, 0); - uint32_t h = CVPixelBufferGetHeight(img); - if (h != frame->height) { - blog(LOG_DEBUG, "Changed height from %d to %d", - frame->height, h); - frame->height = h; - } - uint32_t w = CVPixelBufferGetWidth(img); - if (w != frame->width) { - blog(LOG_DEBUG, "Changed width from %d to %d", - frame->width, w); - frame->width = w; - frame->linesize[0] = w*2; - } - - uint8_t *addr = CVPixelBufferGetBaseAddress(img); - AVCaptureInputPort *port = capture->device_input.ports[0]; CMTime host_pts = CMSyncConvertTime(info.presentationTimeStamp, port.clock, CMClockGetHostTimeClock()); - frame->data[0] = addr; + CVPixelBufferLockBaseAddress(img, kCVPixelBufferLock_ReadOnly); + frame->timestamp = host_pts.value; + frame->data[0] = CVPixelBufferGetBaseAddress(img); obs_source_output_video(capture->source, frame); - CVPixelBufferUnlockBaseAddress(img, 0); + CVPixelBufferUnlockBaseAddress(img, kCVPixelBufferLock_ReadOnly); } @end