blob: 0e3b8e05db06b441b7a6904976fcb616fe71e095 [file] [log] [blame]
Anders Carlsson73119182018-03-15 09:41:03 +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/objcnativeapi/objc/objccallclient.h"
12
13#include <utility>
14
15#import <WebRTC/RTCCameraPreviewView.h>
16#import <WebRTC/RTCVideoCodecFactory.h>
17#import <WebRTC/RTCVideoRenderer.h>
18
Karl Wiberg918f50c2018-07-05 11:40:33 +020019#include "absl/memory/memory.h"
Anders Carlsson73119182018-03-15 09:41:03 +010020#include "api/audio_codecs/builtin_audio_decoder_factory.h"
21#include "api/audio_codecs/builtin_audio_encoder_factory.h"
22#include "api/peerconnectioninterface.h"
23#include "media/engine/webrtcmediaengine.h"
24#include "modules/audio_processing/include/audio_processing.h"
Anders Carlsson73119182018-03-15 09:41:03 +010025#include "sdk/objc/Framework/Native/api/video_capturer.h"
26#include "sdk/objc/Framework/Native/api/video_decoder_factory.h"
27#include "sdk/objc/Framework/Native/api/video_encoder_factory.h"
28#include "sdk/objc/Framework/Native/api/video_renderer.h"
29
30namespace webrtc_examples {
31
32namespace {
33
34class CreateOfferObserver : public webrtc::CreateSessionDescriptionObserver {
35 public:
36 explicit CreateOfferObserver(rtc::scoped_refptr<webrtc::PeerConnectionInterface> pc);
37
38 void OnSuccess(webrtc::SessionDescriptionInterface* desc) override;
Harald Alvestrand73771a82018-05-24 10:53:49 +020039 void OnFailure(webrtc::RTCError error) override;
Anders Carlsson73119182018-03-15 09:41:03 +010040
41 private:
42 const rtc::scoped_refptr<webrtc::PeerConnectionInterface> pc_;
43};
44
45class SetRemoteSessionDescriptionObserver : public webrtc::SetRemoteDescriptionObserverInterface {
46 public:
47 void OnSetRemoteDescriptionComplete(webrtc::RTCError error) override;
48};
49
50class SetLocalSessionDescriptionObserver : public webrtc::SetSessionDescriptionObserver {
51 public:
52 void OnSuccess() override;
Harald Alvestrand73771a82018-05-24 10:53:49 +020053 void OnFailure(webrtc::RTCError error) override;
Anders Carlsson73119182018-03-15 09:41:03 +010054};
55
56} // namespace
57
58ObjCCallClient::ObjCCallClient()
Karl Wiberg918f50c2018-07-05 11:40:33 +020059 : call_started_(false), pc_observer_(absl::make_unique<PCObserver>(this)) {
Anders Carlsson73119182018-03-15 09:41:03 +010060 thread_checker_.DetachFromThread();
61 CreatePeerConnectionFactory();
62}
63
64void ObjCCallClient::Call(RTCVideoCapturer* capturer, id<RTCVideoRenderer> remote_renderer) {
65 RTC_DCHECK_RUN_ON(&thread_checker_);
66
67 rtc::CritScope lock(&pc_mutex_);
68 if (call_started_) {
69 RTC_LOG(LS_WARNING) << "Call already started.";
70 return;
71 }
72 call_started_ = true;
73
74 remote_sink_ = webrtc::ObjCToNativeVideoRenderer(remote_renderer);
75
76 video_source_ =
77 webrtc::ObjCToNativeVideoCapturer(capturer, signaling_thread_.get(), worker_thread_.get());
78
79 CreatePeerConnection();
80 Connect();
81}
82
83void ObjCCallClient::Hangup() {
84 RTC_DCHECK_RUN_ON(&thread_checker_);
85
86 call_started_ = false;
87
88 {
89 rtc::CritScope lock(&pc_mutex_);
90 if (pc_ != nullptr) {
91 pc_->Close();
92 pc_ = nullptr;
93 }
94 }
95
96 remote_sink_ = nullptr;
97 video_source_ = nullptr;
98}
99
100void ObjCCallClient::CreatePeerConnectionFactory() {
101 network_thread_ = rtc::Thread::CreateWithSocketServer();
102 network_thread_->SetName("network_thread", nullptr);
103 RTC_CHECK(network_thread_->Start()) << "Failed to start thread";
104
105 worker_thread_ = rtc::Thread::Create();
106 worker_thread_->SetName("worker_thread", nullptr);
107 RTC_CHECK(worker_thread_->Start()) << "Failed to start thread";
108
109 signaling_thread_ = rtc::Thread::Create();
110 signaling_thread_->SetName("signaling_thread", nullptr);
111 RTC_CHECK(signaling_thread_->Start()) << "Failed to start thread";
112
113 std::unique_ptr<webrtc::VideoDecoderFactory> videoDecoderFactory =
114 webrtc::ObjCToNativeVideoDecoderFactory([[RTCDefaultVideoDecoderFactory alloc] init]);
115 std::unique_ptr<webrtc::VideoEncoderFactory> videoEncoderFactory =
116 webrtc::ObjCToNativeVideoEncoderFactory([[RTCDefaultVideoEncoderFactory alloc] init]);
117
118 std::unique_ptr<cricket::MediaEngineInterface> media_engine =
119 cricket::WebRtcMediaEngineFactory::Create(nullptr /* adm */,
120 webrtc::CreateBuiltinAudioEncoderFactory(),
121 webrtc::CreateBuiltinAudioDecoderFactory(),
122 std::move(videoEncoderFactory),
123 std::move(videoDecoderFactory),
124 nullptr /* audio_mixer */,
125 webrtc::AudioProcessingBuilder().Create());
126 RTC_LOG(LS_INFO) << "Media engine created: " << media_engine.get();
127
128 pcf_ = webrtc::CreateModularPeerConnectionFactory(network_thread_.get(),
129 worker_thread_.get(),
130 signaling_thread_.get(),
131 std::move(media_engine),
132 webrtc::CreateCallFactory(),
133 webrtc::CreateRtcEventLogFactory());
134 RTC_LOG(LS_INFO) << "PeerConnectionFactory created: " << pcf_;
135}
136
137void ObjCCallClient::CreatePeerConnection() {
138 rtc::CritScope lock(&pc_mutex_);
139 webrtc::PeerConnectionInterface::RTCConfiguration config;
140 config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
141 // DTLS SRTP has to be disabled for loopback to work.
142 config.enable_dtls_srtp = false;
143 pc_ = pcf_->CreatePeerConnection(
144 config, nullptr /* port_allocator */, nullptr /* cert_generator */, pc_observer_.get());
145 RTC_LOG(LS_INFO) << "PeerConnection created: " << pc_;
146
147 rtc::scoped_refptr<webrtc::VideoTrackInterface> local_video_track =
148 pcf_->CreateVideoTrack("video", video_source_);
149 pc_->AddTransceiver(local_video_track);
150 RTC_LOG(LS_INFO) << "Local video sink set up: " << local_video_track;
151
152 for (const rtc::scoped_refptr<webrtc::RtpTransceiverInterface>& tranceiver :
153 pc_->GetTransceivers()) {
154 rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track = tranceiver->receiver()->track();
155 if (track && track->kind() == webrtc::MediaStreamTrackInterface::kVideoKind) {
156 static_cast<webrtc::VideoTrackInterface*>(track.get())
157 ->AddOrUpdateSink(remote_sink_.get(), rtc::VideoSinkWants());
158 RTC_LOG(LS_INFO) << "Remote video sink set up: " << track;
159 break;
160 }
161 }
162}
163
164void ObjCCallClient::Connect() {
165 rtc::CritScope lock(&pc_mutex_);
166 pc_->CreateOffer(new rtc::RefCountedObject<CreateOfferObserver>(pc_),
167 webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
168}
169
170ObjCCallClient::PCObserver::PCObserver(ObjCCallClient* client) : client_(client) {}
171
172void ObjCCallClient::PCObserver::OnSignalingChange(
173 webrtc::PeerConnectionInterface::SignalingState new_state) {
174 RTC_LOG(LS_INFO) << "OnSignalingChange: " << new_state;
175}
176
177void ObjCCallClient::PCObserver::OnDataChannel(
178 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
179 RTC_LOG(LS_INFO) << "OnDataChannel";
180}
181
182void ObjCCallClient::PCObserver::OnRenegotiationNeeded() {
183 RTC_LOG(LS_INFO) << "OnRenegotiationNeeded";
184}
185
186void ObjCCallClient::PCObserver::OnIceConnectionChange(
187 webrtc::PeerConnectionInterface::IceConnectionState new_state) {
188 RTC_LOG(LS_INFO) << "OnIceConnectionChange: " << new_state;
189}
190
191void ObjCCallClient::PCObserver::OnIceGatheringChange(
192 webrtc::PeerConnectionInterface::IceGatheringState new_state) {
193 RTC_LOG(LS_INFO) << "OnIceGatheringChange: " << new_state;
194}
195
196void ObjCCallClient::PCObserver::OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
197 RTC_LOG(LS_INFO) << "OnIceCandidate: " << candidate->server_url();
198 rtc::CritScope lock(&client_->pc_mutex_);
199 RTC_DCHECK(client_->pc_ != nullptr);
200 client_->pc_->AddIceCandidate(candidate);
201}
202
203CreateOfferObserver::CreateOfferObserver(rtc::scoped_refptr<webrtc::PeerConnectionInterface> pc)
204 : pc_(pc) {}
205
206void CreateOfferObserver::OnSuccess(webrtc::SessionDescriptionInterface* desc) {
207 std::string sdp;
208 desc->ToString(&sdp);
209 RTC_LOG(LS_INFO) << "Created offer: " << sdp;
210
211 // Ownership of desc was transferred to us, now we transfer it forward.
212 pc_->SetLocalDescription(new rtc::RefCountedObject<SetLocalSessionDescriptionObserver>(), desc);
213
214 // Generate a fake answer.
215 std::unique_ptr<webrtc::SessionDescriptionInterface> answer(
216 webrtc::CreateSessionDescription(webrtc::SdpType::kAnswer, sdp));
217 pc_->SetRemoteDescription(std::move(answer),
218 new rtc::RefCountedObject<SetRemoteSessionDescriptionObserver>());
219}
220
Harald Alvestrand73771a82018-05-24 10:53:49 +0200221void CreateOfferObserver::OnFailure(webrtc::RTCError error) {
222 RTC_LOG(LS_INFO) << "Failed to create offer: " << error.message();
Anders Carlsson73119182018-03-15 09:41:03 +0100223}
224
225void SetRemoteSessionDescriptionObserver::OnSetRemoteDescriptionComplete(webrtc::RTCError error) {
226 RTC_LOG(LS_INFO) << "Set remote description: " << error.message();
227}
228
229void SetLocalSessionDescriptionObserver::OnSuccess() {
230 RTC_LOG(LS_INFO) << "Set local description success!";
231}
232
Harald Alvestrand73771a82018-05-24 10:53:49 +0200233void SetLocalSessionDescriptionObserver::OnFailure(webrtc::RTCError error) {
234 RTC_LOG(LS_INFO) << "Set local description failure: " << error.message();
Anders Carlsson73119182018-03-15 09:41:03 +0100235}
236
237} // namespace webrtc_examples