From efdf778d3ff5927e23a290b33fe5336e816ca9d3 Mon Sep 17 00:00:00 2001 From: "mflodman@webrtc.org" Date: Tue, 23 Apr 2013 11:57:56 +0000 Subject: [PATCH] BUG=1351 Propose of this CL: Close the camera properly on MacOS in order to allow other apps to use it. Changes in this CL: 1. video_capture_qtkit_info_objc.mm _captureDevicesInfo is never released. I have found this memory leak using Instruments from XCode. The patch is releasing it in dealloc. 2. In video_capture_qtkit_objc.h: a) _captureDeviceName is not needed. Is allocated in the class but never used. b) I don't see the role of the NSAutoreleasePool. also if you use it you have to release it when the class is destroyed. Otherwise you will leak memory. Libjingle has for each thread a pool on mac os. 3. In video_capture_qtkit_objc.mm a) the camera is not stopped properly . See the changes from dealloc. NOTE : If you don't call [[_captureVideoDeviceInput device] close] other apps will not be able to use the camera since you are not closing your app b) Removed QTCaptureDevice* videoDevice = (QTCaptureDevice*)[_captureDevices objectAtIndex:0]; I don't know why this because the desired camera is opened in setCaptureDeviceById and can be different than position 0 in the camera array. At this moment if you have two cameras and user want to pick the one on index 1 the app also locks the one on 0 . Other changes I have done to improve (and are not in this CL): a) I have set the FPS properly to the desired. I have succeeded to reduce the CPU with 3 % doing this. The current code for setting FPS is commented in webrtc b) I have removed _rLock from the equation. I don't know if it's good or not but I hadn't understood what exactly we are trying to protect with this. Anyway in the current implementation is never released. Review URL: https://webrtc-codereview.appspot.com/1097014 Patch from Silviu Caragea . git-svn-id: http://webrtc.googlecode.com/svn/trunk@3886 4adac7df-926f-26a2-2b94-8c16560cd09d --- AUTHORS | 1 + .../qtkit/video_capture_qtkit_info_objc.mm | 3 ++ .../mac/qtkit/video_capture_qtkit_objc.h | 12 -------- .../mac/qtkit/video_capture_qtkit_objc.mm | 28 +++++++++---------- 4 files changed, 17 insertions(+), 27 deletions(-) diff --git a/AUTHORS b/AUTHORS index 6f8352933..e97d8e876 100644 --- a/AUTHORS +++ b/AUTHORS @@ -11,3 +11,4 @@ Martin Storsjo Jie Mao Anil Kumar Opera Software ASA +Silviu Caragea diff --git a/webrtc/modules/video_capture/mac/qtkit/video_capture_qtkit_info_objc.mm b/webrtc/modules/video_capture/mac/qtkit/video_capture_qtkit_info_objc.mm index 2a2a8ac87..dbc5db8d0 100644 --- a/webrtc/modules/video_capture/mac/qtkit/video_capture_qtkit_info_objc.mm +++ b/webrtc/modules/video_capture/mac/qtkit/video_capture_qtkit_info_objc.mm @@ -41,6 +41,9 @@ using namespace webrtc; /// ***** Objective-C. Similar to C++ destructor /// ***** Returns nothing - (void)dealloc { + + [_captureDevicesInfo release]; + [super dealloc]; } diff --git a/webrtc/modules/video_capture/mac/qtkit/video_capture_qtkit_objc.h b/webrtc/modules/video_capture/mac/qtkit/video_capture_qtkit_objc.h index d48dbf1c9..597ca053e 100644 --- a/webrtc/modules/video_capture/mac/qtkit/video_capture_qtkit_objc.h +++ b/webrtc/modules/video_capture/mac/qtkit/video_capture_qtkit_objc.h @@ -30,7 +30,6 @@ @interface VideoCaptureMacQTKitObjC : NSObject{ // class properties bool _capturing; - int _counter; int _frameRate; int _frameWidth; int _frameHeight; @@ -49,19 +48,8 @@ QTCaptureDecompressedVideoOutput* _captureDecompressedVideoOutput; NSArray* _captureDevices; int _captureDeviceCount; - int _captureDeviceIndex; - NSString* _captureDeviceName; char _captureDeviceNameUTF8[1024]; char _captureDeviceNameUniqueID[1024]; - char _captureDeviceNameProductID[1024]; - NSString* _key; - NSNumber* _val; - NSDictionary* _videoSettings; - NSString* _captureQuality; - - // other - NSAutoreleasePool* _pool; - } /************************************************************************** * diff --git a/webrtc/modules/video_capture/mac/qtkit/video_capture_qtkit_objc.mm b/webrtc/modules/video_capture/mac/qtkit/video_capture_qtkit_objc.mm index 48f734cc3..702daabd6 100644 --- a/webrtc/modules/video_capture/mac/qtkit/video_capture_qtkit_objc.mm +++ b/webrtc/modules/video_capture/mac/qtkit/video_capture_qtkit_objc.mm @@ -47,10 +47,21 @@ using namespace videocapturemodule; /// ***** Returns nothing - (void)dealloc { if(_captureSession) - { [_captureSession stopRunning]; - [_captureSession release]; + + if (_captureVideoDeviceInput) + { + if([[_captureVideoDeviceInput device] isOpen]) + [[_captureVideoDeviceInput device] close]; + + [_captureVideoDeviceInput release]; } + + [_captureDecompressedVideoOutput release]; + [_captureSession release]; + [_captureDevices release]; + [_rLock release]; + [super dealloc]; } @@ -279,10 +290,7 @@ using namespace videocapturemodule; return [NSNumber numberWithInt:0]; } - _pool = [[NSAutoreleasePool alloc]init]; - memset(_captureDeviceNameUTF8, 0, 1024); - _counter = 0; _framesDelivered = 0; _framesRendered = 0; _captureDeviceCount = 0; @@ -291,7 +299,6 @@ using namespace videocapturemodule; _frameRate = DEFAULT_FRAME_RATE; _frameWidth = DEFAULT_FRAME_WIDTH; _frameHeight = DEFAULT_FRAME_HEIGHT; - _captureDeviceName = [[NSString alloc] initWithFormat:@""]; _rLock = [[VideoCaptureRecursiveLock alloc] init]; _captureSession = [[QTCaptureSession alloc] init]; _captureDecompressedVideoOutput = [[QTCaptureDecompressedVideoOutput alloc] @@ -359,18 +366,9 @@ using namespace videocapturemodule; return [NSNumber numberWithInt:-1]; } - QTCaptureDevice* videoDevice = - (QTCaptureDevice*)[_captureDevices objectAtIndex:0]; - bool success = NO; NSError* error; - success = [videoDevice open:&error]; - if(!success) - { - return [NSNumber numberWithInt:-1]; - } - [_captureDecompressedVideoOutput setPixelBufferAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithDouble:_frameWidth], (id)kCVPixelBufferWidthKey,