1package eu.siacs.conversations.xmpp.jingle;
2
3import android.content.Context;
4import android.media.ToneGenerator;
5import android.os.Build;
6import android.os.Handler;
7import android.os.Looper;
8import android.util.Log;
9
10import com.google.common.base.Optional;
11import com.google.common.base.Preconditions;
12import com.google.common.collect.ImmutableMap;
13import com.google.common.collect.ImmutableSet;
14import com.google.common.collect.Iterables;
15import com.google.common.util.concurrent.Futures;
16import com.google.common.util.concurrent.ListenableFuture;
17import com.google.common.util.concurrent.MoreExecutors;
18import com.google.common.util.concurrent.SettableFuture;
19
20import org.webrtc.AudioSource;
21import org.webrtc.AudioTrack;
22import org.webrtc.Camera2Enumerator;
23import org.webrtc.CameraEnumerationAndroid;
24import org.webrtc.CameraEnumerator;
25import org.webrtc.CameraVideoCapturer;
26import org.webrtc.CandidatePairChangeEvent;
27import org.webrtc.DataChannel;
28import org.webrtc.DefaultVideoDecoderFactory;
29import org.webrtc.DefaultVideoEncoderFactory;
30import org.webrtc.DtmfSender;
31import org.webrtc.EglBase;
32import org.webrtc.IceCandidate;
33import org.webrtc.MediaConstraints;
34import org.webrtc.MediaStream;
35import org.webrtc.MediaStreamTrack;
36import org.webrtc.PeerConnection;
37import org.webrtc.PeerConnectionFactory;
38import org.webrtc.RtpReceiver;
39import org.webrtc.RtpTransceiver;
40import org.webrtc.SdpObserver;
41import org.webrtc.SessionDescription;
42import org.webrtc.SurfaceTextureHelper;
43import org.webrtc.VideoSource;
44import org.webrtc.VideoTrack;
45import org.webrtc.audio.JavaAudioDeviceModule;
46import org.webrtc.voiceengine.WebRtcAudioEffects;
47
48import java.util.ArrayList;
49import java.util.Collections;
50import java.util.LinkedList;
51import java.util.List;
52import java.util.Queue;
53import java.util.Map;
54import java.util.Set;
55import java.util.concurrent.ExecutorService;
56import java.util.concurrent.Executors;
57import java.util.concurrent.atomic.AtomicBoolean;
58
59import javax.annotation.Nonnull;
60import javax.annotation.Nullable;
61
62import eu.siacs.conversations.Config;
63import eu.siacs.conversations.services.AppRTCAudioManager;
64import eu.siacs.conversations.services.XmppConnectionService;
65
66public class WebRTCWrapper {
67
68 private static final String EXTENDED_LOGGING_TAG = WebRTCWrapper.class.getSimpleName();
69
70 private final ExecutorService executorService = Executors.newSingleThreadExecutor();
71
72 private static final Set<String> HARDWARE_AEC_BLACKLIST = new ImmutableSet.Builder<String>()
73 .add("Pixel")
74 .add("Pixel XL")
75 .add("Moto G5")
76 .add("Moto G (5S) Plus")
77 .add("Moto G4")
78 .add("TA-1053")
79 .add("Mi A1")
80 .add("Mi A2")
81 .add("E5823") // Sony z5 compact
82 .add("Redmi Note 5")
83 .add("FP2") // Fairphone FP2
84 .add("MI 5")
85 .add("GT-I9515") // Samsung Galaxy S4 Value Edition (jfvelte)
86 .add("GT-I9515L") // Samsung Galaxy S4 Value Edition (jfvelte)
87 .add("GT-I9505") // Samsung Galaxy S4 (jfltexx)
88 .build();
89
90 private static final int TONE_DURATION = 200;
91 private static final Map<String,Integer> TONE_CODES;
92 static {
93 ImmutableMap.Builder<String,Integer> builder = new ImmutableMap.Builder<>();
94 builder.put("0", ToneGenerator.TONE_DTMF_0);
95 builder.put("1", ToneGenerator.TONE_DTMF_1);
96 builder.put("2", ToneGenerator.TONE_DTMF_2);
97 builder.put("3", ToneGenerator.TONE_DTMF_3);
98 builder.put("4", ToneGenerator.TONE_DTMF_4);
99 builder.put("5", ToneGenerator.TONE_DTMF_5);
100 builder.put("6", ToneGenerator.TONE_DTMF_6);
101 builder.put("7", ToneGenerator.TONE_DTMF_7);
102 builder.put("8", ToneGenerator.TONE_DTMF_8);
103 builder.put("9", ToneGenerator.TONE_DTMF_9);
104 builder.put("*", ToneGenerator.TONE_DTMF_S);
105 builder.put("#", ToneGenerator.TONE_DTMF_P);
106 TONE_CODES = builder.build();
107 }
108
109 private static final int CAPTURING_RESOLUTION = 1920;
110 private static final int CAPTURING_MAX_FRAME_RATE = 30;
111
112 private final EventCallback eventCallback;
113 private final AtomicBoolean readyToReceivedIceCandidates = new AtomicBoolean(false);
114 private final Queue<IceCandidate> iceCandidates = new LinkedList<>();
115 private final AppRTCAudioManager.AudioManagerEvents audioManagerEvents = new AppRTCAudioManager.AudioManagerEvents() {
116 @Override
117 public void onAudioDeviceChanged(AppRTCAudioManager.AudioDevice selectedAudioDevice, Set<AppRTCAudioManager.AudioDevice> availableAudioDevices) {
118 eventCallback.onAudioDeviceChanged(selectedAudioDevice, availableAudioDevices);
119 }
120 };
121 private final Handler mainHandler = new Handler(Looper.getMainLooper());
122 private VideoTrack localVideoTrack = null;
123 private VideoTrack remoteVideoTrack = null;
124 private final PeerConnection.Observer peerConnectionObserver = new PeerConnection.Observer() {
125 @Override
126 public void onSignalingChange(PeerConnection.SignalingState signalingState) {
127 Log.d(EXTENDED_LOGGING_TAG, "onSignalingChange(" + signalingState + ")");
128 //this is called after removeTrack or addTrack
129 //and should then trigger a content-add or content-remove or something
130 //https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/removeTrack
131 }
132
133 @Override
134 public void onConnectionChange(final PeerConnection.PeerConnectionState newState) {
135 eventCallback.onConnectionChange(newState);
136 }
137
138 @Override
139 public void onIceConnectionChange(PeerConnection.IceConnectionState iceConnectionState) {
140 Log.d(EXTENDED_LOGGING_TAG, "onIceConnectionChange(" + iceConnectionState + ")");
141 }
142
143 @Override
144 public void onSelectedCandidatePairChanged(CandidatePairChangeEvent event) {
145 Log.d(Config.LOGTAG, "remote candidate selected: " + event.remote);
146 Log.d(Config.LOGTAG, "local candidate selected: " + event.local);
147 }
148
149 @Override
150 public void onIceConnectionReceivingChange(boolean b) {
151
152 }
153
154 @Override
155 public void onIceGatheringChange(PeerConnection.IceGatheringState iceGatheringState) {
156 Log.d(EXTENDED_LOGGING_TAG, "onIceGatheringChange(" + iceGatheringState + ")");
157 }
158
159 @Override
160 public void onIceCandidate(IceCandidate iceCandidate) {
161 if (readyToReceivedIceCandidates.get()) {
162 eventCallback.onIceCandidate(iceCandidate);
163 } else {
164 iceCandidates.add(iceCandidate);
165 }
166 }
167
168 @Override
169 public void onIceCandidatesRemoved(IceCandidate[] iceCandidates) {
170
171 }
172
173 @Override
174 public void onAddStream(MediaStream mediaStream) {
175 Log.d(EXTENDED_LOGGING_TAG, "onAddStream(numAudioTracks=" + mediaStream.audioTracks.size() + ",numVideoTracks=" + mediaStream.videoTracks.size() + ")");
176 }
177
178 @Override
179 public void onRemoveStream(MediaStream mediaStream) {
180
181 }
182
183 @Override
184 public void onDataChannel(DataChannel dataChannel) {
185
186 }
187
188 @Override
189 public void onRenegotiationNeeded() {
190 Log.d(EXTENDED_LOGGING_TAG, "onRenegotiationNeeded()");
191 final PeerConnection.PeerConnectionState currentState = peerConnection == null ? null : peerConnection.connectionState();
192 if (currentState != null && currentState != PeerConnection.PeerConnectionState.NEW) {
193 eventCallback.onRenegotiationNeeded();
194 }
195 }
196
197 @Override
198 public void onAddTrack(RtpReceiver rtpReceiver, MediaStream[] mediaStreams) {
199 final MediaStreamTrack track = rtpReceiver.track();
200 Log.d(EXTENDED_LOGGING_TAG, "onAddTrack(kind=" + (track == null ? "null" : track.kind()) + ",numMediaStreams=" + mediaStreams.length + ")");
201 if (track instanceof VideoTrack) {
202 remoteVideoTrack = (VideoTrack) track;
203 }
204 }
205
206 @Override
207 public void onTrack(RtpTransceiver transceiver) {
208 Log.d(EXTENDED_LOGGING_TAG, "onTrack(mid=" + transceiver.getMid() + ",media=" + transceiver.getMediaType() + ")");
209 }
210 };
211 @Nullable
212 private PeerConnection peerConnection = null;
213 private AudioTrack localAudioTrack = null;
214 private AppRTCAudioManager appRTCAudioManager = null;
215 private ToneManager toneManager = null;
216 private Context context = null;
217 private EglBase eglBase = null;
218 private CapturerChoice capturerChoice;
219
220 WebRTCWrapper(final EventCallback eventCallback) {
221 this.eventCallback = eventCallback;
222 }
223
224 private static void dispose(final PeerConnection peerConnection) {
225 try {
226 peerConnection.dispose();
227 } catch (final IllegalStateException e) {
228 Log.e(Config.LOGTAG, "unable to dispose of peer connection", e);
229 }
230 }
231
232 @Nullable
233 private static CapturerChoice of(CameraEnumerator enumerator, final String deviceName, Set<String> availableCameras) {
234 final CameraVideoCapturer capturer = enumerator.createCapturer(deviceName, null);
235 if (capturer == null) {
236 return null;
237 }
238 final ArrayList<CameraEnumerationAndroid.CaptureFormat> choices = new ArrayList<>(enumerator.getSupportedFormats(deviceName));
239 Collections.sort(choices, (a, b) -> b.width - a.width);
240 for (final CameraEnumerationAndroid.CaptureFormat captureFormat : choices) {
241 if (captureFormat.width <= CAPTURING_RESOLUTION) {
242 return new CapturerChoice(capturer, captureFormat, availableCameras);
243 }
244 }
245 return null;
246 }
247
248 private static boolean isFrontFacing(final CameraEnumerator cameraEnumerator, final String deviceName) {
249 try {
250 return cameraEnumerator.isFrontFacing(deviceName);
251 } catch (final NullPointerException e) {
252 return false;
253 }
254 }
255
256 public void setup(final XmppConnectionService service, final AppRTCAudioManager.SpeakerPhonePreference speakerPhonePreference) throws InitializationException {
257 try {
258 PeerConnectionFactory.initialize(
259 PeerConnectionFactory.InitializationOptions.builder(service).createInitializationOptions()
260 );
261 } catch (final UnsatisfiedLinkError e) {
262 throw new InitializationException("Unable to initialize PeerConnectionFactory", e);
263 }
264 try {
265 this.eglBase = EglBase.create();
266 } catch (final RuntimeException e) {
267 throw new InitializationException("Unable to create EGL base", e);
268 }
269 this.context = service;
270 this.toneManager = service.getJingleConnectionManager().toneManager;
271 mainHandler.post(() -> {
272 appRTCAudioManager = AppRTCAudioManager.create(service, speakerPhonePreference);
273 toneManager.setAppRtcAudioManagerHasControl(true);
274 appRTCAudioManager.start(audioManagerEvents);
275 eventCallback.onAudioDeviceChanged(appRTCAudioManager.getSelectedAudioDevice(), appRTCAudioManager.getAudioDevices());
276 });
277 }
278
279 synchronized void initializePeerConnection(final Set<Media> media, final List<PeerConnection.IceServer> iceServers) throws InitializationException {
280 Preconditions.checkState(this.eglBase != null);
281 Preconditions.checkNotNull(media);
282 Preconditions.checkArgument(media.size() > 0, "media can not be empty when initializing peer connection");
283 final boolean setUseHardwareAcousticEchoCanceler = WebRtcAudioEffects.canUseAcousticEchoCanceler() && !HARDWARE_AEC_BLACKLIST.contains(Build.MODEL);
284 Log.d(Config.LOGTAG, String.format("setUseHardwareAcousticEchoCanceler(%s) model=%s", setUseHardwareAcousticEchoCanceler, Build.MODEL));
285 PeerConnectionFactory peerConnectionFactory = PeerConnectionFactory.builder()
286 .setVideoDecoderFactory(new DefaultVideoDecoderFactory(eglBase.getEglBaseContext()))
287 .setVideoEncoderFactory(new DefaultVideoEncoderFactory(eglBase.getEglBaseContext(), true, true))
288 .setAudioDeviceModule(JavaAudioDeviceModule.builder(context)
289 .setUseHardwareAcousticEchoCanceler(setUseHardwareAcousticEchoCanceler)
290 .createAudioDeviceModule()
291 )
292 .createPeerConnectionFactory();
293
294
295 final PeerConnection.RTCConfiguration rtcConfig = buildConfiguration(iceServers);
296 final PeerConnection peerConnection = peerConnectionFactory.createPeerConnection(rtcConfig, peerConnectionObserver);
297 if (peerConnection == null) {
298 throw new InitializationException("Unable to create PeerConnection");
299 }
300
301 final Optional<CapturerChoice> optionalCapturerChoice = media.contains(Media.VIDEO) ? getVideoCapturer() : Optional.absent();
302
303 if (optionalCapturerChoice.isPresent()) {
304 this.capturerChoice = optionalCapturerChoice.get();
305 final CameraVideoCapturer capturer = this.capturerChoice.cameraVideoCapturer;
306 final VideoSource videoSource = peerConnectionFactory.createVideoSource(false);
307 SurfaceTextureHelper surfaceTextureHelper = SurfaceTextureHelper.create("webrtc", eglBase.getEglBaseContext());
308 capturer.initialize(surfaceTextureHelper, requireContext(), videoSource.getCapturerObserver());
309 Log.d(Config.LOGTAG, String.format("start capturing at %dx%d@%d", capturerChoice.captureFormat.width, capturerChoice.captureFormat.height, capturerChoice.getFrameRate()));
310 capturer.startCapture(capturerChoice.captureFormat.width, capturerChoice.captureFormat.height, capturerChoice.getFrameRate());
311
312 this.localVideoTrack = peerConnectionFactory.createVideoTrack("my-video-track", videoSource);
313
314 peerConnection.addTrack(this.localVideoTrack);
315 }
316
317
318 if (media.contains(Media.AUDIO)) {
319 //set up audio track
320 final AudioSource audioSource = peerConnectionFactory.createAudioSource(new MediaConstraints());
321 this.localAudioTrack = peerConnectionFactory.createAudioTrack("my-audio-track", audioSource);
322 peerConnection.addTrack(this.localAudioTrack);
323 }
324 peerConnection.setAudioPlayout(true);
325 peerConnection.setAudioRecording(true);
326 this.peerConnection = peerConnection;
327 }
328
329 private static PeerConnection.RTCConfiguration buildConfiguration(final List<PeerConnection.IceServer> iceServers) {
330 final PeerConnection.RTCConfiguration rtcConfig = new PeerConnection.RTCConfiguration(iceServers);
331 rtcConfig.tcpCandidatePolicy = PeerConnection.TcpCandidatePolicy.DISABLED; //XEP-0176 doesn't support tcp
332 rtcConfig.continualGatheringPolicy = PeerConnection.ContinualGatheringPolicy.GATHER_CONTINUALLY;
333 rtcConfig.sdpSemantics = PeerConnection.SdpSemantics.UNIFIED_PLAN;
334 rtcConfig.rtcpMuxPolicy = PeerConnection.RtcpMuxPolicy.NEGOTIATE;
335 rtcConfig.enableImplicitRollback = true;
336 return rtcConfig;
337 }
338
339 void reconfigurePeerConnection(final List<PeerConnection.IceServer> iceServers) {
340 requirePeerConnection().setConfiguration(buildConfiguration(iceServers));
341 }
342
343 void restartIce() {
344 executorService.execute(() -> requirePeerConnection().restartIce());
345 }
346
347 public void setIsReadyToReceiveIceCandidates(final boolean ready) {
348 readyToReceivedIceCandidates.set(ready);
349 while (ready && iceCandidates.peek() != null) {
350 eventCallback.onIceCandidate(iceCandidates.poll());
351 }
352 }
353
354 synchronized void close() {
355 final PeerConnection peerConnection = this.peerConnection;
356 final CapturerChoice capturerChoice = this.capturerChoice;
357 final AppRTCAudioManager audioManager = this.appRTCAudioManager;
358 final EglBase eglBase = this.eglBase;
359 if (peerConnection != null) {
360 dispose(peerConnection);
361 this.peerConnection = null;
362 }
363 if (audioManager != null) {
364 toneManager.setAppRtcAudioManagerHasControl(false);
365 mainHandler.post(audioManager::stop);
366 }
367 this.localVideoTrack = null;
368 this.remoteVideoTrack = null;
369 if (capturerChoice != null) {
370 try {
371 capturerChoice.cameraVideoCapturer.stopCapture();
372 } catch (InterruptedException e) {
373 Log.e(Config.LOGTAG, "unable to stop capturing");
374 }
375 }
376 if (eglBase != null) {
377 eglBase.release();
378 this.eglBase = null;
379 }
380 }
381
382 synchronized void verifyClosed() {
383 if (this.peerConnection != null
384 || this.eglBase != null
385 || this.localVideoTrack != null
386 || this.remoteVideoTrack != null) {
387 final IllegalStateException e = new IllegalStateException("WebRTCWrapper hasn't been closed properly");
388 Log.e(Config.LOGTAG, "verifyClosed() failed. Going to throw", e);
389 throw e;
390 }
391 }
392
393 boolean isCameraSwitchable() {
394 final CapturerChoice capturerChoice = this.capturerChoice;
395 return capturerChoice != null && capturerChoice.availableCameras.size() > 1;
396 }
397
398 boolean isFrontCamera() {
399 final CapturerChoice capturerChoice = this.capturerChoice;
400 return capturerChoice == null || capturerChoice.isFrontCamera;
401 }
402
403 ListenableFuture<Boolean> switchCamera() {
404 final CapturerChoice capturerChoice = this.capturerChoice;
405 if (capturerChoice == null) {
406 return Futures.immediateFailedFuture(new IllegalStateException("CameraCapturer has not been initialized"));
407 }
408 final SettableFuture<Boolean> future = SettableFuture.create();
409 capturerChoice.cameraVideoCapturer.switchCamera(new CameraVideoCapturer.CameraSwitchHandler() {
410 @Override
411 public void onCameraSwitchDone(boolean isFrontCamera) {
412 capturerChoice.isFrontCamera = isFrontCamera;
413 future.set(isFrontCamera);
414 }
415
416 @Override
417 public void onCameraSwitchError(final String message) {
418 future.setException(new IllegalStateException(String.format("Unable to switch camera %s", message)));
419 }
420 });
421 return future;
422 }
423
424 boolean isMicrophoneEnabled() {
425 final AudioTrack audioTrack = this.localAudioTrack;
426 if (audioTrack == null) {
427 throw new IllegalStateException("Local audio track does not exist (yet)");
428 }
429 try {
430 return audioTrack.enabled();
431 } catch (final IllegalStateException e) {
432 //sometimes UI might still be rendering the buttons when a background thread has already ended the call
433 return false;
434 }
435 }
436
437 boolean setMicrophoneEnabled(final boolean enabled) {
438 final AudioTrack audioTrack = this.localAudioTrack;
439 if (audioTrack == null) {
440 throw new IllegalStateException("Local audio track does not exist (yet)");
441 }
442 try {
443 audioTrack.setEnabled(enabled);
444 return true;
445 } catch (final IllegalStateException e) {
446 Log.d(Config.LOGTAG, "unable to toggle microphone", e);
447 //ignoring race condition in case MediaStreamTrack has been disposed
448 return false;
449 }
450 }
451
452 boolean isVideoEnabled() {
453 final VideoTrack videoTrack = this.localVideoTrack;
454 if (videoTrack == null) {
455 return false;
456 }
457 return videoTrack.enabled();
458 }
459
460 void setVideoEnabled(final boolean enabled) {
461 final VideoTrack videoTrack = this.localVideoTrack;
462 if (videoTrack == null) {
463 throw new IllegalStateException("Local video track does not exist");
464 }
465 videoTrack.setEnabled(enabled);
466 }
467
468 synchronized ListenableFuture<SessionDescription> setLocalDescription() {
469 return Futures.transformAsync(getPeerConnectionFuture(), peerConnection -> {
470 final SettableFuture<SessionDescription> future = SettableFuture.create();
471 peerConnection.setLocalDescription(new SetSdpObserver() {
472 @Override
473 public void onSetSuccess() {
474 final SessionDescription description = peerConnection.getLocalDescription();
475 Log.d(EXTENDED_LOGGING_TAG, "set local description:");
476 logDescription(description);
477 future.set(description);
478 }
479
480 @Override
481 public void onSetFailure(final String message) {
482 future.setException(new FailureToSetDescriptionException(message));
483 }
484 });
485 return future;
486 }, MoreExecutors.directExecutor());
487 }
488
489 private static void logDescription(final SessionDescription sessionDescription) {
490 for (final String line : sessionDescription.description.split(eu.siacs.conversations.xmpp.jingle.SessionDescription.LINE_DIVIDER)) {
491 Log.d(EXTENDED_LOGGING_TAG, line);
492 }
493 }
494
495 synchronized ListenableFuture<Void> setRemoteDescription(final SessionDescription sessionDescription) {
496 Log.d(EXTENDED_LOGGING_TAG, "setting remote description:");
497 logDescription(sessionDescription);
498 return Futures.transformAsync(getPeerConnectionFuture(), peerConnection -> {
499 final SettableFuture<Void> future = SettableFuture.create();
500 peerConnection.setRemoteDescription(new SetSdpObserver() {
501 @Override
502 public void onSetSuccess() {
503 future.set(null);
504 }
505
506 @Override
507 public void onSetFailure(final String message) {
508 future.setException(new FailureToSetDescriptionException(message));
509 }
510 }, sessionDescription);
511 return future;
512 }, MoreExecutors.directExecutor());
513 }
514
515 @Nonnull
516 private ListenableFuture<PeerConnection> getPeerConnectionFuture() {
517 final PeerConnection peerConnection = this.peerConnection;
518 if (peerConnection == null) {
519 return Futures.immediateFailedFuture(new PeerConnectionNotInitialized());
520 } else {
521 return Futures.immediateFuture(peerConnection);
522 }
523 }
524
525 private PeerConnection requirePeerConnection() {
526 final PeerConnection peerConnection = this.peerConnection;
527 if (peerConnection == null) {
528 throw new PeerConnectionNotInitialized();
529 }
530 return peerConnection;
531 }
532
533 //TODO: remove - hack to test dtmfSending
534 public DtmfSender getDtmfSender() {
535 return peerConnection.getSenders().get(0).dtmf();
536 }
537
538 public boolean applyDtmfTone(String tone) {
539 if (toneManager == null || peerConnection.getSenders().isEmpty()) {
540 return false;
541 }
542 peerConnection.getSenders().get(0).dtmf().insertDtmf(tone, TONE_DURATION, 100);
543 toneManager.startTone(TONE_CODES.get(tone), TONE_DURATION);
544 return true;
545 }
546
547 void addIceCandidate(IceCandidate iceCandidate) {
548 requirePeerConnection().addIceCandidate(iceCandidate);
549 }
550
551 private Optional<CapturerChoice> getVideoCapturer() {
552 final CameraEnumerator enumerator = new Camera2Enumerator(requireContext());
553 final Set<String> deviceNames = ImmutableSet.copyOf(enumerator.getDeviceNames());
554 for (final String deviceName : deviceNames) {
555 if (isFrontFacing(enumerator, deviceName)) {
556 final CapturerChoice capturerChoice = of(enumerator, deviceName, deviceNames);
557 if (capturerChoice == null) {
558 return Optional.absent();
559 }
560 capturerChoice.isFrontCamera = true;
561 return Optional.of(capturerChoice);
562 }
563 }
564 if (deviceNames.size() == 0) {
565 return Optional.absent();
566 } else {
567 return Optional.fromNullable(of(enumerator, Iterables.get(deviceNames, 0), deviceNames));
568 }
569 }
570
571 PeerConnection.PeerConnectionState getState() {
572 return requirePeerConnection().connectionState();
573 }
574
575 public PeerConnection.SignalingState getSignalingState() {
576 return requirePeerConnection().signalingState();
577 }
578
579
580 EglBase.Context getEglBaseContext() {
581 return this.eglBase.getEglBaseContext();
582 }
583
584 Optional<VideoTrack> getLocalVideoTrack() {
585 return Optional.fromNullable(this.localVideoTrack);
586 }
587
588 Optional<VideoTrack> getRemoteVideoTrack() {
589 return Optional.fromNullable(this.remoteVideoTrack);
590 }
591
592 private Context requireContext() {
593 final Context context = this.context;
594 if (context == null) {
595 throw new IllegalStateException("call setup first");
596 }
597 return context;
598 }
599
600 AppRTCAudioManager getAudioManager() {
601 return appRTCAudioManager;
602 }
603
604 void execute(final Runnable command) {
605 executorService.execute(command);
606 }
607
608 public interface EventCallback {
609 void onIceCandidate(IceCandidate iceCandidate);
610
611 void onConnectionChange(PeerConnection.PeerConnectionState newState);
612
613 void onAudioDeviceChanged(AppRTCAudioManager.AudioDevice selectedAudioDevice, Set<AppRTCAudioManager.AudioDevice> availableAudioDevices);
614
615 void onRenegotiationNeeded();
616 }
617
618 private static abstract class SetSdpObserver implements SdpObserver {
619
620 @Override
621 public void onCreateSuccess(org.webrtc.SessionDescription sessionDescription) {
622 throw new IllegalStateException("Not able to use SetSdpObserver");
623 }
624
625 @Override
626 public void onCreateFailure(String s) {
627 throw new IllegalStateException("Not able to use SetSdpObserver");
628 }
629
630 }
631
632 private static abstract class CreateSdpObserver implements SdpObserver {
633
634
635 @Override
636 public void onSetSuccess() {
637 throw new IllegalStateException("Not able to use CreateSdpObserver");
638 }
639
640
641 @Override
642 public void onSetFailure(String s) {
643 throw new IllegalStateException("Not able to use CreateSdpObserver");
644 }
645 }
646
647 static class InitializationException extends Exception {
648
649 private InitializationException(final String message, final Throwable throwable) {
650 super(message, throwable);
651 }
652
653 private InitializationException(final String message) {
654 super(message);
655 }
656 }
657
658 public static class PeerConnectionNotInitialized extends IllegalStateException {
659
660 private PeerConnectionNotInitialized() {
661 super("initialize PeerConnection first");
662 }
663
664 }
665
666 private static class FailureToSetDescriptionException extends IllegalArgumentException {
667 public FailureToSetDescriptionException(String message) {
668 super(message);
669 }
670 }
671
672 private static class CapturerChoice {
673 private final CameraVideoCapturer cameraVideoCapturer;
674 private final CameraEnumerationAndroid.CaptureFormat captureFormat;
675 private final Set<String> availableCameras;
676 private boolean isFrontCamera = false;
677
678 CapturerChoice(CameraVideoCapturer cameraVideoCapturer, CameraEnumerationAndroid.CaptureFormat captureFormat, Set<String> cameras) {
679 this.cameraVideoCapturer = cameraVideoCapturer;
680 this.captureFormat = captureFormat;
681 this.availableCameras = cameras;
682 }
683
684 int getFrameRate() {
685 return Math.max(captureFormat.framerate.min, Math.min(CAPTURING_MAX_FRAME_RATE, captureFormat.framerate.max));
686 }
687 }
688}