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 #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 frameReceiver_ = frameReceiver; | |
|
dmac
2011/10/17 17:02:29
do
self = [super init];
if (self) {
frameRecei
mflodman_chromium_OOO
2011/10/17 18:40:38
Done.
| |
| 25 return [super init]; | |
| 26 } | |
| 27 | |
| 28 - (void)dealloc { | |
| 29 [captureSession_ release]; | |
| 30 [captureDeviceInput_ release]; | |
| 31 [super dealloc]; | |
| 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 << [[error localizedDescription] UTF8String]; | |
| 56 return NO; | |
| 57 } | |
| 58 captureDeviceInput_ = [[QTCaptureDeviceInput alloc] initWithDevice:device]; | |
| 59 captureSession_ = [[QTCaptureSession alloc] init]; | |
| 60 | |
| 61 QTCaptureDecompressedVideoOutput *captureDecompressedOutput = | |
| 62 [[[QTCaptureDecompressedVideoOutput alloc] init] autorelease]; | |
| 63 [captureDecompressedOutput setDelegate:self]; | |
| 64 if (![captureSession_ addOutput:captureDecompressedOutput error:&error]) { | |
| 65 DLOG(ERROR) << "Could not connect video capture output." | |
| 66 << [[error localizedDescription] UTF8String]; | |
| 67 return NO; | |
| 68 } | |
| 69 return YES; | |
| 70 } else { | |
| 71 // Remove the previously set capture device. | |
| 72 if (!captureDeviceInput_) { | |
| 73 DLOG(ERROR) << "No video capture device set."; | |
| 74 return YES; | |
| 75 } | |
| 76 if ([[captureSession_ inputs] count] > 0) { | |
| 77 // The device is still running. | |
| 78 [self stopCapture]; | |
| 79 } | |
| 80 [captureSession_ release]; | |
| 81 captureSession_ = nil; | |
| 82 [captureDeviceInput_ release]; | |
| 83 captureDeviceInput_ = nil; | |
| 84 return YES; | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 - (BOOL)setCaptureHeight:(int)height width:(int)width frameRate:(int)frameRate { | |
| 89 if (!captureDeviceInput_) { | |
| 90 DLOG(ERROR) << "No video capture device set."; | |
| 91 return NO; | |
| 92 } | |
| 93 if ([[captureSession_ outputs] count] != 1) { | |
| 94 DLOG(ERROR) << "Video capture capabilities already set."; | |
| 95 return NO; | |
| 96 } | |
| 97 | |
| 98 frameWidth_ = width; | |
| 99 frameHeight_ = height; | |
| 100 frameRate_ = frameRate; | |
| 101 | |
| 102 // Set up desired output properties. | |
| 103 NSDictionary *captureDictionary = | |
| 104 [NSDictionary dictionaryWithObjectsAndKeys: | |
| 105 [NSNumber numberWithDouble:frameWidth_], | |
| 106 (id)kCVPixelBufferWidthKey, | |
| 107 [NSNumber numberWithDouble:frameHeight_], | |
| 108 (id)kCVPixelBufferHeightKey, | |
| 109 [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA], | |
| 110 (id)kCVPixelBufferPixelFormatTypeKey, | |
| 111 nil]; | |
| 112 [[[captureSession_ outputs] objectAtIndex:0] | |
| 113 setPixelBufferAttributes:captureDictionary]; | |
| 114 return YES; | |
| 115 } | |
| 116 | |
| 117 - (BOOL)startCapture { | |
| 118 if ([[captureSession_ outputs] count] == 0) { | |
| 119 // Capture properties not set. | |
| 120 DLOG(ERROR) << "Video capture device not initialized."; | |
| 121 return NO; | |
| 122 } | |
| 123 if ([[captureSession_ inputs] count] == 0) { | |
| 124 NSError *error; | |
| 125 if (![captureSession_ addInput:captureDeviceInput_ error:&error]) { | |
| 126 DLOG(ERROR) << "Could not connect video capture device." | |
| 127 << [[error localizedDescription] UTF8String]; | |
| 128 return NO; | |
| 129 } | |
| 130 [captureSession_ startRunning]; | |
| 131 } | |
| 132 return YES; | |
| 133 } | |
| 134 | |
| 135 - (void)stopCapture { | |
| 136 if ([[captureSession_ inputs] count] == 1) { | |
| 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 if(!frameReceiver_) { | |
| 148 return; | |
| 149 } | |
| 150 | |
| 151 // Lock the frame and calculate frame size. | |
| 152 const int kLockFlags = 0; | |
| 153 if (CVPixelBufferLockBaseAddress(videoFrame, kLockFlags) | |
| 154 == kCVReturnSuccess) { | |
| 155 void *baseAddress = CVPixelBufferGetBaseAddress(videoFrame); | |
| 156 size_t bytesPerRow = CVPixelBufferGetBytesPerRow(videoFrame); | |
| 157 int frameHeight = CVPixelBufferGetHeight(videoFrame); | |
| 158 int frameSize = bytesPerRow * frameHeight; | |
| 159 media::VideoCaptureDevice::Capability captureCapability; | |
| 160 captureCapability.width = frameWidth_; | |
| 161 captureCapability.height = frameHeight_; | |
| 162 captureCapability.frame_rate = frameRate_; | |
| 163 captureCapability.color = media::VideoCaptureDevice::kARGB; | |
| 164 | |
| 165 // Deliver the captured video frame. | |
| 166 frameReceiver_->ReceiveFrame(static_cast<UInt8*>(baseAddress), frameSize, | |
| 167 captureCapability); | |
| 168 | |
| 169 CVPixelBufferUnlockBaseAddress(videoFrame, kLockFlags); | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 @end | |
| OLD | NEW |