Add method on AVFoundation capturer to adapt output format.

This CL makes a method available on the AVFoundationVideoCapturer
that adapts the output format of captured video to the specified
width and height.

BUG=webrtc:6753

Review-Url: https://codereview.webrtc.org/2528493004
Cr-Commit-Position: refs/heads/master@{#15351}
diff --git a/webrtc/sdk/objc/Framework/Classes/RTCAVFoundationVideoSource.mm b/webrtc/sdk/objc/Framework/Classes/RTCAVFoundationVideoSource.mm
index 528e8cb..96c5c36 100644
--- a/webrtc/sdk/objc/Framework/Classes/RTCAVFoundationVideoSource.mm
+++ b/webrtc/sdk/objc/Framework/Classes/RTCAVFoundationVideoSource.mm
@@ -32,6 +32,12 @@
   return [super initWithNativeVideoSource:source];
 }
 
+- (void)adaptOutputFormatToWidth:(int)width
+                          height:(int)height
+                             fps:(int)fps {
+  self.capturer->AdaptOutputFormat(width, height, fps);
+}
+
 - (BOOL)canUseBackCamera {
   return self.capturer->CanUseBackCamera();
 }
diff --git a/webrtc/sdk/objc/Framework/Classes/avfoundationvideocapturer.h b/webrtc/sdk/objc/Framework/Classes/avfoundationvideocapturer.h
index ebabb8c..8bf949b 100644
--- a/webrtc/sdk/objc/Framework/Classes/avfoundationvideocapturer.h
+++ b/webrtc/sdk/objc/Framework/Classes/avfoundationvideocapturer.h
@@ -58,6 +58,10 @@
   void CaptureSampleBuffer(CMSampleBufferRef sample_buffer,
                            webrtc::VideoRotation rotation);
 
+  // Called to adjust the size of output frames to supplied |width| and
+  // |height|. Also drops frames to make the output match |fps|.
+  void AdaptOutputFormat(int width, int height, int fps);
+
  private:
   RTCAVFoundationVideoCapturerInternal *_capturer;
   webrtc::I420BufferPool _buffer_pool;
diff --git a/webrtc/sdk/objc/Framework/Classes/avfoundationvideocapturer.mm b/webrtc/sdk/objc/Framework/Classes/avfoundationvideocapturer.mm
index 5742566..28ae3f2 100644
--- a/webrtc/sdk/objc/Framework/Classes/avfoundationvideocapturer.mm
+++ b/webrtc/sdk/objc/Framework/Classes/avfoundationvideocapturer.mm
@@ -113,6 +113,11 @@
   return _capturer.useBackCamera;
 }
 
+void AVFoundationVideoCapturer::AdaptOutputFormat(int width, int height, int fps) {
+  cricket::VideoFormat format(width, height, cricket::VideoFormat::FpsToInterval(fps), 0);
+  video_adapter()->OnOutputFormatRequest(format);
+}
+
 void AVFoundationVideoCapturer::CaptureSampleBuffer(
     CMSampleBufferRef sample_buffer, VideoRotation rotation) {
   if (CMSampleBufferGetNumSamples(sample_buffer) != 1 ||
diff --git a/webrtc/sdk/objc/Framework/Headers/WebRTC/RTCAVFoundationVideoSource.h b/webrtc/sdk/objc/Framework/Headers/WebRTC/RTCAVFoundationVideoSource.h
index 2a732b9..254196d 100644
--- a/webrtc/sdk/objc/Framework/Headers/WebRTC/RTCAVFoundationVideoSource.h
+++ b/webrtc/sdk/objc/Framework/Headers/WebRTC/RTCAVFoundationVideoSource.h
@@ -29,6 +29,16 @@
 
 - (instancetype)init NS_UNAVAILABLE;
 
+/**
+ * Calling this function will cause frames to be scaled down to the
+ * requested resolution. Also, frames will be cropped to match the
+ * requested aspect ratio, and frames will be dropped to match the
+ * requested fps. The requested aspect ratio is orientation agnostic and
+ * will be adjusted to maintain the input orientation, so it doesn't
+ * matter if e.g. 1280x720 or 720x1280 is requested.
+ */
+- (void)adaptOutputFormatToWidth:(int)width height:(int)height fps:(int)fps;
+
 /** Returns whether rear-facing camera is available for use. */
 @property(nonatomic, readonly) BOOL canUseBackCamera;