Skip to content

Commit

Permalink
#3
Browse files Browse the repository at this point in the history
  • Loading branch information
k0sukey committed Jul 25, 2014
1 parent 075a9e5 commit 2d0c2e8
Show file tree
Hide file tree
Showing 15 changed files with 209 additions and 172 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
[submodule "GPUImage"]
path = GPUImage
url = git@github.com:BradLarson/GPUImage.git
2 changes: 2 additions & 0 deletions Classes/BeK0sukeTicameraModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ -(id)hasFrontCamera:(id)args
return NUMBOOL(YES);
}
}

return NUMBOOL(NO);
}

Expand All @@ -126,6 +127,7 @@ -(id)hasBackCamera:(id)args
return NUMBOOL(YES);
}
}

return NUMBOOL(NO);
}

Expand Down
4 changes: 2 additions & 2 deletions Classes/BeK0sukeTicameraView.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#import "TiUIView.h"
#import <AVFoundation/AVFoundation.h>

@interface BeK0sukeTicameraView : TiUIView<AVCaptureAudioDataOutputSampleBufferDelegate, AVCaptureVideoDataOutputSampleBufferDelegate> {
@interface BeK0sukeTicameraView : TiUIView<AVCaptureVideoDataOutputSampleBufferDelegate> {
@private
KrollCallback *successPictureCallback;
KrollCallback *errorPictureCallback;
Expand All @@ -25,7 +25,7 @@
@property (nonatomic, strong) AVCaptureDeviceInput *videoInput;
@property (nonatomic, strong) AVCaptureVideoDataOutput *videoOutput;
@property (nonatomic, strong) AVCaptureSession *videoSession;
@property (nonatomic, strong) UIImageView *videoPreview;
@property (nonatomic, assign) UIImageView *videoPreview;
@property (nonatomic, strong) NSMutableArray *recordingBuffer;
@property (nonatomic, strong) AVAssetWriter *recordingWriter;
@property (nonatomic, strong) AVAssetWriterInput *recordingInput;
Expand Down
140 changes: 89 additions & 51 deletions Classes/BeK0sukeTicameraView.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,58 @@

@implementation BeK0sukeTicameraView

-(void)dealloc
{
RELEASE_TO_NIL(successPictureCallback);
RELEASE_TO_NIL(errorPictureCallback);
RELEASE_TO_NIL(successRecordingCallback);
RELEASE_TO_NIL(errorRecordingCallback);
#ifndef __i386__
[self.videoSession stopRunning];
#endif
[super dealloc];
}

-(id)init
-(void)initializeState
{
self = [super init];
[super initializeState];
#ifndef __i386__
if (self)
{
if (self.videoPreview == nil)
{
self.videoPreview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,
[TiUtils floatValue:[self.proxy valueForKey:@"width"]],
[TiUtils floatValue:[self.proxy valueForKey:@"height"]])];
[self addSubview:self.videoPreview];
}

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(tapDetected:)];
[self addGestureRecognizer:tapRecognizer];
[tapRecognizer release];

[self setupAVCapture];
}
#endif
return self;
}

-(void)frameSizeChanged:(CGRect)frame bounds:(CGRect)bounds
-(void)dealloc
{
#ifndef __i386__
if (self.videoPreview == nil)
isCameraInputOutput = NO;

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device removeObserver:self forKeyPath:@"adjustingExposure"];

[self.videoSession stopRunning];
[self.videoOutput setSampleBufferDelegate:nil queue:dispatch_get_main_queue()];
[self.videoPreview removeFromSuperview];

for (UIGestureRecognizer *gesture in [self gestureRecognizers])
{
self.videoPreview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,
bounds.size.width,
bounds.size.height)];
[self addSubview:self.videoPreview];
if ([gesture isKindOfClass:[UITapGestureRecognizer class]])
{
[self removeGestureRecognizer:gesture];
}
}

[self setupAVCapture];
#endif

RELEASE_TO_NIL(successPictureCallback);
RELEASE_TO_NIL(errorPictureCallback);
RELEASE_TO_NIL(successRecordingCallback);
RELEASE_TO_NIL(errorRecordingCallback);

[super dealloc];
}

