henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2013, Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 29 | #error "This file requires ARC support." |
| 30 | #endif |
| 31 | |
| 32 | #import "RTCPeerConnectionFactory.h" |
| 33 | |
| 34 | #include <vector> |
| 35 | |
| 36 | #import "RTCAudioTrack+internal.h" |
| 37 | #import "RTCICEServer+internal.h" |
| 38 | #import "RTCMediaConstraints+internal.h" |
| 39 | #import "RTCMediaSource+internal.h" |
| 40 | #import "RTCMediaStream+internal.h" |
| 41 | #import "RTCMediaStreamTrack+internal.h" |
| 42 | #import "RTCPeerConnection+internal.h" |
| 43 | #import "RTCPeerConnectionDelegate.h" |
| 44 | #import "RTCPeerConnectionObserver.h" |
| 45 | #import "RTCVideoCapturer+internal.h" |
| 46 | #import "RTCVideoSource+internal.h" |
| 47 | #import "RTCVideoTrack+internal.h" |
| 48 | |
| 49 | #include "talk/app/webrtc/audiotrack.h" |
| 50 | #include "talk/app/webrtc/mediastreaminterface.h" |
| 51 | #include "talk/app/webrtc/peerconnectionfactory.h" |
| 52 | #include "talk/app/webrtc/peerconnectioninterface.h" |
| 53 | #include "talk/app/webrtc/videosourceinterface.h" |
| 54 | #include "talk/app/webrtc/videotrack.h" |
| 55 | #include "talk/base/logging.h" |
fischman@webrtc.org | 1bc1954 | 2013-08-01 18:29:45 +0000 | [diff] [blame] | 56 | #include "talk/base/ssladapter.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 57 | |
| 58 | @interface RTCPeerConnectionFactory () |
| 59 | |
| 60 | @property(nonatomic, assign) talk_base::scoped_refptr< |
| 61 | webrtc::PeerConnectionFactoryInterface> nativeFactory; |
| 62 | |
| 63 | @end |
| 64 | |
| 65 | @implementation RTCPeerConnectionFactory |
| 66 | |
fischman@webrtc.org | 1bc1954 | 2013-08-01 18:29:45 +0000 | [diff] [blame] | 67 | + (void)initializeSSL { |
| 68 | BOOL initialized = talk_base::InitializeSSL(); |
| 69 | NSAssert(initialized, @"Failed to initialize SSL library"); |
| 70 | } |
| 71 | |
| 72 | + (void)deinitializeSSL { |
| 73 | BOOL deinitialized = talk_base::CleanupSSL(); |
| 74 | NSAssert(deinitialized, @"Failed to deinitialize SSL library"); |
| 75 | } |
| 76 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 77 | - (id)init { |
| 78 | if ((self = [super init])) { |
| 79 | _nativeFactory = webrtc::CreatePeerConnectionFactory(); |
| 80 | NSAssert(_nativeFactory, @"Failed to initialize PeerConnectionFactory!"); |
| 81 | // Uncomment to get sensitive logs emitted (to stderr or logcat). |
| 82 | // talk_base::LogMessage::LogToDebug(talk_base::LS_SENSITIVE); |
| 83 | } |
| 84 | return self; |
| 85 | } |
| 86 | |
| 87 | - (RTCPeerConnection *) |
| 88 | peerConnectionWithICEServers:(NSArray *)servers |
| 89 | constraints:(RTCMediaConstraints *)constraints |
| 90 | delegate:(id<RTCPeerConnectionDelegate>)delegate { |
| 91 | webrtc::PeerConnectionInterface::IceServers iceServers; |
| 92 | for (RTCICEServer *server in servers) { |
| 93 | iceServers.push_back(server.iceServer); |
| 94 | } |
| 95 | webrtc::RTCPeerConnectionObserver *observer = |
| 96 | new webrtc::RTCPeerConnectionObserver(delegate); |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 97 | webrtc::DTLSIdentityServiceInterface* dummy_dtls_identity_service = NULL; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 98 | talk_base::scoped_refptr<webrtc::PeerConnectionInterface> peerConnection = |
| 99 | self.nativeFactory->CreatePeerConnection( |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 100 | iceServers, constraints.constraints, dummy_dtls_identity_service, |
| 101 | observer); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 102 | RTCPeerConnection *pc = |
| 103 | [[RTCPeerConnection alloc] initWithPeerConnection:peerConnection |
| 104 | observer:observer]; |
| 105 | observer->SetPeerConnection(pc); |
| 106 | return pc; |
| 107 | } |
| 108 | |
| 109 | - (RTCMediaStream *)mediaStreamWithLabel:(NSString *)label { |
| 110 | talk_base::scoped_refptr<webrtc::MediaStreamInterface> nativeMediaStream = |
| 111 | self.nativeFactory->CreateLocalMediaStream([label UTF8String]); |
| 112 | return [[RTCMediaStream alloc] initWithMediaStream:nativeMediaStream]; |
| 113 | } |
| 114 | |
| 115 | - (RTCVideoSource *)videoSourceWithCapturer:(RTCVideoCapturer *)capturer |
| 116 | constraints:(RTCMediaConstraints *)constraints { |
| 117 | if (!capturer) { |
| 118 | return nil; |
| 119 | } |
| 120 | talk_base::scoped_refptr<webrtc::VideoSourceInterface> source = |
| 121 | self.nativeFactory->CreateVideoSource(capturer.capturer.get(), |
| 122 | constraints.constraints); |
| 123 | return [[RTCVideoSource alloc] initWithMediaSource:source]; |
| 124 | } |
| 125 | |
| 126 | - (RTCVideoTrack *)videoTrackWithID:(NSString *)videoId |
| 127 | source:(RTCVideoSource *)source { |
| 128 | talk_base::scoped_refptr<webrtc::VideoTrackInterface> track = |
| 129 | self.nativeFactory->CreateVideoTrack([videoId UTF8String], |
| 130 | source.videoSource); |
| 131 | return [[RTCVideoTrack alloc] initWithMediaTrack:track]; |
| 132 | } |
| 133 | |
| 134 | - (RTCAudioTrack *)audioTrackWithID:(NSString *)audioId { |
| 135 | talk_base::scoped_refptr<webrtc::AudioTrackInterface> track = |
| 136 | self.nativeFactory->CreateAudioTrack([audioId UTF8String], NULL); |
| 137 | return [[RTCAudioTrack alloc] initWithMediaTrack:track]; |
| 138 | } |
| 139 | |
| 140 | @end |