Created
October 30, 2018 01:42
-
-
Save akhenakh/33396a3ddfb8008e5bba457d99036d5d to your computer and use it in GitHub Desktop.
MJPEG View Controller
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
// | |
// MJPEGViewController.m | |
// iMocs | |
// | |
// Created by akh on 2018-10-29. | |
// Copyright ? 2018 akh. All rights reserved. | |
// | |
#import "MJPEGViewController.h" | |
#define END_MARKER_BYTES { 0xFF, 0xD9 } | |
@interface MJPEGViewController () | |
- (IBAction)reloadTouched:(id)sender; | |
@property (weak, nonatomic) IBOutlet UIView *connectedView; | |
@property (weak, nonatomic) IBOutlet UIImageView *imageView; | |
@property(strong, nonatomic) NSMutableData *buffer; | |
@property(strong, nonatomic) NSURLConnection *conn; | |
@property(strong, nonatomic) NSData *endMarkerData; | |
@end | |
@implementation MJPEGViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view. | |
uint8_t endMarker[2] = END_MARKER_BYTES; | |
self.endMarkerData = [[NSData alloc] initWithBytes:endMarker length:2]; | |
} | |
- (void) play { | |
self.buffer = [NSMutableData data]; | |
dispatch_sync(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){ | |
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.12.1:8080/?action=stream"]]; | |
self.conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO]; | |
[self.conn start]; | |
}); | |
} | |
- (void) stop { | |
[self.conn cancel]; | |
self.connectedView.backgroundColor = [UIColor redColor]; | |
} | |
- (void) viewWillAppear:(BOOL)animated { | |
[self play]; | |
} | |
- (void)viewDidDisappear:(BOOL)animated { | |
[self stop]; | |
} | |
- (IBAction)reloadTouched:(id)sender { | |
[self stop]; | |
[self play]; | |
} | |
- (void) connection:(NSURLConnection *)connection didFailWithError:(nonnull NSError *)error { | |
self.connectedView.backgroundColor = [UIColor redColor]; | |
self.buffer = nil; | |
self.imageView.image = nil; | |
} | |
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { | |
[_buffer appendData:data]; | |
self.connectedView.backgroundColor = [UIColor greenColor]; | |
NSRange endRange = [_buffer rangeOfData:_endMarkerData | |
options:0 | |
range:NSMakeRange(0, _buffer.length)]; | |
long long endLocation = endRange.location + endRange.length; | |
if (_buffer.length >= endLocation) { | |
NSData *imageData = [_buffer subdataWithRange:NSMakeRange(0, endLocation)]; | |
UIImage *receivedImage = [UIImage imageWithData:imageData]; | |
if (receivedImage) { | |
//Background Thread | |
dispatch_async(dispatch_get_main_queue(), ^(void){ | |
self.imageView.image = receivedImage; | |
}); | |
} | |
} | |
} | |
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { | |
//UIImage *img = [UIImage imageWithData:self.buffer]; | |
//self.imageView.image = img; | |
[self.buffer setLength:0]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment