summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/examples/objcnativeapi
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/libwebrtc/examples/objcnativeapi')
-rw-r--r--third_party/libwebrtc/examples/objcnativeapi/Info.plist45
-rw-r--r--third_party/libwebrtc/examples/objcnativeapi/objc/NADAppDelegate.h17
-rw-r--r--third_party/libwebrtc/examples/objcnativeapi/objc/NADAppDelegate.m63
-rw-r--r--third_party/libwebrtc/examples/objcnativeapi/objc/NADViewController.h15
-rw-r--r--third_party/libwebrtc/examples/objcnativeapi/objc/NADViewController.mm154
-rw-r--r--third_party/libwebrtc/examples/objcnativeapi/objc/main.m18
-rw-r--r--third_party/libwebrtc/examples/objcnativeapi/objc/objc_call_client.h82
-rw-r--r--third_party/libwebrtc/examples/objcnativeapi/objc/objc_call_client.mm238
8 files changed, 632 insertions, 0 deletions
diff --git a/third_party/libwebrtc/examples/objcnativeapi/Info.plist b/third_party/libwebrtc/examples/objcnativeapi/Info.plist
new file mode 100644
index 0000000000..cbc9e5f9f3
--- /dev/null
+++ b/third_party/libwebrtc/examples/objcnativeapi/Info.plist
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>CFBundleDevelopmentRegion</key>
+ <string>en</string>
+ <key>CFBundleExecutable</key>
+ <string>$(EXECUTABLE_NAME)</string>
+ <key>CFBundleIdentifier</key>
+ <string>com.google.ObjCNativeAPIDemo</string>
+ <key>CFBundleInfoDictionaryVersion</key>
+ <string>6.0</string>
+ <key>CFBundleName</key>
+ <string>ObjCNativeAPIDemo</string>
+ <key>CFBundlePackageType</key>
+ <string>APPL</string>
+ <key>CFBundleShortVersionString</key>
+ <string>1.0</string>
+ <key>CFBundleVersion</key>
+ <string>1</string>
+ <key>LSRequiresIPhoneOS</key>
+ <true/>
+ <key>UIRequiredDeviceCapabilities</key>
+ <array>
+ <string>armv7</string>
+ </array>
+ <key>UISupportedInterfaceOrientations</key>
+ <array>
+ <string>UIInterfaceOrientationPortrait</string>
+ <string>UIInterfaceOrientationLandscapeLeft</string>
+ <string>UIInterfaceOrientationLandscapeRight</string>
+ </array>
+ <key>UISupportedInterfaceOrientations~ipad</key>
+ <array>
+ <string>UIInterfaceOrientationPortrait</string>
+ <string>UIInterfaceOrientationPortraitUpsideDown</string>
+ <string>UIInterfaceOrientationLandscapeLeft</string>
+ <string>UIInterfaceOrientationLandscapeRight</string>
+ </array>
+ <key>NSCameraUsageDescription</key>
+ <string>Camera access needed for video calling</string>
+ <key>NSMicrophoneUsageDescription</key>
+ <string>Microphone access needed for video calling</string>
+</dict>
+</plist>
diff --git a/third_party/libwebrtc/examples/objcnativeapi/objc/NADAppDelegate.h b/third_party/libwebrtc/examples/objcnativeapi/objc/NADAppDelegate.h
new file mode 100644
index 0000000000..02372dbfd2
--- /dev/null
+++ b/third_party/libwebrtc/examples/objcnativeapi/objc/NADAppDelegate.h
@@ -0,0 +1,17 @@
+/*
+ * 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.
+ */
+
+#import <UIKit/UIKit.h>
+
+@interface NADAppDelegate : UIResponder <UIApplicationDelegate>
+
+@property(strong, nonatomic) UIWindow* window;
+
+@end
diff --git a/third_party/libwebrtc/examples/objcnativeapi/objc/NADAppDelegate.m b/third_party/libwebrtc/examples/objcnativeapi/objc/NADAppDelegate.m
new file mode 100644
index 0000000000..254dd3be76
--- /dev/null
+++ b/third_party/libwebrtc/examples/objcnativeapi/objc/NADAppDelegate.m
@@ -0,0 +1,63 @@
+/*
+ * 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.
+ */
+
+#import "NADAppDelegate.h"
+
+#import "NADViewController.h"
+
+@interface NADAppDelegate ()
+@end
+
+@implementation NADAppDelegate
+
+@synthesize window = _window;
+
+- (BOOL)application:(UIApplication *)application
+ didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
+ _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
+ [_window makeKeyAndVisible];
+
+ NADViewController *viewController = [[NADViewController alloc] init];
+ _window.rootViewController = viewController;
+
+ return YES;
+}
+
+- (void)applicationWillResignActive:(UIApplication *)application {
+ // Sent when the application is about to move from active to inactive state. This can occur for
+ // certain types of temporary interruptions (such as an incoming phone call or SMS message) or
+ // when the user quits the application and it begins the transition to the background state. Use
+ // this method to pause ongoing tasks, disable timers, and invalidate graphics rendering
+ // callbacks. Games should use this method to pause the game.
+}
+
+- (void)applicationDidEnterBackground:(UIApplication *)application {
+ // Use this method to release shared resources, save user data, invalidate timers, and store
+ // enough application state information to restore your application to its current state in case
+ // it is terminated later. If your application supports background execution, this method is
+ // called instead of applicationWillTerminate: when the user quits.
+}
+
+- (void)applicationWillEnterForeground:(UIApplication *)application {
+ // Called as part of the transition from the background to the active state; here you can undo
+ // many of the changes made on entering the background.
+}
+
+- (void)applicationDidBecomeActive:(UIApplication *)application {
+ // Restart any tasks that were paused (or not yet started) while the application was inactive. If
+ // the application was previously in the background, optionally refresh the user interface.
+}
+
+- (void)applicationWillTerminate:(UIApplication *)application {
+ // Called when the application is about to terminate. Save data if appropriate. See also
+ // applicationDidEnterBackground:.
+}
+
+@end
diff --git a/third_party/libwebrtc/examples/objcnativeapi/objc/NADViewController.h b/third_party/libwebrtc/examples/objcnativeapi/objc/NADViewController.h
new file mode 100644
index 0000000000..c43bebb52d
--- /dev/null
+++ b/third_party/libwebrtc/examples/objcnativeapi/objc/NADViewController.h
@@ -0,0 +1,15 @@
+/*
+ * 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.
+ */
+
+#import <UIKit/UIKit.h>
+
+@interface NADViewController : UIViewController
+
+@end
diff --git a/third_party/libwebrtc/examples/objcnativeapi/objc/NADViewController.mm b/third_party/libwebrtc/examples/objcnativeapi/objc/NADViewController.mm
new file mode 100644
index 0000000000..fd244799f8
--- /dev/null
+++ b/third_party/libwebrtc/examples/objcnativeapi/objc/NADViewController.mm
@@ -0,0 +1,154 @@
+/*
+ * 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.
+ */
+
+#import "NADViewController.h"
+
+#import "sdk/objc/base/RTCVideoRenderer.h"
+#import "sdk/objc/components/capturer/RTCCameraVideoCapturer.h"
+#import "sdk/objc/components/renderer/metal/RTCMTLVideoView.h"
+#import "sdk/objc/helpers/RTCCameraPreviewView.h"
+
+#include <memory>
+
+#include "examples/objcnativeapi/objc/objc_call_client.h"
+
+@interface NADViewController ()
+
+@property(nonatomic) RTC_OBJC_TYPE(RTCCameraVideoCapturer) * capturer;
+@property(nonatomic) RTC_OBJC_TYPE(RTCCameraPreviewView) * localVideoView;
+@property(nonatomic) __kindof UIView<RTC_OBJC_TYPE(RTCVideoRenderer)> *remoteVideoView;
+@property(nonatomic) UIButton *callButton;
+@property(nonatomic) UIButton *hangUpButton;
+
+@end
+
+@implementation NADViewController {
+ std::unique_ptr<webrtc_examples::ObjCCallClient> _call_client;
+
+ UIView *_view;
+}
+
+@synthesize capturer = _capturer;
+@synthesize localVideoView = _localVideoView;
+@synthesize remoteVideoView = _remoteVideoView;
+@synthesize callButton = _callButton;
+@synthesize hangUpButton = _hangUpButton;
+
+#pragma mark - View controller lifecycle
+
+- (void)loadView {
+ _view = [[UIView alloc] initWithFrame:CGRectZero];
+
+ _remoteVideoView = [[RTC_OBJC_TYPE(RTCMTLVideoView) alloc] initWithFrame:CGRectZero];
+ _remoteVideoView.translatesAutoresizingMaskIntoConstraints = NO;
+ [_view addSubview:_remoteVideoView];
+
+ _localVideoView = [[RTC_OBJC_TYPE(RTCCameraPreviewView) alloc] initWithFrame:CGRectZero];
+ _localVideoView.translatesAutoresizingMaskIntoConstraints = NO;
+ [_view addSubview:_localVideoView];
+
+ _callButton = [UIButton buttonWithType:UIButtonTypeSystem];
+ _callButton.translatesAutoresizingMaskIntoConstraints = NO;
+ [_callButton setTitle:@"Call" forState:UIControlStateNormal];
+ [_callButton addTarget:self action:@selector(call:) forControlEvents:UIControlEventTouchUpInside];
+ [_view addSubview:_callButton];
+
+ _hangUpButton = [UIButton buttonWithType:UIButtonTypeSystem];
+ _hangUpButton.translatesAutoresizingMaskIntoConstraints = NO;
+ [_hangUpButton setTitle:@"Hang up" forState:UIControlStateNormal];
+ [_hangUpButton addTarget:self
+ action:@selector(hangUp:)
+ forControlEvents:UIControlEventTouchUpInside];
+ [_view addSubview:_hangUpButton];
+
+ UILayoutGuide *margin = _view.layoutMarginsGuide;
+ [_remoteVideoView.leadingAnchor constraintEqualToAnchor:margin.leadingAnchor].active = YES;
+ [_remoteVideoView.topAnchor constraintEqualToAnchor:margin.topAnchor].active = YES;
+ [_remoteVideoView.trailingAnchor constraintEqualToAnchor:margin.trailingAnchor].active = YES;
+ [_remoteVideoView.bottomAnchor constraintEqualToAnchor:margin.bottomAnchor].active = YES;
+
+ [_localVideoView.leadingAnchor constraintEqualToAnchor:margin.leadingAnchor constant:8.0].active =
+ YES;
+ [_localVideoView.topAnchor constraintEqualToAnchor:margin.topAnchor constant:8.0].active = YES;
+ [_localVideoView.widthAnchor constraintEqualToConstant:60].active = YES;
+ [_localVideoView.heightAnchor constraintEqualToConstant:60].active = YES;
+
+ [_callButton.leadingAnchor constraintEqualToAnchor:margin.leadingAnchor constant:8.0].active =
+ YES;
+ [_callButton.bottomAnchor constraintEqualToAnchor:margin.bottomAnchor constant:8.0].active = YES;
+ [_callButton.widthAnchor constraintEqualToConstant:100].active = YES;
+ [_callButton.heightAnchor constraintEqualToConstant:40].active = YES;
+
+ [_hangUpButton.trailingAnchor constraintEqualToAnchor:margin.trailingAnchor constant:8.0].active =
+ YES;
+ [_hangUpButton.bottomAnchor constraintEqualToAnchor:margin.bottomAnchor constant:8.0].active =
+ YES;
+ [_hangUpButton.widthAnchor constraintEqualToConstant:100].active = YES;
+ [_hangUpButton.heightAnchor constraintEqualToConstant:40].active = YES;
+
+ self.view = _view;
+}
+
+- (void)viewDidLoad {
+ [super viewDidLoad];
+
+ self.capturer = [[RTC_OBJC_TYPE(RTCCameraVideoCapturer) alloc] init];
+ self.localVideoView.captureSession = self.capturer.captureSession;
+
+ _call_client.reset(new webrtc_examples::ObjCCallClient());
+
+ // Start capturer.
+ AVCaptureDevice *selectedDevice = nil;
+ NSArray<AVCaptureDevice *> *captureDevices =
+ [RTC_OBJC_TYPE(RTCCameraVideoCapturer) captureDevices];
+ for (AVCaptureDevice *device in captureDevices) {
+ if (device.position == AVCaptureDevicePositionFront) {
+ selectedDevice = device;
+ break;
+ }
+ }
+
+ AVCaptureDeviceFormat *selectedFormat = nil;
+ int targetWidth = 640;
+ int targetHeight = 480;
+ int currentDiff = INT_MAX;
+ NSArray<AVCaptureDeviceFormat *> *formats =
+ [RTC_OBJC_TYPE(RTCCameraVideoCapturer) supportedFormatsForDevice:selectedDevice];
+ for (AVCaptureDeviceFormat *format in formats) {
+ CMVideoDimensions dimension = CMVideoFormatDescriptionGetDimensions(format.formatDescription);
+ FourCharCode pixelFormat = CMFormatDescriptionGetMediaSubType(format.formatDescription);
+ int diff = abs(targetWidth - dimension.width) + abs(targetHeight - dimension.height);
+ if (diff < currentDiff) {
+ selectedFormat = format;
+ currentDiff = diff;
+ } else if (diff == currentDiff && pixelFormat == [_capturer preferredOutputPixelFormat]) {
+ selectedFormat = format;
+ }
+ }
+
+ [self.capturer startCaptureWithDevice:selectedDevice format:selectedFormat fps:30];
+}
+
+- (void)didReceiveMemoryWarning {
+ [super didReceiveMemoryWarning];
+ // Dispose of any resources that can be recreated.
+}
+
+#pragma mark - Actions
+
+- (IBAction)call:(id)sender {
+ _call_client->Call(self.capturer, self.remoteVideoView);
+}
+
+- (IBAction)hangUp:(id)sender {
+ _call_client->Hangup();
+}
+
+@end
diff --git a/third_party/libwebrtc/examples/objcnativeapi/objc/main.m b/third_party/libwebrtc/examples/objcnativeapi/objc/main.m
new file mode 100644
index 0000000000..2c3b5fbbfb
--- /dev/null
+++ b/third_party/libwebrtc/examples/objcnativeapi/objc/main.m
@@ -0,0 +1,18 @@
+/*
+ * 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.
+ */
+
+#import <UIKit/UIKit.h>
+#import "NADAppDelegate.h"
+
+int main(int argc, char* argv[]) {
+ @autoreleasepool {
+ return UIApplicationMain(argc, argv, nil, NSStringFromClass([NADAppDelegate class]));
+ }
+}
diff --git a/third_party/libwebrtc/examples/objcnativeapi/objc/objc_call_client.h b/third_party/libwebrtc/examples/objcnativeapi/objc/objc_call_client.h
new file mode 100644
index 0000000000..cb8501d9ce
--- /dev/null
+++ b/third_party/libwebrtc/examples/objcnativeapi/objc/objc_call_client.h
@@ -0,0 +1,82 @@
+/*
+ * 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.
+ */
+
+#ifndef EXAMPLES_OBJCNATIVEAPI_OBJCCALLCLIENT_H_
+#define EXAMPLES_OBJCNATIVEAPI_OBJCCALLCLIENT_H_
+
+#include <memory>
+#include <string>
+
+#import "sdk/objc/base/RTCMacros.h"
+
+#include "api/peer_connection_interface.h"
+#include "api/scoped_refptr.h"
+#include "api/sequence_checker.h"
+#include "rtc_base/synchronization/mutex.h"
+
+@class RTC_OBJC_TYPE(RTCVideoCapturer);
+@protocol RTC_OBJC_TYPE
+(RTCVideoRenderer);
+
+namespace webrtc_examples {
+
+class ObjCCallClient {
+ public:
+ ObjCCallClient();
+
+ void Call(RTC_OBJC_TYPE(RTCVideoCapturer) * capturer,
+ id<RTC_OBJC_TYPE(RTCVideoRenderer)> remote_renderer);
+ void Hangup();
+
+ private:
+ class PCObserver : public webrtc::PeerConnectionObserver {
+ public:
+ explicit PCObserver(ObjCCallClient* 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:
+ ObjCCallClient* const client_;
+ };
+
+ void CreatePeerConnectionFactory() RTC_RUN_ON(thread_checker_);
+ void CreatePeerConnection() RTC_RUN_ON(thread_checker_);
+ void Connect() RTC_RUN_ON(thread_checker_);
+
+ webrtc::SequenceChecker thread_checker_;
+
+ bool call_started_ RTC_GUARDED_BY(thread_checker_);
+
+ const std::unique_ptr<PCObserver> pc_observer_;
+
+ rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pcf_ RTC_GUARDED_BY(thread_checker_);
+ std::unique_ptr<rtc::Thread> network_thread_ RTC_GUARDED_BY(thread_checker_);
+ std::unique_ptr<rtc::Thread> worker_thread_ RTC_GUARDED_BY(thread_checker_);
+ std::unique_ptr<rtc::Thread> signaling_thread_ RTC_GUARDED_BY(thread_checker_);
+
+ std::unique_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> remote_sink_
+ RTC_GUARDED_BY(thread_checker_);
+ rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> video_source_
+ RTC_GUARDED_BY(thread_checker_);
+
+ webrtc::Mutex pc_mutex_;
+ rtc::scoped_refptr<webrtc::PeerConnectionInterface> pc_ RTC_GUARDED_BY(pc_mutex_);
+};
+
+} // namespace webrtc_examples
+
+#endif // EXAMPLES_OBJCNATIVEAPI_OBJCCALLCLIENT_H_
diff --git a/third_party/libwebrtc/examples/objcnativeapi/objc/objc_call_client.mm b/third_party/libwebrtc/examples/objcnativeapi/objc/objc_call_client.mm
new file mode 100644
index 0000000000..7dd57b499b
--- /dev/null
+++ b/third_party/libwebrtc/examples/objcnativeapi/objc/objc_call_client.mm
@@ -0,0 +1,238 @@
+/*
+ * 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/objcnativeapi/objc/objc_call_client.h"
+
+#include <memory>
+#include <utility>
+
+#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"
+
+#include "api/audio_codecs/builtin_audio_decoder_factory.h"
+#include "api/audio_codecs/builtin_audio_encoder_factory.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 "media/engine/webrtc_media_engine.h"
+#include "modules/audio_processing/include/audio_processing.h"
+#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"
+
+namespace webrtc_examples {
+
+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::SetLocalDescriptionObserverInterface {
+ public:
+ void OnSetLocalDescriptionComplete(webrtc::RTCError error) override;
+};
+
+} // namespace
+
+ObjCCallClient::ObjCCallClient()
+ : call_started_(false), pc_observer_(std::make_unique<PCObserver>(this)) {
+ thread_checker_.Detach();
+ CreatePeerConnectionFactory();
+}
+
+void ObjCCallClient::Call(RTC_OBJC_TYPE(RTCVideoCapturer) * capturer,
+ id<RTC_OBJC_TYPE(RTCVideoRenderer)> remote_renderer) {
+ 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;
+
+ remote_sink_ = webrtc::ObjCToNativeVideoRenderer(remote_renderer);
+
+ video_source_ =
+ webrtc::ObjCToNativeVideoCapturer(capturer, signaling_thread_.get(), worker_thread_.get());
+
+ CreatePeerConnection();
+ Connect();
+}
+
+void ObjCCallClient::Hangup() {
+ RTC_DCHECK_RUN_ON(&thread_checker_);
+
+ call_started_ = false;
+
+ {
+ webrtc::MutexLock lock(&pc_mutex_);
+ 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";
+
+ 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();
+ cricket::MediaEngineDependencies media_deps;
+ media_deps.task_queue_factory = dependencies.task_queue_factory.get();
+ media_deps.audio_encoder_factory = webrtc::CreateBuiltinAudioEncoderFactory();
+ media_deps.audio_decoder_factory = webrtc::CreateBuiltinAudioDecoderFactory();
+ media_deps.video_encoder_factory = webrtc::ObjCToNativeVideoEncoderFactory(
+ [[RTC_OBJC_TYPE(RTCDefaultVideoEncoderFactory) alloc] init]);
+ media_deps.video_decoder_factory = webrtc::ObjCToNativeVideoDecoderFactory(
+ [[RTC_OBJC_TYPE(RTCDefaultVideoDecoderFactory) alloc] init]);
+ media_deps.audio_processing = webrtc::AudioProcessingBuilder().Create();
+ dependencies.media_engine = cricket::CreateMediaEngine(std::move(media_deps));
+ RTC_LOG(LS_INFO) << "Media engine created: " << dependencies.media_engine.get();
+ dependencies.call_factory = webrtc::CreateCallFactory();
+ dependencies.event_log_factory =
+ std::make_unique<webrtc::RtcEventLogFactory>(dependencies.task_queue_factory.get());
+ pcf_ = webrtc::CreateModularPeerConnectionFactory(std::move(dependencies));
+ RTC_LOG(LS_INFO) << "PeerConnectionFactory created: " << pcf_.get();
+}
+
+void ObjCCallClient::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 pc_dependencies(pc_observer_.get());
+ pc_ = pcf_->CreatePeerConnectionOrError(config, std::move(pc_dependencies)).MoveValue();
+ RTC_LOG(LS_INFO) << "PeerConnection created: " << pc_.get();
+
+ rtc::scoped_refptr<webrtc::VideoTrackInterface> local_video_track =
+ pcf_->CreateVideoTrack("video", video_source_.get());
+ 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 ObjCCallClient::Connect() {
+ webrtc::MutexLock lock(&pc_mutex_);
+ pc_->CreateOffer(rtc::make_ref_counted<CreateOfferObserver>(pc_).get(),
+ webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
+}
+
+ObjCCallClient::PCObserver::PCObserver(ObjCCallClient* client) : client_(client) {}
+
+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;
+}
+
+void ObjCCallClient::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(absl::WrapUnique(desc),
+ rtc::make_ref_counted<SetLocalSessionDescriptionObserver>());
+
+ // 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: " << error.message();
+}
+
+void SetRemoteSessionDescriptionObserver::OnSetRemoteDescriptionComplete(webrtc::RTCError error) {
+ RTC_LOG(LS_INFO) << "Set remote description: " << error.message();
+}
+
+void SetLocalSessionDescriptionObserver::OnSetLocalDescriptionComplete(webrtc::RTCError error) {
+ RTC_LOG(LS_INFO) << "Set local description: " << error.message();
+}
+
+} // namespace webrtc_examples