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

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: Test change: PostTask instead of PostDelayedTask. 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 #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 #pragma mark Class methods
16
17 + (NSArray *)deviceNames {
18 return [QTCaptureDevice inputDevicesWithMediaType:QTMediaTypeVideo];
19 }
20
21 #pragma mark Public methods
22
23 - (id)initWithFrameReceiver:(media::VideoCaptureDeviceMac *)frameReceiver {
24 self = [super init];
25 if (self) {
26 frameReceiver_ = frameReceiver;
27 }
28 return self;
29 }
30
31 - (void)dealloc {
32 [captureSession_ release];
33 [captureDeviceInput_ release];
34 [super dealloc];
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 << [[error localizedDescription] UTF8String];
59 return NO;
60 }
61 captureDeviceInput_ = [[QTCaptureDeviceInput alloc] initWithDevice:device];
62 captureSession_ = [[QTCaptureSession alloc] init];
63
64 QTCaptureDecompressedVideoOutput *captureDecompressedOutput =
65 [[[QTCaptureDecompressedVideoOutput alloc] init] autorelease];
66 [captureDecompressedOutput setDelegate:self];
67 if (![captureSession_ addOutput:captureDecompressedOutput error:&error]) {
68 DLOG(ERROR) << "Could not connect video capture output."
69 << [[error localizedDescription] UTF8String];
70 return NO;
71 }
72 return YES;
73 } else {
74 // Remove the previously set capture device.
75 if (!captureDeviceInput_) {
76 DLOG(ERROR) << "No video capture device set.";
77 return YES;
78 }
79 if ([[captureSession_ inputs] count] > 0) {
80 // The device is still running.
81 [self stopCapture];
82 }
83 [captureSession_ release];
84 captureSession_ = nil;
85 [captureDeviceInput_ release];
86 captureDeviceInput_ = nil;
87 return YES;
88 }
89 }
90
91 - (BOOL)setCaptureHeight:(int)height width:(int)width frameRate:(int)frameRate {
92 if (!captureDeviceInput_) {
93 DLOG(ERROR) << "No video capture device set.";
94 return NO;
95 }
96 if ([[captureSession_ outputs] count] != 1) {
97 DLOG(ERROR) << "Video capture capabilities already set.";
98 return NO;
99 }
100
101 frameWidth_ = width;
102 frameHeight_ = height;
103 frameRate_ = frameRate;
104
105 // Set up desired output properties.
106 NSDictionary *captureDictionary =
107 [NSDictionary dictionaryWithObjectsAndKeys:
108 [NSNumber numberWithDouble:frameWidth_],
109 (id)kCVPixelBufferWidthKey,
110 [NSNumber numberWithDouble:frameHeight_],
111 (id)kCVPixelBufferHeightKey,
112 [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA],
113 (id)kCVPixelBufferPixelFormatTypeKey,
114 nil];
115 [[[captureSession_ outputs] objectAtIndex:0]
116 setPixelBufferAttributes:captureDictionary];
117 return YES;
118 }
119
120 - (BOOL)startCapture {
121 if ([[captureSession_ outputs] count] == 0) {
122 // Capture properties not set.
123 DLOG(ERROR) << "Video capture device not initialized.";
124 return NO;
125 }
126 if ([[captureSession_ inputs] count] == 0) {
127 NSError *error;
128 if (![captureSession_ addInput:captureDeviceInput_ error:&error]) {
129 DLOG(ERROR) << "Could not connect video capture device."
130 << [[error localizedDescription] UTF8String];
131 return NO;
132 }
133 [captureSession_ startRunning];
134 }
135 return YES;
136 }
137
138 - (void)stopCapture {
139 if ([[captureSession_ inputs] count] == 1) {
140 [captureSession_ removeInput:captureDeviceInput_];
141 [captureSession_ stopRunning];
142 }
143 }
144
145 // |captureOutput| is called by the capture device to deliver a new frame.
146 - (void)captureOutput:(QTCaptureOutput *)captureOutput
147 didOutputVideoFrame:(CVImageBufferRef)videoFrame
148 withSampleBuffer:(QTSampleBuffer *)sampleBuffer
149 fromConnection:(QTCaptureConnection *)connection {
150 if(!frameReceiver_) {
151 return;
152 }
153
154 // Lock the frame and calculate frame size.
155 const int kLockFlags = 0;
156 if (CVPixelBufferLockBaseAddress(videoFrame, kLockFlags)
157 == kCVReturnSuccess) {
158 void *baseAddress = CVPixelBufferGetBaseAddress(videoFrame);
159 size_t bytesPerRow = CVPixelBufferGetBytesPerRow(videoFrame);
160 int frameHeight = CVPixelBufferGetHeight(videoFrame);
161 int frameSize = bytesPerRow * frameHeight;
162 media::VideoCaptureDevice::Capability captureCapability;
163 captureCapability.width = frameWidth_;
164 captureCapability.height = frameHeight_;
165 captureCapability.frame_rate = frameRate_;
166 captureCapability.color = media::VideoCaptureDevice::kARGB;
167
168 // Deliver the captured video frame.
169 frameReceiver_->ReceiveFrame(static_cast<UInt8*>(baseAddress), frameSize,
170 captureCapability);
171
172 CVPixelBufferUnlockBaseAddress(videoFrame, kLockFlags);
173 }
174 }
175
176 @end
OLDNEW
« no previous file with comments | « media/video/capture/mac/video_capture_device_mac_qtkit.h ('k') | media/video/capture/video_capture_device_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698