blob: 005f369a6d104622dddb0bdbef8071a812f21233 [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"
19#include "examples/androidnativeapi/generated_jni/jni/CallClient_jni.h"
20#include "media/engine/internaldecoderfactory.h"
21#include "media/engine/internalencoderfactory.h"
22#include "media/engine/webrtcmediaengine.h"
23#include "modules/audio_processing/include/audio_processing.h"
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +010024#include "sdk/android/native_api/jni/java_types.h"
25#include "sdk/android/native_api/video/wrapper.h"
26
27namespace webrtc_examples {
28
29class 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
48namespace {
49
50class 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 Alvestrand73771a82018-05-24 10:53:49 +020056 void OnFailure(webrtc::RTCError error) override;
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +010057
58 private:
59 const rtc::scoped_refptr<webrtc::PeerConnectionInterface> pc_;
60};
61
62class SetRemoteSessionDescriptionObserver
63 : public webrtc::SetRemoteDescriptionObserverInterface {
64 public:
65 void OnSetRemoteDescriptionComplete(webrtc::RTCError error) override;
66};
67
68class SetLocalSessionDescriptionObserver
69 : public webrtc::SetSessionDescriptionObserver {
70 public:
71 void OnSuccess() override;
Harald Alvestrand73771a82018-05-24 10:53:49 +020072 void OnFailure(webrtc::RTCError error) override;
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +010073};
74
75} // namespace
76
77AndroidCallClient::AndroidCallClient()
Karl Wiberg918f50c2018-07-05 11:40:33 +020078 : call_started_(false), pc_observer_(absl::make_unique<PCObserver>(this)) {
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +010079 thread_checker_.DetachFromThread();
80 CreatePeerConnectionFactory();
81}
82
Mirko Bonadei94ef4242018-07-20 13:33:06 +020083AndroidCallClient::~AndroidCallClient() = default;
84
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +010085void 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äkic475ac12018-05-16 15:49:18 +0200101 video_source_ = webrtc::CreateJavaVideoSource(env, signaling_thread_.get(),
102 false /* is_screencast */);
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100103
104 CreatePeerConnection();
105 Connect();
106}
107
108void AndroidCallClient::Hangup(JNIEnv* env,
109 const webrtc::JavaRef<jobject>& cls) {
110 RTC_DCHECK_RUN_ON(&thread_checker_);
111
112 call_started_ = false;
113
114 {
115 rtc::CritScope lock(&pc_mutex_);
116 if (pc_ != nullptr) {
117 pc_->Close();
118 pc_ = nullptr;
119 }
120 }
121
122 local_sink_ = nullptr;
123 remote_sink_ = nullptr;
124 video_source_ = nullptr;
125}
126
127void AndroidCallClient::Delete(JNIEnv* env,
128 const webrtc::JavaRef<jobject>& cls) {
129 RTC_DCHECK_RUN_ON(&thread_checker_);
130
131 delete this;
132}
133
Sami Kalliomäkic475ac12018-05-16 15:49:18 +0200134webrtc::ScopedJavaLocalRef<jobject>
135AndroidCallClient::GetJavaVideoCapturerObserver(
136 JNIEnv* env,
137 const webrtc::JavaRef<jobject>& cls) {
138 RTC_DCHECK_RUN_ON(&thread_checker_);
139
140 return video_source_->GetJavaVideoCapturerObserver(env);
141}
142
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100143void AndroidCallClient::CreatePeerConnectionFactory() {
144 network_thread_ = rtc::Thread::CreateWithSocketServer();
145 network_thread_->SetName("network_thread", nullptr);
146 RTC_CHECK(network_thread_->Start()) << "Failed to start thread";
147
148 worker_thread_ = rtc::Thread::Create();
149 worker_thread_->SetName("worker_thread", nullptr);
150 RTC_CHECK(worker_thread_->Start()) << "Failed to start thread";
151
152 signaling_thread_ = rtc::Thread::Create();
153 signaling_thread_->SetName("signaling_thread", nullptr);
154 RTC_CHECK(signaling_thread_->Start()) << "Failed to start thread";
155
156 std::unique_ptr<cricket::MediaEngineInterface> media_engine =
157 cricket::WebRtcMediaEngineFactory::Create(
158 nullptr /* adm */, webrtc::CreateBuiltinAudioEncoderFactory(),
159 webrtc::CreateBuiltinAudioDecoderFactory(),
Karl Wiberg918f50c2018-07-05 11:40:33 +0200160 absl::make_unique<webrtc::InternalEncoderFactory>(),
161 absl::make_unique<webrtc::InternalDecoderFactory>(),
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100162 nullptr /* audio_mixer */, webrtc::AudioProcessingBuilder().Create());
163 RTC_LOG(LS_INFO) << "Media engine created: " << media_engine.get();
164
165 pcf_ = CreateModularPeerConnectionFactory(
166 network_thread_.get(), worker_thread_.get(), signaling_thread_.get(),
167 std::move(media_engine), webrtc::CreateCallFactory(),
168 webrtc::CreateRtcEventLogFactory());
169 RTC_LOG(LS_INFO) << "PeerConnectionFactory created: " << pcf_;
170}
171
172void AndroidCallClient::CreatePeerConnection() {
173 rtc::CritScope lock(&pc_mutex_);
174 webrtc::PeerConnectionInterface::RTCConfiguration config;
175 config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
176 // DTLS SRTP has to be disabled for loopback to work.
177 config.enable_dtls_srtp = false;
178 pc_ = pcf_->CreatePeerConnection(config, nullptr /* port_allocator */,
179 nullptr /* cert_generator */,
180 pc_observer_.get());
181 RTC_LOG(LS_INFO) << "PeerConnection created: " << pc_;
182
183 rtc::scoped_refptr<webrtc::VideoTrackInterface> local_video_track =
184 pcf_->CreateVideoTrack("video", video_source_);
185 local_video_track->AddOrUpdateSink(local_sink_.get(), rtc::VideoSinkWants());
186 pc_->AddTransceiver(local_video_track);
187 RTC_LOG(LS_INFO) << "Local video sink set up: " << local_video_track;
188
189 for (const rtc::scoped_refptr<webrtc::RtpTransceiverInterface>& tranceiver :
190 pc_->GetTransceivers()) {
191 rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track =
192 tranceiver->receiver()->track();
193 if (track &&
194 track->kind() == webrtc::MediaStreamTrackInterface::kVideoKind) {
195 static_cast<webrtc::VideoTrackInterface*>(track.get())
196 ->AddOrUpdateSink(remote_sink_.get(), rtc::VideoSinkWants());
197 RTC_LOG(LS_INFO) << "Remote video sink set up: " << track;
198 break;
199 }
200 }
201}
202
203void AndroidCallClient::Connect() {
204 rtc::CritScope lock(&pc_mutex_);
205 pc_->CreateOffer(new rtc::RefCountedObject<CreateOfferObserver>(pc_),
206 webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
207}
208
209AndroidCallClient::PCObserver::PCObserver(AndroidCallClient* client)
210 : client_(client) {}
211
212void AndroidCallClient::PCObserver::OnSignalingChange(
213 webrtc::PeerConnectionInterface::SignalingState new_state) {
214 RTC_LOG(LS_INFO) << "OnSignalingChange: " << new_state;
215}
216
217void AndroidCallClient::PCObserver::OnDataChannel(
218 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
219 RTC_LOG(LS_INFO) << "OnDataChannel";
220}
221
222void AndroidCallClient::PCObserver::OnRenegotiationNeeded() {
223 RTC_LOG(LS_INFO) << "OnRenegotiationNeeded";
224}
225
226void AndroidCallClient::PCObserver::OnIceConnectionChange(
227 webrtc::PeerConnectionInterface::IceConnectionState new_state) {
228 RTC_LOG(LS_INFO) << "OnIceConnectionChange: " << new_state;
229}
230
231void AndroidCallClient::PCObserver::OnIceGatheringChange(
232 webrtc::PeerConnectionInterface::IceGatheringState new_state) {
233 RTC_LOG(LS_INFO) << "OnIceGatheringChange: " << new_state;
234}
235
236void AndroidCallClient::PCObserver::OnIceCandidate(
237 const webrtc::IceCandidateInterface* candidate) {
238 RTC_LOG(LS_INFO) << "OnIceCandidate: " << candidate->server_url();
239 rtc::CritScope lock(&client_->pc_mutex_);
240 RTC_DCHECK(client_->pc_ != nullptr);
241 client_->pc_->AddIceCandidate(candidate);
242}
243
244CreateOfferObserver::CreateOfferObserver(
245 rtc::scoped_refptr<webrtc::PeerConnectionInterface> pc)
246 : pc_(pc) {}
247
248void CreateOfferObserver::OnSuccess(webrtc::SessionDescriptionInterface* desc) {
249 std::string sdp;
250 desc->ToString(&sdp);
251 RTC_LOG(LS_INFO) << "Created offer: " << sdp;
252
253 // Ownership of desc was transferred to us, now we transfer it forward.
254 pc_->SetLocalDescription(
255 new rtc::RefCountedObject<SetLocalSessionDescriptionObserver>(), desc);
256
257 // Generate a fake answer.
258 std::unique_ptr<webrtc::SessionDescriptionInterface> answer(
259 webrtc::CreateSessionDescription(webrtc::SdpType::kAnswer, sdp));
260 pc_->SetRemoteDescription(
261 std::move(answer),
262 new rtc::RefCountedObject<SetRemoteSessionDescriptionObserver>());
263}
264
Harald Alvestrand73771a82018-05-24 10:53:49 +0200265void CreateOfferObserver::OnFailure(webrtc::RTCError error) {
266 RTC_LOG(LS_INFO) << "Failed to create offer: " << ToString(error.type())
267 << ": " << error.message();
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100268}
269
270void SetRemoteSessionDescriptionObserver::OnSetRemoteDescriptionComplete(
271 webrtc::RTCError error) {
272 RTC_LOG(LS_INFO) << "Set remote description: " << error.message();
273}
274
275void SetLocalSessionDescriptionObserver::OnSuccess() {
276 RTC_LOG(LS_INFO) << "Set local description success!";
277}
278
Harald Alvestrand73771a82018-05-24 10:53:49 +0200279void SetLocalSessionDescriptionObserver::OnFailure(webrtc::RTCError error) {
280 RTC_LOG(LS_INFO) << "Set local description failure: "
281 << ToString(error.type()) << ": " << error.message();
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100282}
283
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100284static jlong JNI_CallClient_CreateClient(
285 JNIEnv* env,
286 const webrtc::JavaParamRef<jclass>& cls) {
287 return webrtc::NativeToJavaPointer(new webrtc_examples::AndroidCallClient());
288}
Sami Kalliomäkic475ac12018-05-16 15:49:18 +0200289
290} // namespace webrtc_examples