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