mateagar / Motion-JPEG-Image-View-for-iOS

Generic UIImageView subclass designed to load and play Motion-JPEG streams such as commonly used with IP cameras, etc.
http://thinkflood.com/support/redeye/software/open-source-software/motion-jpeg-image-view-ios/
122 stars 39 forks source link

IOS6 Freeze Issue #5

Closed GregoryAustinFoster closed 11 years ago

GregoryAustinFoster commented 11 years ago

Hello,

I am also seeing the freeze issue only under IOS6 where I did not have this problem before under IOS5. Has anyone found a work around? After days of Googling, I see some mjpeg streams have different formats that may be a cause...

To see the issue, please feel free to connect to my cameras mjpg stream at: http://admin:admin@24.51.192.24:213/cgi/mjpg/mjpg.cgi

You can see that Safari 6.0.2 also exhibits this issue so I am stuck trying to find a workaround...

Thanks you for any help you can offer, Madison

TonciGitHub commented 11 years ago

The problem is that on iOS 6 didReceiveResponse callback doesn't get called as often as it does on iOs 5 so the memory buffer doesn't get reallocated. If you look at didReceiveData callback it always assumes that the next image is at the beginning of the buffer, but if the buffer doesn't get reallocated after every image we will just see the first image in the buffer. If the buffer never gets reallocated we will only see the first image and it will look as a freeze. So the solution is to write our own memory management and not to relay on didReceiveResponse. This is my proposal of .m file:

// // MotionJpegImageView.mm // VideoTest // // Created by Matthew Eagar on 10/3/11. // Copyright 2011 ThinkFlood Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is furnished // to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE.

import "MotionJpegImageView.h"

import "AppDelegate.h"

pragma mark CredentialAlertView Class Declaration

@class CredentialAlertView;

@protocol CredentialAlertDelegate

@end

@interface CredentialAlertView : UIAlertView <UITextFieldDelegate, UIAlertViewDelegate> {

@private UITextField _usernameField; UITextField _passwordField; id _credentialDelegate;

}

@property (nonatomic, readwrite, copy) NSString username; @property (nonatomic, readwrite, copy) NSString password; @property (nonatomic, readwrite, assign) id credentialDelegate;

@end

pragma mark - Constants

define ALERT_HEIGHT 200.0

define ALERT_Y_POSITION 55.0

define BUTTON_MARGIN 15.0

define TEXT_FIELD_MARGIN 5.0

pragma mark - CredentialAlertView Implementation

@implementation CredentialAlertView

pragma mark - Properties

@dynamic username; @dynamic password; @synthesize credentialDelegate = _credentialDelegate;

pragma mark - Initializers

pragma mark - Overrides

pragma mark - UIAlertView Delegate Methods

pragma mark - UITextField Delegate Methods

}

}

@end

pragma mark - Constants

define BEGIN_MARKER_BYTES { 0xFF, 0xD8 }

static NSData *_beginMarkerData = nil;

define END_MARKER_BYTES { 0xFF, 0xD9 }

static NSData *_endMarkerData = nil;

pragma mark - Private Method Declarations

@interface MotionJpegImageView ()

@end

pragma mark - Implementation

@implementation MotionJpegImageView

@synthesize url = _url; @synthesize username = _username; @synthesize password = _password; @synthesize allowSelfSignedCertificates = _allowSelfSignedCertificates; @synthesize allowClearTextCredentials = _allowClearTextCredentials; @dynamic isPlaying;

pragma mark - Initializers

pragma mark - Overrides

pragma mark - Public Methods

pragma mark - Private Methods

pragma mark - NSURLConnection Delegate Methods

//- (void)connection:(NSURLConnection )connection didReceiveResponse:(NSURLResponse )response { //// DLog(@""); //}

//- (void)connection:(NSURLConnection )connection didReceiveData:(NSData )data { // [_receivedData appendData:data]; //
// NSRange endRange = [_receivedData rangeOfData:_endMarkerData // options:0 // range:NSMakeRange(0, _receivedData.length)]; //
// long endLocation = endRange.location + endRange.length; //
// if (_receivedData.length >= endLocation) { // NSData imageData = [_receivedData subdataWithRange:NSMakeRange(0, endLocation)]; //
// UIImage
receivedImage = [UIImage imageWithData:imageData]; // if (receivedImage) { // [self setImage:receivedImage]; // } // } //}

//- (void)connection:(NSURLConnection )connection didReceiveData:(NSData )data { // [_receivedData appendData:data]; //
// NSRange endRange = [_receivedData rangeOfData:_endMarkerData // options:0 // range:NSMakeRange(0, _receivedData.length)]; //
// long endLocation = endRange.location + endRange.length; //
// if (_receivedData.length >= endLocation) { // NSData imageData = [_receivedData subdataWithRange:NSMakeRange(0, endLocation)]; //
// UIImage
receivedImage = [UIImage imageWithData:imageData]; // if (receivedImage) { // [self setImage:receivedImage]; // [_receivedData replaceBytesInRange:NSMakeRange(0, endLocation+1) withBytes:NULL length:0]; // // DLog(@"%i", _receivedData.length); // } // } //}

pragma mark - CredentialAlertView Delegate Methods

@end

Hope this helps... Antonijo

GregoryAustinFoster commented 11 years ago

Thant makes perfect sense!

Thank you so much for taking the time to explain!

Madison

jackie11 commented 8 years ago

I need help. I have now found since updating my iphone 5 on ios 6, to ios 9, and mjpeg stream I play from my cameras will freeze after 5 seconds! It will not work for more than a few seconds on my iphone. I've tried 3 other iphones on ios9 and same thing. ANd I've tried 3 additional iphones on ios 6 and the stream never freezes! What gives?