blob: 0939457c4085788bc050bbb3e10514710162203f [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) {
Magnus Jedvert9025bd52019-02-06 14:48:57 +010046 final NativeAndroidVideoTrackSource.FrameAdaptationParameters parameters =
47 nativeAndroidVideoTrackSource.adaptFrame(frame);
48 if (parameters == null) {
49 // Drop frame.
50 return;
51 }
52
53 final VideoFrame.Buffer adaptedBuffer =
54 frame.getBuffer().cropAndScale(parameters.cropX, parameters.cropY, parameters.cropWidth,
55 parameters.cropHeight, parameters.scaleWidth, parameters.scaleHeight);
56 // TODO(magjed): Add video processing hook here.
57 nativeAndroidVideoTrackSource.onFrameCaptured(
58 new VideoFrame(adaptedBuffer, frame.getRotation(), parameters.timestampNs));
59 adaptedBuffer.release();
Magnus Jedvert99b275d2019-02-05 16:39:41 +010060 }
61 };
Magnus Jedvert1a759c62018-04-24 15:11:02 +020062
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063 public VideoSource(long nativeSource) {
64 super(nativeSource);
Magnus Jedvert99b275d2019-02-05 16:39:41 +010065 this.nativeAndroidVideoTrackSource = new NativeAndroidVideoTrackSource(nativeSource);
Magnus Jedvert1a759c62018-04-24 15:11:02 +020066 }
67
Magnus Jedvert7640fcf2016-09-21 16:20:03 +020068 /**
69 * Calling this function will cause frames to be scaled down to the requested resolution. Also,
70 * frames will be cropped to match the requested aspect ratio, and frames will be dropped to match
71 * the requested fps. The requested aspect ratio is orientation agnostic and will be adjusted to
72 * maintain the input orientation, so it doesn't matter if e.g. 1280x720 or 720x1280 is requested.
73 */
74 public void adaptOutputFormat(int width, int height, int fps) {
Magnus Jedvert06aa2092018-10-26 14:00:18 +020075 final int maxSide = Math.max(width, height);
76 final int minSide = Math.min(width, height);
77 adaptOutputFormat(maxSide, minSide, minSide, maxSide, fps);
78 }
79
80 /**
81 * Same as above, but allows setting two different target resolutions depending on incoming
82 * frame orientation. This gives more fine-grained control and can e.g. be used to force landscape
83 * video to be cropped to portrait video.
84 */
85 public void adaptOutputFormat(
86 int landscapeWidth, int landscapeHeight, int portraitWidth, int portraitHeight, int fps) {
Magnus Jedvert99b275d2019-02-05 16:39:41 +010087 adaptOutputFormat(new AspectRatio(landscapeWidth, landscapeHeight),
88 /* maxLandscapePixelCount= */ landscapeWidth * landscapeHeight,
89 new AspectRatio(portraitWidth, portraitHeight),
90 /* maxPortraitPixelCount= */ portraitWidth * portraitHeight, fps);
91 }
92
93 /** Same as above, with even more control as each constraint is optional. */
94 public void adaptOutputFormat(AspectRatio targetLandscapeAspectRatio,
95 @Nullable Integer maxLandscapePixelCount, AspectRatio targetPortraitAspectRatio,
96 @Nullable Integer maxPortraitPixelCount, @Nullable Integer maxFps) {
97 nativeAndroidVideoTrackSource.adaptOutputFormat(targetLandscapeAspectRatio,
98 maxLandscapePixelCount, targetPortraitAspectRatio, maxPortraitPixelCount, maxFps);
Magnus Jedvert7640fcf2016-09-21 16:20:03 +020099 }
100
Sami Kalliomäki05b552f2018-07-05 17:06:51 +0200101 public CapturerObserver getCapturerObserver() {
Magnus Jedvert1a759c62018-04-24 15:11:02 +0200102 return capturerObserver;
103 }
104
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200105 /** Returns a pointer to webrtc::VideoTrackSourceInterface. */
106 long getNativeVideoTrackSource() {
107 return getNativeMediaSource();
108 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109}