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
29package org.webrtc;
30
31import java.util.List;
32
33/**
34 * Java wrapper for a C++ PeerConnectionFactoryInterface.  Main entry point to
35 * the PeerConnection API for clients.
36 */
37public 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  // Callers may specify either |initializeAudio| or |initializeVideo| as false
47  // to skip initializing the respective engine (and avoid the need for the
48  // respective permissions).
49  // |renderEGLContext| can be provided to suport HW video decoding to
50  // texture and will be used to create a shared EGL context on video
51  // decoding thread.
52  public static native boolean initializeAndroidGlobals(
53      Object context, boolean initializeAudio, boolean initializeVideo,
54      Object renderEGLContext);
55
56  public PeerConnectionFactory() {
57    nativeFactory = nativeCreatePeerConnectionFactory();
58    if (nativeFactory == 0) {
59      throw new RuntimeException("Failed to initialize PeerConnectionFactory!");
60    }
61  }
62
63
64  public PeerConnection createPeerConnection(
65      List<PeerConnection.IceServer> iceServers,
66      MediaConstraints constraints,
67      PeerConnection.Observer observer) {
68    long nativeObserver = nativeCreateObserver(observer);
69    if (nativeObserver == 0) {
70      return null;
71    }
72    long nativePeerConnection = nativeCreatePeerConnection(
73        nativeFactory, iceServers, constraints, nativeObserver);
74    if (nativePeerConnection == 0) {
75      return null;
76    }
77    return new PeerConnection(nativePeerConnection, nativeObserver);
78  }
79
80  public MediaStream createLocalMediaStream(String label) {
81    return new MediaStream(
82        nativeCreateLocalMediaStream(nativeFactory, label));
83  }
84
85  public VideoSource createVideoSource(
86      VideoCapturer capturer, MediaConstraints constraints) {
87    return new VideoSource(nativeCreateVideoSource(
88        nativeFactory, capturer.takeNativeVideoCapturer(), constraints));
89  }
90
91  public VideoTrack createVideoTrack(String id, VideoSource source) {
92    return new VideoTrack(nativeCreateVideoTrack(
93        nativeFactory, id, source.nativeSource));
94  }
95
96  public AudioSource createAudioSource(MediaConstraints constraints) {
97    return new AudioSource(nativeCreateAudioSource(nativeFactory, constraints));
98  }
99
100  public AudioTrack createAudioTrack(String id, AudioSource source) {
101    return new AudioTrack(nativeCreateAudioTrack(
102        nativeFactory, id, source.nativeSource));
103  }
104
105  public void dispose() {
106    freeFactory(nativeFactory);
107  }
108
109  private static native long nativeCreatePeerConnectionFactory();
110
111  private static native long nativeCreateObserver(
112      PeerConnection.Observer observer);
113
114  private static native long nativeCreatePeerConnection(
115      long nativeFactory, List<PeerConnection.IceServer> iceServers,
116      MediaConstraints constraints, long nativeObserver);
117
118  private static native long nativeCreateLocalMediaStream(
119      long nativeFactory, String label);
120
121  private static native long nativeCreateVideoSource(
122      long nativeFactory, long nativeVideoCapturer,
123      MediaConstraints constraints);
124
125  private static native long nativeCreateVideoTrack(
126      long nativeFactory, String id, long nativeVideoSource);
127
128  private static native long nativeCreateAudioSource(
129      long nativeFactory, MediaConstraints constraints);
130
131  private static native long nativeCreateAudioTrack(
132      long nativeFactory, String id, long nativeSource);
133
134  private static native void freeFactory(long nativeFactory);
135}
136