diff options
-rw-r--r-- | content/renderer/media/rtc_video_capturer.cc | 4 | ||||
-rw-r--r-- | media/video/capture/mac/video_capture_device_mac.mm | 37 |
2 files changed, 41 insertions, 0 deletions
diff --git a/content/renderer/media/rtc_video_capturer.cc b/content/renderer/media/rtc_video_capturer.cc index 432c1a4..d6d665c 100644 --- a/content/renderer/media/rtc_video_capturer.cc +++ b/content/renderer/media/rtc_video_capturer.cc @@ -41,6 +41,10 @@ cricket::CaptureState RtcVideoCapturer::Start( delegate_->StartCapture(cap, base::Bind(&RtcVideoCapturer::OnFrameCaptured, base::Unretained(this)), base::Bind(&RtcVideoCapturer::OnStateChange, base::Unretained(this))); + // Update the desired aspect ratio so that later the video frame can be + // cropped to meet the requirement if the camera returns a different + // resolution than the |cap|. + UpdateAspectRatio(cap.width, cap.height); return cricket::CS_STARTING; } diff --git a/media/video/capture/mac/video_capture_device_mac.mm b/media/video/capture/mac/video_capture_device_mac.mm index 6e74d95..f18eb54 100644 --- a/media/video/capture/mac/video_capture_device_mac.mm +++ b/media/video/capture/mac/video_capture_device_mac.mm @@ -15,6 +15,37 @@ namespace { const int kMinFrameRate = 1; const int kMaxFrameRate = 30; +struct Resolution { + int width; + int height; +}; + +const Resolution kWellSupportedResolutions[] = { + { 320, 240 }, + { 640, 480 }, + { 1280, 720 }, +}; + +// 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; +} + } namespace media { @@ -59,6 +90,12 @@ void VideoCaptureDeviceMac::Allocate(int width, int height, int frame_rate, if (state_ != kIdle) { return; } + + // QTKit can scale captured frame to any size requested, which would lead to + // undesired aspect ratio change. Tries to open the camera with a natively + // supported format and let the client to crop/pad the captured frames. + GetBestMatchSupportedResolution(&width, &height); + observer_ = observer; NSString* deviceId = [NSString stringWithUTF8String:device_name_.unique_id.c_str()]; |