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