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 "sdk/android/src/jni/pc/sdp_observer.h"
12 
13 #include <utility>
14 
15 #include "sdk/android/generated_peerconnection_jni/SdpObserver_jni.h"
16 #include "sdk/android/native_api/jni/java_types.h"
17 #include "sdk/android/src/jni/jni_helpers.h"
18 #include "sdk/media_constraints.h"
19 
20 namespace webrtc {
21 namespace jni {
22 
CreateSdpObserverJni(JNIEnv * env,const JavaRef<jobject> & j_observer,std::unique_ptr<MediaConstraints> constraints)23 CreateSdpObserverJni::CreateSdpObserverJni(
24     JNIEnv* env,
25     const JavaRef<jobject>& j_observer,
26     std::unique_ptr<MediaConstraints> constraints)
27     : j_observer_global_(env, j_observer),
28       constraints_(std::move(constraints)) {}
29 
30 CreateSdpObserverJni::~CreateSdpObserverJni() = default;
31 
OnSuccess(SessionDescriptionInterface * desc)32 void CreateSdpObserverJni::OnSuccess(SessionDescriptionInterface* desc) {
33   JNIEnv* env = AttachCurrentThreadIfNeeded();
34   std::string sdp;
35   RTC_CHECK(desc->ToString(&sdp)) << "got so far: " << sdp;
36   Java_SdpObserver_onCreateSuccess(
37       env, j_observer_global_,
38       NativeToJavaSessionDescription(env, sdp, desc->type()));
39   // OnSuccess transfers ownership of the description (there's a TODO to make
40   // it use unique_ptr...).
41   delete desc;
42 }
43 
OnFailure(webrtc::RTCError error)44 void CreateSdpObserverJni::OnFailure(webrtc::RTCError error) {
45   JNIEnv* env = AttachCurrentThreadIfNeeded();
46   Java_SdpObserver_onCreateFailure(env, j_observer_global_,
47                                    NativeToJavaString(env, error.message()));
48 }
49 
SetSdpObserverJni(JNIEnv * env,const JavaRef<jobject> & j_observer,std::unique_ptr<MediaConstraints> constraints)50 SetSdpObserverJni::SetSdpObserverJni(
51     JNIEnv* env,
52     const JavaRef<jobject>& j_observer,
53     std::unique_ptr<MediaConstraints> constraints)
54     : j_observer_global_(env, j_observer),
55       constraints_(std::move(constraints)) {}
56 
57 SetSdpObserverJni::~SetSdpObserverJni() = default;
58 
OnSuccess()59 void SetSdpObserverJni::OnSuccess() {
60   JNIEnv* env = AttachCurrentThreadIfNeeded();
61   Java_SdpObserver_onSetSuccess(env, j_observer_global_);
62 }
63 
OnFailure(webrtc::RTCError error)64 void SetSdpObserverJni::OnFailure(webrtc::RTCError error) {
65   JNIEnv* env = AttachCurrentThreadIfNeeded();
66   Java_SdpObserver_onSetFailure(env, j_observer_global_,
67                                 NativeToJavaString(env, error.message()));
68 }
69 
70 }  // namespace jni
71 }  // namespace webrtc
72