@@ -52,24 +52,19 @@ - (void)captureOutput:(AVCaptureOutput *)captureOutput
5252 didOutputSampleBuffer : (CMSampleBufferRef)sampleBuffer
5353 fromConnection : (AVCaptureConnection *)connection
5454{
55- CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer (sampleBuffer);
56- if (firstTime) {
57- width = CVPixelBufferGetWidth (imageBuffer);
58- height = CVPixelBufferGetHeight (imageBuffer);
59-
60- // We unlock the image buffer
61- CVPixelBufferUnlockBaseAddress (imageBuffer, 0 );
62-
63- pixels = malloc (width * height * 4 );
64- firstTime = false ;
65- }
66- else {
67- CVPixelBufferLockBaseAddress (imageBuffer, 0 );
68- unsigned int *isrc4 = (unsigned int *)CVPixelBufferGetBaseAddress (imageBuffer);
69- memcpy (pixels, isrc4, height * width * 4 );
70- CVPixelBufferUnlockBaseAddress (imageBuffer, 0 );
71- }
72- frameCount++;
55+ CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer (sampleBuffer);
56+ if (firstTime) {
57+ width = CVPixelBufferGetWidth (imageBuffer);
58+ height = CVPixelBufferGetHeight (imageBuffer);
59+ pixels = malloc (width * height * 4 );
60+ }
61+ CVPixelBufferLockBaseAddress (imageBuffer, 0 );
62+ memcpy ( pixels,
63+ CVPixelBufferGetBaseAddress (imageBuffer),
64+ width * height * 4 );
65+ firstTime = false ;
66+ CVPixelBufferUnlockBaseAddress (imageBuffer, 0 );
67+ frameCount++;
7368}
7469
7570// If desiredWidth == 0 && desiredHeight == 0 then initialize
@@ -84,9 +79,8 @@ -(SqueakVideoGrabber*)initCapture:(int)deviceNum
8479 }]];
8580
8681 // NSLog(@"devices count %d\n", [devices count]);
87- if ([devices count ] == 0 ) {
82+ if ([devices count ] == 0 )
8883 return NULL ;
89- }
9084
9185 deviceID = deviceNum > ([devices count ] - 1 )
9286 ? ([devices count ] - 1 )
@@ -131,9 +125,8 @@ -(SqueakVideoGrabber*)initCapture:(int)deviceNum
131125 [captureOutput setVideoSettings: videoSettings];
132126
133127 // And we create a capture session
134- if (captureSession) {
128+ if (captureSession)
135129 captureSession = NULL ;
136- }
137130 captureSession = [[AVCaptureSession alloc ] init ];
138131#if IOS
139132 [captureSession autorelease ];
@@ -169,17 +162,14 @@ -(SqueakVideoGrabber*)initCapture:(int)deviceNum
169162
170163#undef USEPRESETFOR
171164
172- if (preset) {
165+ if (preset)
173166 [captureSession setSessionPreset: preset];
174- }
175167
176168 // We add input and output
177- if ([captureSession canAddInput: captureInput]) {
169+ if ([captureSession canAddInput: captureInput])
178170 [captureSession addInput: captureInput];
179- }
180- if ([captureSession canAddOutput: captureOutput]) {
171+ if ([captureSession canAddOutput: captureOutput])
181172 [captureSession addOutput: captureOutput];
182- }
183173
184174 // We start the capture Session
185175 [captureSession commitConfiguration ];
@@ -194,15 +184,15 @@ -(SqueakVideoGrabber*)initCapture:(int)deviceNum
194184
195185-(void )startCapture : (sqInt)cameraNum {
196186 if (!bInitCalled) {
197- [self initCapture: cameraNum-1 desiredWidth: 640 desiredHeight: 480 ];
187+ ( void ) [self initCapture: cameraNum-1 desiredWidth: 640 desiredHeight: 480 ];
198188 }
199- [captureSession startRunning ];
189+ // [captureSession startRunning];
200190 [captureInput.device lockForConfiguration: nil ];
201191
202- // if ( [captureInput.device isExposureModeSupported:AVCaptureExposureModeAutoExpose] ) [captureInput.device setExposureMode:AVCaptureExposureModeAutoExpose ];
203- if ([captureInput.device isFocusModeSupported: AVCaptureFocusModeAutoFocus]) {
192+ // if ( [captureInput.device isExposureModeSupported:AVCaptureExposureModeAutoExpose] )
193+ [captureInput.device setExposureMode: AVCaptureExposureModeAutoExpose ];
194+ if ([captureInput.device isFocusModeSupported: AVCaptureFocusModeAutoFocus])
204195 [captureInput.device setFocusMode: AVCaptureFocusModeAutoFocus];
205- }
206196}
207197
208198-(void )stopCapture : (sqInt)cameraNum {
@@ -238,10 +228,10 @@ -(void)stopCapture: (sqInt)cameraNum {
238228
239229SqueakVideoGrabber *
240230init (sqInt cameraNum, int desiredWidth, int desiredHeight) {
241- SqueakVideoGrabber *this = [SqueakVideoGrabber alloc ];
242- return [this initCapture: cameraNum-1
243- desiredWidth: desiredWidth
244- desiredHeight: desiredHeight];
231+ SqueakVideoGrabber *grabber = [SqueakVideoGrabber alloc ];
232+ return [grabber initCapture: cameraNum-1
233+ desiredWidth: desiredWidth
234+ desiredHeight: desiredHeight];
245235}
246236
247237void
@@ -264,56 +254,63 @@ -(void)stopCapture: (sqInt)cameraNum {
264254
265255sqInt
266256CameraOpen (sqInt cameraNum, sqInt desiredWidth, sqInt desiredHeight) {
267- if (cameraNum<1 || cameraNum>8 ) return false ;
268- SqueakVideoGrabber *this = grabbers[cameraNum-1 ];
257+ if (cameraNum<1 || cameraNum>8 )
258+ return false ;
259+ SqueakVideoGrabber *grabber = grabbers[cameraNum-1 ];
269260
270- if (this )
261+ if (grabber )
271262 return true ;
272263
273- this = init (cameraNum, desiredWidth, desiredHeight);
274- if (!this )
264+ grabber = init (cameraNum, desiredWidth, desiredHeight);
265+ if (!grabber )
275266 return false ;
276- [this startCapture: cameraNum];
267+ [grabber startCapture: cameraNum];
277268 return true ;
278269}
279270
280271void
281272CameraClose (sqInt cameraNum)
282273{
283- if (cameraNum<1 || cameraNum>8 ) return ;
284- SqueakVideoGrabber *this = grabbers[cameraNum-1 ];
285- if (!this) return ;
286- [this stopCapture: cameraNum];
274+ if (cameraNum<1 || cameraNum>8 )
275+ return ;
276+ SqueakVideoGrabber *grabber = grabbers[cameraNum-1 ];
277+ if (!grabber)
278+ return ;
279+ [grabber stopCapture: cameraNum];
287280}
288281
289282sqInt
290283CameraExtent (sqInt cameraNum)
291284{
292- SqueakVideoGrabber *this ;
285+ SqueakVideoGrabber *grabber ;
293286
294287 /* if the camera is already open answer its extent */
295288 if (cameraNum >= 1 && cameraNum <= 8
296- && (this = grabbers[cameraNum-1 ]))
297- return this->width <<16 | this->height ;
298- if (!getDeviceName (cameraNum)) return 0 ;
289+ && (grabber = grabbers[cameraNum-1 ]))
290+ return grabber->width <<16 | grabber->height ;
291+ if (!getDeviceName (cameraNum))
292+ return 0 ;
299293 /* if not yet open, open with default resolution and answer default extent */
300- this = grabbers[cameraNum-1 ];
301- if (!this )
302- this = init (cameraNum, 0 , 0 );
303- return this ? (this ->width <<16 | this ->height ) : 0 ;
294+ grabber = grabbers[cameraNum-1 ];
295+ if (!grabber )
296+ grabber = init (cameraNum, 0 , 0 );
297+ return grabber ? (grabber ->width <<16 | grabber ->height ) : 0 ;
304298}
305299
306300sqInt
307301CameraGetFrame (sqInt cameraNum, unsigned char *buf, sqInt pixelCount)
308302{
309- if (cameraNum<1 || cameraNum>8 ) return -1 ;
310- SqueakVideoGrabber *this = grabbers[cameraNum-1 ];
311- if (!this)
303+ if (cameraNum<1 || cameraNum>8 )
304+ return -1 ;
305+ SqueakVideoGrabber *grabber = grabbers[cameraNum-1 ];
306+ if (!grabber)
312307 return -1 ;
313- if (!this->firstTime ) {
314- int ourFrames = this->frameCount ;
315- this->frameCount = 0 ;
316- memcpy (buf, this->pixels , pixelCount * 4 );
308+ if (!grabber->firstTime ) {
309+ int ourFrames = grabber->frameCount ;
310+ #define min (a,b ) ((a)<=(b)?(a):(b))
311+ long actualPixelCount = grabber->width * grabber->height ;
312+ memcpy (buf, grabber->pixels , min (pixelCount,actualPixelCount) * 4 );
313+ grabber->frameCount = 0 ;
317314 return ourFrames;
318315 }
319316 return 0 ;
0 commit comments