Add an example app for iOS native API.
Demonstrates how to use the iOS native API to wrap components into C++ classes. This CL also introduces a native API wrapper for the capturer. The C++ code is forked from the corresponding CL for Android at https://webrtc-review.googlesource.com/c/src/+/60540 Bug: webrtc:8832 Change-Id: I12d9f30e701c0222628e329218f6d5bfca26e6e0 Reviewed-on: https://webrtc-review.googlesource.com/61422 Commit-Queue: Anders Carlsson <andersc@webrtc.org> Reviewed-by: Kári Helgason <kthelgason@webrtc.org> Cr-Commit-Position: refs/heads/master@{#22484}
This commit is contained in:

committed by
Commit Bot

parent
8cf0a87bc3
commit
7311918269
45
examples/objcnativeapi/Info.plist
Normal file
45
examples/objcnativeapi/Info.plist
Normal file
@ -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>
|
17
examples/objcnativeapi/objc/NADAppDelegate.h
Normal file
17
examples/objcnativeapi/objc/NADAppDelegate.h
Normal file
@ -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
|
63
examples/objcnativeapi/objc/NADAppDelegate.m
Normal file
63
examples/objcnativeapi/objc/NADAppDelegate.m
Normal file
@ -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
|
15
examples/objcnativeapi/objc/NADViewController.h
Normal file
15
examples/objcnativeapi/objc/NADViewController.h
Normal file
@ -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
|
157
examples/objcnativeapi/objc/NADViewController.mm
Normal file
157
examples/objcnativeapi/objc/NADViewController.mm
Normal file
@ -0,0 +1,157 @@
|
||||
/*
|
||||
* 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 <WebRTC/RTCCameraPreviewView.h>
|
||||
#import <WebRTC/RTCCameraVideoCapturer.h>
|
||||
#import <WebRTC/RTCEAGLVideoView.h>
|
||||
#import <WebRTC/RTCMTLVideoView.h>
|
||||
#import <WebRTC/RTCVideoRenderer.h>
|
||||
#include <memory>
|
||||
|
||||
#include "objccallclient.h"
|
||||
|
||||
@interface NADViewController ()
|
||||
|
||||
@property(nonatomic) RTCCameraVideoCapturer *capturer;
|
||||
@property(nonatomic) RTCCameraPreviewView *localVideoView;
|
||||
@property(nonatomic) __kindof UIView<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];
|
||||
|
||||
#if defined(RTC_SUPPORTS_METAL)
|
||||
_remoteVideoView = [[RTCMTLVideoView alloc] initWithFrame:CGRectZero];
|
||||
#else
|
||||
_remoteVideoView = [[RTCEAGLVideoView alloc] initWithFrame:CGRectZero];
|
||||
#endif
|
||||
_remoteVideoView.translatesAutoresizingMaskIntoConstraints = NO;
|
||||
[_view addSubview:_remoteVideoView];
|
||||
|
||||
_localVideoView = [[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 = [[RTCCameraVideoCapturer alloc] init];
|
||||
self.localVideoView.captureSession = self.capturer.captureSession;
|
||||
|
||||
_call_client.reset(new webrtc_examples::ObjCCallClient());
|
||||
|
||||
// Start capturer.
|
||||
AVCaptureDevice *selectedDevice = nil;
|
||||
NSArray<AVCaptureDevice *> *captureDevices = [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 =
|
||||
[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
|
18
examples/objcnativeapi/objc/main.m
Normal file
18
examples/objcnativeapi/objc/main.m
Normal file
@ -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]));
|
||||
}
|
||||
}
|
84
examples/objcnativeapi/objc/objccallclient.h
Normal file
84
examples/objcnativeapi/objc/objccallclient.h
Normal file
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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>
|
||||
|
||||
#include "api/peerconnectioninterface.h"
|
||||
#include "rtc_base/criticalsection.h"
|
||||
#include "rtc_base/scoped_ref_ptr.h"
|
||||
#include "rtc_base/thread_checker.h"
|
||||
|
||||
@class RTCVideoCapturer;
|
||||
@protocol RTCVideoRenderer;
|
||||
|
||||
namespace webrtc_examples {
|
||||
|
||||
class ObjCCallClient {
|
||||
public:
|
||||
ObjCCallClient();
|
||||
|
||||
void Call(RTCVideoCapturer* capturer, id<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:
|
||||
const ObjCCallClient* client_;
|
||||
};
|
||||
|
||||
void CreatePeerConnectionFactory() RTC_RUN_ON(thread_checker_);
|
||||
void CreatePeerConnection() RTC_RUN_ON(thread_checker_);
|
||||
void Connect() RTC_RUN_ON(thread_checker_);
|
||||
|
||||
rtc::ThreadChecker 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_);
|
||||
|
||||
rtc::CriticalSection pc_mutex_;
|
||||
rtc::scoped_refptr<webrtc::PeerConnectionInterface> pc_
|
||||
RTC_GUARDED_BY(pc_mutex_);
|
||||
};
|
||||
|
||||
} // namespace webrtc_examples
|
||||
|
||||
#endif // EXAMPLES_OBJCNATIVEAPI_OBJCCALLCLIENT_H_
|
237
examples/objcnativeapi/objc/objccallclient.mm
Normal file
237
examples/objcnativeapi/objc/objccallclient.mm
Normal file
@ -0,0 +1,237 @@
|
||||
/*
|
||||
* 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/objccallclient.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
#import <WebRTC/RTCCameraPreviewView.h>
|
||||
#import <WebRTC/RTCVideoCodecFactory.h>
|
||||
#import <WebRTC/RTCVideoRenderer.h>
|
||||
|
||||
#include "api/audio_codecs/builtin_audio_decoder_factory.h"
|
||||
#include "api/audio_codecs/builtin_audio_encoder_factory.h"
|
||||
#include "api/peerconnectioninterface.h"
|
||||
#include "media/engine/webrtcmediaengine.h"
|
||||
#include "modules/audio_processing/include/audio_processing.h"
|
||||
#include "rtc_base/ptr_util.h"
|
||||
#include "sdk/objc/Framework/Native/api/video_capturer.h"
|
||||
#include "sdk/objc/Framework/Native/api/video_decoder_factory.h"
|
||||
#include "sdk/objc/Framework/Native/api/video_encoder_factory.h"
|
||||
#include "sdk/objc/Framework/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(const std::string& 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::SetSessionDescriptionObserver {
|
||||
public:
|
||||
void OnSuccess() override;
|
||||
void OnFailure(const std::string& error) override;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
ObjCCallClient::ObjCCallClient()
|
||||
: call_started_(false), pc_observer_(rtc::MakeUnique<PCObserver>(this)) {
|
||||
thread_checker_.DetachFromThread();
|
||||
CreatePeerConnectionFactory();
|
||||
}
|
||||
|
||||
void ObjCCallClient::Call(RTCVideoCapturer* capturer, id<RTCVideoRenderer> remote_renderer) {
|
||||
RTC_DCHECK_RUN_ON(&thread_checker_);
|
||||
|
||||
rtc::CritScope 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;
|
||||
|
||||
{
|
||||
rtc::CritScope 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";
|
||||
|
||||
std::unique_ptr<webrtc::VideoDecoderFactory> videoDecoderFactory =
|
||||
webrtc::ObjCToNativeVideoDecoderFactory([[RTCDefaultVideoDecoderFactory alloc] init]);
|
||||
std::unique_ptr<webrtc::VideoEncoderFactory> videoEncoderFactory =
|
||||
webrtc::ObjCToNativeVideoEncoderFactory([[RTCDefaultVideoEncoderFactory alloc] init]);
|
||||
|
||||
std::unique_ptr<cricket::MediaEngineInterface> media_engine =
|
||||
cricket::WebRtcMediaEngineFactory::Create(nullptr /* adm */,
|
||||
webrtc::CreateBuiltinAudioEncoderFactory(),
|
||||
webrtc::CreateBuiltinAudioDecoderFactory(),
|
||||
std::move(videoEncoderFactory),
|
||||
std::move(videoDecoderFactory),
|
||||
nullptr /* audio_mixer */,
|
||||
webrtc::AudioProcessingBuilder().Create());
|
||||
RTC_LOG(LS_INFO) << "Media engine created: " << media_engine.get();
|
||||
|
||||
pcf_ = webrtc::CreateModularPeerConnectionFactory(network_thread_.get(),
|
||||
worker_thread_.get(),
|
||||
signaling_thread_.get(),
|
||||
std::move(media_engine),
|
||||
webrtc::CreateCallFactory(),
|
||||
webrtc::CreateRtcEventLogFactory());
|
||||
RTC_LOG(LS_INFO) << "PeerConnectionFactory created: " << pcf_;
|
||||
}
|
||||
|
||||
void ObjCCallClient::CreatePeerConnection() {
|
||||
rtc::CritScope lock(&pc_mutex_);
|
||||
webrtc::PeerConnectionInterface::RTCConfiguration config;
|
||||
config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
|
||||
// DTLS SRTP has to be disabled for loopback to work.
|
||||
config.enable_dtls_srtp = false;
|
||||
pc_ = pcf_->CreatePeerConnection(
|
||||
config, nullptr /* port_allocator */, nullptr /* cert_generator */, pc_observer_.get());
|
||||
RTC_LOG(LS_INFO) << "PeerConnection created: " << pc_;
|
||||
|
||||
rtc::scoped_refptr<webrtc::VideoTrackInterface> local_video_track =
|
||||
pcf_->CreateVideoTrack("video", video_source_);
|
||||
pc_->AddTransceiver(local_video_track);
|
||||
RTC_LOG(LS_INFO) << "Local video sink set up: " << local_video_track;
|
||||
|
||||
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;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ObjCCallClient::Connect() {
|
||||
rtc::CritScope lock(&pc_mutex_);
|
||||
pc_->CreateOffer(new rtc::RefCountedObject<CreateOfferObserver>(pc_),
|
||||
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();
|
||||
rtc::CritScope 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(new rtc::RefCountedObject<SetLocalSessionDescriptionObserver>(), desc);
|
||||
|
||||
// Generate a fake answer.
|
||||
std::unique_ptr<webrtc::SessionDescriptionInterface> answer(
|
||||
webrtc::CreateSessionDescription(webrtc::SdpType::kAnswer, sdp));
|
||||
pc_->SetRemoteDescription(std::move(answer),
|
||||
new rtc::RefCountedObject<SetRemoteSessionDescriptionObserver>());
|
||||
}
|
||||
|
||||
void CreateOfferObserver::OnFailure(const std::string& error) {
|
||||
RTC_LOG(LS_INFO) << "Failed to create offer: " << error;
|
||||
}
|
||||
|
||||
void SetRemoteSessionDescriptionObserver::OnSetRemoteDescriptionComplete(webrtc::RTCError error) {
|
||||
RTC_LOG(LS_INFO) << "Set remote description: " << error.message();
|
||||
}
|
||||
|
||||
void SetLocalSessionDescriptionObserver::OnSuccess() {
|
||||
RTC_LOG(LS_INFO) << "Set local description success!";
|
||||
}
|
||||
|
||||
void SetLocalSessionDescriptionObserver::OnFailure(const std::string& error) {
|
||||
RTC_LOG(LS_INFO) << "Set local description failure: " << error;
|
||||
}
|
||||
|
||||
} // namespace webrtc_examples
|
Reference in New Issue
Block a user