From 7640fcf6ed29c3c08843ec8651bcb090e158b27e Mon Sep 17 00:00:00 2001 From: Magnus Jedvert Date: Wed, 21 Sep 2016 16:20:03 +0200 Subject: [PATCH] Android VideoSource: Add adaptOutputFormat function The Java VideoSource class wraps the C++ AndroidVideoTrackSource. AndroidVideoTrackSource is the object actually owning the VideoAdapter. We currently control the VideoAdapter through the Java VideoCapturer, but it is more natural and direct to control it through the Java VideoSource class. This CL adds the necessary function to do this, and the function in VideoCapturer is deprecated. BUG=webrtc:6391 R=sakal@webrtc.org Review URL: https://codereview.webrtc.org/2350933006 . Cr-Commit-Position: refs/heads/master@{#14332} --- .../java/src/org/webrtc/VideoCapturer.java | 4 ++++ .../java/src/org/webrtc/VideoSource.java | 19 ++++++++++++++----- .../jni/androidvideotracksource_jni.cc | 8 ++++++++ .../Camera1CapturerUsingByteBufferTest.java | 2 +- .../Camera1CapturerUsingTextureTest.java | 2 +- .../src/org/webrtc/Camera2CapturerTest.java | 2 +- .../CameraVideoCapturerTestFixtures.java | 2 +- webrtc/api/androidvideotracksource.cc | 2 -- .../appspot/apprtc/PeerConnectionClient.java | 2 +- 9 files changed, 31 insertions(+), 12 deletions(-) diff --git a/webrtc/api/android/java/src/org/webrtc/VideoCapturer.java b/webrtc/api/android/java/src/org/webrtc/VideoCapturer.java index c0b4d69bc2..0a5e7d592a 100644 --- a/webrtc/api/android/java/src/org/webrtc/VideoCapturer.java +++ b/webrtc/api/android/java/src/org/webrtc/VideoCapturer.java @@ -112,6 +112,10 @@ public interface VideoCapturer { */ void stopCapture() throws InterruptedException; + /** + * Use VideoSource.adaptOutputFormat() instead. + */ + @Deprecated void onOutputFormatRequest(int width, int height, int framerate); void changeCaptureFormat(int width, int height, int framerate); diff --git a/webrtc/api/android/java/src/org/webrtc/VideoSource.java b/webrtc/api/android/java/src/org/webrtc/VideoSource.java index f664f19d2c..239940d765 100644 --- a/webrtc/api/android/java/src/org/webrtc/VideoSource.java +++ b/webrtc/api/android/java/src/org/webrtc/VideoSource.java @@ -12,14 +12,23 @@ package org.webrtc; /** - * Java version of VideoSourceInterface, extended with stop/restart - * functionality to allow explicit control of the camera device on android, - * where there is no support for multiple open capture devices and the cost of - * holding a camera open (even if MediaStreamTrack.setEnabled(false) is muting - * its output to the encoder) can be too high to bear. + * Java wrapper of native AndroidVideoTrackSource. */ public class VideoSource extends MediaSource { public VideoSource(long nativeSource) { super(nativeSource); } + + /** + * 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. + */ + public void adaptOutputFormat(int width, int height, int fps) { + nativeAdaptOutputFormat(nativeSource, width, height, fps); + } + + private static native void nativeAdaptOutputFormat( + long nativeSource, int width, int height, int fps); } diff --git a/webrtc/api/android/jni/androidvideotracksource_jni.cc b/webrtc/api/android/jni/androidvideotracksource_jni.cc index 2d1529938a..9b251276b6 100644 --- a/webrtc/api/android/jni/androidvideotracksource_jni.cc +++ b/webrtc/api/android/jni/androidvideotracksource_jni.cc @@ -87,4 +87,12 @@ JOW_OBSERVER_METHOD(void, nativeOnOutputFormatRequest) source->OnOutputFormatRequest(j_width, j_height, j_fps); } +JOW(void, VideoSource_nativeAdaptOutputFormat) +(JNIEnv* jni, jclass, jlong j_source, jint j_width, jint j_height, jint j_fps) { + LOG(LS_INFO) << "VideoSource_nativeAdaptOutputFormat"; + webrtc::AndroidVideoTrackSource* source = + AndroidVideoTrackSourceFromJavaProxy(j_source); + source->OnOutputFormatRequest(j_width, j_height, j_fps); +} + } // namespace webrtc_jni diff --git a/webrtc/api/androidtests/src/org/webrtc/Camera1CapturerUsingByteBufferTest.java b/webrtc/api/androidtests/src/org/webrtc/Camera1CapturerUsingByteBufferTest.java index fa827f7208..8edcd1eba9 100644 --- a/webrtc/api/androidtests/src/org/webrtc/Camera1CapturerUsingByteBufferTest.java +++ b/webrtc/api/androidtests/src/org/webrtc/Camera1CapturerUsingByteBufferTest.java @@ -143,7 +143,7 @@ public class Camera1CapturerUsingByteBufferTest extends InstrumentationTestCase fixtures.returnBufferLateEndToEnd(); } - // This test that frames forwarded to a renderer is scaled if onOutputFormatRequest is + // This test that frames forwarded to a renderer is scaled if adaptOutputFormat is // called. This test both Java and C++ parts of of the stack. @MediumTest public void testScaleCameraOutput() throws InterruptedException { diff --git a/webrtc/api/androidtests/src/org/webrtc/Camera1CapturerUsingTextureTest.java b/webrtc/api/androidtests/src/org/webrtc/Camera1CapturerUsingTextureTest.java index 1674edc945..7a6c37a33e 100644 --- a/webrtc/api/androidtests/src/org/webrtc/Camera1CapturerUsingTextureTest.java +++ b/webrtc/api/androidtests/src/org/webrtc/Camera1CapturerUsingTextureTest.java @@ -145,7 +145,7 @@ public class Camera1CapturerUsingTextureTest extends InstrumentationTestCase { fixtures.cameraFreezedEventOnBufferStarvation(); } - // This test that frames forwarded to a renderer is scaled if onOutputFormatRequest is + // This test that frames forwarded to a renderer is scaled if adaptOutputFormat is // called. This test both Java and C++ parts of of the stack. @MediumTest public void testScaleCameraOutput() throws InterruptedException { diff --git a/webrtc/api/androidtests/src/org/webrtc/Camera2CapturerTest.java b/webrtc/api/androidtests/src/org/webrtc/Camera2CapturerTest.java index 349b7da4ff..0326e54809 100644 --- a/webrtc/api/androidtests/src/org/webrtc/Camera2CapturerTest.java +++ b/webrtc/api/androidtests/src/org/webrtc/Camera2CapturerTest.java @@ -269,7 +269,7 @@ public class Camera2CapturerTest extends InstrumentationTestCase { fixtures.cameraFreezedEventOnBufferStarvation(); } - // This test that frames forwarded to a renderer is scaled if onOutputFormatRequest is + // This test that frames forwarded to a renderer is scaled if adaptOutputFormat is // called. This test both Java and C++ parts of of the stack. @MediumTest public void testScaleCameraOutput() throws InterruptedException { diff --git a/webrtc/api/androidtests/src/org/webrtc/CameraVideoCapturerTestFixtures.java b/webrtc/api/androidtests/src/org/webrtc/CameraVideoCapturerTestFixtures.java index 3c38228097..9e41d21b22 100644 --- a/webrtc/api/androidtests/src/org/webrtc/CameraVideoCapturerTestFixtures.java +++ b/webrtc/api/androidtests/src/org/webrtc/CameraVideoCapturerTestFixtures.java @@ -660,7 +660,7 @@ class CameraVideoCapturerTestFixtures { final int scaledHeight = startHeight / 2; // Request the captured frames to be scaled. - capturerInstance.capturer.onOutputFormatRequest(scaledWidth, scaledHeight, frameRate); + videoTrackWithRenderer.source.adaptOutputFormat(scaledWidth, scaledHeight, frameRate); boolean gotExpectedResolution = false; int numberOfInspectedFrames = 0; diff --git a/webrtc/api/androidvideotracksource.cc b/webrtc/api/androidvideotracksource.cc index f0bd26a510..6fe8c95784 100644 --- a/webrtc/api/androidvideotracksource.cc +++ b/webrtc/api/androidvideotracksource.cc @@ -228,8 +228,6 @@ void AndroidVideoTrackSource::OnFrame(const cricket::VideoFrame& frame, void AndroidVideoTrackSource::OnOutputFormatRequest(int width, int height, int fps) { - RTC_DCHECK(camera_thread_checker_.CalledOnValidThread()); - cricket::VideoFormat format(width, height, cricket::VideoFormat::FpsToInterval(fps), 0); video_adapter_.OnOutputFormatRequest(format); diff --git a/webrtc/examples/androidapp/src/org/appspot/apprtc/PeerConnectionClient.java b/webrtc/examples/androidapp/src/org/appspot/apprtc/PeerConnectionClient.java index 1d049f91d4..5c50e5aeb5 100644 --- a/webrtc/examples/androidapp/src/org/appspot/apprtc/PeerConnectionClient.java +++ b/webrtc/examples/androidapp/src/org/appspot/apprtc/PeerConnectionClient.java @@ -1058,7 +1058,7 @@ public class PeerConnectionClient { return; } Log.d(TAG, "changeCaptureFormat: " + width + "x" + height + "@" + framerate); - videoCapturer.onOutputFormatRequest(width, height, framerate); + videoSource.adaptOutputFormat(width, height, framerate); } // Implementation detail: observe ICE & stream changes and react accordingly.