Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/osx/isight/main.m
Views: 11780
/**********************************************************************1* NAME2*3* isight -- An injectable bundle to capture an image from the4* attached iSight video camera.5*6* SYNOPSIS7* inject-bundle isight <pid>8* inject-bundle isight <cmd> [ <args> ... ]9* run-bundle isight10*11* DESCRIPTION12* This bundle is meant to be injected into a running or newly13* launched process by inject-bundle. It will capture a single14* image from the iSight video camera and store it as15* /tmp/isight.jpg.16*17* This bundle uses Tim Omernick's CocoaSequence Grabber from18* MacFUSE procfs.19*20* LICENSE21* Due to inclusion of GPL-licensed code, this bundle is also22* licended under the GNU Public License.23*24**********************************************************************/2526#import "CocoaSequenceGrabber.h"2728BOOL shouldKeepRunning = YES;2930/*31* This delegate handles the didReceiveFrame callback from CSGCamera,32* which we use to convert the image to a JPEG.33*/34@interface CSGCameraDelegate : CSGCamera35{36CFMutableDataRef data;37}3839/*40* Assign a CFMutableDataRef to receive JPEG image data41*/42- (void)setDataRef:(CFMutableDataRef)dataRef;4344/*45* Convert captured frame into a JPEG datastream, stored in a CFDataRef46*/47- (void)camera:(CSGCamera *)aCamera didReceiveFrame:(CSGImage *)aFrame;4849@end5051@implementation CSGCameraDelegate5253- (void)setDataRef:(CFMutableDataRef)dataRef54{55data = dataRef;56}5758- (void)camera:(CSGCamera *)aCamera didReceiveFrame:(CSGImage *)aFrame;59{60// First, we must convert to a TIFF bitmap61NSBitmapImageRep *imageRep =62[NSBitmapImageRep imageRepWithData: [aFrame TIFFRepresentation]];6364NSNumber *quality = [NSNumber numberWithFloat: 0.1];6566NSDictionary *props =67[NSDictionary dictionaryWithObject:quality68forKey:NSImageCompressionFactor];6970// Now convert TIFF bitmap to JPEG compressed image71NSData *jpeg =72[imageRep representationUsingType: NSJPEGFileType properties:props];7374// Store JPEG image in a CFDataRef75CFIndex jpegLen = CFDataGetLength((CFDataRef)jpeg);76CFDataSetLength(data, jpegLen);77CFDataReplaceBytes(data, CFRangeMake((CFIndex)0, jpegLen),78CFDataGetBytePtr((CFDataRef)jpeg), jpegLen);7980// Stop the camera and signal that we should exit the run loop81[aCamera stop];82shouldKeepRunning = NO;83}8485@end8687void run(int socket)88{89NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];9091/*92* Use CocoaSequenceGrabber to capture a single image from the93* iSight camera and store it as a JPEG data stream in picture.94*/95CFMutableDataRef picture = CFDataCreateMutable(NULL, 0);96CSGCameraDelegate *delegate = [[CSGCameraDelegate alloc] init];97[delegate setDataRef:picture];9899CSGCamera *camera = [[CSGCamera alloc] init];100[camera setDelegate:delegate];101[camera startWithSize:NSMakeSize(640, 480)];102103/*104* Execute RunLoop until global flag is cleared105*/106NSRunLoop *theRL = [NSRunLoop currentRunLoop];107while (shouldKeepRunning && [theRL runMode:NSDefaultRunLoopMode108beforeDate:[NSDate distantFuture]]);109110/*111* Write out picture to to socket112*/113if (socket > 0) {114size_t len = CFDataGetLength(picture);115write(socket, &len, sizeof(len));116write(socket, CFDataGetBytePtr(picture), len);117}118[pool release];119}120121122