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