Sami Kalliomäki | 3e77afd | 2018-03-08 16:43:16 +0100 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "examples/androidnativeapi/jni/android_call_client.h" |
Sami Kalliomäki | 3e77afd | 2018-03-08 16:43:16 +0100 | [diff] [blame] | 12 | |
| 13 | #include <utility> |
| 14 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 15 | #include "absl/memory/memory.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 16 | #include "api/peer_connection_interface.h" |
Danil Chapovalov | 03b4f9d | 2019-05-16 19:02:10 +0200 | [diff] [blame^] | 17 | #include "api/task_queue/default_task_queue_factory.h" |
Sami Kalliomäki | 3e77afd | 2018-03-08 16:43:16 +0100 | [diff] [blame] | 18 | #include "examples/androidnativeapi/generated_jni/jni/CallClient_jni.h" |
Bjorn Terelius | b8b3c99 | 2019-01-09 11:15:34 +0100 | [diff] [blame] | 19 | #include "logging/rtc_event_log/rtc_event_log_factory.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "media/engine/internal_decoder_factory.h" |
| 21 | #include "media/engine/internal_encoder_factory.h" |
| 22 | #include "media/engine/webrtc_media_engine.h" |
Danil Chapovalov | 03b4f9d | 2019-05-16 19:02:10 +0200 | [diff] [blame^] | 23 | #include "media/engine/webrtc_media_engine_defaults.h" |
Sami Kalliomäki | 3e77afd | 2018-03-08 16:43:16 +0100 | [diff] [blame] | 24 | #include "sdk/android/native_api/jni/java_types.h" |
| 25 | #include "sdk/android/native_api/video/wrapper.h" |
| 26 | |
| 27 | namespace webrtc_examples { |
| 28 | |
| 29 | class AndroidCallClient::PCObserver : public webrtc::PeerConnectionObserver { |
| 30 | public: |
| 31 | explicit PCObserver(AndroidCallClient* client); |
| 32 | |
| 33 | void OnSignalingChange( |
| 34 | webrtc::PeerConnectionInterface::SignalingState new_state) override; |
| 35 | void OnDataChannel( |
| 36 | rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) override; |
| 37 | void OnRenegotiationNeeded() override; |
| 38 | void OnIceConnectionChange( |
| 39 | webrtc::PeerConnectionInterface::IceConnectionState new_state) override; |
| 40 | void OnIceGatheringChange( |
| 41 | webrtc::PeerConnectionInterface::IceGatheringState new_state) override; |
| 42 | void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override; |
| 43 | |
| 44 | private: |
| 45 | const AndroidCallClient* client_; |
| 46 | }; |
| 47 | |
| 48 | namespace { |
| 49 | |
| 50 | class CreateOfferObserver : public webrtc::CreateSessionDescriptionObserver { |
| 51 | public: |
| 52 | explicit CreateOfferObserver( |
| 53 | rtc::scoped_refptr<webrtc::PeerConnectionInterface> pc); |
| 54 | |
| 55 | void OnSuccess(webrtc::SessionDescriptionInterface* desc) override; |
Harald Alvestrand | 73771a8 | 2018-05-24 10:53:49 +0200 | [diff] [blame] | 56 | void OnFailure(webrtc::RTCError error) override; |
Sami Kalliomäki | 3e77afd | 2018-03-08 16:43:16 +0100 | [diff] [blame] | 57 | |
| 58 | private: |
| 59 | const rtc::scoped_refptr<webrtc::PeerConnectionInterface> pc_; |
| 60 | }; |
| 61 | |
| 62 | class SetRemoteSessionDescriptionObserver |
| 63 | : public webrtc::SetRemoteDescriptionObserverInterface { |
| 64 | public: |
| 65 | void OnSetRemoteDescriptionComplete(webrtc::RTCError error) override; |
| 66 | }; |
| 67 | |
| 68 | class SetLocalSessionDescriptionObserver |
| 69 | : public webrtc::SetSessionDescriptionObserver { |
| 70 | public: |
| 71 | void OnSuccess() override; |
Harald Alvestrand | 73771a8 | 2018-05-24 10:53:49 +0200 | [diff] [blame] | 72 | void OnFailure(webrtc::RTCError error) override; |
Sami Kalliomäki | 3e77afd | 2018-03-08 16:43:16 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | } // namespace |
| 76 | |
| 77 | AndroidCallClient::AndroidCallClient() |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 78 | : call_started_(false), pc_observer_(absl::make_unique<PCObserver>(this)) { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 79 | thread_checker_.Detach(); |
Sami Kalliomäki | 3e77afd | 2018-03-08 16:43:16 +0100 | [diff] [blame] | 80 | CreatePeerConnectionFactory(); |
| 81 | } |
| 82 | |
Mirko Bonadei | 94ef424 | 2018-07-20 13:33:06 +0200 | [diff] [blame] | 83 | AndroidCallClient::~AndroidCallClient() = default; |
| 84 | |
Sami Kalliomäki | 3e77afd | 2018-03-08 16:43:16 +0100 | [diff] [blame] | 85 | void AndroidCallClient::Call(JNIEnv* env, |
| 86 | const webrtc::JavaRef<jobject>& cls, |
| 87 | const webrtc::JavaRef<jobject>& local_sink, |
| 88 | const webrtc::JavaRef<jobject>& remote_sink) { |
| 89 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 90 | |
| 91 | rtc::CritScope lock(&pc_mutex_); |
| 92 | if (call_started_) { |
| 93 | RTC_LOG(LS_WARNING) << "Call already started."; |
| 94 | return; |
| 95 | } |
| 96 | call_started_ = true; |
| 97 | |
| 98 | local_sink_ = webrtc::JavaToNativeVideoSink(env, local_sink.obj()); |
| 99 | remote_sink_ = webrtc::JavaToNativeVideoSink(env, remote_sink.obj()); |
| 100 | |
Sami Kalliomäki | c475ac1 | 2018-05-16 15:49:18 +0200 | [diff] [blame] | 101 | video_source_ = webrtc::CreateJavaVideoSource(env, signaling_thread_.get(), |
Magnus Jedvert | 9514071 | 2018-11-15 12:07:32 +0100 | [diff] [blame] | 102 | /* is_screencast= */ false, |
| 103 | /* align_timestamps= */ true); |
Sami Kalliomäki | 3e77afd | 2018-03-08 16:43:16 +0100 | [diff] [blame] | 104 | |
| 105 | CreatePeerConnection(); |
| 106 | Connect(); |
| 107 | } |
| 108 | |
| 109 | void AndroidCallClient::Hangup(JNIEnv* env, |
| 110 | const webrtc::JavaRef<jobject>& cls) { |
| 111 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 112 | |
| 113 | call_started_ = false; |
| 114 | |
| 115 | { |
| 116 | rtc::CritScope lock(&pc_mutex_); |
| 117 | if (pc_ != nullptr) { |
| 118 | pc_->Close(); |
| 119 | pc_ = nullptr; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | local_sink_ = nullptr; |
| 124 | remote_sink_ = nullptr; |
| 125 | video_source_ = nullptr; |
| 126 | } |
| 127 | |
| 128 | void AndroidCallClient::Delete(JNIEnv* env, |
| 129 | const webrtc::JavaRef<jobject>& cls) { |
| 130 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 131 | |
| 132 | delete this; |
| 133 | } |
| 134 | |
Sami Kalliomäki | c475ac1 | 2018-05-16 15:49:18 +0200 | [diff] [blame] | 135 | webrtc::ScopedJavaLocalRef<jobject> |
| 136 | AndroidCallClient::GetJavaVideoCapturerObserver( |
| 137 | JNIEnv* env, |
| 138 | const webrtc::JavaRef<jobject>& cls) { |
| 139 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 140 | |
| 141 | return video_source_->GetJavaVideoCapturerObserver(env); |
| 142 | } |
| 143 | |
Sami Kalliomäki | 3e77afd | 2018-03-08 16:43:16 +0100 | [diff] [blame] | 144 | void AndroidCallClient::CreatePeerConnectionFactory() { |
| 145 | network_thread_ = rtc::Thread::CreateWithSocketServer(); |
| 146 | network_thread_->SetName("network_thread", nullptr); |
| 147 | RTC_CHECK(network_thread_->Start()) << "Failed to start thread"; |
| 148 | |
| 149 | worker_thread_ = rtc::Thread::Create(); |
| 150 | worker_thread_->SetName("worker_thread", nullptr); |
| 151 | RTC_CHECK(worker_thread_->Start()) << "Failed to start thread"; |
| 152 | |
| 153 | signaling_thread_ = rtc::Thread::Create(); |
| 154 | signaling_thread_->SetName("signaling_thread", nullptr); |
| 155 | RTC_CHECK(signaling_thread_->Start()) << "Failed to start thread"; |
| 156 | |
Danil Chapovalov | 03b4f9d | 2019-05-16 19:02:10 +0200 | [diff] [blame^] | 157 | webrtc::PeerConnectionFactoryDependencies pcf_deps; |
| 158 | pcf_deps.network_thread = network_thread_.get(); |
| 159 | pcf_deps.worker_thread = worker_thread_.get(); |
| 160 | pcf_deps.signaling_thread = signaling_thread_.get(); |
| 161 | pcf_deps.task_queue_factory = webrtc::CreateDefaultTaskQueueFactory(); |
| 162 | pcf_deps.call_factory = webrtc::CreateCallFactory(); |
| 163 | pcf_deps.event_log_factory = absl::make_unique<webrtc::RtcEventLogFactory>( |
| 164 | pcf_deps.task_queue_factory.get()); |
Sami Kalliomäki | 3e77afd | 2018-03-08 16:43:16 +0100 | [diff] [blame] | 165 | |
Danil Chapovalov | 03b4f9d | 2019-05-16 19:02:10 +0200 | [diff] [blame^] | 166 | cricket::MediaEngineDependencies media_deps; |
| 167 | media_deps.task_queue_factory = pcf_deps.task_queue_factory.get(); |
| 168 | media_deps.video_encoder_factory = |
| 169 | absl::make_unique<webrtc::InternalEncoderFactory>(); |
| 170 | media_deps.video_decoder_factory = |
| 171 | absl::make_unique<webrtc::InternalDecoderFactory>(); |
| 172 | webrtc::SetMediaEngineDefaults(&media_deps); |
| 173 | pcf_deps.media_engine = cricket::CreateMediaEngine(std::move(media_deps)); |
| 174 | RTC_LOG(LS_INFO) << "Media engine created: " << pcf_deps.media_engine.get(); |
| 175 | |
| 176 | pcf_ = CreateModularPeerConnectionFactory(std::move(pcf_deps)); |
Sami Kalliomäki | 3e77afd | 2018-03-08 16:43:16 +0100 | [diff] [blame] | 177 | RTC_LOG(LS_INFO) << "PeerConnectionFactory created: " << pcf_; |
| 178 | } |
| 179 | |
| 180 | void AndroidCallClient::CreatePeerConnection() { |
| 181 | rtc::CritScope lock(&pc_mutex_); |
| 182 | webrtc::PeerConnectionInterface::RTCConfiguration config; |
| 183 | config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan; |
| 184 | // DTLS SRTP has to be disabled for loopback to work. |
| 185 | config.enable_dtls_srtp = false; |
| 186 | pc_ = pcf_->CreatePeerConnection(config, nullptr /* port_allocator */, |
| 187 | nullptr /* cert_generator */, |
| 188 | pc_observer_.get()); |
| 189 | RTC_LOG(LS_INFO) << "PeerConnection created: " << pc_; |
| 190 | |
| 191 | rtc::scoped_refptr<webrtc::VideoTrackInterface> local_video_track = |
| 192 | pcf_->CreateVideoTrack("video", video_source_); |
| 193 | local_video_track->AddOrUpdateSink(local_sink_.get(), rtc::VideoSinkWants()); |
| 194 | pc_->AddTransceiver(local_video_track); |
| 195 | RTC_LOG(LS_INFO) << "Local video sink set up: " << local_video_track; |
| 196 | |
| 197 | for (const rtc::scoped_refptr<webrtc::RtpTransceiverInterface>& tranceiver : |
| 198 | pc_->GetTransceivers()) { |
| 199 | rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track = |
| 200 | tranceiver->receiver()->track(); |
| 201 | if (track && |
| 202 | track->kind() == webrtc::MediaStreamTrackInterface::kVideoKind) { |
| 203 | static_cast<webrtc::VideoTrackInterface*>(track.get()) |
| 204 | ->AddOrUpdateSink(remote_sink_.get(), rtc::VideoSinkWants()); |
| 205 | RTC_LOG(LS_INFO) << "Remote video sink set up: " << track; |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | void AndroidCallClient::Connect() { |
| 212 | rtc::CritScope lock(&pc_mutex_); |
| 213 | pc_->CreateOffer(new rtc::RefCountedObject<CreateOfferObserver>(pc_), |
| 214 | webrtc::PeerConnectionInterface::RTCOfferAnswerOptions()); |
| 215 | } |
| 216 | |
| 217 | AndroidCallClient::PCObserver::PCObserver(AndroidCallClient* client) |
| 218 | : client_(client) {} |
| 219 | |
| 220 | void AndroidCallClient::PCObserver::OnSignalingChange( |
| 221 | webrtc::PeerConnectionInterface::SignalingState new_state) { |
| 222 | RTC_LOG(LS_INFO) << "OnSignalingChange: " << new_state; |
| 223 | } |
| 224 | |
| 225 | void AndroidCallClient::PCObserver::OnDataChannel( |
| 226 | rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) { |
| 227 | RTC_LOG(LS_INFO) << "OnDataChannel"; |
| 228 | } |
| 229 | |
| 230 | void AndroidCallClient::PCObserver::OnRenegotiationNeeded() { |
| 231 | RTC_LOG(LS_INFO) << "OnRenegotiationNeeded"; |
| 232 | } |
| 233 | |
| 234 | void AndroidCallClient::PCObserver::OnIceConnectionChange( |
| 235 | webrtc::PeerConnectionInterface::IceConnectionState new_state) { |
| 236 | RTC_LOG(LS_INFO) << "OnIceConnectionChange: " << new_state; |
| 237 | } |
| 238 | |
| 239 | void AndroidCallClient::PCObserver::OnIceGatheringChange( |
| 240 | webrtc::PeerConnectionInterface::IceGatheringState new_state) { |
| 241 | RTC_LOG(LS_INFO) << "OnIceGatheringChange: " << new_state; |
| 242 | } |
| 243 | |
| 244 | void AndroidCallClient::PCObserver::OnIceCandidate( |
| 245 | const webrtc::IceCandidateInterface* candidate) { |
| 246 | RTC_LOG(LS_INFO) << "OnIceCandidate: " << candidate->server_url(); |
| 247 | rtc::CritScope lock(&client_->pc_mutex_); |
| 248 | RTC_DCHECK(client_->pc_ != nullptr); |
| 249 | client_->pc_->AddIceCandidate(candidate); |
| 250 | } |
| 251 | |
| 252 | CreateOfferObserver::CreateOfferObserver( |
| 253 | rtc::scoped_refptr<webrtc::PeerConnectionInterface> pc) |
| 254 | : pc_(pc) {} |
| 255 | |
| 256 | void CreateOfferObserver::OnSuccess(webrtc::SessionDescriptionInterface* desc) { |
| 257 | std::string sdp; |
| 258 | desc->ToString(&sdp); |
| 259 | RTC_LOG(LS_INFO) << "Created offer: " << sdp; |
| 260 | |
| 261 | // Ownership of desc was transferred to us, now we transfer it forward. |
| 262 | pc_->SetLocalDescription( |
| 263 | new rtc::RefCountedObject<SetLocalSessionDescriptionObserver>(), desc); |
| 264 | |
| 265 | // Generate a fake answer. |
| 266 | std::unique_ptr<webrtc::SessionDescriptionInterface> answer( |
| 267 | webrtc::CreateSessionDescription(webrtc::SdpType::kAnswer, sdp)); |
| 268 | pc_->SetRemoteDescription( |
| 269 | std::move(answer), |
| 270 | new rtc::RefCountedObject<SetRemoteSessionDescriptionObserver>()); |
| 271 | } |
| 272 | |
Harald Alvestrand | 73771a8 | 2018-05-24 10:53:49 +0200 | [diff] [blame] | 273 | void CreateOfferObserver::OnFailure(webrtc::RTCError error) { |
| 274 | RTC_LOG(LS_INFO) << "Failed to create offer: " << ToString(error.type()) |
| 275 | << ": " << error.message(); |
Sami Kalliomäki | 3e77afd | 2018-03-08 16:43:16 +0100 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | void SetRemoteSessionDescriptionObserver::OnSetRemoteDescriptionComplete( |
| 279 | webrtc::RTCError error) { |
| 280 | RTC_LOG(LS_INFO) << "Set remote description: " << error.message(); |
| 281 | } |
| 282 | |
| 283 | void SetLocalSessionDescriptionObserver::OnSuccess() { |
| 284 | RTC_LOG(LS_INFO) << "Set local description success!"; |
| 285 | } |
| 286 | |
Harald Alvestrand | 73771a8 | 2018-05-24 10:53:49 +0200 | [diff] [blame] | 287 | void SetLocalSessionDescriptionObserver::OnFailure(webrtc::RTCError error) { |
| 288 | RTC_LOG(LS_INFO) << "Set local description failure: " |
| 289 | << ToString(error.type()) << ": " << error.message(); |
Sami Kalliomäki | 3e77afd | 2018-03-08 16:43:16 +0100 | [diff] [blame] | 290 | } |
| 291 | |
Artem Titarenko | e5e36dd | 2018-12-03 11:02:28 +0100 | [diff] [blame] | 292 | static jlong JNI_CallClient_CreateClient(JNIEnv* env) { |
Sami Kalliomäki | 3e77afd | 2018-03-08 16:43:16 +0100 | [diff] [blame] | 293 | return webrtc::NativeToJavaPointer(new webrtc_examples::AndroidCallClient()); |
| 294 | } |
Sami Kalliomäki | c475ac1 | 2018-05-16 15:49:18 +0200 | [diff] [blame] | 295 | |
| 296 | } // namespace webrtc_examples |