OLD | NEW |
---|---|
(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 #include "base/logging.h" | |
10 #include "media/video/capture/mac/video_capture_device_mac.h" | |
11 #include "media/video/capture/video_capture_device.h" | |
12 | |
13 @implementation VideoCaptureDeviceMacQTKit | |
14 | |
15 - (id)init { | |
16 return [super init]; | |
dmac
2011/10/11 00:25:47
no need to have an empty init like this.
mflodman_chromium_OOO
2011/10/12 19:21:32
Changed to include frameObserver registration.
| |
17 } | |
18 | |
19 - (void)dealloc { | |
20 [captureSession_ release]; | |
21 [captureDeviceInput_ release]; | |
22 [super dealloc]; | |
23 } | |
24 | |
25 #pragma mark Class methods | |
26 | |
27 + (NSArray *)deviceNames { | |
28 return [QTCaptureDevice inputDevicesWithMediaType:QTMediaTypeVideo]; | |
29 } | |
30 | |
31 #pragma mark Public methods | |
32 | |
33 - (void)setFrameReceiver:(media::VideoCaptureDeviceMac *)frameReceiver { | |
34 frameReceiver_ = frameReceiver; | |
35 } | |
36 | |
37 - (BOOL)setCaptureDevice:(NSString *)deviceId { | |
38 if (deviceId) { | |
39 // Set the capture device. | |
40 if (captureDeviceInput_) { | |
41 DLOG(ERROR) << "Video capture device already set."; | |
42 return NO; | |
43 } | |
44 | |
45 NSArray *captureDevices = | |
46 [QTCaptureDevice inputDevicesWithMediaType:QTMediaTypeVideo]; | |
47 NSArray *captureDevicesNames = | |
48 [captureDevices valueForKey:@"uniqueID"]; | |
49 NSUInteger index = [captureDevicesNames indexOfObject:deviceId]; | |
50 if (index == NSNotFound) { | |
51 DLOG(ERROR) << "Video capture device not found."; | |
52 return NO; | |
53 } | |
54 QTCaptureDevice *device = [captureDevices objectAtIndex:index]; | |
55 NSError *error; | |
56 if (![device open:&error]) { | |
57 DLOG(ERROR) << "Could not open video capture device."; | |
58 return NO; | |
59 } | |
60 captureDeviceInput_ = [[QTCaptureDeviceInput alloc] initWithDevice:device]; | |
61 captureSession_ = [[QTCaptureSession alloc] init]; | |
62 | |
63 QTCaptureDecompressedVideoOutput *captureDecompressedOutput = | |
64 [[[QTCaptureDecompressedVideoOutput alloc] init] autorelease]; | |
65 [captureDecompressedOutput setDelegate:self]; | |
66 if (![captureSession_ addOutput:captureDecompressedOutput error:&error]) { | |
67 DLOG(ERROR) << "Could not connect video capture output."; | |
68 return NO; | |
69 } | |
70 return YES; | |
71 } else { | |
72 // Remove the previously set capture device. | |
73 if (!captureDeviceInput_) { | |
74 DLOG(ERROR) << "No video capture device set."; | |
75 return YES; | |
76 } | |
77 if ([[captureSession_ inputs] count] > 0) { | |
78 // The device is still running. | |
79 [self stopCapture]; | |
80 } | |
81 [captureSession_ release]; | |
82 captureSession_ = nil; | |
83 [captureDeviceInput_ release]; | |
84 captureDeviceInput_ = nil; | |
85 return YES; | |
86 } | |
87 } | |
88 | |
89 - (BOOL)setCaptureHeight:(int)height:(int)width:(int)frameRate { | |
90 if (!captureDeviceInput_) { | |
91 DLOG(ERROR) << "No video capture device set."; | |
92 return NO; | |
93 } | |
94 if ([[captureSession_ outputs] count] != 1) { | |
95 DLOG(ERROR) << "Video capture capabilities already set."; | |
96 return NO; | |
97 } | |
98 | |
99 frameWidth_ = width; | |
100 frameHeight_ = height; | |
101 frameRate_ = frameRate; | |
102 | |
103 // Set up desired output properties. | |
104 NSDictionary *captureDictionary = | |
105 [NSDictionary dictionaryWithObjectsAndKeys: | |
106 [NSNumber numberWithDouble:frameWidth_], | |
107 (id)kCVPixelBufferWidthKey, | |
108 [NSNumber numberWithDouble:frameHeight_], | |
109 (id)kCVPixelBufferHeightKey, | |
110 [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA], | |
111 (id)kCVPixelBufferPixelFormatTypeKey, | |
112 nil]; | |
113 [[[captureSession_ outputs] objectAtIndex:0] | |
114 setPixelBufferAttributes:captureDictionary]; | |
115 return YES; | |
116 } | |
117 | |
118 - (BOOL)startCapture { | |
119 if ([[captureSession_ outputs] count] == 0) { | |
120 // Capture properties not set. | |
121 DLOG(ERROR) << "Video capture device not initialized."; | |
122 return NO; | |
123 } | |
124 if ([[captureSession_ inputs] count] == 0) { | |
125 NSError *error; | |
126 if (![captureSession_ addInput:captureDeviceInput_ error:&error]) { | |
127 DLOG(ERROR) << "Could not connect video capture device."; | |
128 return NO; | |
129 } | |
130 [captureSession_ startRunning]; | |
131 } | |
132 return YES; | |
133 } | |
134 | |
135 - (void)stopCapture { | |
136 if ([[captureSession_ inputs] count] > 0) { | |
dmac
2011/10/11 00:25:47
are you ever expecting the inputs to be anything o
mflodman_chromium_OOO
2011/10/12 19:21:32
Changed, good comment.
| |
137 [captureSession_ removeInput:captureDeviceInput_]; | |
138 [captureSession_ stopRunning]; | |
139 } | |
140 } | |
141 | |
142 // |captureOutput| is called by the capture device to deliver a new frame. | |
143 - (void)captureOutput:(QTCaptureOutput *)captureOutput | |
144 didOutputVideoFrame:(CVImageBufferRef)videoFrame | |
145 withSampleBuffer:(QTSampleBuffer *)sampleBuffer | |
146 fromConnection:(QTCaptureConnection *)connection { | |
147 | |
148 if(!frameReceiver_) { | |
149 return; | |
150 } | |
151 | |
152 // Lock the frame and calculate frame size. | |
153 const int LOCK_FLAGS = 0; | |
154 if (CVPixelBufferLockBaseAddress(videoFrame, LOCK_FLAGS) | |
155 == kCVReturnSuccess) { | |
156 void *baseAddress = CVPixelBufferGetBaseAddress(videoFrame); | |
157 size_t bytesPerRow = CVPixelBufferGetBytesPerRow(videoFrame); | |
158 int frameHeight = CVPixelBufferGetHeight(videoFrame); | |
159 int frameSize = bytesPerRow * frameHeight; | |
160 media::VideoCaptureDevice::Capability captureCapability; | |
161 captureCapability.width = frameWidth_; | |
162 captureCapability.height = frameHeight_; | |
163 captureCapability.frame_rate = frameRate_; | |
164 captureCapability.color = media::VideoCaptureDevice::kARGB; | |
165 | |
166 // Deliver the captured video frame. | |
167 frameReceiver_->ReceiveFrame((UInt8 *)baseAddress, frameSize, | |
168 captureCapability); | |
169 | |
170 CVPixelBufferUnlockBaseAddress(videoFrame, LOCK_FLAGS); | |
171 } | |
172 } | |
173 | |
174 @end | |
OLD | NEW |