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/capture/video/mac/video_capture_device_mac.h" | 5 #include "media/capture/video/mac/video_capture_device_mac.h" |
6 | 6 |
7 #include <IOKit/IOCFPlugIn.h> | 7 #include <IOKit/IOCFPlugIn.h> |
8 #include <IOKit/usb/IOUSBLib.h> | 8 #include <IOKit/usb/IOUSBLib.h> |
9 #include <IOKit/usb/USBSpec.h> | 9 #include <IOKit/usb/USBSpec.h> |
10 #include <stddef.h> | 10 #include <stddef.h> |
11 #include <stdint.h> | 11 #include <stdint.h> |
12 | 12 |
13 #include <limits> | 13 #include <limits> |
14 #include <utility> | 14 #include <utility> |
15 | 15 |
16 #include "base/bind.h" | 16 #include "base/bind.h" |
17 #include "base/location.h" | 17 #include "base/location.h" |
18 #include "base/logging.h" | 18 #include "base/logging.h" |
19 #include "base/mac/scoped_ioobject.h" | 19 #include "base/mac/scoped_ioobject.h" |
20 #include "base/mac/scoped_ioplugininterface.h" | 20 #include "base/mac/scoped_ioplugininterface.h" |
21 #include "base/macros.h" | 21 #include "base/macros.h" |
22 #include "base/single_thread_task_runner.h" | 22 #include "base/single_thread_task_runner.h" |
23 #include "base/strings/string_number_conversions.h" | 23 #include "base/strings/string_number_conversions.h" |
24 #include "base/thread_task_runner_handle.h" | 24 #include "base/thread_task_runner_handle.h" |
25 #include "base/time/time.h" | 25 #include "base/time/time.h" |
26 #import "media/base/mac/avfoundation_glue.h" | 26 #import "media/base/mac/avfoundation_glue.h" |
27 #include "media/base/timestamp_constants.h" | 27 #include "media/base/timestamp_constants.h" |
28 #import "media/capture/video/mac/platform_video_capturing_mac.h" | |
29 #import "media/capture/video/mac/video_capture_device_avfoundation_mac.h" | 28 #import "media/capture/video/mac/video_capture_device_avfoundation_mac.h" |
30 #include "ui/gfx/geometry/size.h" | 29 #include "ui/gfx/geometry/size.h" |
31 | 30 |
32 @implementation DeviceNameAndTransportType | 31 @implementation DeviceNameAndTransportType |
33 | 32 |
34 - (id)initWithName:(NSString*)deviceName transportType:(int32_t)transportType { | 33 - (id)initWithName:(NSString*)deviceName transportType:(int32_t)transportType { |
35 if (self = [super init]) { | 34 if (self = [super init]) { |
36 deviceName_.reset([deviceName copy]); | 35 deviceName_.reset([deviceName copy]); |
37 transportType_ = transportType; | 36 transportType_ = transportType; |
38 } | 37 } |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 : device_name_(device_name), | 315 : device_name_(device_name), |
317 task_runner_(base::ThreadTaskRunnerHandle::Get()), | 316 task_runner_(base::ThreadTaskRunnerHandle::Get()), |
318 state_(kNotInitialized), | 317 state_(kNotInitialized), |
319 capture_device_(nil), | 318 capture_device_(nil), |
320 first_timestamp_(media::kNoTimestamp()), | 319 first_timestamp_(media::kNoTimestamp()), |
321 weak_factory_(this) { | 320 weak_factory_(this) { |
322 } | 321 } |
323 | 322 |
324 VideoCaptureDeviceMac::~VideoCaptureDeviceMac() { | 323 VideoCaptureDeviceMac::~VideoCaptureDeviceMac() { |
325 DCHECK(task_runner_->BelongsToCurrentThread()); | 324 DCHECK(task_runner_->BelongsToCurrentThread()); |
326 [capture_device_ release]; | |
327 } | 325 } |
328 | 326 |
329 void VideoCaptureDeviceMac::AllocateAndStart( | 327 void VideoCaptureDeviceMac::AllocateAndStart( |
330 const VideoCaptureParams& params, | 328 const VideoCaptureParams& params, |
331 scoped_ptr<VideoCaptureDevice::Client> client) { | 329 scoped_ptr<VideoCaptureDevice::Client> client) { |
332 DCHECK(task_runner_->BelongsToCurrentThread()); | 330 DCHECK(task_runner_->BelongsToCurrentThread()); |
333 if (state_ != kIdle) { | 331 if (state_ != kIdle) { |
334 return; | 332 return; |
335 } | 333 } |
336 | 334 |
(...skipping 12 matching lines...) Expand all Loading... |
349 } | 347 } |
350 | 348 |
351 capture_format_.frame_size = params.requested_format.frame_size; | 349 capture_format_.frame_size = params.requested_format.frame_size; |
352 capture_format_.frame_rate = | 350 capture_format_.frame_rate = |
353 std::max(kMinFrameRate, | 351 std::max(kMinFrameRate, |
354 std::min(params.requested_format.frame_rate, kMaxFrameRate)); | 352 std::min(params.requested_format.frame_rate, kMaxFrameRate)); |
355 // Leave the pixel format selection to AVFoundation. The pixel format | 353 // Leave the pixel format selection to AVFoundation. The pixel format |
356 // will be passed to |ReceiveFrame|. | 354 // will be passed to |ReceiveFrame|. |
357 capture_format_.pixel_format = PIXEL_FORMAT_UNKNOWN; | 355 capture_format_.pixel_format = PIXEL_FORMAT_UNKNOWN; |
358 | 356 |
359 if (!UpdateCaptureResolution()) | 357 if (!UpdateCaptureResolution()) |
360 return; | 358 return; |
361 | 359 |
362 // Try setting the power line frequency removal (anti-flicker). The built-in | 360 // Try setting the power line frequency removal (anti-flicker). The built-in |
363 // cameras are normally suspended so the configuration must happen right | 361 // cameras are normally suspended so the configuration must happen right |
364 // before starting capture and during configuration. | 362 // before starting capture and during configuration. |
365 const std::string& device_model = device_name_.GetModel(); | 363 const std::string& device_model = device_name_.GetModel(); |
366 if (device_model.length() > 2 * kVidPidSize) { | 364 if (device_model.length() > 2 * kVidPidSize) { |
367 std::string vendor_id = device_model.substr(0, kVidPidSize); | 365 std::string vendor_id = device_model.substr(0, kVidPidSize); |
368 std::string model_id = device_model.substr(kVidPidSize + 1); | 366 std::string model_id = device_model.substr(kVidPidSize + 1); |
369 int vendor_id_as_int, model_id_as_int; | 367 int vendor_id_as_int, model_id_as_int; |
370 if (base::HexStringToInt(base::StringPiece(vendor_id), &vendor_id_as_int) && | 368 if (base::HexStringToInt(base::StringPiece(vendor_id), &vendor_id_as_int) && |
(...skipping 19 matching lines...) Expand all Loading... |
390 [capture_device_ setFrameReceiver:nil]; | 388 [capture_device_ setFrameReceiver:nil]; |
391 client_.reset(); | 389 client_.reset(); |
392 state_ = kIdle; | 390 state_ = kIdle; |
393 } | 391 } |
394 | 392 |
395 bool VideoCaptureDeviceMac::Init( | 393 bool VideoCaptureDeviceMac::Init( |
396 VideoCaptureDevice::Name::CaptureApiType capture_api_type) { | 394 VideoCaptureDevice::Name::CaptureApiType capture_api_type) { |
397 DCHECK(task_runner_->BelongsToCurrentThread()); | 395 DCHECK(task_runner_->BelongsToCurrentThread()); |
398 DCHECK_EQ(state_, kNotInitialized); | 396 DCHECK_EQ(state_, kNotInitialized); |
399 | 397 |
400 if (capture_api_type == Name::AVFOUNDATION) { | 398 if (capture_api_type != Name::AVFOUNDATION) |
401 capture_device_ = | 399 return false; |
402 [[VideoCaptureDeviceAVFoundation alloc] initWithFrameReceiver:this]; | 400 |
403 } | 401 capture_device_.reset( |
| 402 [[VideoCaptureDeviceAVFoundation alloc] initWithFrameReceiver:this]); |
404 | 403 |
405 if (!capture_device_) | 404 if (!capture_device_) |
406 return false; | 405 return false; |
407 | 406 |
408 state_ = kIdle; | 407 state_ = kIdle; |
409 return true; | 408 return true; |
410 } | 409 } |
411 | 410 |
412 void VideoCaptureDeviceMac::ReceiveFrame(const uint8_t* video_frame, | 411 void VideoCaptureDeviceMac::ReceiveFrame(const uint8_t* video_frame, |
413 int video_frame_length, | 412 int video_frame_length, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 if (![capture_device_ setCaptureHeight:capture_format_.frame_size.height() | 463 if (![capture_device_ setCaptureHeight:capture_format_.frame_size.height() |
465 width:capture_format_.frame_size.width() | 464 width:capture_format_.frame_size.width() |
466 frameRate:capture_format_.frame_rate]) { | 465 frameRate:capture_format_.frame_rate]) { |
467 ReceiveError(FROM_HERE, "Could not configure capture device."); | 466 ReceiveError(FROM_HERE, "Could not configure capture device."); |
468 return false; | 467 return false; |
469 } | 468 } |
470 return true; | 469 return true; |
471 } | 470 } |
472 | 471 |
473 } // namespace media | 472 } // namespace media |
OLD | NEW |