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 #include "media/video/capture/mac/video_capture_device_mac.h" |
| 6 |
| 7 #import <QTKit/QTKit.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/time.h" |
| 11 #include "media/video/capture/mac/video_capture_device_mac_qtkit.h" |
| 12 |
| 13 namespace media { |
| 14 |
| 15 void VideoCaptureDevice::GetDeviceNames(Names* device_names) { |
| 16 // Loop through all available devices and add to |device_names|. |
| 17 device_names->clear(); |
| 18 // TODO(mflodman) Return name and id as NSArray* instead of QTCaptureDevice*. |
| 19 for (QTCaptureDevice* device in [VideoCaptureDeviceMacQTKit deviceNames]) { |
| 20 Name name; |
| 21 NSString* qt_device_name = [device localizedDisplayName]; |
| 22 name.device_name = [qt_device_name UTF8String]; |
| 23 NSString* qt_unique_id = [device uniqueID]; |
| 24 name.unique_id = [qt_unique_id UTF8String]; |
| 25 device_names->push_back(name); |
| 26 } |
| 27 } |
| 28 |
| 29 VideoCaptureDevice* VideoCaptureDevice::Create(const Name& device_name) { |
| 30 VideoCaptureDeviceMac* capture_device = |
| 31 new VideoCaptureDeviceMac(device_name); |
| 32 if (!capture_device->Init()) { |
| 33 LOG(ERROR) << "Could not initialize VideoCaptureDevice."; |
| 34 delete capture_device; |
| 35 return NULL; |
| 36 } |
| 37 return capture_device; |
| 38 } |
| 39 |
| 40 VideoCaptureDeviceMac::VideoCaptureDeviceMac(const Name& device_name) |
| 41 : device_name_(device_name), |
| 42 observer_(NULL), |
| 43 state_(kNotInitialized), |
| 44 capture_device_(NULL) { |
| 45 } |
| 46 |
| 47 VideoCaptureDeviceMac::~VideoCaptureDeviceMac() { |
| 48 [capture_device_ release]; |
| 49 } |
| 50 |
| 51 void VideoCaptureDeviceMac::Allocate(int width, int height, int frame_rate, |
| 52 EventHandler* observer) { |
| 53 if (state_ != kIdle) { |
| 54 return; |
| 55 } |
| 56 observer_ = observer; |
| 57 NSString* deviceId = |
| 58 [NSString stringWithUTF8String:device_name_.unique_id.c_str()]; |
| 59 |
| 60 if (![capture_device_ setCaptureDevice:deviceId]) { |
| 61 SetErrorState("Could not open capture device."); |
| 62 return; |
| 63 } |
| 64 if ([capture_device_ setCaptureHeight:height |
| 65 width:width |
| 66 frameRate:frame_rate] == NO) { |
| 67 SetErrorState("Could not configure capture device."); |
| 68 return; |
| 69 } |
| 70 |
| 71 state_ = kAllocated; |
| 72 Capability current_settings; |
| 73 current_settings.color = kARGB; |
| 74 current_settings.width = width; |
| 75 current_settings.height = height; |
| 76 current_settings.frame_rate = frame_rate; |
| 77 |
| 78 observer_->OnFrameInfo(current_settings); |
| 79 } |
| 80 |
| 81 void VideoCaptureDeviceMac::Start() { |
| 82 DCHECK_EQ(state_, kAllocated); |
| 83 if (![capture_device_ startCapture]) { |
| 84 SetErrorState("Could not start capture device."); |
| 85 return; |
| 86 } |
| 87 state_ = kCapturing; |
| 88 } |
| 89 |
| 90 void VideoCaptureDeviceMac::Stop() { |
| 91 DCHECK_EQ(state_, kCapturing); |
| 92 [capture_device_ stopCapture]; |
| 93 state_ = kAllocated; |
| 94 } |
| 95 |
| 96 void VideoCaptureDeviceMac::DeAllocate() { |
| 97 if (state_ != kAllocated && state_ != kCapturing) { |
| 98 return; |
| 99 } |
| 100 if (state_ == kCapturing) { |
| 101 [capture_device_ stopCapture]; |
| 102 } |
| 103 [capture_device_ setCaptureDevice:nil]; |
| 104 state_ = kIdle; |
| 105 } |
| 106 |
| 107 const VideoCaptureDevice::Name& VideoCaptureDeviceMac::device_name() { |
| 108 return device_name_; |
| 109 } |
| 110 |
| 111 bool VideoCaptureDeviceMac::Init() { |
| 112 DCHECK_EQ(state_, kNotInitialized); |
| 113 |
| 114 Names device_names; |
| 115 GetDeviceNames(&device_names); |
| 116 for (Names::iterator it = device_names.begin(); |
| 117 it != device_names.end(); |
| 118 ++it) { |
| 119 if (device_name_.unique_id == it->unique_id) { |
| 120 capture_device_ = |
| 121 [[VideoCaptureDeviceMacQTKit alloc] initWithFrameReceiver:this]; |
| 122 if (!capture_device_) { |
| 123 return false; |
| 124 } |
| 125 state_ = kIdle; |
| 126 return true; |
| 127 } |
| 128 } |
| 129 return false; |
| 130 } |
| 131 |
| 132 void VideoCaptureDeviceMac::ReceiveFrame(const uint8* video_frame, |
| 133 int video_frame_length, |
| 134 const Capability& frame_info) { |
| 135 observer_->OnIncomingCapturedFrame(video_frame, video_frame_length, |
| 136 base::Time::Now()); |
| 137 } |
| 138 |
| 139 void VideoCaptureDeviceMac::SetErrorState(const std::string& reason) { |
| 140 DLOG(ERROR) << reason; |
| 141 state_ = kError; |
| 142 observer_->OnError(); |
| 143 } |
| 144 |
| 145 } // namespace media |
OLD | NEW |