-(void)dispatchCallback:(NSArray*)args
Expand All @@ -64,9 +75,10 @@ -(void)setupAVCapture
#ifndef __i386__
NSError *error = nil;

AVCaptureDevice *captureDevice = [self deviceWithPosition: [TiUtils intValue:[self.proxy valueForKey:@"cameraPosition"]
AVCaptureDevice *device = [self deviceWithPosition: [TiUtils intValue:[self.proxy valueForKey:@"cameraPosition"]
def:AVCaptureDevicePositionBack]];
self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:captureDevice error:&error];
self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:device
error:&error];

if (!self.videoInput)
{
Expand All @@ -82,25 +94,37 @@ -(void)setupAVCapture
def:AVCaptureSessionPresetMedium];
[self.videoSession commitConfiguration];

[captureDevice addObserver:self
forKeyPath:@"adjustingExposure"
options:NSKeyValueObservingOptionNew
context:nil];
[device addObserver:self
forKeyPath:@"adjustingExposure"
options:NSKeyValueObservingOptionNew
context:nil];

self.videoOutput = [[AVCaptureVideoDataOutput alloc] init];
[self.videoSession addOutput:self.videoOutput];

dispatch_queue_t queue = dispatch_queue_create("be.k0suke.ticamera.captureQueue", NULL);
[self.videoOutput setAlwaysDiscardsLateVideoFrames:TRUE];
[self.videoOutput setSampleBufferDelegate:self queue:queue];
dispatch_release(queue);

self.videoOutput.videoSettings = @{
(id)kCVPixelBufferPixelFormatTypeKey : [NSNumber numberWithInt:kCVPixelFormatType_32BGRA]
};

AVCaptureConnection *videoConnection = [self.videoOutput connectionWithMediaType:AVMediaTypeVideo];
videoConnection.videoMinFrameDuration = CMTimeMake(1, [TiUtils intValue:[self.proxy valueForKey:@"frameDuration"]
def:16]);
if ([device respondsToSelector:@selector(setActiveVideoMinFrameDuration:)])
{
[device lockForConfiguration:&error];
[device setActiveVideoMinFrameDuration:CMTimeMake(1, [TiUtils intValue:[self.proxy valueForKey:@"frameDuration"] def:16])];
[device unlockForConfiguration];
}
else
{
AVCaptureConnection *videoConnection = [self.videoOutput connectionWithMediaType:AVMediaTypeVideo];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
videoConnection.videoMinFrameDuration = CMTimeMake(1, [TiUtils intValue:[self.proxy valueForKey:@"frameDuration"] def:16]);
#pragma clang diagnostic pop
}

isCameraInputOutput = YES;

Expand Down Expand Up @@ -156,7 +180,9 @@ -(void)takePicture:(id)args
id listener = [[successPictureCallback retain] autorelease];
NSMutableDictionary *event = [TiUtils dictionaryWithCode:0 message:nil];
[event setObject:[[[TiBlob alloc] initWithImage:self.videoPreview.image] autorelease] forKey:@"media"];
[NSThread detachNewThreadSelector:@selector(dispatchCallback:) toTarget:self withObject:[NSArray arrayWithObjects:@"success", event, listener, nil]];
[NSThread detachNewThreadSelector:@selector(dispatchCallback:)
toTarget:self
withObject:[NSArray arrayWithObjects:@"success", event, listener, nil]];
}
}
else
Expand All @@ -165,7 +191,9 @@ -(void)takePicture:(id)args
{
id listener = [[errorPictureCallback retain] autorelease];
NSMutableDictionary *event = [TiUtils dictionaryWithCode:-1 message:@"AVCaptureConnection connect failed"];
[NSThread detachNewThreadSelector:@selector(dispatchCallback:) toTarget:self withObject:[NSArray arrayWithObjects:@"error", event, listener, nil]];
[NSThread detachNewThreadSelector:@selector(dispatchCallback:)
toTarget:self
withObject:[NSArray arrayWithObjects:@"error", event, listener, nil]];
}
}
#endif
Expand All @@ -192,8 +220,8 @@ -(void)startRecording:(id)args
@"output",
[format stringFromDate:[NSDate date]],
@".mov"]];
recordingSize = CGSizeMake([TiUtils intValue:[self.proxy valueForKey:@"width"]],
[TiUtils intValue:[self.proxy valueForKey:@"height"]]);
recordingSize = CGSizeMake([TiUtils floatValue:[self.proxy valueForKey:@"width"]],
[TiUtils floatValue:[self.proxy valueForKey:@"height"]]);

