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