From a8ae57480cb031b2effc48064da22f860bde2e4d Mon Sep 17 00:00:00 2001 From: Florian Zwoch Date: Wed, 25 Feb 2015 13:52:21 +0100 Subject: avfvideosrc: allow "capture-screen" mode to select which screen to capture via the "device-index" option https://bugzilla.gnome.org/show_bug.cgi?id=745161 --- sys/applemedia/avfvideosrc.m | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) (limited to 'sys/applemedia') diff --git a/sys/applemedia/avfvideosrc.m b/sys/applemedia/avfvideosrc.m index ec225498f..b0246171e 100644 --- a/sys/applemedia/avfvideosrc.m +++ b/sys/applemedia/avfvideosrc.m @@ -24,6 +24,9 @@ #include "avfvideosrc.h" #import +#if !HAVE_IOS +#import +#endif #include #include #include "coremediabuffer.h" @@ -75,9 +78,6 @@ G_DEFINE_TYPE (GstAVFVideoSrc, gst_avf_video_src, GST_TYPE_PUSH_SRC); gint deviceIndex; BOOL doStats; -#if !HAVE_IOS - CGDirectDisplayID displayId; -#endif AVCaptureSession *session; AVCaptureInput *input; @@ -126,6 +126,9 @@ G_DEFINE_TYPE (GstAVFVideoSrc, gst_avf_video_src, GST_TYPE_PUSH_SRC); - (BOOL)openDevice; - (void)closeDevice; - (GstVideoFormat)getGstVideoFormat:(NSNumber *)pixel_format; +#if !HAVE_IOS +- (CGDirectDisplayID)getDisplayIdFromDeviceIndex; +#endif - (BOOL)getDeviceCaps:(GstCaps *)result; - (BOOL)setDeviceCaps:(GstVideoInfo *)info; - (BOOL)getSessionPresetCaps:(GstCaps *)result; @@ -169,9 +172,6 @@ didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer captureScreenMouseClicks = NO; useVideoMeta = NO; textureCache = NULL; -#if !HAVE_IOS - displayId = kCGDirectMainDisplay; -#endif mainQueue = dispatch_queue_create ("org.freedesktop.gstreamer.avfvideosrc.main", NULL); @@ -200,7 +200,7 @@ didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer NSString *mediaType = AVMediaTypeVideo; NSError *err; - if (deviceIndex == -1) { + if (deviceIndex == DEFAULT_DEVICE_INDEX) { device = [AVCaptureDevice defaultDeviceWithMediaType:mediaType]; if (device == nil) { GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND, @@ -241,8 +241,14 @@ didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer #if HAVE_IOS return NO; #else + CGDirectDisplayID displayId; + GST_DEBUG_OBJECT (element, "Opening screen input"); + displayId = [self getDisplayIdFromDeviceIndex]; + if (displayId == 0) + return NO; + AVCaptureScreenInput *screenInput = [[AVCaptureScreenInput alloc] initWithDisplayID:displayId]; @@ -368,6 +374,26 @@ didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer return gst_format; } +#if !HAVE_IOS +- (CGDirectDisplayID)getDisplayIdFromDeviceIndex +{ + NSDictionary *description; + NSNumber *displayId; + NSArray *screens = [NSScreen screens]; + + if (deviceIndex == DEFAULT_DEVICE_INDEX) + return kCGDirectMainDisplay; + if (deviceIndex >= [screens count]) { + GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND, + ("Invalid screen capture device index"), (NULL)); + return 0; + } + description = [[screens objectAtIndex:deviceIndex] deviceDescription]; + displayId = [description objectForKey:@"NSScreenNumber"]; + return [displayId unsignedIntegerValue]; +} +#endif + - (BOOL)getDeviceCaps:(GstCaps *)result { NSArray *formats = [device valueForKey:@"formats"]; @@ -547,7 +573,7 @@ didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer if (captureScreen) { #if !HAVE_IOS - CGRect rect = CGDisplayBounds (displayId); + CGRect rect = CGDisplayBounds ([self getDisplayIdFromDeviceIndex]); for (NSNumber *pixel_format in pixel_formats) { GstVideoFormat gst_format = [self getGstVideoFormat:pixel_format]; if (gst_format != GST_VIDEO_FORMAT_UNKNOWN) -- cgit v1.2.1