Commit 53ff537f authored by Nguyen Nguyen's avatar Nguyen Nguyen

temporary fixes

parent a5814871
...@@ -109,6 +109,7 @@ ...@@ -109,6 +109,7 @@
#ifdef __cplusplus #ifdef __cplusplus
// delegate method for processing image frames // delegate method for processing image frames
- (void)processImage:(cv::Mat&)image; - (void)processImage:(cv::Mat&)image;
#endif #endif
@end @end
...@@ -128,8 +129,6 @@ ...@@ -128,8 +129,6 @@
AVAssetWriterInputPixelBufferAdaptor* recordPixelBufferAdaptor; AVAssetWriterInputPixelBufferAdaptor* recordPixelBufferAdaptor;
AVAssetWriter* recordAssetWriter; AVAssetWriter* recordAssetWriter;
CMTime lastSampleTime;
} }
@property (nonatomic, assign) id<CvVideoCameraDelegate> delegate; @property (nonatomic, assign) id<CvVideoCameraDelegate> delegate;
...@@ -140,6 +139,7 @@ ...@@ -140,6 +139,7 @@
@property (nonatomic, retain) AVAssetWriterInput* recordAssetWriterInput; @property (nonatomic, retain) AVAssetWriterInput* recordAssetWriterInput;
@property (nonatomic, retain) AVAssetWriterInputPixelBufferAdaptor* recordPixelBufferAdaptor; @property (nonatomic, retain) AVAssetWriterInputPixelBufferAdaptor* recordPixelBufferAdaptor;
@property (nonatomic, retain) AVAssetWriter* recordAssetWriter; @property (nonatomic, retain) AVAssetWriter* recordAssetWriter;
@property (nonatomic, readonly) int64_t timestampMs;
- (void)adjustLayoutToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation; - (void)adjustLayoutToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
- (void)layoutPreviewLayer; - (void)layoutPreviewLayer;
......
...@@ -401,6 +401,7 @@ ...@@ -401,6 +401,7 @@
NSLog(@"[Camera] device connected? %@", device.connected ? @"YES" : @"NO"); NSLog(@"[Camera] device connected? %@", device.connected ? @"YES" : @"NO");
NSLog(@"[Camera] device position %@", (device.position == AVCaptureDevicePositionBack) ? @"back" : @"front"); NSLog(@"[Camera] device position %@", (device.position == AVCaptureDevicePositionBack) ? @"back" : @"front");
#if 0
AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio]; AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
NSError *error = nil; NSError *error = nil;
//AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&error]; //AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&error];
...@@ -427,6 +428,7 @@ ...@@ -427,6 +428,7 @@
self.audioCaptureConnection = [audioOut connectionWithMediaType:AVMediaTypeAudio]; self.audioCaptureConnection = [audioOut connectionWithMediaType:AVMediaTypeAudio];
NSLog(@"Audio has been setup with callback "); NSLog(@"Audio has been setup with callback ");
#endif
} }
......
...@@ -43,6 +43,11 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}; ...@@ -43,6 +43,11 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
@interface CvVideoCamera () { @interface CvVideoCamera () {
NSString* mediaPath; NSString* mediaPath;
int recordCountDown;
CMTime _lastSampleTime;
int64_t _timestampMs;
dispatch_queue_t movieWriterQueue;
} }
...@@ -54,6 +59,7 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}; ...@@ -54,6 +59,7 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
@property (nonatomic, retain) CALayer *customPreviewLayer; @property (nonatomic, retain) CALayer *customPreviewLayer;
@property (nonatomic, retain) AVCaptureVideoDataOutput *videoDataOutput; @property (nonatomic, retain) AVCaptureVideoDataOutput *videoDataOutput;
@property (nonatomic, retain) AVCaptureMovieFileOutput *movieFileOutput; @property (nonatomic, retain) AVCaptureMovieFileOutput *movieFileOutput;
@property (nonatomic, retain) dispatch_queue_t movieWriterQueue;
@end @end
...@@ -82,6 +88,9 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}; ...@@ -82,6 +88,9 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
@synthesize recordPixelBufferAdaptor; @synthesize recordPixelBufferAdaptor;
@synthesize recordAssetWriter; @synthesize recordAssetWriter;
@synthesize timestampMs = _timestampMs;
...@@ -89,12 +98,14 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}; ...@@ -89,12 +98,14 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
- (id)initWithParentView:(UIView*)parent; - (id)initWithParentView:(UIView*)parent;
{ {
recordCountDown = 1000000000;
self = [super initWithParentView:parent]; self = [super initWithParentView:parent];
if (self) { if (self) {
self.useAVCaptureVideoPreviewLayer = NO; self.useAVCaptureVideoPreviewLayer = NO;
self.recordVideo = NO; self.recordVideo = NO;
self.rotateVideo = NO; self.rotateVideo = NO;
} }
movieWriterQueue = nil;
return self; return self;
} }
...@@ -105,6 +116,8 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}; ...@@ -105,6 +116,8 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
- (void)start; - (void)start;
{ {
recordCountDown = 5;
movieWriterQueue = nil;
[super start]; [super start];
if (self.recordVideo == YES) { if (self.recordVideo == YES) {
...@@ -116,11 +129,8 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}; ...@@ -116,11 +129,8 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
if (error == nil) { if (error == nil) {
NSLog(@"[Camera] Delete file %@", [self videoFileString]); NSLog(@"[Camera] Delete file %@", [self videoFileString]);
} }
if ([[NSFileManager defaultManager] fileExistsAtPath:[self mediaFileString]]) {
[[NSFileManager defaultManager] removeItemAtPath:[self mediaFileString] error:&error];
}
} }
} }
...@@ -146,6 +156,9 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}; ...@@ -146,6 +156,9 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
self.recordAssetWriter = nil; self.recordAssetWriter = nil;
self.recordAssetWriterInput = nil; self.recordAssetWriterInput = nil;
self.recordPixelBufferAdaptor = nil; self.recordPixelBufferAdaptor = nil;
if (movieWriterQueue)
dispatch_release(movieWriterQueue);
self.movieWriterQueue = nil;
} }
[self.customPreviewLayer removeFromSuperlayer]; [self.customPreviewLayer removeFromSuperlayer];
...@@ -344,6 +357,9 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}; ...@@ -344,6 +357,9 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
[self.videoDataOutput setSampleBufferDelegate:self queue:videoDataOutputQueue]; [self.videoDataOutput setSampleBufferDelegate:self queue:videoDataOutputQueue];
if (self.recordVideo == YES && movieWriterQueue == nil) {
movieWriterQueue = dispatch_queue_create("opencv_movieWriter", DISPATCH_QUEUE_SERIAL);
}
NSLog(@"[Camera] created AVCaptureVideoDataOutput at %d FPS", self.defaultFPS); NSLog(@"[Camera] created AVCaptureVideoDataOutput at %d FPS", self.defaultFPS);
} }
...@@ -477,6 +493,15 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}; ...@@ -477,6 +493,15 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
} }
#pragma mark - Protocol AVCaptureVideoDataOutputSampleBufferDelegate #pragma mark - Protocol AVCaptureVideoDataOutputSampleBufferDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didDropSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
if (connection == self.audioCaptureConnection) {
NSLog(@"Audio sample did drop ");
return;
}
NSLog(@"Video Frame did drop ");
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{ {
...@@ -484,7 +509,7 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}; ...@@ -484,7 +509,7 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
(void)connection; (void)connection;
if (connection == self.audioCaptureConnection) { if (connection == self.audioCaptureConnection) {
NSLog(@"Audio Sample came in "); //NSLog(@"Audio Sample came in ");
return; return;
} }
...@@ -526,15 +551,22 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}; ...@@ -526,15 +551,22 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
} }
CMTime lastSampleTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
int64_t msec = lastSampleTime.value / (lastSampleTime.timescale / 1000);
_timestampMs = msec;
//NSLog(@"Timestamp %u / %u, msec = %lu ", lastSampleTime.value, lastSampleTime.timescale, msec);
// delegate image processing to the delegate // delegate image processing to the delegate
cv::Mat image((int)height, (int)width, format_opencv, bufferAddress, bytesPerRow); cv::Mat image((int)height, (int)width, format_opencv, bufferAddress, bytesPerRow);
CGImage* dstImage;
if ([self.delegate respondsToSelector:@selector(processImage:)]) { if ([self.delegate respondsToSelector:@selector(processImage:)]) {
[self.delegate processImage:image]; [self.delegate processImage:image];
} }
CGImage* dstImage;
// check if matrix data pointer or dimensions were changed by the delegate // check if matrix data pointer or dimensions were changed by the delegate
bool iOSimage = false; bool iOSimage = false;
if (height == (size_t)image.rows && width == (size_t)image.cols && format_opencv == image.type() && bufferAddress == image.data && bytesPerRow == image.step) { if (height == (size_t)image.rows && width == (size_t)image.cols && format_opencv == image.type() && bufferAddress == image.data && bytesPerRow == image.step) {
...@@ -595,17 +627,20 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}; ...@@ -595,17 +627,20 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
// render buffer // render buffer
//dispatch_sync(dispatch_get_main_queue(), ^{
dispatch_sync(dispatch_get_main_queue(), ^{ dispatch_sync(dispatch_get_main_queue(), ^{
self.customPreviewLayer.contents = (__bridge id)dstImage; self.customPreviewLayer.contents = (__bridge id)dstImage;
}); });
if (recordCountDown > 0)
recordCountDown--;
if (self.recordVideo == YES && recordCountDown <= 0) {
//CMTimeShow(lastSampleTime);
if (self.recordVideo == YES) {
lastSampleTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
// CMTimeShow(lastSampleTime);
if (self.recordAssetWriter.status != AVAssetWriterStatusWriting) { if (self.recordAssetWriter.status != AVAssetWriterStatusWriting) {
[self.recordAssetWriter startWriting]; [self.recordAssetWriter startWriting];
[self.recordAssetWriter startSessionAtSourceTime:lastSampleTime]; [self.recordAssetWriter startSessionAtSourceTime:_lastSampleTime];
if (self.recordAssetWriter.status != AVAssetWriterStatusWriting) { if (self.recordAssetWriter.status != AVAssetWriterStatusWriting) {
NSLog(@"[Camera] Recording Error: asset writer status is not writing: %@", self.recordAssetWriter.error); NSLog(@"[Camera] Recording Error: asset writer status is not writing: %@", self.recordAssetWriter.error);
return; return;
...@@ -623,10 +658,8 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}; ...@@ -623,10 +658,8 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
if (pixelBuffer != nullptr) if (pixelBuffer != nullptr)
CVPixelBufferRelease(pixelBuffer); CVPixelBufferRelease(pixelBuffer);
} }
} }
// cleanup // cleanup
CGImageRelease(dstImage); CGImageRelease(dstImage);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment