创建 MJPEG 查看器 Iphone

发布于 2024-08-17 19:33:50 字数 2027 浏览 6 评论 0原文

我正在尝试用 Objective C 制作一个 MJPEG 查看器,但我遇到了很多问题。

首先,我使用 AsyncSocket(http://code.google.com/p/cocoaasyncsocket/ ),这让我可以连接到主机。

这是到目前为止我

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);
}

在 didConnectToHost 方法中得到的内容:

 - (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
}

}

这会调用 MJPEG 流,但不会调用它来获取更多数据。我认为它所做的只是加载第一块数据,然后断开连接。

我这样做是完全错误的还是隧道尽头有光明?

谢谢!

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

First off, I'm 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的他你的她 2024-08-24 19:33:50

尝试在 UiWebView 中加载 mjpeg,它应该能够本地播放。

假设你有一个名为“myWebView”的 UiWebView,类似这样的东西应该可以工作:

NSURLRequest* urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://kamera5.vfp.slu.se/axis-cgi/mjpg/video.cgi"]];
[myWebView loadRequest:urlRequest];

我希望有帮助!

Try loading the mjpeg in a UiWebView, it should be able to play it natively.

Assuming you have a UiWebView called "myWebView", something like this should work:

NSURLRequest* urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://kamera5.vfp.slu.se/axis-cgi/mjpg/video.cgi"]];
[myWebView loadRequest:urlRequest];

I hope that helps!

小鸟爱天空丶 2024-08-24 19:33:50

主要问题是 webkit 从不释放数据,所以一段时间后它就会爆炸。

the main problem is that webkit never relase the data, so after a while it explode.

简美 2024-08-24 19:33:50

这可能最好使用 JavaScript 来完成,因为否则没有与 UIWebView 通信的好方法。

That would probably best be done with JavaScript since there isn't a good way to communicate with UIWebView otherwise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文