Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/devtools_bridge/android/session_dependency_factory_android. h" | |
| 6 | |
| 7 #include "base/android/jni_string.h" | |
| 8 #include "base/android/scoped_java_ref.h" | |
| 9 #include "base/bind.h" | |
| 10 #include "components/devtools_bridge/abstract_data_channel.h" | |
| 11 #include "components/devtools_bridge/abstract_peer_connection.h" | |
| 12 #include "components/devtools_bridge/rtc_configuration.h" | |
| 13 #include "jni/SessionDependencyFactoryNative_jni.h" | |
| 14 | |
| 15 namespace devtools_bridge { | |
| 16 namespace android { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 class JNIInvokeUtil { | |
|
mnaganov (inactive)
2014/11/10 18:10:26
We don't usually use such helper classes: this one
SeRya
2014/11/11 08:08:19
Done.
| |
| 21 public: | |
| 22 JNIInvokeUtil() { | |
| 23 env_ = base::android::AttachCurrentThread(); | |
| 24 } | |
| 25 | |
| 26 JNIEnv* env() { return env_; } | |
| 27 | |
| 28 jstring convert(std::string const& str) { | |
| 29 return base::android::ConvertUTF8ToJavaString(env_, str).Release(); | |
| 30 } | |
| 31 | |
| 32 private: | |
| 33 JNIEnv* env_; | |
| 34 }; | |
| 35 | |
| 36 } // namespace | |
| 37 | |
| 38 class PeerConnectionDelegateImpl | |
| 39 : public AbstractPeerConnection::Delegate { | |
| 40 public: | |
| 41 PeerConnectionDelegateImpl(JNIEnv* env, jobject java_object) { | |
| 42 java_object_.Reset(env, java_object); | |
| 43 connected_ = false; | |
| 44 } | |
| 45 | |
| 46 virtual void OnIceConnectionChange(bool connected) override { | |
| 47 JNIInvokeUtil util; | |
| 48 Java_SessionDependencyFactoryNative_notifyIceConnectionChange( | |
| 49 util.env(), java_object_.obj(), connected); | |
| 50 } | |
| 51 | |
| 52 virtual void OnIceCandidate( | |
| 53 std::string const& sdp_mid, int sdp_mline_index, std::string const& sdp) | |
| 54 override { | |
| 55 JNIInvokeUtil util; | |
| 56 Java_SessionDependencyFactoryNative_notifyIceCandidate( | |
| 57 util.env(), java_object_.obj(), util.convert(sdp_mid), | |
| 58 sdp_mline_index, util.convert(sdp)); | |
| 59 } | |
| 60 | |
| 61 void NotifyLocalOfferCreatedAndSetSet(std::string const& description) { | |
| 62 JNIInvokeUtil util; | |
| 63 Java_SessionDependencyFactoryNative_notifyLocalOfferCreatedAndSetSet( | |
| 64 util.env(), java_object_.obj(), util.convert(description)); | |
| 65 } | |
| 66 | |
| 67 virtual void OnLocalOfferCreatedAndSetSet(std::string const& description) | |
| 68 override { | |
| 69 JNIInvokeUtil util; | |
| 70 Java_SessionDependencyFactoryNative_notifyLocalOfferCreatedAndSetSet( | |
| 71 util.env(), java_object_.obj(), util.convert(description)); | |
| 72 } | |
| 73 | |
| 74 virtual void OnLocalAnswerCreatedAndSetSet(std::string const& description) | |
| 75 override { | |
| 76 JNIInvokeUtil util; | |
| 77 Java_SessionDependencyFactoryNative_notifyLocalAnswerCreatedAndSetSet( | |
| 78 util.env(), java_object_.obj(), util.convert(description)); | |
| 79 } | |
| 80 | |
| 81 virtual void OnRemoteDescriptionSet() override { | |
| 82 JNIInvokeUtil util; | |
| 83 Java_SessionDependencyFactoryNative_notifyRemoteDescriptionSet( | |
| 84 util.env(), java_object_.obj()); | |
| 85 } | |
| 86 | |
| 87 virtual void OnFailure(std::string const& description) override { | |
| 88 JNIInvokeUtil util; | |
| 89 Java_SessionDependencyFactoryNative_notifyConnectionFailure( | |
| 90 util.env(), java_object_.obj(), util.convert(description)); | |
| 91 } | |
| 92 | |
| 93 private: | |
| 94 base::android::ScopedJavaGlobalRef<jobject> java_object_; | |
| 95 bool connected_; | |
| 96 }; | |
| 97 | |
| 98 namespace { | |
| 99 | |
| 100 class JNIHandlingUtil { | |
| 101 public: | |
| 102 JNIHandlingUtil(JNIEnv* env) : env_(env) { | |
| 103 } | |
| 104 | |
| 105 static jlong cast(void* obj) { | |
| 106 return reinterpret_cast<jlong>(obj); | |
| 107 } | |
| 108 | |
| 109 static RTCConfiguration* toConfig(jlong ptr) { | |
| 110 return reinterpret_cast<RTCConfiguration*>(ptr); | |
| 111 } | |
| 112 | |
| 113 static SessionDependencyFactoryAndroid* toFactory( | |
| 114 jlong ptr) { | |
| 115 return reinterpret_cast<SessionDependencyFactoryAndroid*>(ptr); | |
| 116 } | |
| 117 | |
| 118 static AbstractPeerConnection* toConnection(jlong ptr) { | |
| 119 return reinterpret_cast<AbstractPeerConnection*>(ptr); | |
| 120 } | |
| 121 | |
| 122 static AbstractDataChannel* toDataChannel(jlong ptr) { | |
| 123 return reinterpret_cast<AbstractDataChannel*>(ptr); | |
| 124 } | |
| 125 | |
| 126 std::string convert(jstring str) { | |
| 127 return base::android::ConvertJavaStringToUTF8(env_, str); | |
| 128 } | |
| 129 | |
| 130 private: | |
| 131 JNIEnv* const env_; | |
| 132 }; | |
| 133 | |
| 134 static void CleanupOnSignalingThread() { | |
| 135 // Called on signaling thread when SessionDependencyFactory is destroying. | |
| 136 base::android::DetachFromVM(); | |
| 137 } | |
| 138 | |
| 139 } // namespace | |
| 140 | |
| 141 // SessionDependencyFactoryNative | |
| 142 | |
| 143 SessionDependencyFactoryAndroid::SessionDependencyFactoryAndroid() | |
| 144 : impl_(SessionDependencyFactory::CreateInstance( | |
| 145 base::Bind(&CleanupOnSignalingThread))) { | |
| 146 } | |
| 147 | |
| 148 SessionDependencyFactoryAndroid::~SessionDependencyFactoryAndroid() { | |
| 149 } | |
| 150 | |
| 151 // static | |
| 152 void SessionDependencyFactoryAndroid::RegisterNatives(JNIEnv* env) { | |
| 153 RegisterNativesImpl(env); | |
| 154 } | |
| 155 | |
| 156 scoped_ptr<AbstractPeerConnection> | |
| 157 SessionDependencyFactoryAndroid::CreatePeerConnection( | |
| 158 scoped_ptr<RTCConfiguration> config, | |
| 159 scoped_ptr<AbstractPeerConnection::Delegate> delegate) { | |
| 160 return impl_->CreatePeerConnection(config.Pass(), delegate.Pass()); | |
| 161 } | |
| 162 | |
| 163 // JNI generated methods | |
| 164 | |
| 165 static jlong CreateFactory(JNIEnv* env, jclass jcaller) { | |
| 166 return JNIHandlingUtil::cast(new SessionDependencyFactoryAndroid()); | |
| 167 } | |
| 168 | |
| 169 static void DestroyFactory(JNIEnv* env, jclass jcaller, jlong ptr) { | |
| 170 delete JNIHandlingUtil::toFactory(ptr); | |
| 171 } | |
| 172 | |
| 173 static jlong CreateConfig(JNIEnv* env, jclass jcaller) { | |
| 174 return JNIHandlingUtil::cast( | |
| 175 RTCConfiguration::CreateInstance().release()); | |
| 176 } | |
| 177 | |
| 178 static void AddIceServer( | |
| 179 JNIEnv* env, jclass jcaller, jlong configPtr, | |
| 180 jstring uri, jstring username, jstring credential) { | |
| 181 JNIHandlingUtil util(env); | |
| 182 | |
| 183 JNIHandlingUtil::toConfig(configPtr)->AddIceServer( | |
| 184 util.convert(uri), util.convert(username), util.convert(credential)); | |
| 185 } | |
| 186 | |
| 187 static jlong CreatePeerConnection( | |
| 188 JNIEnv* env, jclass jcaller, | |
| 189 jlong factoryPtr, jlong configPtr, jobject observer) { | |
| 190 auto factory = JNIHandlingUtil::toFactory(factoryPtr); | |
| 191 auto config = JNIHandlingUtil::toConfig(configPtr); | |
| 192 | |
| 193 auto delegate = new PeerConnectionDelegateImpl(env, observer); | |
| 194 | |
| 195 return JNIHandlingUtil::cast(factory->CreatePeerConnection( | |
| 196 make_scoped_ptr(config), make_scoped_ptr(delegate)).release()); | |
| 197 } | |
| 198 | |
| 199 static void DestroyPeerConnection( | |
| 200 JNIEnv* env, jclass jcaller, jlong peerConnectionPtr) { | |
| 201 delete JNIHandlingUtil::toConnection(peerConnectionPtr); | |
| 202 } | |
| 203 | |
| 204 static void CreateAndSetLocalOffer( | |
| 205 JNIEnv* env, jclass jcaller, jlong peerConnectionPtr) { | |
| 206 JNIHandlingUtil::toConnection(peerConnectionPtr)->CreateAndSetLocalOffer(); | |
| 207 } | |
| 208 | |
| 209 static void CreateAndSetLocalAnswer( | |
| 210 JNIEnv* env, jclass jcaller, jlong peerConnectionPtr) { | |
| 211 JNIHandlingUtil::toConnection(peerConnectionPtr)->CreateAndSetLocalAnswer(); | |
| 212 } | |
| 213 | |
| 214 static void SetRemoteOffer( | |
| 215 JNIEnv* env, jclass jcaller, jlong peerConnectionPtr, jstring description) { | |
| 216 JNIHandlingUtil util(env); | |
| 217 JNIHandlingUtil::toConnection(peerConnectionPtr)->SetRemoteOffer( | |
| 218 util.convert(description)); | |
| 219 } | |
| 220 | |
| 221 static void SetRemoteAnswer( | |
| 222 JNIEnv* env, jclass jcaller, jlong peerConnectionPtr, jstring description) { | |
| 223 JNIHandlingUtil util(env); | |
| 224 JNIHandlingUtil::toConnection(peerConnectionPtr)->SetRemoteAnswer( | |
| 225 util.convert(description)); | |
| 226 } | |
| 227 | |
| 228 static void AddIceCandidate( | |
| 229 JNIEnv* env, jclass jcaller, | |
| 230 jlong peerConnectionPtr, jstring sdpMid, jint sdpMLineIndex, jstring sdp) { | |
| 231 JNIHandlingUtil util(env); | |
| 232 util.toConnection(peerConnectionPtr)->AddIceCandidate( | |
| 233 util.convert(sdpMid), sdpMLineIndex, util.convert(sdp)); | |
| 234 } | |
| 235 | |
| 236 static jlong CreateDataChannel( | |
| 237 JNIEnv* env, jclass jcaller, jlong peerConnectionPtr, jint channelId) { | |
| 238 return JNIHandlingUtil::cast( | |
| 239 JNIHandlingUtil::toConnection( | |
| 240 peerConnectionPtr)->CreateDataChannel(channelId).release()); | |
| 241 } | |
| 242 | |
| 243 static void DestroyDataChannel( | |
| 244 JNIEnv* env, jclass jcaller, jlong dataChannelPtr) { | |
| 245 delete JNIHandlingUtil::toDataChannel(dataChannelPtr); | |
| 246 } | |
| 247 | |
| 248 } // namespace android | |
| 249 } // namespace devtools_bridge | |
| OLD | NEW |