blob: 69afc89d2ed2ff33c63042474565e86eaa05a0de [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
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"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "api/peer_connection_interface.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"
Bjorn Tereliusb8b3c992019-01-09 11:15:34 +010021#include "logging/rtc_event_log/rtc_event_log_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#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äki3e77afd2018-03-08 16:43:16 +010025#include "modules/audio_processing/include/audio_processing.h"
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +010026#include "sdk/android/native_api/jni/java_types.h"
27#include "sdk/android/native_api/video/wrapper.h"
28
29namespace webrtc_examples {
30
31class 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
50namespace {
51
52class 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 Alvestrand73771a82018-05-24 10:53:49 +020058 void OnFailure(webrtc::RTCError error) override;
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +010059
60 private:
61 const rtc::scoped_refptr<webrtc::PeerConnectionInterface> pc_;
62};
63
64class SetRemoteSessionDescriptionObserver
65 : public webrtc::SetRemoteDescriptionObserverInterface {
66 public:
67 void OnSetRemoteDescriptionComplete(webrtc::RTCError error) override;
68};
69
70class SetLocalSessionDescriptionObserver
71 : public webrtc::SetSessionDescriptionObserver {
72 public:
73 void OnSuccess() override;
Harald Alvestrand73771a82018-05-24 10:53:49 +020074 void OnFailure(webrtc::RTCError error) override;
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +010075};
76
77} // namespace
78
79AndroidCallClient::AndroidCallClient()
Karl Wiberg918f50c2018-07-05 11:40:33 +020080 : call_started_(false), pc_observer_(absl::make_unique<PCObserver>(this)) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020081 thread_checker_.Detach();
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +010082 CreatePeerConnectionFactory();
83}
84
Mirko Bonadei94ef4242018-07-20 13:33:06 +020085AndroidCallClient::~AndroidCallClient() = default;
86
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +010087void 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äkic475ac12018-05-16 15:49:18 +0200103 video_source_ = webrtc::CreateJavaVideoSource(env, signaling_thread_.get(),
Magnus Jedvert95140712018-11-15 12:07:32 +0100104 /* is_screencast= */ false,
105 /* align_timestamps= */ true);
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100106
107 CreatePeerConnection();
108 Connect();
109}
110
111void 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
130void 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äkic475ac12018-05-16 15:49:18 +0200137webrtc::ScopedJavaLocalRef<jobject>
138AndroidCallClient::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äki3e77afd2018-03-08 16:43:16 +0100146void 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 Wiberg918f50c2018-07-05 11:40:33 +0200163 absl::make_unique<webrtc::InternalEncoderFactory>(),
164 absl::make_unique<webrtc::InternalDecoderFactory>(),
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800165 webrtc::CreateBuiltinVideoBitrateAllocatorFactory(),
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100166 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
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