Sami Kalliomäki | ff1de0a | 2018-05-16 12:49:47 +0200 | [diff] [blame] | 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/videosource.h" |
| 12 | |
| 13 | #include "sdk/android/src/jni/androidvideotracksource.h" |
| 14 | #include "sdk/android/src/jni/nativecapturerobserver.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: |
| 26 | JavaVideoTrackSourceImpl(JNIEnv* env, |
| 27 | rtc::Thread* signaling_thread, |
| 28 | bool is_screencast) |
| 29 | : android_video_track_source_( |
| 30 | new rtc::RefCountedObject<jni::AndroidVideoTrackSource>( |
| 31 | signaling_thread, |
| 32 | env, |
| 33 | is_screencast)), |
| 34 | native_capturer_observer_(jni::CreateJavaNativeCapturerObserver( |
| 35 | env, |
| 36 | android_video_track_source_)) {} |
| 37 | |
| 38 | ScopedJavaLocalRef<jobject> GetJavaVideoCapturerObserver( |
| 39 | JNIEnv* env) override { |
| 40 | return ScopedJavaLocalRef<jobject>(env, native_capturer_observer_); |
| 41 | } |
| 42 | |
| 43 | // Delegate VideoTrackSourceInterface methods to android_video_track_source_. |
| 44 | void RegisterObserver(ObserverInterface* observer) override { |
| 45 | android_video_track_source_->RegisterObserver(observer); |
| 46 | } |
| 47 | |
| 48 | void UnregisterObserver(ObserverInterface* observer) override { |
| 49 | android_video_track_source_->UnregisterObserver(observer); |
| 50 | } |
| 51 | |
| 52 | SourceState state() const override { |
| 53 | return android_video_track_source_->state(); |
| 54 | } |
| 55 | |
| 56 | bool remote() const override { return android_video_track_source_->remote(); } |
| 57 | |
| 58 | void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink, |
| 59 | const rtc::VideoSinkWants& wants) override { |
| 60 | // The method is defined private in the implementation so we have to access |
| 61 | // it through the interface... |
| 62 | static_cast<VideoTrackSourceInterface*>(android_video_track_source_.get()) |
| 63 | ->AddOrUpdateSink(sink, wants); |
| 64 | } |
| 65 | |
| 66 | void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override { |
| 67 | // The method is defined private in the implementation so we have to access |
| 68 | // it through the interface... |
| 69 | static_cast<VideoTrackSourceInterface*>(android_video_track_source_.get()) |
| 70 | ->RemoveSink(sink); |
| 71 | } |
| 72 | |
| 73 | bool is_screencast() const override { |
| 74 | return android_video_track_source_->is_screencast(); |
| 75 | } |
| 76 | |
Danil Chapovalov | 196100e | 2018-06-21 10:17:24 +0200 | [diff] [blame] | 77 | absl::optional<bool> needs_denoising() const override { |
Sami Kalliomäki | ff1de0a | 2018-05-16 12:49:47 +0200 | [diff] [blame] | 78 | return android_video_track_source_->needs_denoising(); |
| 79 | } |
| 80 | |
| 81 | bool GetStats(Stats* stats) override { |
| 82 | // The method is defined private in the implementation so we have to access |
| 83 | // it through the interface... |
| 84 | return static_cast<VideoTrackSourceInterface*>( |
| 85 | android_video_track_source_.get()) |
| 86 | ->GetStats(stats); |
| 87 | } |
| 88 | |
| 89 | private: |
| 90 | rtc::scoped_refptr<jni::AndroidVideoTrackSource> android_video_track_source_; |
| 91 | ScopedJavaGlobalRef<jobject> native_capturer_observer_; |
| 92 | }; |
| 93 | |
| 94 | } // namespace |
| 95 | |
| 96 | rtc::scoped_refptr<JavaVideoTrackSourceInterface> CreateJavaVideoSource( |
| 97 | JNIEnv* jni, |
| 98 | rtc::Thread* signaling_thread, |
| 99 | bool is_screencast) { |
| 100 | return new rtc::RefCountedObject<JavaVideoTrackSourceImpl>( |
| 101 | jni, signaling_thread, is_screencast); |
| 102 | } |
| 103 | |
| 104 | } // namespace webrtc |