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 #ifndef WEBRTC_SDK_ANDROID_SRC_JNI_RTCSTATSCOLLECTORCALLBACKWRAPPER_H_ | 11 #ifndef WEBRTC_SDK_ANDROID_SRC_JNI_PC_SDPOBSERVER_JNI_H_ |
12 #define WEBRTC_SDK_ANDROID_SRC_JNI_RTCSTATSCOLLECTORCALLBACKWRAPPER_H_ | 12 #define WEBRTC_SDK_ANDROID_SRC_JNI_PC_SDPOBSERVER_JNI_H_ |
13 | 13 |
14 #include <jni.h> | 14 #include <memory> |
| 15 #include <string> |
15 | 16 |
16 #include "webrtc/api/peerconnectioninterface.h" | 17 #include "webrtc/api/peerconnectioninterface.h" |
17 #include "webrtc/sdk/android/src/jni/jni_helpers.h" | 18 #include "webrtc/sdk/android/src/jni/jni_helpers.h" |
| 19 #include "webrtc/sdk/android/src/jni/pc/mediaconstraints_jni.h" |
18 | 20 |
19 namespace webrtc_jni { | 21 namespace webrtc_jni { |
20 | 22 |
21 // Adapter for a Java RTCStatsCollectorCallback presenting a C++ | 23 // Adapter for a Java StatsObserver presenting a C++ |
22 // RTCStatsCollectorCallback and dispatching the callback from C++ back to | 24 // CreateSessionDescriptionObserver or SetSessionDescriptionObserver and |
23 // Java. | 25 // dispatching the callback from C++ back to Java. |
24 class RTCStatsCollectorCallbackWrapper | 26 template <class T> // T is one of {Create,Set}SessionDescriptionObserver. |
25 : public webrtc::RTCStatsCollectorCallback { | 27 class SdpObserverJni : public T { |
26 public: | 28 public: |
27 RTCStatsCollectorCallbackWrapper(JNIEnv* jni, jobject j_callback); | 29 SdpObserverJni(JNIEnv* jni, |
| 30 jobject j_observer, |
| 31 MediaConstraintsJni* constraints) |
| 32 : constraints_(constraints), |
| 33 j_observer_global_(jni, j_observer), |
| 34 j_observer_class_(jni, GetObjectClass(jni, j_observer)) {} |
28 | 35 |
29 void OnStatsDelivered( | 36 virtual ~SdpObserverJni() {} |
30 const rtc::scoped_refptr<const webrtc::RTCStatsReport>& report) override; | 37 |
| 38 // Can't mark override because of templating. |
| 39 virtual void OnSuccess() { |
| 40 ScopedLocalRefFrame local_ref_frame(jni()); |
| 41 jmethodID m = GetMethodID(jni(), *j_observer_class_, "onSetSuccess", "()V"); |
| 42 jni()->CallVoidMethod(*j_observer_global_, m); |
| 43 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; |
| 44 } |
| 45 |
| 46 // Can't mark override because of templating. |
| 47 virtual void OnSuccess(webrtc::SessionDescriptionInterface* desc) { |
| 48 ScopedLocalRefFrame local_ref_frame(jni()); |
| 49 jmethodID m = GetMethodID(jni(), *j_observer_class_, "onCreateSuccess", |
| 50 "(Lorg/webrtc/SessionDescription;)V"); |
| 51 jobject j_sdp = NativeToJavaSessionDescription(jni(), desc); |
| 52 jni()->CallVoidMethod(*j_observer_global_, m, j_sdp); |
| 53 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; |
| 54 // OnSuccess transfers ownership of the description (there's a TODO to make |
| 55 // it use unique_ptr...). |
| 56 delete desc; |
| 57 } |
| 58 |
| 59 protected: |
| 60 // Common implementation for failure of Set & Create types, distinguished by |
| 61 // |op| being "Set" or "Create". |
| 62 void DoOnFailure(const std::string& op, const std::string& error) { |
| 63 jmethodID m = GetMethodID(jni(), *j_observer_class_, "on" + op + "Failure", |
| 64 "(Ljava/lang/String;)V"); |
| 65 jstring j_error_string = JavaStringFromStdString(jni(), error); |
| 66 jni()->CallVoidMethod(*j_observer_global_, m, j_error_string); |
| 67 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; |
| 68 } |
| 69 |
| 70 JNIEnv* jni() { return AttachCurrentThreadIfNeeded(); } |
31 | 71 |
32 private: | 72 private: |
33 // Helper functions for converting C++ RTCStatsReport to Java equivalent. | 73 std::unique_ptr<MediaConstraintsJni> constraints_; |
34 jobject ReportToJava( | 74 const ScopedGlobalRef<jobject> j_observer_global_; |
35 JNIEnv* jni, | 75 const ScopedGlobalRef<jclass> j_observer_class_; |
36 const rtc::scoped_refptr<const webrtc::RTCStatsReport>& report); | 76 }; |
37 jobject StatsToJava(JNIEnv* jni, const webrtc::RTCStats& stats); | |
38 jobject MemberToJava(JNIEnv* jni, | |
39 const webrtc::RTCStatsMemberInterface* member); | |
40 | 77 |
41 const ScopedGlobalRef<jobject> j_callback_global_; | 78 class CreateSdpObserverJni |
42 const ScopedGlobalRef<jclass> j_callback_class_; | 79 : public SdpObserverJni<webrtc::CreateSessionDescriptionObserver> { |
43 const jclass j_stats_report_class_; | 80 public: |
44 const jmethodID j_stats_report_ctor_; | 81 CreateSdpObserverJni(JNIEnv* jni, |
45 const jclass j_stats_class_; | 82 jobject j_observer, |
46 const jmethodID j_stats_ctor_; | 83 MediaConstraintsJni* constraints) |
47 const jclass j_linked_hash_map_class_; | 84 : SdpObserverJni(jni, j_observer, constraints) {} |
48 const jmethodID j_linked_hash_map_ctor_; | 85 |
49 const jmethodID j_linked_hash_map_put_; | 86 void OnFailure(const std::string& error) override { |
50 const jclass j_boolean_class_; | 87 ScopedLocalRefFrame local_ref_frame(jni()); |
51 const jmethodID j_boolean_ctor_; | 88 SdpObserverJni::DoOnFailure(std::string("Create"), error); |
52 const jclass j_integer_class_; | 89 } |
53 const jmethodID j_integer_ctor_; | 90 }; |
54 const jclass j_long_class_; | 91 |
55 const jmethodID j_long_ctor_; | 92 class SetSdpObserverJni |
56 const jclass j_big_integer_class_; | 93 : public SdpObserverJni<webrtc::SetSessionDescriptionObserver> { |
57 const jmethodID j_big_integer_ctor_; | 94 public: |
58 const jclass j_double_class_; | 95 SetSdpObserverJni(JNIEnv* jni, |
59 const jmethodID j_double_ctor_; | 96 jobject j_observer, |
60 const jclass j_string_class_; | 97 MediaConstraintsJni* constraints) |
| 98 : SdpObserverJni(jni, j_observer, constraints) {} |
| 99 |
| 100 void OnFailure(const std::string& error) override { |
| 101 ScopedLocalRefFrame local_ref_frame(jni()); |
| 102 SdpObserverJni::DoOnFailure(std::string("Set"), error); |
| 103 } |
61 }; | 104 }; |
62 | 105 |
63 } // namespace webrtc_jni | 106 } // namespace webrtc_jni |
64 | 107 |
65 #endif // WEBRTC_SDK_ANDROID_SRC_JNI_RTCSTATSCOLLECTORCALLBACKWRAPPER_H_ | 108 #endif // WEBRTC_SDK_ANDROID_SRC_JNI_PC_SDPOBSERVER_JNI_H_ |
OLD | NEW |