Skip to content

Instantly share code, notes, and snippets.

@pmilosev
Last active March 3, 2017 09:23
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save pmilosev/1608099 to your computer and use it in GitHub Desktop.
Save pmilosev/1608099 to your computer and use it in GitHub Desktop.
Stream iPhone's screen over WIFI
/*
* 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("name.of.your.queue.here", 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