2018-03-15 09:41:03 +01:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "examples/objcnativeapi/objc/objc_call_client.h"
|
2018-03-15 09:41:03 +01:00
|
|
|
|
2019-09-17 17:06:18 +02:00
|
|
|
#include <memory>
|
2018-03-15 09:41:03 +01:00
|
|
|
#include <utility>
|
|
|
|
|
|
2018-08-30 09:30:29 +02:00
|
|
|
#import "sdk/objc/base/RTCVideoRenderer.h"
|
|
|
|
|
#import "sdk/objc/components/video_codec/RTCDefaultVideoDecoderFactory.h"
|
|
|
|
|
#import "sdk/objc/components/video_codec/RTCDefaultVideoEncoderFactory.h"
|
|
|
|
|
#import "sdk/objc/helpers/RTCCameraPreviewView.h"
|
2018-03-15 09:41:03 +01:00
|
|
|
|
2024-04-19 15:07:08 +00:00
|
|
|
#include "api/audio/audio_processing.h"
|
2024-10-24 16:16:47 +02:00
|
|
|
#include "api/audio/builtin_audio_processing_builder.h"
|
2018-03-15 09:41:03 +01:00
|
|
|
#include "api/audio_codecs/builtin_audio_decoder_factory.h"
|
|
|
|
|
#include "api/audio_codecs/builtin_audio_encoder_factory.h"
|
2023-11-02 13:11:21 +01:00
|
|
|
#include "api/enable_media.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "api/peer_connection_interface.h"
|
2019-06-26 15:49:47 +02:00
|
|
|
#include "api/rtc_event_log/rtc_event_log_factory.h"
|
2019-05-17 13:20:14 +02:00
|
|
|
#include "api/task_queue/default_task_queue_factory.h"
|
2018-08-30 09:30:29 +02:00
|
|
|
#include "sdk/objc/native/api/video_capturer.h"
|
|
|
|
|
#include "sdk/objc/native/api/video_decoder_factory.h"
|
|
|
|
|
#include "sdk/objc/native/api/video_encoder_factory.h"
|
|
|
|
|
#include "sdk/objc/native/api/video_renderer.h"
|
2018-03-15 09:41:03 +01:00
|
|
|
|
|
|
|
|
namespace webrtc_examples {
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
class CreateOfferObserver : public webrtc::CreateSessionDescriptionObserver {
|
|
|
|
|
public:
|
2025-01-07 02:46:23 -08:00
|
|
|
explicit CreateOfferObserver(
|
|
|
|
|
rtc::scoped_refptr<webrtc::PeerConnectionInterface> pc);
|
2018-03-15 09:41:03 +01:00
|
|
|
|
|
|
|
|
void OnSuccess(webrtc::SessionDescriptionInterface* desc) override;
|
2018-05-24 10:53:49 +02:00
|
|
|
void OnFailure(webrtc::RTCError error) override;
|
2018-03-15 09:41:03 +01:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
const rtc::scoped_refptr<webrtc::PeerConnectionInterface> pc_;
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-07 02:46:23 -08:00
|
|
|
class SetRemoteSessionDescriptionObserver
|
|
|
|
|
: public webrtc::SetRemoteDescriptionObserverInterface {
|
2018-03-15 09:41:03 +01:00
|
|
|
public:
|
|
|
|
|
void OnSetRemoteDescriptionComplete(webrtc::RTCError error) override;
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-07 02:46:23 -08:00
|
|
|
class SetLocalSessionDescriptionObserver
|
|
|
|
|
: public webrtc::SetLocalDescriptionObserverInterface {
|
2018-03-15 09:41:03 +01:00
|
|
|
public:
|
2022-04-25 16:59:19 +02:00
|
|
|
void OnSetLocalDescriptionComplete(webrtc::RTCError error) override;
|
2018-03-15 09:41:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
ObjCCallClient::ObjCCallClient()
|
2019-09-17 17:06:18 +02:00
|
|
|
: call_started_(false), pc_observer_(std::make_unique<PCObserver>(this)) {
|
2019-04-08 15:20:44 +02:00
|
|
|
thread_checker_.Detach();
|
2018-03-15 09:41:03 +01:00
|
|
|
CreatePeerConnectionFactory();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 16:14:32 +02:00
|
|
|
void ObjCCallClient::Call(RTC_OBJC_TYPE(RTCVideoCapturer) * capturer,
|
|
|
|
|
id<RTC_OBJC_TYPE(RTCVideoRenderer)> remote_renderer) {
|
2018-03-15 09:41:03 +01:00
|
|
|
RTC_DCHECK_RUN_ON(&thread_checker_);
|
|
|
|
|
|
2020-07-07 09:17:56 +02:00
|
|
|
webrtc::MutexLock lock(&pc_mutex_);
|
2018-03-15 09:41:03 +01:00
|
|
|
if (call_started_) {
|
|
|
|
|
RTC_LOG(LS_WARNING) << "Call already started.";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
call_started_ = true;
|
|
|
|
|
|
|
|
|
|
remote_sink_ = webrtc::ObjCToNativeVideoRenderer(remote_renderer);
|
|
|
|
|
|
2025-01-07 02:46:23 -08:00
|
|
|
video_source_ = webrtc::ObjCToNativeVideoCapturer(
|
|
|
|
|
capturer, signaling_thread_.get(), worker_thread_.get());
|
2018-03-15 09:41:03 +01:00
|
|
|
|
|
|
|
|
CreatePeerConnection();
|
|
|
|
|
Connect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ObjCCallClient::Hangup() {
|
|
|
|
|
RTC_DCHECK_RUN_ON(&thread_checker_);
|
|
|
|
|
|
|
|
|
|
call_started_ = false;
|
|
|
|
|
|
|
|
|
|
{
|
2020-07-07 09:17:56 +02:00
|
|
|
webrtc::MutexLock lock(&pc_mutex_);
|
2018-03-15 09:41:03 +01:00
|
|
|
if (pc_ != nullptr) {
|
|
|
|
|
pc_->Close();
|
|
|
|
|
pc_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
remote_sink_ = nullptr;
|
|
|
|
|
video_source_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ObjCCallClient::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";
|
|
|
|
|
|
2019-05-17 13:20:14 +02:00
|
|
|
webrtc::PeerConnectionFactoryDependencies dependencies;
|
|
|
|
|
dependencies.network_thread = network_thread_.get();
|
|
|
|
|
dependencies.worker_thread = worker_thread_.get();
|
|
|
|
|
dependencies.signaling_thread = signaling_thread_.get();
|
|
|
|
|
dependencies.task_queue_factory = webrtc::CreateDefaultTaskQueueFactory();
|
2025-01-07 02:46:23 -08:00
|
|
|
dependencies.audio_encoder_factory =
|
|
|
|
|
webrtc::CreateBuiltinAudioEncoderFactory();
|
|
|
|
|
dependencies.audio_decoder_factory =
|
|
|
|
|
webrtc::CreateBuiltinAudioDecoderFactory();
|
2023-11-02 13:11:21 +01:00
|
|
|
dependencies.video_encoder_factory = webrtc::ObjCToNativeVideoEncoderFactory(
|
2020-05-04 16:14:32 +02:00
|
|
|
[[RTC_OBJC_TYPE(RTCDefaultVideoEncoderFactory) alloc] init]);
|
2023-11-02 13:11:21 +01:00
|
|
|
dependencies.video_decoder_factory = webrtc::ObjCToNativeVideoDecoderFactory(
|
2020-05-04 16:14:32 +02:00
|
|
|
[[RTC_OBJC_TYPE(RTCDefaultVideoDecoderFactory) alloc] init]);
|
2025-01-07 02:46:23 -08:00
|
|
|
dependencies.audio_processing_builder =
|
|
|
|
|
std::make_unique<webrtc::BuiltinAudioProcessingBuilder>();
|
2023-11-02 13:11:21 +01:00
|
|
|
webrtc::EnableMedia(dependencies);
|
2025-01-07 02:46:23 -08:00
|
|
|
dependencies.event_log_factory =
|
|
|
|
|
std::make_unique<webrtc::RtcEventLogFactory>();
|
2019-05-17 13:20:14 +02:00
|
|
|
pcf_ = webrtc::CreateModularPeerConnectionFactory(std::move(dependencies));
|
2022-04-25 16:59:19 +02:00
|
|
|
RTC_LOG(LS_INFO) << "PeerConnectionFactory created: " << pcf_.get();
|
2018-03-15 09:41:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ObjCCallClient::CreatePeerConnection() {
|
2020-07-07 09:17:56 +02:00
|
|
|
webrtc::MutexLock lock(&pc_mutex_);
|
2018-03-15 09:41:03 +01:00
|
|
|
webrtc::PeerConnectionInterface::RTCConfiguration config;
|
|
|
|
|
config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
|
2021-10-13 15:26:26 +00:00
|
|
|
// Encryption has to be disabled for loopback to work.
|
|
|
|
|
webrtc::PeerConnectionFactoryInterface::Options options;
|
|
|
|
|
options.disable_encryption = true;
|
|
|
|
|
pcf_->SetOptions(options);
|
2019-05-17 13:20:14 +02:00
|
|
|
webrtc::PeerConnectionDependencies pc_dependencies(pc_observer_.get());
|
2025-01-07 02:46:23 -08:00
|
|
|
pc_ = pcf_->CreatePeerConnectionOrError(config, std::move(pc_dependencies))
|
|
|
|
|
.MoveValue();
|
2022-04-25 16:59:19 +02:00
|
|
|
RTC_LOG(LS_INFO) << "PeerConnection created: " << pc_.get();
|
2018-03-15 09:41:03 +01:00
|
|
|
|
|
|
|
|
rtc::scoped_refptr<webrtc::VideoTrackInterface> local_video_track =
|
2023-03-20 14:13:42 +00:00
|
|
|
pcf_->CreateVideoTrack(video_source_, "video");
|
2018-03-15 09:41:03 +01:00
|
|
|
pc_->AddTransceiver(local_video_track);
|
2022-04-25 16:59:19 +02:00
|
|
|
RTC_LOG(LS_INFO) << "Local video sink set up: " << local_video_track.get();
|
2018-03-15 09:41:03 +01:00
|
|
|
|
|
|
|
|
for (const rtc::scoped_refptr<webrtc::RtpTransceiverInterface>& tranceiver :
|
|
|
|
|
pc_->GetTransceivers()) {
|
2025-01-07 02:46:23 -08:00
|
|
|
rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track =
|
|
|
|
|
tranceiver->receiver()->track();
|
|
|
|
|
if (track &&
|
|
|
|
|
track->kind() == webrtc::MediaStreamTrackInterface::kVideoKind) {
|
2018-03-15 09:41:03 +01:00
|
|
|
static_cast<webrtc::VideoTrackInterface*>(track.get())
|
|
|
|
|
->AddOrUpdateSink(remote_sink_.get(), rtc::VideoSinkWants());
|
2022-04-25 16:59:19 +02:00
|
|
|
RTC_LOG(LS_INFO) << "Remote video sink set up: " << track.get();
|
2018-03-15 09:41:03 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ObjCCallClient::Connect() {
|
2020-07-07 09:17:56 +02:00
|
|
|
webrtc::MutexLock lock(&pc_mutex_);
|
2022-04-25 16:59:19 +02:00
|
|
|
pc_->CreateOffer(rtc::make_ref_counted<CreateOfferObserver>(pc_).get(),
|
2018-03-15 09:41:03 +01:00
|
|
|
webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-07 02:46:23 -08:00
|
|
|
ObjCCallClient::PCObserver::PCObserver(ObjCCallClient* client)
|
|
|
|
|
: client_(client) {}
|
2018-03-15 09:41:03 +01:00
|
|
|
|
|
|
|
|
void ObjCCallClient::PCObserver::OnSignalingChange(
|
|
|
|
|
webrtc::PeerConnectionInterface::SignalingState new_state) {
|
|
|
|
|
RTC_LOG(LS_INFO) << "OnSignalingChange: " << new_state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ObjCCallClient::PCObserver::OnDataChannel(
|
|
|
|
|
rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
|
|
|
|
|
RTC_LOG(LS_INFO) << "OnDataChannel";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ObjCCallClient::PCObserver::OnRenegotiationNeeded() {
|
|
|
|
|
RTC_LOG(LS_INFO) << "OnRenegotiationNeeded";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ObjCCallClient::PCObserver::OnIceConnectionChange(
|
|
|
|
|
webrtc::PeerConnectionInterface::IceConnectionState new_state) {
|
|
|
|
|
RTC_LOG(LS_INFO) << "OnIceConnectionChange: " << new_state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ObjCCallClient::PCObserver::OnIceGatheringChange(
|
|
|
|
|
webrtc::PeerConnectionInterface::IceGatheringState new_state) {
|
|
|
|
|
RTC_LOG(LS_INFO) << "OnIceGatheringChange: " << new_state;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-07 02:46:23 -08:00
|
|
|
void ObjCCallClient::PCObserver::OnIceCandidate(
|
|
|
|
|
const webrtc::IceCandidateInterface* candidate) {
|
2018-03-15 09:41:03 +01:00
|
|
|
RTC_LOG(LS_INFO) << "OnIceCandidate: " << candidate->server_url();
|
2020-07-07 09:17:56 +02:00
|
|
|
webrtc::MutexLock lock(&client_->pc_mutex_);
|
2018-03-15 09:41:03 +01:00
|
|
|
RTC_DCHECK(client_->pc_ != nullptr);
|
|
|
|
|
client_->pc_->AddIceCandidate(candidate);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-07 02:46:23 -08:00
|
|
|
CreateOfferObserver::CreateOfferObserver(
|
|
|
|
|
rtc::scoped_refptr<webrtc::PeerConnectionInterface> pc)
|
2018-03-15 09:41:03 +01:00
|
|
|
: 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.
|
2025-01-07 02:46:23 -08:00
|
|
|
pc_->SetLocalDescription(
|
|
|
|
|
absl::WrapUnique(desc),
|
|
|
|
|
rtc::make_ref_counted<SetLocalSessionDescriptionObserver>());
|
2018-03-15 09:41:03 +01:00
|
|
|
|
|
|
|
|
// Generate a fake answer.
|
|
|
|
|
std::unique_ptr<webrtc::SessionDescriptionInterface> answer(
|
|
|
|
|
webrtc::CreateSessionDescription(webrtc::SdpType::kAnswer, sdp));
|
2025-01-07 02:46:23 -08:00
|
|
|
pc_->SetRemoteDescription(
|
|
|
|
|
std::move(answer),
|
|
|
|
|
rtc::make_ref_counted<SetRemoteSessionDescriptionObserver>());
|
2018-03-15 09:41:03 +01:00
|
|
|
}
|
|
|
|
|
|
2018-05-24 10:53:49 +02:00
|
|
|
void CreateOfferObserver::OnFailure(webrtc::RTCError error) {
|
|
|
|
|
RTC_LOG(LS_INFO) << "Failed to create offer: " << error.message();
|
2018-03-15 09:41:03 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-07 02:46:23 -08:00
|
|
|
void SetRemoteSessionDescriptionObserver::OnSetRemoteDescriptionComplete(
|
|
|
|
|
webrtc::RTCError error) {
|
2018-03-15 09:41:03 +01:00
|
|
|
RTC_LOG(LS_INFO) << "Set remote description: " << error.message();
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-07 02:46:23 -08:00
|
|
|
void SetLocalSessionDescriptionObserver::OnSetLocalDescriptionComplete(
|
|
|
|
|
webrtc::RTCError error) {
|
2022-04-25 16:59:19 +02:00
|
|
|
RTC_LOG(LS_INFO) << "Set local description: " << error.message();
|
2018-03-15 09:41:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc_examples
|