blob: f783a57ea81a54535a250017ebbdc946ea8218f9 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
henrike@webrtc.org28e20752013-07-10 00:45:36 +000011package org.webrtc;
12
Artem Titarenko69540f42018-12-10 12:30:46 +010013import android.support.annotation.Nullable;
Magnus Jedvert1a759c62018-04-24 15:11:02 +020014
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000015/**
Magnus Jedvert7640fcf2016-09-21 16:20:03 +020016 * Java wrapper of native AndroidVideoTrackSource.
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000017 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018public class VideoSource extends MediaSource {
Magnus Jedvert99b275d2019-02-05 16:39:41 +010019 /** Simple aspect ratio clas for use in constraining output format. */
20 public static class AspectRatio {
21 public static final AspectRatio UNDEFINED = new AspectRatio(/* width= */ 0, /* height= */ 0);
22
23 public final int width;
24 public final int height;
25
26 public AspectRatio(int width, int height) {
27 this.width = width;
28 this.height = height;
29 }
30 }
31
32 private final NativeAndroidVideoTrackSource nativeAndroidVideoTrackSource;
33 private final CapturerObserver capturerObserver = new CapturerObserver() {
34 @Override
35 public void onCapturerStarted(boolean success) {
36 nativeAndroidVideoTrackSource.setState(success);
37 }
38
39 @Override
40 public void onCapturerStopped() {
41 nativeAndroidVideoTrackSource.setState(/* isLive= */ false);
42 }
43
44 @Override
45 public void onFrameCaptured(VideoFrame frame) {
46 nativeAndroidVideoTrackSource.onFrameCaptured(frame);
47 }
48 };
Magnus Jedvert1a759c62018-04-24 15:11:02 +020049
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050 public VideoSource(long nativeSource) {
51 super(nativeSource);
Magnus Jedvert99b275d2019-02-05 16:39:41 +010052 this.nativeAndroidVideoTrackSource = new NativeAndroidVideoTrackSource(nativeSource);
Magnus Jedvert1a759c62018-04-24 15:11:02 +020053 }
54
Magnus Jedvert7640fcf2016-09-21 16:20:03 +020055 /**
56 * Calling this function will cause frames to be scaled down to the requested resolution. Also,
57 * frames will be cropped to match the requested aspect ratio, and frames will be dropped to match
58 * the requested fps. The requested aspect ratio is orientation agnostic and will be adjusted to
59 * maintain the input orientation, so it doesn't matter if e.g. 1280x720 or 720x1280 is requested.
60 */
61 public void adaptOutputFormat(int width, int height, int fps) {
Magnus Jedvert06aa2092018-10-26 14:00:18 +020062 final int maxSide = Math.max(width, height);
63 final int minSide = Math.min(width, height);
64 adaptOutputFormat(maxSide, minSide, minSide, maxSide, fps);
65 }
66
67 /**
68 * Same as above, but allows setting two different target resolutions depending on incoming
69 * frame orientation. This gives more fine-grained control and can e.g. be used to force landscape
70 * video to be cropped to portrait video.
71 */
72 public void adaptOutputFormat(
73 int landscapeWidth, int landscapeHeight, int portraitWidth, int portraitHeight, int fps) {
Magnus Jedvert99b275d2019-02-05 16:39:41 +010074 adaptOutputFormat(new AspectRatio(landscapeWidth, landscapeHeight),
75 /* maxLandscapePixelCount= */ landscapeWidth * landscapeHeight,
76 new AspectRatio(portraitWidth, portraitHeight),
77 /* maxPortraitPixelCount= */ portraitWidth * portraitHeight, fps);
78 }
79
80 /** Same as above, with even more control as each constraint is optional. */
81 public void adaptOutputFormat(AspectRatio targetLandscapeAspectRatio,
82 @Nullable Integer maxLandscapePixelCount, AspectRatio targetPortraitAspectRatio,
83 @Nullable Integer maxPortraitPixelCount, @Nullable Integer maxFps) {
84 nativeAndroidVideoTrackSource.adaptOutputFormat(targetLandscapeAspectRatio,
85 maxLandscapePixelCount, targetPortraitAspectRatio, maxPortraitPixelCount, maxFps);
Magnus Jedvert7640fcf2016-09-21 16:20:03 +020086 }
87
Sami Kalliomäki05b552f2018-07-05 17:06:51 +020088 public CapturerObserver getCapturerObserver() {
Magnus Jedvert1a759c62018-04-24 15:11:02 +020089 return capturerObserver;
90 }
91
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020092 /** Returns a pointer to webrtc::VideoTrackSourceInterface. */
93 long getNativeVideoTrackSource() {
94 return getNativeMediaSource();
95 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096}