Index: media/video/capture/mac/video_capture_device_mac.mm |
diff --git a/media/video/capture/mac/video_capture_device_mac.mm b/media/video/capture/mac/video_capture_device_mac.mm |
index 882c1522944b67331d48676b0291e37a2f17dcb5..50b92c1b6516c7e5dc68abd70abf5a5287af5fd2 100644 |
--- a/media/video/capture/mac/video_capture_device_mac.mm |
+++ b/media/video/capture/mac/video_capture_device_mac.mm |
@@ -20,6 +20,7 @@ |
#import "media/video/capture/mac/platform_video_capturing_mac.h" |
#import "media/video/capture/mac/video_capture_device_avfoundation_mac.h" |
#import "media/video/capture/mac/video_capture_device_qtkit_mac.h" |
+#include "ui/gfx/size.h" |
@implementation DeviceNameAndTransportType |
@@ -50,18 +51,9 @@ const float kMaxFrameRate = 30.0f; |
// In device identifiers, the USB VID and PID are stored in 4 bytes each. |
const size_t kVidPidSize = 4; |
-const struct Resolution { |
- const int width; |
- const int height; |
-} kQVGA = { 320, 240 }, |
- kVGA = { 640, 480 }, |
- kHD = { 1280, 720 }; |
- |
-const struct Resolution* const kWellSupportedResolutions[] = { |
- &kQVGA, |
- &kVGA, |
- &kHD, |
-}; |
+static const gfx::Size kQVGA(320, 240), |
tommi (sloooow) - chröme
2014/09/09 20:11:10
Do these require static initialization? (i.e. code
magjed_chromium
2014/09/10 18:34:21
Ok, cool. I don't think it's possible to remove th
|
+ kVGA(640, 480), |
+ kHD(1280, 720); |
// Rescaling the image to fix the pixel aspect ratio runs the risk of making |
// the aspect ratio worse, if QTKit selects a new source mode with a different |
@@ -94,26 +86,6 @@ typedef struct IOUSBInterfaceDescriptor { |
UInt8 bUnitID; |
} IOUSBInterfaceDescriptor; |
-// TODO(ronghuawu): Replace this with CapabilityList::GetBestMatchedCapability. |
-void GetBestMatchSupportedResolution(int* width, int* height) { |
- int min_diff = kint32max; |
- int matched_width = *width; |
- int matched_height = *height; |
- int desired_res_area = *width * *height; |
- for (size_t i = 0; i < arraysize(kWellSupportedResolutions); ++i) { |
- int area = kWellSupportedResolutions[i]->width * |
- kWellSupportedResolutions[i]->height; |
- int diff = std::abs(desired_res_area - area); |
- if (diff < min_diff) { |
- min_diff = diff; |
- matched_width = kWellSupportedResolutions[i]->width; |
- matched_height = kWellSupportedResolutions[i]->height; |
- } |
- } |
- *width = matched_width; |
- *height = matched_height; |
-} |
- |
// Tries to create a user-side device interface for a given USB device. Returns |
// true if interface was found and passes it back in |device_interface|. The |
// caller should release |device_interface|. |
@@ -368,15 +340,27 @@ void VideoCaptureDeviceMac::AllocateAndStart( |
if (state_ != kIdle) { |
return; |
} |
- int width = params.requested_format.frame_size.width(); |
- int height = params.requested_format.frame_size.height(); |
- float frame_rate = params.requested_format.frame_rate; |
// QTKit API can scale captured frame to any size requested, which would lead |
// to undesired aspect ratio changes. Try to open the camera with a known |
// supported format and let the client crop/pad the captured frames. |
- if (!AVFoundationGlue::IsAVFoundationSupported()) |
- GetBestMatchSupportedResolution(&width, &height); |
+ gfx::Size resolution = params.requested_format.frame_size; |
+ if (!AVFoundationGlue::IsAVFoundationSupported()) { |
+ // We only care about resolution in this case. Therefore, set fps to 0 and |
+ // pixel format to unknown |
+ static const VideoCaptureFormat kWellSupportedResolutions[] = { |
+ VideoCaptureFormat(kQVGA, 0, PIXEL_FORMAT_UNKNOWN), |
+ VideoCaptureFormat(kVGA, 0, PIXEL_FORMAT_UNKNOWN), |
+ VideoCaptureFormat(kHD, 0, PIXEL_FORMAT_UNKNOWN)}; |
+ // Get the most closely matched format using CompareVideoFormat |
+ const VideoCaptureFormat& best_match = |
+ *std::min_element( |
+ kWellSupportedResolutions, |
+ kWellSupportedResolutions + arraysize(kWellSupportedResolutions), |
+ CompareVideoFormat( |
+ VideoCaptureFormat(resolution, 0, PIXEL_FORMAT_UNKNOWN))); |
+ resolution = best_match.frame_size; |
+ } |
client_ = client.Pass(); |
if (device_name_.capture_api_type() == Name::AVFOUNDATION) |
@@ -392,13 +376,11 @@ void VideoCaptureDeviceMac::AllocateAndStart( |
SetErrorState("Could not open capture device."); |
return; |
} |
- if (frame_rate < kMinFrameRate) |
- frame_rate = kMinFrameRate; |
- else if (frame_rate > kMaxFrameRate) |
- frame_rate = kMaxFrameRate; |
- capture_format_.frame_size.SetSize(width, height); |
- capture_format_.frame_rate = frame_rate; |
+ capture_format_.frame_size = resolution; |
+ capture_format_.frame_rate = |
+ std::max(kMinFrameRate, |
+ std::min(params.requested_format.frame_rate, kMaxFrameRate)); |
capture_format_.pixel_format = PIXEL_FORMAT_UYVY; |
// QTKit: Set the capture resolution only if this is VGA or smaller, otherwise |
@@ -408,8 +390,9 @@ void VideoCaptureDeviceMac::AllocateAndStart( |
// latency, because the webcam will need to be reopened if its default |
// resolution is not HD or VGA. |
// AVfoundation is configured for all resolutions. |
- if (AVFoundationGlue::IsAVFoundationSupported() || width <= kVGA.width || |
- height <= kVGA.height) { |
+ if (AVFoundationGlue::IsAVFoundationSupported() || |
+ resolution.width() <= kVGA.width() || |
+ resolution.height() <= kVGA.height()) { |
if (!UpdateCaptureResolution()) |
return; |
} |
@@ -478,8 +461,8 @@ void VideoCaptureDeviceMac::ReceiveFrame( |
// controlled by QTKit/AVFoundation. |
if (!final_resolution_selected_) { |
DCHECK(!AVFoundationGlue::IsAVFoundationSupported()); |
- if (capture_format_.frame_size.width() > kVGA.width || |
- capture_format_.frame_size.height() > kVGA.height) { |
+ if (capture_format_.frame_size.width() > kVGA.width() || |
+ capture_format_.frame_size.height() > kVGA.height()) { |
// We are requesting HD. Make sure that the picture is good, otherwise |
// drop down to VGA. |
bool change_to_vga = false; |
@@ -503,7 +486,7 @@ void VideoCaptureDeviceMac::ReceiveFrame( |
} |
if (change_to_vga) |
- capture_format_.frame_size.SetSize(kVGA.width, kVGA.height); |
+ capture_format_.frame_size = kVGA; |
} |
if (capture_format_.frame_size == frame_format.frame_size && |