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