-
-
Save cloudjanak/72e42ba6eee290e5f13f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 1) Download CocoaHTTPServer from Github: https://github.com/robbiehanson/CocoaHTTPServer | |
* 2) Include the downloaded source (except the samples) in your project | |
* 3) Define the SCREENCAST macro for the target/configuration you want to enable the screencast for (SCREENCAST=1) | |
* 4) Add the code below to your main.m file before the main() method | |
*/ | |
#if SCREENCAST | |
#import "HTTPServer.h" | |
#import "HTTPConnection.h" | |
#import "HTTPDataResponse.h" | |
#import <dispatch/dispatch.h> | |
#import <QuartzCore/QuartzCore.h> | |
HTTPServer *httpServer = nil; | |
NSData *imageData = nil; | |
CGImageRef UIGetScreenImage(void); | |
@interface ScreencastHTTPConnection : HTTPConnection {} | |
@end | |
@implementation ScreencastHTTPConnection | |
- (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method | |
URI:(NSString *)path | |
{ | |
NSString *filePath = [self filePathForURI:path]; | |
NSString *documentRoot = [config documentRoot]; | |
NSString *relativePath = [filePath substringFromIndex:[documentRoot length]]; | |
/* check if the screen image needs to be returned */ | |
if ([relativePath hasPrefix:@"/screencast.jpg"]) { | |
return [[HTTPDataResponse alloc] initWithData:imageData]; | |
} | |
/* otherwise return the HTML */ | |
NSString *htmlContent = @"\ | |
<html>\ | |
<head>\ | |
<script type='text/JavaScript'>\ | |
var x=0, y=0;\ | |
var canvas, context, img;\ | |
function timedRefresh(timeoutPeriod)\ | |
{\ | |
canvas = document.getElementById('image');\ | |
context = canvas.getContext('2d');\ | |
img = new Image();\ | |
img.src = 'screencast.jpg?t=' + new Date().getTime();\ | |
img.onload = function() {\ | |
context.drawImage(img, x, y);\ | |
setTimeout('timedRefresh('+timeoutPeriod+')',timeoutPeriod);\ | |
};\ | |
}\ | |
</script>\ | |
<title>JavaScript Refresh Example</title>\ | |
</head>\ | |
<body onload='JavaScript:timedRefresh(10);'>\ | |
<canvas id='image' width='320px' height='480px'/>\ | |
</html>"; | |
NSData *htmlData = [htmlContent dataUsingEncoding:NSASCIIStringEncoding]; | |
return [[HTTPDataResponse alloc] initWithData:htmlData]; | |
} | |
@end | |
#endif | |
/* Add the snippet bellow in your main() method, before the UIApplicationMain method call */ | |
#if SCREENCAST | |
/* start a worker thread that will continiously capture the screen */ | |
dispatch_queue_t worker = | |
dispatch_queue_create("com.netcetera.mgb008.screencast", NULL); | |
dispatch_async(worker, ^{ | |
@autoreleasepool { | |
// wait until the window is available for the application | |
CALayer *layer = nil; | |
while (!layer) { | |
usleep(50000); | |
layer = [[[[UIApplication sharedApplication] | |
windows] lastObject] layer]; | |
} | |
// loop infinitly, retriving the image of the current screen | |
while (true) { | |
@autoreleasepool { | |
UIGraphicsBeginImageContext(CGSizeMake(320, 480)); | |
[layer renderInContext:UIGraphicsGetCurrentContext()]; | |
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
imageData = UIImageJPEGRepresentation(image, 0.0); | |
usleep(50000); | |
} | |
} | |
} | |
}); | |
/* setup the server */ | |
httpServer = [HTTPServer new]; | |
[httpServer setPort:8080]; | |
[httpServer setConnectionClass:[ScreencastHTTPConnection class]]; | |
[httpServer setDocumentRoot:[[NSBundle mainBundle] resourcePath]]; | |
/* start the server */ | |
NSError *error = nil; | |
if (![httpServer start:&error]) { | |
NSLog(@"Can not start the HTTP server: %@", error); | |
} | |
#endif | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment