blob: 03968335d9c8f45252790928aa2f8186aa218e69 [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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "examples/androidnativeapi/jni/android_call_client.h"
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +010012
13#include <utility>
14
Mirko Bonadei317a1f02019-09-17 17:06:18 +020015#include <memory>
16
Steve Anton10542f22019-01-11 09:11:00 -080017#include "api/peer_connection_interface.h"
Danil Chapovalov4ba04b72019-06-26 15:49:47 +020018#include "api/rtc_event_log/rtc_event_log_factory.h"
Danil Chapovalov03b4f9d2019-05-16 19:02:10 +020019#include "api/task_queue/default_task_queue_factory.h"
Eric Stevenson0d65fb52019-06-24 13:09:50 -040020#include "examples/androidnativeapi/generated_jni/CallClient_jni.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "media/engine/internal_decoder_factory.h"
22#include "media/engine/internal_encoder_factory.h"
23#include "media/engine/webrtc_media_engine.h"
Danil Chapovalov03b4f9d2019-05-16 19:02:10 +020024#include "media/engine/webrtc_media_engine_defaults.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()
Mirko Bonadei317a1f02019-09-17 17:06:18 +020079 : call_started_(false), pc_observer_(std::make_unique<PCObserver>(this)) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020080 thread_checker_.Detach();
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +010081 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,
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +010087 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(),
Magnus Jedvert95140712018-11-15 12:07:32 +0100102 /* is_screencast= */ false,
103 /* align_timestamps= */ true);
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100104
105 CreatePeerConnection();
106 Connect();
107}
108
Mirko Bonadei96ea8c02019-07-29 15:33:57 +0200109void AndroidCallClient::Hangup(JNIEnv* env) {
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100110 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
Mirko Bonadei96ea8c02019-07-29 15:33:57 +0200127void AndroidCallClient::Delete(JNIEnv* env) {
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100128 RTC_DCHECK_RUN_ON(&thread_checker_);
129
130 delete this;
131}
132
Sami Kalliomäkic475ac12018-05-16 15:49:18 +0200133webrtc::ScopedJavaLocalRef<jobject>
Mirko Bonadei96ea8c02019-07-29 15:33:57 +0200134AndroidCallClient::GetJavaVideoCapturerObserver(JNIEnv* env) {
Sami Kalliomäkic475ac12018-05-16 15:49:18 +0200135 RTC_DCHECK_RUN_ON(&thread_checker_);
136
137 return video_source_->GetJavaVideoCapturerObserver(env);
138}
139
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100140void 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
Danil Chapovalov03b4f9d2019-05-16 19:02:10 +0200153 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();
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200159 pcf_deps.event_log_factory = std::make_unique<webrtc::RtcEventLogFactory>(
Danil Chapovalov03b4f9d2019-05-16 19:02:10 +0200160 pcf_deps.task_queue_factory.get());
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100161
Danil Chapovalov03b4f9d2019-05-16 19:02:10 +0200162 cricket::MediaEngineDependencies media_deps;
163 media_deps.task_queue_factory = pcf_deps.task_queue_factory.get();
164 media_deps.video_encoder_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200165 std::make_unique<webrtc::InternalEncoderFactory>();
Danil Chapovalov03b4f9d2019-05-16 19:02:10 +0200166 media_deps.video_decoder_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200167 std::make_unique<webrtc::InternalDecoderFactory>();
Danil Chapovalov03b4f9d2019-05-16 19:02:10 +0200168 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));
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100173 RTC_LOG(LS_INFO) << "PeerConnectionFactory created: " << pcf_;
174}
175
176void 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
207void AndroidCallClient::Connect() {
208 rtc::CritScope lock(&pc_mutex_);
209 pc_->CreateOffer(new rtc::RefCountedObject<CreateOfferObserver>(pc_),
210 webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
211}
212
213AndroidCallClient::PCObserver::PCObserver(AndroidCallClient* client)
214 : client_(client) {}
215
216void AndroidCallClient::PCObserver::OnSignalingChange(
217 webrtc::PeerConnectionInterface::SignalingState new_state) {
218 RTC_LOG(LS_INFO) << "OnSignalingChange: " << new_state;
219}
220
221void AndroidCallClient::PCObserver::OnDataChannel(
222 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
223 RTC_LOG(LS_INFO) << "OnDataChannel";
224}
225
226void AndroidCallClient::PCObserver::OnRenegotiationNeeded() {
227 RTC_LOG(LS_INFO) << "OnRenegotiationNeeded";
228}
229
230void AndroidCallClient::PCObserver::OnIceConnectionChange(
231 webrtc::PeerConnectionInterface::IceConnectionState new_state) {
232 RTC_LOG(LS_INFO) << "OnIceConnectionChange: " << new_state;
233}
234
235void AndroidCallClient::PCObserver::OnIceGatheringChange(
236 webrtc::PeerConnectionInterface::IceGatheringState new_state) {
237 RTC_LOG(LS_INFO) << "OnIceGatheringChange: " << new_state;
238}
239
240void 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
248CreateOfferObserver::CreateOfferObserver(
249 rtc::scoped_refptr<webrtc::PeerConnectionInterface> pc)
250 : pc_(pc) {}
251
252void 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 Alvestrand73771a82018-05-24 10:53:49 +0200269void CreateOfferObserver::OnFailure(webrtc::RTCError error) {
270 RTC_LOG(LS_INFO) << "Failed to create offer: " << ToString(error.type())
271 << ": " << error.message();
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100272}
273
274void SetRemoteSessionDescriptionObserver::OnSetRemoteDescriptionComplete(
275 webrtc::RTCError error) {
276 RTC_LOG(LS_INFO) << "Set remote description: " << error.message();
277}
278
279void SetLocalSessionDescriptionObserver::OnSuccess() {
280 RTC_LOG(LS_INFO) << "Set local description success!";
281}
282
Harald Alvestrand73771a82018-05-24 10:53:49 +0200283void SetLocalSessionDescriptionObserver::OnFailure(webrtc::RTCError error) {
284 RTC_LOG(LS_INFO) << "Set local description failure: "
285 << ToString(error.type()) << ": " << error.message();
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100286}
287
Artem Titarenkoe5e36dd2018-12-03 11:02:28 +0100288static jlong JNI_CallClient_CreateClient(JNIEnv* env) {
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100289 return webrtc::NativeToJavaPointer(new webrtc_examples::AndroidCallClient());
290}
Sami Kalliomäkic475ac12018-05-16 15:49:18 +0200291
292} // namespace webrtc_examples