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