WebRTCWrapper.java

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