OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2017 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 "webrtc/sdk/android/src/jni/pc/statsobserver_jni.h" | |
12 | |
13 #include "webrtc/sdk/android/src/jni/classreferenceholder.h" | |
14 | |
15 namespace webrtc_jni { | |
16 | |
17 // Convenience, used since callbacks occur on the signaling thread, which may | |
18 // be a non-Java thread. | |
19 static JNIEnv* jni() { | |
20 return AttachCurrentThreadIfNeeded(); | |
21 } | |
22 | |
23 StatsObserverJni::StatsObserverJni(JNIEnv* jni, jobject j_observer) | |
24 : j_observer_global_(jni, j_observer), | |
25 j_observer_class_(jni, GetObjectClass(jni, j_observer)), | |
26 j_stats_report_class_(jni, FindClass(jni, "org/webrtc/StatsReport")), | |
27 j_stats_report_ctor_(GetMethodID(jni, | |
28 *j_stats_report_class_, | |
29 "<init>", | |
30 "(Ljava/lang/String;Ljava/lang/String;D" | |
31 "[Lorg/webrtc/StatsReport$Value;)V")), | |
32 j_value_class_(jni, FindClass(jni, "org/webrtc/StatsReport$Value")), | |
33 j_value_ctor_(GetMethodID(jni, | |
34 *j_value_class_, | |
35 "<init>", | |
36 "(Ljava/lang/String;Ljava/lang/String;)V")) {} | |
37 | |
38 void StatsObserverJni::OnComplete(const webrtc::StatsReports& reports) { | |
39 ScopedLocalRefFrame local_ref_frame(jni()); | |
40 jobjectArray j_reports = ReportsToJava(jni(), reports); | |
41 jmethodID m = GetMethodID(jni(), *j_observer_class_, "onComplete", | |
42 "([Lorg/webrtc/StatsReport;)V"); | |
43 jni()->CallVoidMethod(*j_observer_global_, m, j_reports); | |
44 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; | |
45 } | |
46 | |
47 jobjectArray StatsObserverJni::ReportsToJava( | |
48 JNIEnv* jni, | |
49 const webrtc::StatsReports& reports) { | |
50 jobjectArray reports_array = | |
51 jni->NewObjectArray(reports.size(), *j_stats_report_class_, NULL); | |
52 int i = 0; | |
53 for (const auto* report : reports) { | |
54 ScopedLocalRefFrame local_ref_frame(jni); | |
55 jstring j_id = JavaStringFromStdString(jni, report->id()->ToString()); | |
56 jstring j_type = JavaStringFromStdString(jni, report->TypeToString()); | |
57 jobjectArray j_values = ValuesToJava(jni, report->values()); | |
58 jobject j_report = | |
59 jni->NewObject(*j_stats_report_class_, j_stats_report_ctor_, j_id, | |
60 j_type, report->timestamp(), j_values); | |
61 jni->SetObjectArrayElement(reports_array, i++, j_report); | |
62 } | |
63 return reports_array; | |
64 } | |
65 | |
66 jobjectArray StatsObserverJni::ValuesToJava( | |
67 JNIEnv* jni, | |
68 const webrtc::StatsReport::Values& values) { | |
69 jobjectArray j_values = | |
70 jni->NewObjectArray(values.size(), *j_value_class_, NULL); | |
71 int i = 0; | |
72 for (const auto& it : values) { | |
73 ScopedLocalRefFrame local_ref_frame(jni); | |
74 // Should we use the '.name' enum value here instead of converting the | |
75 // name to a string? | |
76 jstring j_name = JavaStringFromStdString(jni, it.second->display_name()); | |
77 jstring j_value = JavaStringFromStdString(jni, it.second->ToString()); | |
78 jobject j_element_value = | |
79 jni->NewObject(*j_value_class_, j_value_ctor_, j_name, j_value); | |
80 jni->SetObjectArrayElement(j_values, i++, j_element_value); | |
81 } | |
82 return j_values; | |
83 } | |
84 | |
85 } // namespace webrtc_jni | |
OLD | NEW |