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