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