• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 package org.webrtc.examples.androidvoip;
12 
13 import android.content.Context;
14 import android.os.Handler;
15 import android.os.HandlerThread;
16 import java.util.ArrayList;
17 import java.util.List;
18 
19 public class VoipClient {
20   private static final String TAG = "VoipClient";
21 
22   private final HandlerThread thread;
23   private final Handler handler;
24 
25   private long nativeClient;
26   private OnVoipClientTaskCompleted listener;
27 
VoipClient(Context applicationContext, OnVoipClientTaskCompleted listener)28   public VoipClient(Context applicationContext, OnVoipClientTaskCompleted listener) {
29     this.listener = listener;
30     thread = new HandlerThread(TAG + "Thread");
31     thread.start();
32     handler = new Handler(thread.getLooper());
33 
34     handler.post(() -> {
35       nativeClient = nativeCreateClient(applicationContext);
36       listener.onVoipClientInitializationCompleted(/* isSuccessful */ nativeClient != 0);
37     });
38   }
39 
isInitialized()40   private boolean isInitialized() {
41     return nativeClient != 0;
42   }
43 
getAndSetUpSupportedCodecs()44   public void getAndSetUpSupportedCodecs() {
45     handler.post(() -> {
46       if (isInitialized()) {
47         listener.onGetSupportedCodecsCompleted(nativeGetSupportedCodecs(nativeClient));
48       } else {
49         listener.onUninitializedVoipClient();
50       }
51     });
52   }
53 
getAndSetUpLocalIPAddress()54   public void getAndSetUpLocalIPAddress() {
55     handler.post(() -> {
56       if (isInitialized()) {
57         listener.onGetLocalIPAddressCompleted(nativeGetLocalIPAddress(nativeClient));
58       } else {
59         listener.onUninitializedVoipClient();
60       }
61     });
62   }
63 
setEncoder(String encoder)64   public void setEncoder(String encoder) {
65     handler.post(() -> {
66       if (isInitialized()) {
67         nativeSetEncoder(nativeClient, encoder);
68       } else {
69         listener.onUninitializedVoipClient();
70       }
71     });
72   }
73 
setDecoders(List<String> decoders)74   public void setDecoders(List<String> decoders) {
75     handler.post(() -> {
76       if (isInitialized()) {
77         nativeSetDecoders(nativeClient, decoders);
78       } else {
79         listener.onUninitializedVoipClient();
80       }
81     });
82   }
83 
setLocalAddress(String ipAddress, int portNumber)84   public void setLocalAddress(String ipAddress, int portNumber) {
85     handler.post(() -> {
86       if (isInitialized()) {
87         nativeSetLocalAddress(nativeClient, ipAddress, portNumber);
88       } else {
89         listener.onUninitializedVoipClient();
90       }
91     });
92   }
93 
setRemoteAddress(String ipAddress, int portNumber)94   public void setRemoteAddress(String ipAddress, int portNumber) {
95     handler.post(() -> {
96       if (isInitialized()) {
97         nativeSetRemoteAddress(nativeClient, ipAddress, portNumber);
98       } else {
99         listener.onUninitializedVoipClient();
100       }
101     });
102   }
103 
startSession()104   public void startSession() {
105     handler.post(() -> {
106       if (isInitialized()) {
107         listener.onStartSessionCompleted(nativeStartSession(nativeClient));
108       } else {
109         listener.onUninitializedVoipClient();
110       }
111     });
112   }
113 
stopSession()114   public void stopSession() {
115     handler.post(() -> {
116       if (isInitialized()) {
117         listener.onStopSessionCompleted(nativeStopSession(nativeClient));
118       } else {
119         listener.onUninitializedVoipClient();
120       }
121     });
122   }
123 
startSend()124   public void startSend() {
125     handler.post(() -> {
126       if (isInitialized()) {
127         listener.onStartSendCompleted(nativeStartSend(nativeClient));
128       } else {
129         listener.onUninitializedVoipClient();
130       }
131     });
132   }
133 
stopSend()134   public void stopSend() {
135     handler.post(() -> {
136       if (isInitialized()) {
137         listener.onStopSendCompleted(nativeStopSend(nativeClient));
138       } else {
139         listener.onUninitializedVoipClient();
140       }
141     });
142   }
143 
startPlayout()144   public void startPlayout() {
145     handler.post(() -> {
146       if (isInitialized()) {
147         listener.onStartPlayoutCompleted(nativeStartPlayout(nativeClient));
148       } else {
149         listener.onUninitializedVoipClient();
150       }
151     });
152   }
153 
stopPlayout()154   public void stopPlayout() {
155     handler.post(() -> {
156       if (isInitialized()) {
157         listener.onStopPlayoutCompleted(nativeStopPlayout(nativeClient));
158       } else {
159         listener.onUninitializedVoipClient();
160       }
161     });
162   }
163 
close()164   public void close() {
165     handler.post(() -> {
166       nativeDelete(nativeClient);
167       nativeClient = 0;
168     });
169     thread.quitSafely();
170   }
171 
nativeCreateClient(Context applicationContext)172   private static native long nativeCreateClient(Context applicationContext);
nativeGetSupportedCodecs(long nativeAndroidVoipClient)173   private static native List<String> nativeGetSupportedCodecs(long nativeAndroidVoipClient);
nativeGetLocalIPAddress(long nativeAndroidVoipClient)174   private static native String nativeGetLocalIPAddress(long nativeAndroidVoipClient);
nativeSetEncoder(long nativeAndroidVoipClient, String encoder)175   private static native void nativeSetEncoder(long nativeAndroidVoipClient, String encoder);
nativeSetDecoders(long nativeAndroidVoipClient, List<String> decoders)176   private static native void nativeSetDecoders(long nativeAndroidVoipClient, List<String> decoders);
nativeSetLocalAddress( long nativeAndroidVoipClient, String ipAddress, int portNumber)177   private static native void nativeSetLocalAddress(
178       long nativeAndroidVoipClient, String ipAddress, int portNumber);
nativeSetRemoteAddress( long nativeAndroidVoipClient, String ipAddress, int portNumber)179   private static native void nativeSetRemoteAddress(
180       long nativeAndroidVoipClient, String ipAddress, int portNumber);
nativeStartSession(long nativeAndroidVoipClient)181   private static native boolean nativeStartSession(long nativeAndroidVoipClient);
nativeStopSession(long nativeAndroidVoipClient)182   private static native boolean nativeStopSession(long nativeAndroidVoipClient);
nativeStartSend(long nativeAndroidVoipClient)183   private static native boolean nativeStartSend(long nativeAndroidVoipClient);
nativeStopSend(long nativeAndroidVoipClient)184   private static native boolean nativeStopSend(long nativeAndroidVoipClient);
nativeStartPlayout(long nativeAndroidVoipClient)185   private static native boolean nativeStartPlayout(long nativeAndroidVoipClient);
nativeStopPlayout(long nativeAndroidVoipClient)186   private static native boolean nativeStopPlayout(long nativeAndroidVoipClient);
nativeDelete(long nativeAndroidVoipClient)187   private static native void nativeDelete(long nativeAndroidVoipClient);
188 }
189