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 |
| 11 @implementation VideoCaptureDeviceMacQTKit |
| 12 |
| 13 - (id)init { |
| 14 return [super init]; |
| 15 } |
| 16 |
| 17 - (void)dealloc { |
| 18 [captureSession_ release]; |
| 19 [captureDecompressedOutput_ release]; |
| 20 [captureDeviceInput_ release]; |
| 21 [super dealloc]; |
| 22 } |
| 23 |
| 24 #pragma mark Class methods |
| 25 |
| 26 + (NSArray *)deviceNames { |
| 27 return [QTCaptureDevice inputDevicesWithMediaType:QTMediaTypeVideo]; |
| 28 } |
| 29 |
| 30 #pragma mark Public methods |
| 31 |
| 32 - (void)setFrameReceiver:(media::VideoCaptureDeviceMac *)frameReceiver { |
| 33 frameReceiver_ = frameReceiver; |
| 34 } |
| 35 |
| 36 - (BOOL)setCaptureDevice:(NSString *)deviceId { |
| 37 if (deviceId) { |
| 38 // Set the capture device. |
| 39 if (captureDeviceInput_) { |
| 40 DLOG(ERROR) << "Video capture device already set."; |
| 41 return NO; |
| 42 } |
| 43 |
| 44 NSArray *captureDevices = |
| 45 [QTCaptureDevice inputDevicesWithMediaType:QTMediaTypeVideo]; |
| 46 NSArray *captureDevicesNames = |
| 47 [captureDevices valueForKey:@"uniqueID"]; |
| 48 NSUInteger index = [captureDevicesNames indexOfObject:deviceId]; |
| 49 if (index == NSNotFound) { |
| 50 DLOG(ERROR) << "Video capture device not found."; |
| 51 return NO; |
| 52 } |
| 53 QTCaptureDevice *device = [captureDevices objectAtIndex:index]; |
| 54 NSError *error = nil; |
| 55 if (![device open:&error]) { |
| 56 DLOG(ERROR) << "Could not open video capture device."; |
| 57 return NO; |
| 58 } |
| 59 captureDeviceInput_ = [[QTCaptureDeviceInput alloc] initWithDevice:device]; |
| 60 captureSession_ = [[QTCaptureSession alloc] init]; |
| 61 captureDecompressedOutput_ = |
| 62 [[QTCaptureDecompressedVideoOutput alloc] init]; |
| 63 [captureDecompressedOutput_ setDelegate:self]; |
| 64 return YES; |
| 65 } else { |
| 66 // Remove the previously set capture device. |
| 67 if (!captureDeviceInput_) { |
| 68 return YES; |
| 69 } |
| 70 if ([[captureSession_ inputs] count] > 0) { |
| 71 // The device is still running. |
| 72 [self stopCapture]; |
| 73 } |
| 74 [captureDecompressedOutput_ release]; |
| 75 captureDecompressedOutput_ = nil; |
| 76 [captureSession_ release]; |
| 77 captureSession_ = nil; |
| 78 [captureDeviceInput_ release]; |
| 79 captureDeviceInput_ = nil; |
| 80 return YES; |
| 81 } |
| 82 } |
| 83 |
| 84 - (BOOL)setCaptureCapabilities:(int)height:(int)width:(int)frameRate { |
| 85 if (!captureDeviceInput_) { |
| 86 DLOG(ERROR) << "No video capture device set."; |
| 87 return NO; |
| 88 } |
| 89 |
| 90 frameWidth_ = width; |
| 91 frameHeight_ = height; |
| 92 frameRate_ = frameRate; |
| 93 |
| 94 // Set up desired output properties. |
| 95 NSDictionary *captureDictionary = |
| 96 [NSDictionary dictionaryWithObjectsAndKeys: |
| 97 [NSNumber numberWithDouble:frameWidth_], |
| 98 (id)kCVPixelBufferWidthKey, |
| 99 [NSNumber numberWithDouble:frameHeight_], |
| 100 (id)kCVPixelBufferHeightKey, |
| 101 [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA], |
| 102 (id)kCVPixelBufferPixelFormatTypeKey, |
| 103 nil]; |
| 104 [captureDecompressedOutput_ setPixelBufferAttributes:captureDictionary]; |
| 105 |
| 106 // Connect the output with the session. |
| 107 NSError *error; |
| 108 if (![captureSession_ addOutput:captureDecompressedOutput_ error:&error]) { |
| 109 DLOG(ERROR) << "Could not connect video capture output."; |
| 110 return NO; |
| 111 } |
| 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."; |
| 125 return NO; |
| 126 } |
| 127 [captureSession_ startRunning]; |
| 128 } |
| 129 return YES; |
| 130 } |
| 131 |
| 132 - (void)stopCapture { |
| 133 if ([[captureSession_ inputs] count] > 0) { |
| 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 |
| 145 if(!frameReceiver_) { |
| 146 return; |
| 147 } |
| 148 |
| 149 // Lock the frame and calculate frame size. |
| 150 const int LOCK_FLAGS = 0; |
| 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_->IncomingFrame(baseAddress, frameSize, captureCapability); |
| 165 |
| 166 CVPixelBufferUnlockBaseAddress(videoFrame, LOCK_FLAGS); |
| 167 } |
| 168 } |
| 169 |
| 170 @end |
OLD | NEW |