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