-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoSource.mm
More file actions
139 lines (113 loc) · 4.8 KB
/
VideoSource.mm
File metadata and controls
139 lines (113 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// VideoSource.m
//
// Created by Yoseph Radding on 1/16/15.
// Copyright (c) 2015 Yoseph Radding. All rights reserved.
//
#import "VideoSource.h"
@interface VideoSource () <AVCaptureVideoDataOutputSampleBufferDelegate>
@end
@implementation VideoSource
-(id) init{
if(self = [super init]){
AVCaptureSession * captureSession = [[AVCaptureSession alloc] init];
if ( [captureSession canSetSessionPreset:AVCaptureSessionPreset640x480] ) {
[captureSession setSessionPreset:AVCaptureSessionPreset640x480];
NSLog(@"Capturing video at 640x480");
} else {
NSLog(@"Could not configure AVCaptureSession video input");
}
_captureSession = captureSession;
}
return self;
}
- (void)dealloc {
[_captureSession stopRunning];
}
- (AVCaptureDevice*)cameraWithPosition:(AVCaptureDevicePosition)position {
NSArray * devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for ( AVCaptureDevice * device in devices ) {
if ( [device position] == position ) {
return device;
}
}
return nil;
}
- (BOOL)startWithDevicePosition:(AVCaptureDevicePosition)devicePosition {
// (1) Find camera device at the specific position
AVCaptureDevice * videoDevice = [self cameraWithPosition:devicePosition];
if ( !videoDevice ) {
NSLog(@"Could not initialize camera at position %ld", devicePosition);
return FALSE;
}
// (2) Obtain input port for camera device
NSError * error;
AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
if ( !error ) {
[self setDeviceInput:videoInput];
} else {
NSLog(@"Could not open input port for device %@ (%@)", videoDevice, [error localizedDescription]);
return FALSE;
}
// (3) Configure input port for captureSession
if ( [self.captureSession canAddInput:videoInput] ) {
[self.captureSession addInput:videoInput];
} else {
NSLog(@"Could not add input port to capture session %@", self.captureSession);
return FALSE;
}
// (4) Configure output port for captureSession
[self addVideoDataOutput];
// (5) Start captureSession running
[self.captureSession startRunning];
// AVCaptureConnection *connection = [[AVCaptureConnection alloc] initWithInputPorts:[[[self.captureSession inputs] firstObject] ports] output:[[self.captureSession outputs] firstObject]];
// [connection setVideoOrientation:AVCaptureVideoOrientationPortrait];
return TRUE;
}
- (void) addVideoDataOutput {
// (1) Instantiate a new video data output object
AVCaptureVideoDataOutput * captureOutput = [[AVCaptureVideoDataOutput alloc] init];
captureOutput.alwaysDiscardsLateVideoFrames = YES;
// (2) The sample buffer delegate requires a serial dispatch queue
dispatch_queue_t queue;
queue = dispatch_queue_create("com.YosephRadding.find.opencv", DISPATCH_QUEUE_SERIAL);
[captureOutput setSampleBufferDelegate:self queue:queue];
// dispatch_release(queue);
// (3) Define the pixel format for the video data output
NSString * key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
NSNumber * value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
NSDictionary * settings = @{key:value};
[captureOutput setVideoSettings:settings];
// (4) Configure the output port on the captureSession property
[self.captureSession addOutput:captureOutput];
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
// (1) Convert CMSampleBufferRef to CVImageBufferRef
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
// (2) Lock pixel buffer
CVPixelBufferLockBaseAddress(imageBuffer, kCVPixelBufferLock_ReadOnly);
// (3) Construct VideoFrame struct
uint8_t *baseAddress = (uint8_t*)CVPixelBufferGetBaseAddress(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
size_t stride = CVPixelBufferGetBytesPerRow(imageBuffer);
VideoFrame frame = {width, height, stride, baseAddress};
// (4) Dispatch VideoFrame to VideoSource delegate
[self.delegate frameReady:frame];
// (5) Unlock pixel buffer
CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
}
-(void) toggleFlashLight{
if(self.deviceInput.device.torchMode == AVCaptureTorchModeOff){
[self.deviceInput.device lockForConfiguration:nil];
[self.deviceInput.device setTorchMode:AVCaptureTorchModeOn];
}
else{
[self.deviceInput.device setTorchMode:AVCaptureTorchModeOff];
[self.deviceInput.device unlockForConfiguration];
}
}
@end