Creating an MJPEG Viewer Iphone

Posted by Tony on Stack Overflow See other posts from Stack Overflow or by Tony
Published on 2010-01-12T22:32:00Z Indexed on 2010/05/08 20:18 UTC
Read the original article Hit count: 496

Filed under:
|
|
|
|

Hey all,

Im trying to make a MJPEG viewer in Objective C but I'm having a bunch of issues with it.

First off, Im using AsyncSocket(http://code.google.com/p/cocoaasyncsocket/) which lets me connect to the host.

Here's what I got so far

NSLog(@"Ready");
asyncSocket = [[AsyncSocket alloc] initWithDelegate:self];
//http://kamera5.vfp.slu.se/axis-cgi/mjpg/video.cgi
NSError *err = nil;
if(![asyncSocket connectToHost:@"kamera5.vfp.slu.se" onPort:80 error:&err])
{
    NSLog(@"Error: %@", err);
}

then in the didConnectToHost method:

 - (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{
NSLog(@"Accepted client %@:%hu", host, port);


NSString *urlString = [NSString stringWithFormat:@"http://kamera5.vfp.slu.se/axis-cgi/mjpg/video.cgi"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"GET"];

    //set headers
NSString *_host = [NSString stringWithFormat:host];
[request addValue:_host forHTTPHeaderField: @"Host"];

NSString *KeepAlive = [NSString stringWithFormat:@"300"];
[request addValue:KeepAlive forHTTPHeaderField: @"Keep-Alive"];

NSString *connection = [NSString stringWithFormat:@"keep-alive"];
[request addValue:connection forHTTPHeaderField: @"Connection"];


//get response
NSHTTPURLResponse* urlResponse = nil;  
NSError *error = [[NSError alloc] init];  
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; 

NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

NSLog(@"Response Code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
    NSLog(@"Response: %@", result);
            //here you get the response
}

}

This calls the MJPEG stream, but it doesn't call it to get more data. What I think its doing is just loading the first chunk of data, then disconnecting.

Am I doing this totally wrong or is there light at the end of this tunnel?

Thanks!

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective