| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2017 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2017 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <jni.h> | 11 #include <jni.h> |
| 12 | 12 |
| 13 #include "webrtc/api/mediastreaminterface.h" | 13 #include "webrtc/api/mediastreaminterface.h" |
| 14 #include "webrtc/rtc_base/logging.h" | 14 #include "webrtc/rtc_base/logging.h" |
| 15 #include "webrtc/sdk/android/src/jni/classreferenceholder.h" | 15 #include "webrtc/sdk/android/src/jni/classreferenceholder.h" |
| 16 #include "webrtc/sdk/android/src/jni/jni_helpers.h" | 16 #include "webrtc/sdk/android/src/jni/jni_helpers.h" |
| 17 #include "webrtc/sdk/android/src/jni/native_handle_impl.h" | 17 #include "webrtc/sdk/android/src/jni/native_handle_impl.h" |
| 18 | 18 |
| 19 namespace webrtc_jni { | 19 namespace webrtc { |
| 20 namespace jni { |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 class VideoSinkWrapper : public rtc::VideoSinkInterface<webrtc::VideoFrame> { | 24 class VideoSinkWrapper : public rtc::VideoSinkInterface<VideoFrame> { |
| 24 public: | 25 public: |
| 25 VideoSinkWrapper(JNIEnv* jni, jobject j_sink); | 26 VideoSinkWrapper(JNIEnv* jni, jobject j_sink); |
| 26 ~VideoSinkWrapper() override {} | 27 ~VideoSinkWrapper() override {} |
| 27 | 28 |
| 28 private: | 29 private: |
| 29 void OnFrame(const webrtc::VideoFrame& frame) override; | 30 void OnFrame(const VideoFrame& frame) override; |
| 30 | 31 |
| 31 jmethodID j_on_frame_method_; | 32 jmethodID j_on_frame_method_; |
| 32 | 33 |
| 33 const JavaVideoFrameFactory java_video_frame_factory_; | 34 const JavaVideoFrameFactory java_video_frame_factory_; |
| 34 const ScopedGlobalRef<jobject> j_sink_; | 35 const ScopedGlobalRef<jobject> j_sink_; |
| 35 }; | 36 }; |
| 36 | 37 |
| 37 VideoSinkWrapper::VideoSinkWrapper(JNIEnv* jni, jobject j_sink) | 38 VideoSinkWrapper::VideoSinkWrapper(JNIEnv* jni, jobject j_sink) |
| 38 : java_video_frame_factory_(jni), j_sink_(jni, j_sink) { | 39 : java_video_frame_factory_(jni), j_sink_(jni, j_sink) { |
| 39 jclass j_video_sink_class = FindClass(jni, "org/webrtc/VideoSink"); | 40 jclass j_video_sink_class = FindClass(jni, "org/webrtc/VideoSink"); |
| 40 j_on_frame_method_ = jni->GetMethodID(j_video_sink_class, "onFrame", | 41 j_on_frame_method_ = jni->GetMethodID(j_video_sink_class, "onFrame", |
| 41 "(Lorg/webrtc/VideoFrame;)V"); | 42 "(Lorg/webrtc/VideoFrame;)V"); |
| 42 } | 43 } |
| 43 | 44 |
| 44 void VideoSinkWrapper::OnFrame(const webrtc::VideoFrame& frame) { | 45 void VideoSinkWrapper::OnFrame(const VideoFrame& frame) { |
| 45 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 46 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 46 ScopedLocalRefFrame local_ref_frame(jni); | 47 ScopedLocalRefFrame local_ref_frame(jni); |
| 47 jni->CallVoidMethod(*j_sink_, j_on_frame_method_, | 48 jni->CallVoidMethod(*j_sink_, j_on_frame_method_, |
| 48 java_video_frame_factory_.ToJavaFrame(jni, frame)); | 49 java_video_frame_factory_.ToJavaFrame(jni, frame)); |
| 49 } | 50 } |
| 50 | 51 |
| 51 } // namespace | 52 } // namespace |
| 52 | 53 |
| 53 extern "C" JNIEXPORT void JNICALL | 54 extern "C" JNIEXPORT void JNICALL |
| 54 Java_org_webrtc_VideoTrack_nativeAddSink(JNIEnv* jni, | 55 Java_org_webrtc_VideoTrack_nativeAddSink(JNIEnv* jni, |
| 55 jclass, | 56 jclass, |
| 56 jlong j_native_track, | 57 jlong j_native_track, |
| 57 jlong j_native_sink) { | 58 jlong j_native_sink) { |
| 58 reinterpret_cast<webrtc::VideoTrackInterface*>(j_native_track) | 59 reinterpret_cast<VideoTrackInterface*>(j_native_track) |
| 59 ->AddOrUpdateSink( | 60 ->AddOrUpdateSink( |
| 60 reinterpret_cast<rtc::VideoSinkInterface<webrtc::VideoFrame>*>( | 61 reinterpret_cast<rtc::VideoSinkInterface<VideoFrame>*>(j_native_sink), |
| 61 j_native_sink), | |
| 62 rtc::VideoSinkWants()); | 62 rtc::VideoSinkWants()); |
| 63 } | 63 } |
| 64 | 64 |
| 65 extern "C" JNIEXPORT void JNICALL | 65 extern "C" JNIEXPORT void JNICALL |
| 66 Java_org_webrtc_VideoTrack_nativeRemoveSink(JNIEnv* jni, | 66 Java_org_webrtc_VideoTrack_nativeRemoveSink(JNIEnv* jni, |
| 67 jclass, | 67 jclass, |
| 68 jlong j_native_track, | 68 jlong j_native_track, |
| 69 jlong j_native_sink) { | 69 jlong j_native_sink) { |
| 70 reinterpret_cast<webrtc::VideoTrackInterface*>(j_native_track) | 70 reinterpret_cast<VideoTrackInterface*>(j_native_track) |
| 71 ->RemoveSink( | 71 ->RemoveSink(reinterpret_cast<rtc::VideoSinkInterface<VideoFrame>*>( |
| 72 reinterpret_cast<rtc::VideoSinkInterface<webrtc::VideoFrame>*>( | 72 j_native_sink)); |
| 73 j_native_sink)); | |
| 74 } | 73 } |
| 75 | 74 |
| 76 extern "C" JNIEXPORT jlong JNICALL | 75 extern "C" JNIEXPORT jlong JNICALL |
| 77 Java_org_webrtc_VideoTrack_nativeWrapSink(JNIEnv* jni, jclass, jobject sink) { | 76 Java_org_webrtc_VideoTrack_nativeWrapSink(JNIEnv* jni, jclass, jobject sink) { |
| 78 return jlongFromPointer(new VideoSinkWrapper(jni, sink)); | 77 return jlongFromPointer(new VideoSinkWrapper(jni, sink)); |
| 79 } | 78 } |
| 80 | 79 |
| 81 extern "C" JNIEXPORT void JNICALL | 80 extern "C" JNIEXPORT void JNICALL |
| 82 Java_org_webrtc_VideoTrack_nativeFreeSink(JNIEnv* jni, | 81 Java_org_webrtc_VideoTrack_nativeFreeSink(JNIEnv* jni, |
| 83 jclass, | 82 jclass, |
| 84 jlong j_native_sink) { | 83 jlong j_native_sink) { |
| 85 delete reinterpret_cast<rtc::VideoSinkInterface<webrtc::VideoFrame>*>( | 84 delete reinterpret_cast<rtc::VideoSinkInterface<VideoFrame>*>(j_native_sink); |
| 86 j_native_sink); | |
| 87 } | 85 } |
| 88 | 86 |
| 89 } // namespace webrtc_jni | 87 } // namespace jni |
| 88 } // namespace webrtc |
| OLD | NEW |