Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(210)

Side by Side Diff: media/video/capture/mac/video_capture_device_mac_qtkit.mm

Issue 8177008: Adding VideoCaptureDevice for Mac. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #import "media/video/capture/mac/video_capture_device_mac_qtkit.h"
6
7 #import <QTKit/QTKit.h>
8
9 @implementation VideoCaptureDeviceMacQTKit
10
11 - (id)init {
12 if (self = [super init]) {
13 pool_ = [[NSAutoreleasePool alloc]init];
dmac 2011/10/06 23:36:22 space between ] and init
dmac 2011/10/06 23:36:22 do you really need an autoreleasepool? please expl
mflodman_chromium_OOO 2011/10/07 13:03:51 Done.
mflodman_chromium_OOO 2011/10/07 13:03:51 Removed. One of my object-c misunderstandings.
14 frameRate_ = 0;
15 frameWidth_ = 0;
dmac 2011/10/06 23:36:22 no need to set things to zero/nil. This happens au
mflodman_chromium_OOO 2011/10/07 13:03:51 Done.
16 frameHeight_ = 0;
17 captureSession_ = nil;
18 captureDecompressedOutput_ = nil;
19 captureDeviceInput_ = nil;
20 }
21 return self;
22 }
23
24 - (void)dealloc {
25 if(captureSession_) {
dmac 2011/10/06 23:36:22 no need to check if these are nil
mflodman_chromium_OOO 2011/10/07 13:03:51 Done.
26 [captureSession_ release];
27 captureSession_ = nil;
28 }
29 if (captureDecompressedOutput_) {
30 [captureDecompressedOutput_ release];
31 captureDecompressedOutput_ = nil;
32 }
33 if (captureDeviceInput_) {
34 [captureDeviceInput_ release];
35 captureDeviceInput_ = nil;
36 }
37 [pool_ drain];
38 [super dealloc];
39 }
40
41 #pragma mark Class methods
42
43 + (void) deviceNames:(NSArray *) deviceList {
dmac 2011/10/06 23:36:22 no space between ) and device (in both cases)
mflodman_chromium_OOO 2011/10/07 13:03:51 Modified.
44 [deviceList initWithArray:[QTCaptureDevice
dmac 2011/10/06 23:36:22 This does not do what you think it does. My guess
mflodman_chromium_OOO 2011/10/07 13:03:51 Done.
45 inputDevicesWithMediaType:QTMediaTypeVideo]];
46 }
47
48 #pragma mark Public methods
49
50 - (void)registerReceiver:(media::VideoCaptureDeviceMac *)frameReceiver {
dmac 2011/10/06 23:36:22 should this be "setFrameReceiver" since you can on
mflodman_chromium_OOO 2011/10/07 13:03:51 Good point, changed.
51 frameReceiver_ = frameReceiver;
52 }
53
54 - (BOOL)setCaptureDevice:(const char *)name {
dmac 2011/10/06 23:36:22 so on one side of the interface you take an NSStri
mflodman_chromium_OOO 2011/10/07 13:03:51 Done.
55 if (captureDeviceInput_) {
56 // A device is already set.
57 return NO;
58 }
59
60 // Find the requested device, open it and set as input.
61 NSArray* captureDevices;
62 captureDevices = [[[NSArray alloc] initWithArray:
dmac 2011/10/06 23:36:22 There's a couple of bugs here, but can I suggest a
mflodman_chromium_OOO 2011/10/07 13:03:51 Changed to the suggested code, almost... I changed
63 [QTCaptureDevice inputDevicesWithMediaType:QTMediaTypeVideo]]
64 autorelease];
65
66 if ([captureDevices count] == 0) {
67 return NO;
68 }
69
70 for(NSUInteger index = 0; index < captureDevices.count; ++index) {
71 NSError* error;
72 QTCaptureDevice* captureDevice =
73 (QTCaptureDevice*)[captureDevices objectAtIndex:index];
74
75 char captureDeviceName[1024];
76 [[captureDevice localizedDisplayName] getCString:captureDeviceName
77 maxLength:1024
78 encoding:NSUTF8StringEncoding];
79 if (!strcmp(captureDeviceName, name)) {
80 if (![captureDevice open:&error]) {
81 return NO;
82 }
83 captureDeviceInput_ = [QTCaptureDeviceInput alloc];
84 [captureDeviceInput_ initWithDevice:captureDevice];
85 captureSession_ = [[QTCaptureSession alloc] init];
86 captureDecompressedOutput_ =
87 [[QTCaptureDecompressedVideoOutput alloc] init];
88 [captureDecompressedOutput_ setDelegate:self];
89 return YES;
90 }
91 }
92 return NO;
93 }
94
95 - (void)removeCaptureDevice {
96 if ([[captureSession_ inputs] count] > 0) {
97 // The device is still running.
98 [self stopCapture];
99 }
100 if (captureDecompressedOutput_) {
dmac 2011/10/06 23:36:22 no need to check. Just call release on it. Same w
mflodman_chromium_OOO 2011/10/07 13:03:51 Done.
101 [captureDecompressedOutput_ release];
102 captureDecompressedOutput_ = nil;
103 }
104 if(captureSession_) {
105 [captureSession_ release];
106 captureSession_ = nil;
107 }
108 if (captureDeviceInput_) {
109 [captureDeviceInput_ release];
110 captureDeviceInput_ = nil;
111 }
112 }
113
114 - (BOOL)setCaptureHeight:(int)height
115 AndWidth:(int)width
116 AndFrameRate:(int)frameRate {
117 // Verify an input device has been set.
118 if (!captureDeviceInput_) {
119 return NO;
120 }
121
122 frameWidth_ = width;
123 frameHeight_ = height;
124 frameRate_ = frameRate;
125
126 // Set up desired output properties.
127 NSDictionary* captureDictionary =
128 [NSDictionary dictionaryWithObjectsAndKeys:
129 [NSNumber numberWithDouble:frameWidth_],
130 (id)kCVPixelBufferWidthKey,
131 [NSNumber numberWithDouble:frameHeight_],
132 (id)kCVPixelBufferHeightKey,
133 [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA],
134 (id)kCVPixelBufferPixelFormatTypeKey,
135 nil];
136 [captureDecompressedOutput_ setPixelBufferAttributes:captureDictionary];
137
138 // Connect the output with the session.
139 NSError* error;
140 if (![captureSession_ addOutput:captureDecompressedOutput_ error:&error]) {
141 return NO;
dmac 2011/10/06 23:36:22 log the error?
mflodman_chromium_OOO 2011/10/07 13:03:51 Done.
142 }
143 return YES;
144 }
145
146 - (BOOL)startCapture {
147 if ([[captureSession_ outputs] count] == 0) {
148 // Capture properties not set.
149 return NO;
150 }
151 if ([[captureSession_ inputs] count] == 0) {
152 NSError* error;
153 if (![captureSession_ addInput:captureDeviceInput_ error:&error]) {
154 return NO;
dmac 2011/10/06 23:36:22 log the error?
mflodman_chromium_OOO 2011/10/07 13:03:51 Done.
155 }
156 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantFuture]];
dmac 2011/10/06 23:36:22 what breaks out of this runloop?
mflodman_chromium_OOO 2011/10/07 13:03:51 The runloop was originally from sample, which I pr
157 [captureSession_ startRunning];
158 }
159 return YES;
160 }
161
162 - (void)stopCapture {
163 if ([[captureSession_ inputs] count] > 0) {
164 [captureSession_ removeInput:captureDeviceInput_];
165 [captureSession_ stopRunning];
166 }
167 }
168
169 // |captureOutput| is called by the capture device to deliver a new frame.
170 - (void)captureOutput:(QTCaptureOutput *)captureOutput
171 didOutputVideoFrame:(CVImageBufferRef)videoFrame
172 withSampleBuffer:(QTSampleBuffer *)sampleBuffer
173 fromConnection:(QTCaptureConnection *)connection {
174
175 // Lock the frame and calculate frame size.
176 const int LOCK_FLAGS = 0;
177 CVPixelBufferLockBaseAddress(videoFrame, LOCK_FLAGS);
178 void* baseAddress = CVPixelBufferGetBaseAddress(videoFrame);
179 size_t bytesPerRow = CVPixelBufferGetBytesPerRow(videoFrame);
180 int frameHeight = CVPixelBufferGetHeight(videoFrame);
181
182 if(frameReceiver_) {
dmac 2011/10/06 23:36:22 do you want to check frameReceiver_ before doing a
mflodman_chromium_OOO 2011/10/07 13:03:51 Good point, changed.
183 int frameSize = bytesPerRow * frameHeight;
184 CVBufferRetain(videoFrame);
dmac 2011/10/06 23:36:22 why do feel you need to retain and release the vid
mflodman_chromium_OOO 2011/10/07 13:03:51 It was to make sure videoFrame was valid during th
185 media::VideoCaptureDevice::Capability captureCapability;
186 captureCapability.width = frameWidth_;
187 captureCapability.height = frameHeight_;
188 captureCapability.frame_rate = frameRate_;
189 captureCapability.color = media::VideoCaptureDevice::kARGB;
190
191 frameReceiver_->IncomingFrame(baseAddress, frameSize, captureCapability);
192
193 CVBufferRelease(videoFrame);
194 }
195 CVPixelBufferUnlockBaseAddress(videoFrame, LOCK_FLAGS);
196 }
197
198 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698