NSError *error = nil;
self.recordingWriter = [[AVAssetWriter alloc] initWithURL:recordingUrl fileType:AVFileTypeQuickTimeMovie error:&error];
Expand Down Expand Up @@ -254,7 +282,9 @@ -(void)stopRecording:(id)args
{
id listener = [[errorRecordingCallback retain] autorelease];
NSMutableDictionary *event = [TiUtils dictionaryWithCode:-1 message:@"video recording not start"];
[NSThread detachNewThreadSelector:@selector(dispatchCallback:) toTarget:self withObject:[NSArray arrayWithObjects:@"error", event, listener, nil]];
[NSThread detachNewThreadSelector:@selector(dispatchCallback:)
toTarget:self
withObject:[NSArray arrayWithObjects:@"error", event, listener, nil]];
}
return;
}
Expand Down Expand Up @@ -285,8 +315,12 @@ -(void)stopRecording:(id)args
NSMutableDictionary *event = [TiUtils dictionaryWithCode:0 message:nil];
NSData *data = [NSData dataWithContentsOfURL:recordingUrl];
[event setObject:[[[TiBlob alloc] initWithData:data mimetype:@"video/quicktime"] autorelease] forKey:@"media"];
[NSThread detachNewThreadSelector:@selector(dispatchCallback:) toTarget:self withObject:[NSArray arrayWithObjects:@"success", event, listener, nil]];
[NSThread detachNewThreadSelector:@selector(dispatchCallback:)
toTarget:self
withObject:[NSArray arrayWithObjects:@"success", event, listener, nil]];
}

dispatch_release(queue);
});
}];
return;
Expand Down Expand Up @@ -328,7 +362,11 @@ -(void)startInterval:(id)args
NSNumber *delay = [NSNumber numberWithFloat:[[args objectForKey:@"intervalDelay"] intValue] / 1000];
isInterval = NO;

self.intervalTimer = [NSTimer scheduledTimerWithTimeInterval:[delay floatValue] target:self selector:@selector(intervalFlag:) userInfo:nil repeats:YES];
self.intervalTimer = [NSTimer scheduledTimerWithTimeInterval:[delay floatValue]
target:self
selector:@selector(intervalFlag:)
userInfo:nil
repeats:YES];
}

-(void)intervalFlag:(NSTimer*)timer
Expand Down Expand Up @@ -357,9 +395,8 @@ -(id)isFrontCamera:(id)args
return NUMBOOL(YES);
}
}

return NUMBOOL(NO);
#endif
return NUMBOOL(NO);
}

-(id)isBackCamera:(id)args
Expand All @@ -370,14 +407,13 @@ -(id)isBackCamera:(id)args
NSError *error = nil;

AVCaptureDevicePosition position = [[self.videoInput device] position];
if (position == AVCaptureDevicePositionFront)
if (position == AVCaptureDevicePositionBack)
{
return NUMBOOL(NO);
return NUMBOOL(YES);
}
}

return NUMBOOL(YES);
#endif
return NUMBOOL(NO);
}

-(id)isTorch:(id)args
Expand All @@ -390,9 +426,8 @@ -(id)isTorch:(id)args
{
return NUMBOOL(YES);
}

return NUMBOOL(NO);
#endif
return NUMBOOL(NO);
}

-(void)toggleCamera:(id)args
Expand Down Expand Up @@ -634,7 +669,10 @@ -(void)captureOutput:(AVCaptureOutput *)captureOutput
}

dispatch_async(dispatch_get_main_queue(), ^{
self.videoPreview.image = image;
if (isCameraInputOutput)
{
self.videoPreview.image = image;
}
});
}

Expand Down Expand Up @@ -669,9 +707,9 @@ -(void)tapDetected:(UITapGestureRecognizer*)tapRecognizer
}

-(void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if (!adjustingExposure)
{
Expand Down
1 change: 0 additions & 1 deletion GPUImage
Submodule GPUImage deleted from 898676

0 comments on commit 2d0c2e8

Please sign in to comment.