blob: 747b3d9009a071fe422800c7d63fcfda8559c4a0 [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(),
103 false /* is_screencast */);
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100104
105 CreatePeerConnection();
106 Connect();
107}
108
109void AndroidCallClient::Hangup(JNIEnv* env,
110 const webrtc::JavaRef<jobject>& cls) {
111 RTC_DCHECK_RUN_ON(&thread_checker_);
112
113 call_started_ = false;
114
115 {
116 rtc::CritScope lock(&pc_mutex_);
117 if (pc_ != nullptr) {
118 pc_->Close();
119 pc_ = nullptr;
120 }
121 }
122
123 local_sink_ = nullptr;
124 remote_sink_ = nullptr;
125 video_source_ = nullptr;
126}
127
128void AndroidCallClient::Delete(JNIEnv* env,
129 const webrtc::JavaRef<jobject>& cls) {
130 RTC_DCHECK_RUN_ON(&thread_checker_);
131
132 delete this;
133}
134
Sami Kalliomäkic475ac12018-05-16 15:49:18 +0200135webrtc::ScopedJavaLocalRef<jobject>
136AndroidCallClient::GetJavaVideoCapturerObserver(
137 JNIEnv* env,
138 const webrtc::JavaRef<jobject>& cls) {
139 RTC_DCHECK_RUN_ON(&thread_checker_);
140
141 return video_source_->GetJavaVideoCapturerObserver(env);
142}
143
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100144void AndroidCallClient::CreatePeerConnectionFactory() {
145 network_thread_ = rtc::Thread::CreateWithSocketServer();
146 network_thread_->SetName("network_thread", nullptr);
147 RTC_CHECK(network_thread_->Start()) << "Failed to start thread";
148
149 worker_thread_ = rtc::Thread::Create();
150 worker_thread_->SetName("worker_thread", nullptr);
151 RTC_CHECK(worker_thread_->Start()) << "Failed to start thread";
152
153 signaling_thread_ = rtc::Thread::Create();
154 signaling_thread_->SetName("signaling_thread", nullptr);
155 RTC_CHECK(signaling_thread_->Start()) << "Failed to start thread";
156
157 std::unique_ptr<cricket::MediaEngineInterface> media_engine =
158 cricket::WebRtcMediaEngineFactory::Create(
159 nullptr /* adm */, webrtc::CreateBuiltinAudioEncoderFactory(),
160 webrtc::CreateBuiltinAudioDecoderFactory(),
Karl Wiberg918f50c2018-07-05 11:40:33 +0200161 absl::make_unique<webrtc::InternalEncoderFactory>(),
162 absl::make_unique<webrtc::InternalDecoderFactory>(),
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800163 webrtc::CreateBuiltinVideoBitrateAllocatorFactory(),
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100164 nullptr /* audio_mixer */, webrtc::AudioProcessingBuilder().Create());
165 RTC_LOG(LS_INFO) << "Media engine created: " << media_engine.get();
166
167 pcf_ = CreateModularPeerConnectionFactory(
168 network_thread_.get(), worker_thread_.get(), signaling_thread_.get(),
169 std::move(media_engine), webrtc::CreateCallFactory(),
170 webrtc::CreateRtcEventLogFactory());
171 RTC_LOG(LS_INFO) << "PeerConnectionFactory created: " << pcf_;
172}
173
174void AndroidCallClient::CreatePeerConnection() {
175 rtc::CritScope lock(&pc_mutex_);
176 webrtc::PeerConnectionInterface::RTCConfiguration config;
177 config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
178 // DTLS SRTP has to be disabled for loopback to work.
179 config.enable_dtls_srtp = false;
180 pc_ = pcf_->CreatePeerConnection(config, nullptr /* port_allocator */,
181 nullptr /* cert_generator */,
182 pc_observer_.get());
183 RTC_LOG(LS_INFO) << "PeerConnection created: " << pc_;
184
185 rtc::scoped_refptr<webrtc::VideoTrackInterface> local_video_track =
186 pcf_->CreateVideoTrack("video", video_source_);
187 local_video_track->AddOrUpdateSink(local_sink_.get(), rtc::VideoSinkWants());
188 pc_->AddTransceiver(local_video_track);
189 RTC_LOG(LS_INFO) << "Local video sink set up: " << local_video_track;
190
191 for (const rtc::scoped_refptr<webrtc::RtpTransceiverInterface>& tranceiver :
192 pc_->GetTransceivers()) {
193 rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track =
194 tranceiver->receiver()->track();
195 if (track &&
196 track->kind() == webrtc::MediaStreamTrackInterface::kVideoKind) {
197 static_cast<webrtc::VideoTrackInterface*>(track.get())
198 ->AddOrUpdateSink(remote_sink_.get(), rtc::VideoSinkWants());
199 RTC_LOG(LS_INFO) << "Remote video sink set up: " << track;
200 break;
201 }
202 }
203}
204
205void AndroidCallClient::Connect() {
206 rtc::CritScope lock(&pc_mutex_);
207 pc_->CreateOffer(new rtc::RefCountedObject<CreateOfferObserver>(pc_),
208 webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
209}
210
211AndroidCallClient::PCObserver::PCObserver(AndroidCallClient* client)
212 : client_(client) {}
213
214void AndroidCallClient::PCObserver::OnSignalingChange(
215 webrtc::PeerConnectionInterface::SignalingState new_state) {
216 RTC_LOG(LS_INFO) << "OnSignalingChange: " << new_state;
217}
218
219void AndroidCallClient::PCObserver::OnDataChannel(
220 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
221 RTC_LOG(LS_INFO) << "OnDataChannel";
222}
223
224void AndroidCallClient::PCObserver::OnRenegotiationNeeded() {
225 RTC_LOG(LS_INFO) << "OnRenegotiationNeeded";
226}
227
228void AndroidCallClient::PCObserver::OnIceConnectionChange(
229 webrtc::PeerConnectionInterface::IceConnectionState new_state) {
230 RTC_LOG(LS_INFO) << "OnIceConnectionChange: " << new_state;
231}
232
233void AndroidCallClient::PCObserver::OnIceGatheringChange(
234 webrtc::PeerConnectionInterface::IceGatheringState new_state) {
235 RTC_LOG(LS_INFO) << "OnIceGatheringChange: " << new_state;
236}
237
238void AndroidCallClient::PCObserver::OnIceCandidate(
239 const webrtc::IceCandidateInterface* candidate) {
240 RTC_LOG(LS_INFO) << "OnIceCandidate: " << candidate->server_url();
241 rtc::CritScope lock(&client_->pc_mutex_);
242 RTC_DCHECK(client_->pc_ != nullptr);
243 client_->pc_->AddIceCandidate(candidate);
244}
245
246CreateOfferObserver::CreateOfferObserver(
247 rtc::scoped_refptr<webrtc::PeerConnectionInterface> pc)
248 : pc_(pc) {}
249
250void CreateOfferObserver::OnSuccess(webrtc::SessionDescriptionInterface* desc) {
251 std::string sdp;
252 desc->ToString(&sdp);
253 RTC_LOG(LS_INFO) << "Created offer: " << sdp;
254
255 // Ownership of desc was transferred to us, now we transfer it forward.
256 pc_->SetLocalDescription(
257 new rtc::RefCountedObject<SetLocalSessionDescriptionObserver>(), desc);
258
259 // Generate a fake answer.
260 std::unique_ptr<webrtc::SessionDescriptionInterface> answer(
261 webrtc::CreateSessionDescription(webrtc::SdpType::kAnswer, sdp));
262 pc_->SetRemoteDescription(
263 std::move(answer),
264 new rtc::RefCountedObject<SetRemoteSessionDescriptionObserver>());
265}
266
Harald Alvestrand73771a82018-05-24 10:53:49 +0200267void CreateOfferObserver::OnFailure(webrtc::RTCError error) {
268 RTC_LOG(LS_INFO) << "Failed to create offer: " << ToString(error.type())
269 << ": " << error.message();
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100270}
271
272void SetRemoteSessionDescriptionObserver::OnSetRemoteDescriptionComplete(
273 webrtc::RTCError error) {
274 RTC_LOG(LS_INFO) << "Set remote description: " << error.message();
275}
276
277void SetLocalSessionDescriptionObserver::OnSuccess() {
278 RTC_LOG(LS_INFO) << "Set local description success!";
279}
280
Harald Alvestrand73771a82018-05-24 10:53:49 +0200281void SetLocalSessionDescriptionObserver::OnFailure(webrtc::RTCError error) {
282 RTC_LOG(LS_INFO) << "Set local description failure: "
283 << ToString(error.type()) << ": " << error.message();
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100284}
285
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100286static jlong JNI_CallClient_CreateClient(
287 JNIEnv* env,
288 const webrtc::JavaParamRef<jclass>& cls) {
289 return webrtc::NativeToJavaPointer(new webrtc_examples::AndroidCallClient());
290}
Sami Kalliomäkic475ac12018-05-16 15:49:18 +0200291
292} // namespace webrtc_examples