1 /*
2 * Copyright 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/src/jni/pc/rtp_transceiver.h"
12
13 #include <string>
14
15 #include "sdk/android/generated_peerconnection_jni/RtpTransceiver_jni.h"
16 #include "sdk/android/native_api/jni/java_types.h"
17 #include "sdk/android/src/jni/jni_helpers.h"
18 #include "sdk/android/src/jni/pc/media_stream_track.h"
19 #include "sdk/android/src/jni/pc/rtp_parameters.h"
20 #include "sdk/android/src/jni/pc/rtp_receiver.h"
21 #include "sdk/android/src/jni/pc/rtp_sender.h"
22
23 namespace webrtc {
24 namespace jni {
25
26 namespace {
27
NativeToJavaRtpTransceiverDirection(JNIEnv * jni,RtpTransceiverDirection rtp_transceiver_direction)28 ScopedJavaLocalRef<jobject> NativeToJavaRtpTransceiverDirection(
29 JNIEnv* jni,
30 RtpTransceiverDirection rtp_transceiver_direction) {
31 return Java_RtpTransceiverDirection_fromNativeIndex(
32 jni, static_cast<int>(rtp_transceiver_direction));
33 }
34
35 } // namespace
36
JavaToNativeRtpTransceiverInit(JNIEnv * jni,const JavaRef<jobject> & j_init)37 RtpTransceiverInit JavaToNativeRtpTransceiverInit(
38 JNIEnv* jni,
39 const JavaRef<jobject>& j_init) {
40 RtpTransceiverInit init;
41
42 // Convert the direction.
43 init.direction = static_cast<RtpTransceiverDirection>(
44 Java_RtpTransceiverInit_getDirectionNativeIndex(jni, j_init));
45
46 // Convert the stream ids.
47 ScopedJavaLocalRef<jobject> j_stream_ids =
48 Java_RtpTransceiverInit_getStreamIds(jni, j_init);
49 init.stream_ids = JavaListToNativeVector<std::string, jstring>(
50 jni, j_stream_ids, &JavaToNativeString);
51
52 // Convert the send encodings.
53 ScopedJavaLocalRef<jobject> j_send_encodings =
54 Java_RtpTransceiverInit_getSendEncodings(jni, j_init);
55 init.send_encodings = JavaListToNativeVector<RtpEncodingParameters, jobject>(
56 jni, j_send_encodings, &JavaToNativeRtpEncodingParameters);
57 return init;
58 }
59
NativeToJavaRtpTransceiver(JNIEnv * env,rtc::scoped_refptr<RtpTransceiverInterface> transceiver)60 ScopedJavaLocalRef<jobject> NativeToJavaRtpTransceiver(
61 JNIEnv* env,
62 rtc::scoped_refptr<RtpTransceiverInterface> transceiver) {
63 if (!transceiver) {
64 return nullptr;
65 }
66 // Transceiver will now have shared ownership by the Java object.
67 return Java_RtpTransceiver_Constructor(
68 env, jlongFromPointer(transceiver.release()));
69 }
70
JavaRtpTransceiverGlobalOwner(JNIEnv * env,const JavaRef<jobject> & j_transceiver)71 JavaRtpTransceiverGlobalOwner::JavaRtpTransceiverGlobalOwner(
72 JNIEnv* env,
73 const JavaRef<jobject>& j_transceiver)
74 : j_transceiver_(env, j_transceiver) {}
75
76 JavaRtpTransceiverGlobalOwner::JavaRtpTransceiverGlobalOwner(
77 JavaRtpTransceiverGlobalOwner&& other) = default;
78
~JavaRtpTransceiverGlobalOwner()79 JavaRtpTransceiverGlobalOwner::~JavaRtpTransceiverGlobalOwner() {
80 if (j_transceiver_.obj()) {
81 Java_RtpTransceiver_dispose(AttachCurrentThreadIfNeeded(), j_transceiver_);
82 }
83 }
84
JNI_RtpTransceiver_GetMediaType(JNIEnv * jni,jlong j_rtp_transceiver_pointer)85 ScopedJavaLocalRef<jobject> JNI_RtpTransceiver_GetMediaType(
86 JNIEnv* jni,
87 jlong j_rtp_transceiver_pointer) {
88 return NativeToJavaMediaType(
89 jni, reinterpret_cast<RtpTransceiverInterface*>(j_rtp_transceiver_pointer)
90 ->media_type());
91 }
92
JNI_RtpTransceiver_GetMid(JNIEnv * jni,jlong j_rtp_transceiver_pointer)93 ScopedJavaLocalRef<jstring> JNI_RtpTransceiver_GetMid(
94 JNIEnv* jni,
95 jlong j_rtp_transceiver_pointer) {
96 absl::optional<std::string> mid =
97 reinterpret_cast<RtpTransceiverInterface*>(j_rtp_transceiver_pointer)
98 ->mid();
99 return NativeToJavaString(jni, mid);
100 }
101
JNI_RtpTransceiver_GetSender(JNIEnv * jni,jlong j_rtp_transceiver_pointer)102 ScopedJavaLocalRef<jobject> JNI_RtpTransceiver_GetSender(
103 JNIEnv* jni,
104 jlong j_rtp_transceiver_pointer) {
105 return NativeToJavaRtpSender(
106 jni, reinterpret_cast<RtpTransceiverInterface*>(j_rtp_transceiver_pointer)
107 ->sender());
108 }
109
JNI_RtpTransceiver_GetReceiver(JNIEnv * jni,jlong j_rtp_transceiver_pointer)110 ScopedJavaLocalRef<jobject> JNI_RtpTransceiver_GetReceiver(
111 JNIEnv* jni,
112 jlong j_rtp_transceiver_pointer) {
113 return NativeToJavaRtpReceiver(
114 jni, reinterpret_cast<RtpTransceiverInterface*>(j_rtp_transceiver_pointer)
115 ->receiver());
116 }
117
JNI_RtpTransceiver_Stopped(JNIEnv * jni,jlong j_rtp_transceiver_pointer)118 jboolean JNI_RtpTransceiver_Stopped(JNIEnv* jni,
119 jlong j_rtp_transceiver_pointer) {
120 return reinterpret_cast<RtpTransceiverInterface*>(j_rtp_transceiver_pointer)
121 ->stopped();
122 }
123
JNI_RtpTransceiver_Direction(JNIEnv * jni,jlong j_rtp_transceiver_pointer)124 ScopedJavaLocalRef<jobject> JNI_RtpTransceiver_Direction(
125 JNIEnv* jni,
126 jlong j_rtp_transceiver_pointer) {
127 return NativeToJavaRtpTransceiverDirection(
128 jni, reinterpret_cast<RtpTransceiverInterface*>(j_rtp_transceiver_pointer)
129 ->direction());
130 }
131
JNI_RtpTransceiver_CurrentDirection(JNIEnv * jni,jlong j_rtp_transceiver_pointer)132 ScopedJavaLocalRef<jobject> JNI_RtpTransceiver_CurrentDirection(
133 JNIEnv* jni,
134 jlong j_rtp_transceiver_pointer) {
135 absl::optional<RtpTransceiverDirection> direction =
136 reinterpret_cast<RtpTransceiverInterface*>(j_rtp_transceiver_pointer)
137 ->current_direction();
138 return direction ? NativeToJavaRtpTransceiverDirection(jni, *direction)
139 : nullptr;
140 }
141
JNI_RtpTransceiver_StopInternal(JNIEnv * jni,jlong j_rtp_transceiver_pointer)142 void JNI_RtpTransceiver_StopInternal(JNIEnv* jni,
143 jlong j_rtp_transceiver_pointer) {
144 reinterpret_cast<RtpTransceiverInterface*>(j_rtp_transceiver_pointer)
145 ->StopInternal();
146 }
147
JNI_RtpTransceiver_StopStandard(JNIEnv * jni,jlong j_rtp_transceiver_pointer)148 void JNI_RtpTransceiver_StopStandard(JNIEnv* jni,
149 jlong j_rtp_transceiver_pointer) {
150 reinterpret_cast<RtpTransceiverInterface*>(j_rtp_transceiver_pointer)
151 ->StopStandard();
152 }
153
JNI_RtpTransceiver_SetDirection(JNIEnv * jni,jlong j_rtp_transceiver_pointer,const base::android::JavaParamRef<jobject> & j_rtp_transceiver_direction)154 jboolean JNI_RtpTransceiver_SetDirection(
155 JNIEnv* jni,
156 jlong j_rtp_transceiver_pointer,
157 const base::android::JavaParamRef<jobject>& j_rtp_transceiver_direction) {
158 if (IsNull(jni, j_rtp_transceiver_direction)) {
159 return false;
160 }
161 RtpTransceiverDirection direction = static_cast<RtpTransceiverDirection>(
162 Java_RtpTransceiverDirection_getNativeIndex(jni,
163 j_rtp_transceiver_direction));
164 webrtc::RTCError error =
165 reinterpret_cast<RtpTransceiverInterface*>(j_rtp_transceiver_pointer)
166 ->SetDirectionWithError(direction);
167 if (!error.ok()) {
168 RTC_LOG(LS_WARNING) << "SetDirection failed, code "
169 << ToString(error.type()) << ", message "
170 << error.message();
171 }
172 return error.ok();
173 }
174
175 } // namespace jni
176 } // namespace webrtc
177