blob: 4da20b995a323dda73ae1b81f24fba669fe72e19 [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
15#include "api/audio_codecs/builtin_audio_decoder_factory.h"
16#include "api/audio_codecs/builtin_audio_encoder_factory.h"
17#include "api/peerconnectioninterface.h"
18#include "examples/androidnativeapi/generated_jni/jni/CallClient_jni.h"
19#include "media/engine/internaldecoderfactory.h"
20#include "media/engine/internalencoderfactory.h"
21#include "media/engine/webrtcmediaengine.h"
22#include "modules/audio_processing/include/audio_processing.h"
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +010023#include "rtc_base/ptr_util.h"
24#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;
56 void OnFailure(const std::string& error) override;
57
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;
72 void OnFailure(const std::string& error) override;
73};
74
75} // namespace
76
77AndroidCallClient::AndroidCallClient()
78 : call_started_(false), pc_observer_(rtc::MakeUnique<PCObserver>(this)) {
79 thread_checker_.DetachFromThread();
80 CreatePeerConnectionFactory();
81}
82
83void AndroidCallClient::Call(JNIEnv* env,
84 const webrtc::JavaRef<jobject>& cls,
85 const webrtc::JavaRef<jobject>& local_sink,
86 const webrtc::JavaRef<jobject>& remote_sink) {
87 RTC_DCHECK_RUN_ON(&thread_checker_);
88
89 rtc::CritScope lock(&pc_mutex_);
90 if (call_started_) {
91 RTC_LOG(LS_WARNING) << "Call already started.";
92 return;
93 }
94 call_started_ = true;
95
96 local_sink_ = webrtc::JavaToNativeVideoSink(env, local_sink.obj());
97 remote_sink_ = webrtc::JavaToNativeVideoSink(env, remote_sink.obj());
98
Sami Kalliomäkic475ac12018-05-16 15:49:18 +020099 video_source_ = webrtc::CreateJavaVideoSource(env, signaling_thread_.get(),
100 false /* is_screencast */);
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100101
102 CreatePeerConnection();
103 Connect();
104}
105
106void AndroidCallClient::Hangup(JNIEnv* env,
107 const webrtc::JavaRef<jobject>& cls) {
108 RTC_DCHECK_RUN_ON(&thread_checker_);
109
110 call_started_ = false;
111
112 {
113 rtc::CritScope lock(&pc_mutex_);
114 if (pc_ != nullptr) {
115 pc_->Close();
116 pc_ = nullptr;
117 }
118 }
119
120 local_sink_ = nullptr;
121 remote_sink_ = nullptr;
122 video_source_ = nullptr;
123}
124
125void AndroidCallClient::Delete(JNIEnv* env,
126 const webrtc::JavaRef<jobject>& cls) {
127 RTC_DCHECK_RUN_ON(&thread_checker_);
128
129 delete this;
130}
131
Sami Kalliomäkic475ac12018-05-16 15:49:18 +0200132webrtc::ScopedJavaLocalRef<jobject>
133AndroidCallClient::GetJavaVideoCapturerObserver(
134 JNIEnv* env,
135 const webrtc::JavaRef<jobject>& cls) {
136 RTC_DCHECK_RUN_ON(&thread_checker_);
137
138 return video_source_->GetJavaVideoCapturerObserver(env);
139}
140
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100141void AndroidCallClient::CreatePeerConnectionFactory() {
142 network_thread_ = rtc::Thread::CreateWithSocketServer();
143 network_thread_->SetName("network_thread", nullptr);
144 RTC_CHECK(network_thread_->Start()) << "Failed to start thread";
145
146 worker_thread_ = rtc::Thread::Create();
147 worker_thread_->SetName("worker_thread", nullptr);
148 RTC_CHECK(worker_thread_->Start()) << "Failed to start thread";
149
150 signaling_thread_ = rtc::Thread::Create();
151 signaling_thread_->SetName("signaling_thread", nullptr);
152 RTC_CHECK(signaling_thread_->Start()) << "Failed to start thread";
153
154 std::unique_ptr<cricket::MediaEngineInterface> media_engine =
155 cricket::WebRtcMediaEngineFactory::Create(
156 nullptr /* adm */, webrtc::CreateBuiltinAudioEncoderFactory(),
157 webrtc::CreateBuiltinAudioDecoderFactory(),
158 rtc::MakeUnique<webrtc::InternalEncoderFactory>(),
159 rtc::MakeUnique<webrtc::InternalDecoderFactory>(),
160 nullptr /* audio_mixer */, webrtc::AudioProcessingBuilder().Create());
161 RTC_LOG(LS_INFO) << "Media engine created: " << media_engine.get();
162
163 pcf_ = CreateModularPeerConnectionFactory(
164 network_thread_.get(), worker_thread_.get(), signaling_thread_.get(),
165 std::move(media_engine), webrtc::CreateCallFactory(),
166 webrtc::CreateRtcEventLogFactory());
167 RTC_LOG(LS_INFO) << "PeerConnectionFactory created: " << pcf_;
168}
169
170void AndroidCallClient::CreatePeerConnection() {
171 rtc::CritScope lock(&pc_mutex_);
172 webrtc::PeerConnectionInterface::RTCConfiguration config;
173 config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
174 // DTLS SRTP has to be disabled for loopback to work.
175 config.enable_dtls_srtp = false;
176 pc_ = pcf_->CreatePeerConnection(config, nullptr /* port_allocator */,
177 nullptr /* cert_generator */,
178 pc_observer_.get());
179 RTC_LOG(LS_INFO) << "PeerConnection created: " << pc_;
180
181 rtc::scoped_refptr<webrtc::VideoTrackInterface> local_video_track =
182 pcf_->CreateVideoTrack("video", video_source_);
183 local_video_track->AddOrUpdateSink(local_sink_.get(), rtc::VideoSinkWants());
184 pc_->AddTransceiver(local_video_track);
185 RTC_LOG(LS_INFO) << "Local video sink set up: " << local_video_track;
186
187 for (const rtc::scoped_refptr<webrtc::RtpTransceiverInterface>& tranceiver :
188 pc_->GetTransceivers()) {
189 rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track =
190 tranceiver->receiver()->track();
191 if (track &&
192 track->kind() == webrtc::MediaStreamTrackInterface::kVideoKind) {
193 static_cast<webrtc::VideoTrackInterface*>(track.get())
194 ->AddOrUpdateSink(remote_sink_.get(), rtc::VideoSinkWants());
195 RTC_LOG(LS_INFO) << "Remote video sink set up: " << track;
196 break;
197 }
198 }
199}
200
201void AndroidCallClient::Connect() {
202 rtc::CritScope lock(&pc_mutex_);
203 pc_->CreateOffer(new rtc::RefCountedObject<CreateOfferObserver>(pc_),
204 webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
205}
206
207AndroidCallClient::PCObserver::PCObserver(AndroidCallClient* client)
208 : client_(client) {}
209
210void AndroidCallClient::PCObserver::OnSignalingChange(
211 webrtc::PeerConnectionInterface::SignalingState new_state) {
212 RTC_LOG(LS_INFO) << "OnSignalingChange: " << new_state;
213}
214
215void AndroidCallClient::PCObserver::OnDataChannel(
216 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
217 RTC_LOG(LS_INFO) << "OnDataChannel";
218}
219
220void AndroidCallClient::PCObserver::OnRenegotiationNeeded() {
221 RTC_LOG(LS_INFO) << "OnRenegotiationNeeded";
222}
223
224void AndroidCallClient::PCObserver::OnIceConnectionChange(
225 webrtc::PeerConnectionInterface::IceConnectionState new_state) {
226 RTC_LOG(LS_INFO) << "OnIceConnectionChange: " << new_state;
227}
228
229void AndroidCallClient::PCObserver::OnIceGatheringChange(
230 webrtc::PeerConnectionInterface::IceGatheringState new_state) {
231 RTC_LOG(LS_INFO) << "OnIceGatheringChange: " << new_state;
232}
233
234void AndroidCallClient::PCObserver::OnIceCandidate(
235 const webrtc::IceCandidateInterface* candidate) {
236 RTC_LOG(LS_INFO) << "OnIceCandidate: " << candidate->server_url();
237 rtc::CritScope lock(&client_->pc_mutex_);
238 RTC_DCHECK(client_->pc_ != nullptr);
239 client_->pc_->AddIceCandidate(candidate);
240}
241
242CreateOfferObserver::CreateOfferObserver(
243 rtc::scoped_refptr<webrtc::PeerConnectionInterface> pc)
244 : pc_(pc) {}
245
246void CreateOfferObserver::OnSuccess(webrtc::SessionDescriptionInterface* desc) {
247 std::string sdp;
248 desc->ToString(&sdp);
249 RTC_LOG(LS_INFO) << "Created offer: " << sdp;
250
251 // Ownership of desc was transferred to us, now we transfer it forward.
252 pc_->SetLocalDescription(
253 new rtc::RefCountedObject<SetLocalSessionDescriptionObserver>(), desc);
254
255 // Generate a fake answer.
256 std::unique_ptr<webrtc::SessionDescriptionInterface> answer(
257 webrtc::CreateSessionDescription(webrtc::SdpType::kAnswer, sdp));
258 pc_->SetRemoteDescription(
259 std::move(answer),
260 new rtc::RefCountedObject<SetRemoteSessionDescriptionObserver>());
261}
262
263void CreateOfferObserver::OnFailure(const std::string& error) {
264 RTC_LOG(LS_INFO) << "Failed to create offer: " << error;
265}
266
267void SetRemoteSessionDescriptionObserver::OnSetRemoteDescriptionComplete(
268 webrtc::RTCError error) {
269 RTC_LOG(LS_INFO) << "Set remote description: " << error.message();
270}
271
272void SetLocalSessionDescriptionObserver::OnSuccess() {
273 RTC_LOG(LS_INFO) << "Set local description success!";
274}
275
276void SetLocalSessionDescriptionObserver::OnFailure(const std::string& error) {
277 RTC_LOG(LS_INFO) << "Set local description failure: " << error;
278}
279
Sami Kalliomäki3e77afd2018-03-08 16:43:16 +0100280static jlong JNI_CallClient_CreateClient(
281 JNIEnv* env,
282 const webrtc::JavaParamRef<jclass>& cls) {
283 return webrtc::NativeToJavaPointer(new webrtc_examples::AndroidCallClient());
284}
Sami Kalliomäkic475ac12018-05-16 15:49:18 +0200285
286} // namespace webrtc_examples