| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/video/capture/mac/video_capture_device_mac.h" | 5 #include "media/video/capture/mac/video_capture_device_mac.h" |
| 6 | 6 |
| 7 #import <QTKit/QTKit.h> | 7 #import <QTKit/QTKit.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/time.h" | 10 #include "base/time.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 } | 49 } |
| 50 | 50 |
| 51 namespace media { | 51 namespace media { |
| 52 | 52 |
| 53 void VideoCaptureDevice::GetDeviceNames(Names* device_names) { | 53 void VideoCaptureDevice::GetDeviceNames(Names* device_names) { |
| 54 // Loop through all available devices and add to |device_names|. | 54 // Loop through all available devices and add to |device_names|. |
| 55 device_names->clear(); | 55 device_names->clear(); |
| 56 | 56 |
| 57 NSDictionary* capture_devices = [VideoCaptureDeviceQTKit deviceNames]; | 57 NSDictionary* capture_devices = [VideoCaptureDeviceQTKit deviceNames]; |
| 58 for (NSString* key in capture_devices) { | 58 for (NSString* key in capture_devices) { |
| 59 Name name; | 59 Name name([[capture_devices valueForKey:key] UTF8String], |
| 60 name.device_name = [[capture_devices valueForKey:key] UTF8String]; | 60 [key UTF8String]); |
| 61 name.unique_id = [key UTF8String]; | |
| 62 device_names->push_back(name); | 61 device_names->push_back(name); |
| 63 } | 62 } |
| 64 } | 63 } |
| 65 | 64 |
| 66 VideoCaptureDevice* VideoCaptureDevice::Create(const Name& device_name) { | 65 VideoCaptureDevice* VideoCaptureDevice::Create(const Name& device_name) { |
| 67 VideoCaptureDeviceMac* capture_device = | 66 VideoCaptureDeviceMac* capture_device = |
| 68 new VideoCaptureDeviceMac(device_name); | 67 new VideoCaptureDeviceMac(device_name); |
| 69 if (!capture_device->Init()) { | 68 if (!capture_device->Init()) { |
| 70 LOG(ERROR) << "Could not initialize VideoCaptureDevice."; | 69 LOG(ERROR) << "Could not initialize VideoCaptureDevice."; |
| 71 delete capture_device; | 70 delete capture_device; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 91 return; | 90 return; |
| 92 } | 91 } |
| 93 | 92 |
| 94 // QTKit can scale captured frame to any size requested, which would lead to | 93 // QTKit can scale captured frame to any size requested, which would lead to |
| 95 // undesired aspect ratio change. Tries to open the camera with a natively | 94 // undesired aspect ratio change. Tries to open the camera with a natively |
| 96 // supported format and let the client to crop/pad the captured frames. | 95 // supported format and let the client to crop/pad the captured frames. |
| 97 GetBestMatchSupportedResolution(&width, &height); | 96 GetBestMatchSupportedResolution(&width, &height); |
| 98 | 97 |
| 99 observer_ = observer; | 98 observer_ = observer; |
| 100 NSString* deviceId = | 99 NSString* deviceId = |
| 101 [NSString stringWithUTF8String:device_name_.unique_id.c_str()]; | 100 [NSString stringWithUTF8String:device_name_.id().c_str()]; |
| 102 | 101 |
| 103 [capture_device_ setFrameReceiver:this]; | 102 [capture_device_ setFrameReceiver:this]; |
| 104 | 103 |
| 105 if (![capture_device_ setCaptureDevice:deviceId]) { | 104 if (![capture_device_ setCaptureDevice:deviceId]) { |
| 106 SetErrorState("Could not open capture device."); | 105 SetErrorState("Could not open capture device."); |
| 107 return; | 106 return; |
| 108 } | 107 } |
| 109 if (frame_rate < kMinFrameRate) | 108 if (frame_rate < kMinFrameRate) |
| 110 frame_rate = kMinFrameRate; | 109 frame_rate = kMinFrameRate; |
| 111 else if (frame_rate > kMaxFrameRate) | 110 else if (frame_rate > kMaxFrameRate) |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 | 159 |
| 161 const VideoCaptureDevice::Name& VideoCaptureDeviceMac::device_name() { | 160 const VideoCaptureDevice::Name& VideoCaptureDeviceMac::device_name() { |
| 162 return device_name_; | 161 return device_name_; |
| 163 } | 162 } |
| 164 | 163 |
| 165 bool VideoCaptureDeviceMac::Init() { | 164 bool VideoCaptureDeviceMac::Init() { |
| 166 DCHECK_EQ(state_, kNotInitialized); | 165 DCHECK_EQ(state_, kNotInitialized); |
| 167 | 166 |
| 168 Names device_names; | 167 Names device_names; |
| 169 GetDeviceNames(&device_names); | 168 GetDeviceNames(&device_names); |
| 170 for (Names::iterator it = device_names.begin(); | 169 Name* found = device_names.FindById(device_name_.id()); |
| 171 it != device_names.end(); | 170 if (!found) |
| 172 ++it) { | 171 return false; |
| 173 if (device_name_.unique_id == it->unique_id) { | 172 |
| 174 capture_device_ = | 173 capture_device_ = |
| 175 [[VideoCaptureDeviceQTKit alloc] initWithFrameReceiver:this]; | 174 [[VideoCaptureDeviceQTKit alloc] initWithFrameReceiver:this]; |
| 176 if (!capture_device_) { | 175 if (!capture_device_) |
| 177 return false; | 176 return false; |
| 178 } | 177 |
| 179 state_ = kIdle; | 178 state_ = kIdle; |
| 180 return true; | 179 return true; |
| 181 } | |
| 182 } | |
| 183 return false; | |
| 184 } | 180 } |
| 185 | 181 |
| 186 void VideoCaptureDeviceMac::ReceiveFrame( | 182 void VideoCaptureDeviceMac::ReceiveFrame( |
| 187 const uint8* video_frame, | 183 const uint8* video_frame, |
| 188 int video_frame_length, | 184 int video_frame_length, |
| 189 const VideoCaptureCapability& frame_info) { | 185 const VideoCaptureCapability& frame_info) { |
| 190 observer_->OnIncomingCapturedFrame( | 186 observer_->OnIncomingCapturedFrame( |
| 191 video_frame, video_frame_length, base::Time::Now(), 0, false, false); | 187 video_frame, video_frame_length, base::Time::Now(), 0, false, false); |
| 192 } | 188 } |
| 193 | 189 |
| 194 void VideoCaptureDeviceMac::SetErrorState(const std::string& reason) { | 190 void VideoCaptureDeviceMac::SetErrorState(const std::string& reason) { |
| 195 DLOG(ERROR) << reason; | 191 DLOG(ERROR) << reason; |
| 196 state_ = kError; | 192 state_ = kError; |
| 197 observer_->OnError(); | 193 observer_->OnError(); |
| 198 } | 194 } |
| 199 | 195 |
| 200 } // namespace media | 196 } // namespace media |
| OLD | NEW |