Home | History | Annotate | Download | only in webrtc
      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 
     29 package org.webrtc;
     30 
     31 import java.util.List;
     32 
     33 /**
     34  * Java wrapper for a C++ PeerConnectionFactoryInterface.  Main entry point to
     35  * the PeerConnection API for clients.
     36  */
     37 public class PeerConnectionFactory {
     38   static {
     39     System.loadLibrary("jingle_peerconnection_so");
     40   }
     41 
     42   private final long nativeFactory;
     43 
     44   // |context| is an android.content.Context object, but we keep it untyped here
     45   // to allow building on non-Android platforms.
     46   public static native boolean initializeAndroidGlobals(Object context);
     47 
     48   public PeerConnectionFactory() {
     49     nativeFactory = nativeCreatePeerConnectionFactory();
     50     if (nativeFactory == 0) {
     51       throw new RuntimeException("Failed to initialize PeerConnectionFactory!");
     52     }
     53   }
     54 
     55 
     56   public PeerConnection createPeerConnection(
     57       List<PeerConnection.IceServer> iceServers,
     58       MediaConstraints constraints,
     59       PeerConnection.Observer observer) {
     60     long nativeObserver = nativeCreateObserver(observer);
     61     if (nativeObserver == 0) {
     62       return null;
     63     }
     64     long nativePeerConnection = nativeCreatePeerConnection(
     65         nativeFactory, iceServers, constraints, nativeObserver);
     66     if (nativePeerConnection == 0) {
     67       return null;
     68     }
     69     return new PeerConnection(nativePeerConnection, nativeObserver);
     70   }
     71 
     72   public MediaStream createLocalMediaStream(String label) {
     73     return new MediaStream(
     74         nativeCreateLocalMediaStream(nativeFactory, label));
     75   }
     76 
     77   public VideoSource createVideoSource(
     78       VideoCapturer capturer, MediaConstraints constraints) {
     79     return new VideoSource(nativeCreateVideoSource(
     80         nativeFactory, capturer.nativeVideoCapturer, constraints));
     81   }
     82 
     83   public VideoTrack createVideoTrack(String id, VideoSource source) {
     84     return new VideoTrack(nativeCreateVideoTrack(
     85         nativeFactory, id, source.nativeSource));
     86   }
     87 
     88   public AudioTrack createAudioTrack(String id) {
     89     return new AudioTrack(nativeCreateAudioTrack(nativeFactory, id));
     90   }
     91 
     92   public void dispose() {
     93     freeFactory(nativeFactory);
     94   }
     95 
     96   private static native long nativeCreatePeerConnectionFactory();
     97 
     98   private static native long nativeCreateObserver(
     99       PeerConnection.Observer observer);
    100 
    101   private static native long nativeCreatePeerConnection(
    102       long nativeFactory, List<PeerConnection.IceServer> iceServers,
    103       MediaConstraints constraints, long nativeObserver);
    104 
    105   private static native long nativeCreateLocalMediaStream(
    106       long nativeFactory, String label);
    107 
    108   private static native long nativeCreateVideoSource(
    109       long nativeFactory, long nativeVideoCapturer,
    110       MediaConstraints constraints);
    111 
    112   private static native long nativeCreateVideoTrack(
    113       long nativeFactory, String id, long nativeVideoSource);
    114 
    115   private static native long nativeCreateAudioTrack(
    116       long nativeFactory, String id);
    117 
    118   private static native void freeFactory(long nativeFactory);
    119 }
    120