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