Home | History | Annotate | Download | only in objc
      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+Internal.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 "RTCPeerConnectionInterface+Internal.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/peerconnectioninterface.h"
     52 #include "talk/app/webrtc/videosourceinterface.h"
     53 #include "talk/app/webrtc/videotrack.h"
     54 #include "webrtc/base/logging.h"
     55 #include "webrtc/base/ssladapter.h"
     56 
     57 @implementation RTCPeerConnectionFactory {
     58   rtc::scoped_ptr<rtc::Thread> _signalingThread;
     59   rtc::scoped_ptr<rtc::Thread> _workerThread;
     60 }
     61 
     62 @synthesize nativeFactory = _nativeFactory;
     63 
     64 + (void)initializeSSL {
     65   BOOL initialized = rtc::InitializeSSL();
     66   NSAssert(initialized, @"Failed to initialize SSL library");
     67 }
     68 
     69 + (void)deinitializeSSL {
     70   BOOL deinitialized = rtc::CleanupSSL();
     71   NSAssert(deinitialized, @"Failed to deinitialize SSL library");
     72 }
     73 
     74 - (id)init {
     75   if ((self = [super init])) {
     76     _signalingThread.reset(new rtc::Thread());
     77     BOOL result = _signalingThread->Start();
     78     NSAssert(result, @"Failed to start signaling thread.");
     79     _workerThread.reset(new rtc::Thread());
     80     result = _workerThread->Start();
     81     NSAssert(result, @"Failed to start worker thread.");
     82 
     83     _nativeFactory = webrtc::CreatePeerConnectionFactory(
     84         _signalingThread.get(), _workerThread.get(), nullptr, nullptr, nullptr);
     85     NSAssert(_nativeFactory, @"Failed to initialize PeerConnectionFactory!");
     86     // Uncomment to get sensitive logs emitted (to stderr or logcat).
     87     // rtc::LogMessage::LogToDebug(rtc::LS_SENSITIVE);
     88   }
     89   return self;
     90 }
     91 
     92 - (RTCPeerConnection *)peerConnectionWithConfiguration:(RTCConfiguration *)configuration
     93                                            constraints:(RTCMediaConstraints *)constraints
     94                                               delegate:(id<RTCPeerConnectionDelegate>)delegate {
     95   return [[RTCPeerConnection alloc] initWithFactory:self.nativeFactory.get()
     96                                              config:configuration.nativeConfiguration
     97                                         constraints:constraints.constraints
     98                                            delegate:delegate];
     99 }
    100 
    101 - (RTCPeerConnection*)
    102     peerConnectionWithICEServers:(NSArray*)servers
    103                      constraints:(RTCMediaConstraints*)constraints
    104                         delegate:(id<RTCPeerConnectionDelegate>)delegate {
    105   webrtc::PeerConnectionInterface::IceServers iceServers;
    106   for (RTCICEServer* server in servers) {
    107     iceServers.push_back(server.iceServer);
    108   }
    109   RTCPeerConnection* pc =
    110       [[RTCPeerConnection alloc] initWithFactory:self.nativeFactory.get()
    111                                       iceServers:iceServers
    112                                      constraints:constraints.constraints];
    113   pc.delegate = delegate;
    114   return pc;
    115 }
    116 
    117 - (RTCMediaStream*)mediaStreamWithLabel:(NSString*)label {
    118   rtc::scoped_refptr<webrtc::MediaStreamInterface> nativeMediaStream =
    119       self.nativeFactory->CreateLocalMediaStream([label UTF8String]);
    120   return [[RTCMediaStream alloc] initWithMediaStream:nativeMediaStream];
    121 }
    122 
    123 - (RTCVideoSource*)videoSourceWithCapturer:(RTCVideoCapturer*)capturer
    124                                constraints:(RTCMediaConstraints*)constraints {
    125   if (!capturer) {
    126     return nil;
    127   }
    128   rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
    129       self.nativeFactory->CreateVideoSource([capturer takeNativeCapturer],
    130                                             constraints.constraints);
    131   return [[RTCVideoSource alloc] initWithMediaSource:source];
    132 }
    133 
    134 - (RTCVideoTrack*)videoTrackWithID:(NSString*)videoId
    135                             source:(RTCVideoSource*)source {
    136   rtc::scoped_refptr<webrtc::VideoTrackInterface> track =
    137       self.nativeFactory->CreateVideoTrack([videoId UTF8String],
    138                                            source.videoSource);
    139   return [[RTCVideoTrack alloc] initWithMediaTrack:track];
    140 }
    141 
    142 - (RTCAudioTrack*)audioTrackWithID:(NSString*)audioId {
    143   rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
    144       self.nativeFactory->CreateAudioTrack([audioId UTF8String], NULL);
    145   return [[RTCAudioTrack alloc] initWithMediaTrack:track];
    146 }
    147 
    148 @end
    149