1 /*
2  *  Copyright (c) 2018 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 #include "sdk/android/native_api/video/video_source.h"
12 
13 #include "sdk/android/src/jni/android_video_track_source.h"
14 #include "sdk/android/src/jni/native_capturer_observer.h"
15 
16 namespace webrtc {
17 
18 namespace {
19 
20 // Hides full jni::AndroidVideoTrackSource interface and provides an instance of
21 // NativeCapturerObserver associated with the video source. Does not extend
22 // AndroidVideoTrackSource to avoid diamond inheritance on
23 // VideoTrackSourceInterface.
24 class JavaVideoTrackSourceImpl : public JavaVideoTrackSourceInterface {
25  public:
JavaVideoTrackSourceImpl(JNIEnv * env,rtc::Thread * signaling_thread,bool is_screencast,bool align_timestamps)26   JavaVideoTrackSourceImpl(JNIEnv* env,
27                            rtc::Thread* signaling_thread,
28                            bool is_screencast,
29                            bool align_timestamps)
30       : android_video_track_source_(
31             new rtc::RefCountedObject<jni::AndroidVideoTrackSource>(
32                 signaling_thread,
33                 env,
34                 is_screencast,
35                 align_timestamps)),
36         native_capturer_observer_(jni::CreateJavaNativeCapturerObserver(
37             env,
38             android_video_track_source_)) {}
39 
GetJavaVideoCapturerObserver(JNIEnv * env)40   ScopedJavaLocalRef<jobject> GetJavaVideoCapturerObserver(
41       JNIEnv* env) override {
42     return ScopedJavaLocalRef<jobject>(env, native_capturer_observer_);
43   }
44 
45   // Delegate VideoTrackSourceInterface methods to android_video_track_source_.
RegisterObserver(ObserverInterface * observer)46   void RegisterObserver(ObserverInterface* observer) override {
47     android_video_track_source_->RegisterObserver(observer);
48   }
49 
UnregisterObserver(ObserverInterface * observer)50   void UnregisterObserver(ObserverInterface* observer) override {
51     android_video_track_source_->UnregisterObserver(observer);
52   }
53 
state() const54   SourceState state() const override {
55     return android_video_track_source_->state();
56   }
57 
remote() const58   bool remote() const override { return android_video_track_source_->remote(); }
59 
AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame> * sink,const rtc::VideoSinkWants & wants)60   void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
61                        const rtc::VideoSinkWants& wants) override {
62     // The method is defined private in the implementation so we have to access
63     // it through the interface...
64     static_cast<VideoTrackSourceInterface*>(android_video_track_source_.get())
65         ->AddOrUpdateSink(sink, wants);
66   }
67 
RemoveSink(rtc::VideoSinkInterface<VideoFrame> * sink)68   void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {
69     // The method is defined private in the implementation so we have to access
70     // it through the interface...
71     static_cast<VideoTrackSourceInterface*>(android_video_track_source_.get())
72         ->RemoveSink(sink);
73   }
74 
is_screencast() const75   bool is_screencast() const override {
76     return android_video_track_source_->is_screencast();
77   }
78 
needs_denoising() const79   absl::optional<bool> needs_denoising() const override {
80     return android_video_track_source_->needs_denoising();
81   }
82 
GetStats(Stats * stats)83   bool GetStats(Stats* stats) override {
84     // The method is defined private in the implementation so we have to access
85     // it through the interface...
86     return static_cast<VideoTrackSourceInterface*>(
87                android_video_track_source_.get())
88         ->GetStats(stats);
89   }
90 
91  private:
92   rtc::scoped_refptr<jni::AndroidVideoTrackSource> android_video_track_source_;
93   ScopedJavaGlobalRef<jobject> native_capturer_observer_;
94 };
95 
96 }  // namespace
97 
CreateJavaVideoSource(JNIEnv * jni,rtc::Thread * signaling_thread,bool is_screencast,bool align_timestamps)98 rtc::scoped_refptr<JavaVideoTrackSourceInterface> CreateJavaVideoSource(
99     JNIEnv* jni,
100     rtc::Thread* signaling_thread,
101     bool is_screencast,
102     bool align_timestamps) {
103   return new rtc::RefCountedObject<JavaVideoTrackSourceImpl>(
104       jni, signaling_thread, is_screencast, align_timestamps);
105 }
106 
107 }  // namespace webrtc
108