1 /* 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. 3 * 4 * 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. 9 */ 10 11 #ifndef API_ANDROID_JNI_ANDROIDVIDEOTRACKSOURCE_H_ 12 #define API_ANDROID_JNI_ANDROIDVIDEOTRACKSOURCE_H_ 13 14 #include <jni.h> 15 16 #include "common_video/libyuv/include/webrtc_libyuv.h" 17 #include "media/base/adapted_video_track_source.h" 18 #include "rtc_base/checks.h" 19 #include "rtc_base/thread.h" 20 #include "rtc_base/timestamp_aligner.h" 21 #include "sdk/android/src/jni/jni_helpers.h" 22 23 namespace webrtc { 24 namespace jni { 25 26 // This class needs to be used in conjunction with the Java corresponding class 27 // NativeAndroidVideoTrackSource. This class is thred safe and methods can be 28 // called from any thread, but if frames A, B, ..., are sent to adaptFrame(), 29 // the adapted frames adaptedA, adaptedB, ..., needs to be passed in the same 30 // order to onFrameCaptured(). 31 class AndroidVideoTrackSource : public rtc::AdaptedVideoTrackSource { 32 public: 33 AndroidVideoTrackSource(rtc::Thread* signaling_thread, 34 JNIEnv* jni, 35 bool is_screencast, 36 bool align_timestamps); 37 ~AndroidVideoTrackSource() override; 38 39 bool is_screencast() const override; 40 41 // Indicates that the encoder should denoise video before encoding it. 42 // If it is not set, the default configuration is used which is different 43 // depending on video codec. 44 absl::optional<bool> needs_denoising() const override; 45 46 void SetState(SourceState state); 47 48 SourceState state() const override; 49 50 bool remote() const override; 51 52 // This function should be called before delivering any frame to determine if 53 // the frame should be dropped or what the cropping and scaling parameters 54 // should be. This function is thread safe and can be called from any thread. 55 // This function returns 56 // NativeAndroidVideoTrackSource.FrameAdaptationParameters, or null if the 57 // frame should be dropped. 58 ScopedJavaLocalRef<jobject> AdaptFrame(JNIEnv* env, 59 jint j_width, 60 jint j_height, 61 jint j_rotation, 62 jlong j_timestamp_ns); 63 64 // This function converts and passes the frame on to the rest of the C++ 65 // WebRTC layer. Note that GetFrameAdaptationParameters() is expected to be 66 // called first and that the delivered frame conforms to those parameters. 67 // This function is thread safe and can be called from any thread. 68 void OnFrameCaptured(JNIEnv* env, 69 jint j_rotation, 70 jlong j_timestamp_ns, 71 const JavaRef<jobject>& j_video_frame_buffer); 72 73 void SetState(JNIEnv* env, 74 jboolean j_is_live); 75 76 void AdaptOutputFormat(JNIEnv* env, 77 jint j_landscape_width, 78 jint j_landscape_height, 79 const JavaRef<jobject>& j_max_landscape_pixel_count, 80 jint j_portrait_width, 81 jint j_portrait_height, 82 const JavaRef<jobject>& j_max_portrait_pixel_count, 83 const JavaRef<jobject>& j_max_fps); 84 85 void SetIsScreencast(JNIEnv* env, jboolean j_is_screencast); 86 87 private: 88 rtc::Thread* signaling_thread_; 89 std::atomic<SourceState> state_; 90 std::atomic<bool> is_screencast_; 91 rtc::TimestampAligner timestamp_aligner_; 92 const bool align_timestamps_; 93 }; 94 95 } // namespace jni 96 } // namespace webrtc 97 98 #endif // API_ANDROID_JNI_ANDROIDVIDEOTRACKSOURCE_H_ 99