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 = buildConfiguration(iceServers);
271        final PeerConnection peerConnection = peerConnectionFactory.createPeerConnection(rtcConfig, peerConnectionObserver);
272        if (peerConnection == null) {
273            throw new InitializationException("Unable to create PeerConnection");
274        }
275
276        final Optional<CapturerChoice> optionalCapturerChoice = media.contains(Media.VIDEO) ? getVideoCapturer() : Optional.absent();
277
278        if (optionalCapturerChoice.isPresent()) {
279            this.capturerChoice = optionalCapturerChoice.get();
280            final CameraVideoCapturer capturer = this.capturerChoice.cameraVideoCapturer;
281            final VideoSource videoSource = peerConnectionFactory.createVideoSource(false);
282            SurfaceTextureHelper surfaceTextureHelper = SurfaceTextureHelper.create("webrtc", eglBase.getEglBaseContext());
283            capturer.initialize(surfaceTextureHelper, requireContext(), videoSource.getCapturerObserver());
284            Log.d(Config.LOGTAG, String.format("start capturing at %dx%d@%d", capturerChoice.captureFormat.width, capturerChoice.captureFormat.height, capturerChoice.getFrameRate()));
285            capturer.startCapture(capturerChoice.captureFormat.width, capturerChoice.captureFormat.height, capturerChoice.getFrameRate());
286
287            this.localVideoTrack = peerConnectionFactory.createVideoTrack("my-video-track", videoSource);
288
289            peerConnection.addTrack(this.localVideoTrack);
290        }
291
292
293        if (media.contains(Media.AUDIO)) {
294            //set up audio track
295            final AudioSource audioSource = peerConnectionFactory.createAudioSource(new MediaConstraints());
296            this.localAudioTrack = peerConnectionFactory.createAudioTrack("my-audio-track", audioSource);
297            peerConnection.addTrack(this.localAudioTrack);
298        }
299        peerConnection.setAudioPlayout(true);
300        peerConnection.setAudioRecording(true);
301        this.peerConnection = peerConnection;
302    }
303
304    private static PeerConnection.RTCConfiguration buildConfiguration(final List<PeerConnection.IceServer> iceServers) {
305        final PeerConnection.RTCConfiguration rtcConfig = new PeerConnection.RTCConfiguration(iceServers);
306        rtcConfig.tcpCandidatePolicy = PeerConnection.TcpCandidatePolicy.DISABLED; //XEP-0176 doesn't support tcp
307        rtcConfig.continualGatheringPolicy = PeerConnection.ContinualGatheringPolicy.GATHER_CONTINUALLY;
308        rtcConfig.sdpSemantics = PeerConnection.SdpSemantics.UNIFIED_PLAN;
309        rtcConfig.rtcpMuxPolicy = PeerConnection.RtcpMuxPolicy.NEGOTIATE;
310        rtcConfig.enableImplicitRollback = true;
311        return rtcConfig;
312    }
313
314    void reconfigurePeerConnection(final List<PeerConnection.IceServer> iceServers) {
315        requirePeerConnection().setConfiguration(buildConfiguration(iceServers));
316    }
317
318    void restartIce() {
319        executorService.execute(() -> requirePeerConnection().restartIce());
320    }
321
322    public void setIsReadyToReceiveIceCandidates(final boolean ready) {
323        readyToReceivedIceCandidates.set(ready);
324        while (ready && iceCandidates.peek() != null) {
325            eventCallback.onIceCandidate(iceCandidates.poll());
326        }
327    }
328
329    synchronized void close() {
330        final PeerConnection peerConnection = this.peerConnection;
331        final CapturerChoice capturerChoice = this.capturerChoice;
332        final AppRTCAudioManager audioManager = this.appRTCAudioManager;
333        final EglBase eglBase = this.eglBase;
334        if (peerConnection != null) {
335            dispose(peerConnection);
336            this.peerConnection = null;
337        }
338        if (audioManager != null) {
339            toneManager.setAppRtcAudioManagerHasControl(false);
340            mainHandler.post(audioManager::stop);
341        }
342        this.localVideoTrack = null;
343        this.remoteVideoTrack = null;
344        if (capturerChoice != null) {
345            try {
346                capturerChoice.cameraVideoCapturer.stopCapture();
347            } catch (InterruptedException e) {
348                Log.e(Config.LOGTAG, "unable to stop capturing");
349            }
350        }
351        if (eglBase != null) {
352            eglBase.release();
353            this.eglBase = null;
354        }
355    }
356
357    synchronized void verifyClosed() {
358        if (this.peerConnection != null
359                || this.eglBase != null
360                || this.localVideoTrack != null
361                || this.remoteVideoTrack != null) {
362            final IllegalStateException e = new IllegalStateException("WebRTCWrapper hasn't been closed properly");
363            Log.e(Config.LOGTAG, "verifyClosed() failed. Going to throw", e);
364            throw e;
365        }
366    }
367
368    boolean isCameraSwitchable() {
369        final CapturerChoice capturerChoice = this.capturerChoice;
370        return capturerChoice != null && capturerChoice.availableCameras.size() > 1;
371    }
372
373    boolean isFrontCamera() {
374        final CapturerChoice capturerChoice = this.capturerChoice;
375        return capturerChoice == null || capturerChoice.isFrontCamera;
376    }
377
378    ListenableFuture<Boolean> switchCamera() {
379        final CapturerChoice capturerChoice = this.capturerChoice;
380        if (capturerChoice == null) {
381            return Futures.immediateFailedFuture(new IllegalStateException("CameraCapturer has not been initialized"));
382        }
383        final SettableFuture<Boolean> future = SettableFuture.create();
384        capturerChoice.cameraVideoCapturer.switchCamera(new CameraVideoCapturer.CameraSwitchHandler() {
385            @Override
386            public void onCameraSwitchDone(boolean isFrontCamera) {
387                capturerChoice.isFrontCamera = isFrontCamera;
388                future.set(isFrontCamera);
389            }
390
391            @Override
392            public void onCameraSwitchError(final String message) {
393                future.setException(new IllegalStateException(String.format("Unable to switch camera %s", message)));
394            }
395        });
396        return future;
397    }
398
399    boolean isMicrophoneEnabled() {
400        final AudioTrack audioTrack = this.localAudioTrack;
401        if (audioTrack == null) {
402            throw new IllegalStateException("Local audio track does not exist (yet)");
403        }
404        try {
405            return audioTrack.enabled();
406        } catch (final IllegalStateException e) {
407            //sometimes UI might still be rendering the buttons when a background thread has already ended the call
408            return false;
409        }
410    }
411
412    boolean setMicrophoneEnabled(final boolean enabled) {
413        final AudioTrack audioTrack = this.localAudioTrack;
414        if (audioTrack == null) {
415            throw new IllegalStateException("Local audio track does not exist (yet)");
416        }
417        try {
418            audioTrack.setEnabled(enabled);
419            return true;
420        } catch (final IllegalStateException e) {
421            Log.d(Config.LOGTAG, "unable to toggle microphone", e);
422            //ignoring race condition in case MediaStreamTrack has been disposed
423            return false;
424        }
425    }
426
427    boolean isVideoEnabled() {
428        final VideoTrack videoTrack = this.localVideoTrack;
429        if (videoTrack == null) {
430            return false;
431        }
432        return videoTrack.enabled();
433    }
434
435    void setVideoEnabled(final boolean enabled) {
436        final VideoTrack videoTrack = this.localVideoTrack;
437        if (videoTrack == null) {
438            throw new IllegalStateException("Local video track does not exist");
439        }
440        videoTrack.setEnabled(enabled);
441    }
442
443    synchronized ListenableFuture<SessionDescription> setLocalDescription() {
444        return Futures.transformAsync(getPeerConnectionFuture(), peerConnection -> {
445            final SettableFuture<SessionDescription> future = SettableFuture.create();
446            peerConnection.setLocalDescription(new SetSdpObserver() {
447                @Override
448                public void onSetSuccess() {
449                    final SessionDescription description = peerConnection.getLocalDescription();
450                    Log.d(EXTENDED_LOGGING_TAG, "set local description:");
451                    logDescription(description);
452                    future.set(description);
453                }
454
455                @Override
456                public void onSetFailure(final String message) {
457                    future.setException(new FailureToSetDescriptionException(message));
458                }
459            });
460            return future;
461        }, MoreExecutors.directExecutor());
462    }
463
464    private static void logDescription(final SessionDescription sessionDescription) {
465        for (final String line : sessionDescription.description.split(eu.siacs.conversations.xmpp.jingle.SessionDescription.LINE_DIVIDER)) {
466            Log.d(EXTENDED_LOGGING_TAG, line);
467        }
468    }
469
470    synchronized ListenableFuture<Void> setRemoteDescription(final SessionDescription sessionDescription) {
471        Log.d(EXTENDED_LOGGING_TAG, "setting remote description:");
472        logDescription(sessionDescription);
473        return Futures.transformAsync(getPeerConnectionFuture(), peerConnection -> {
474            final SettableFuture<Void> future = SettableFuture.create();
475            peerConnection.setRemoteDescription(new SetSdpObserver() {
476                @Override
477                public void onSetSuccess() {
478                    future.set(null);
479                }
480
481                @Override
482                public void onSetFailure(final String message) {
483                    future.setException(new FailureToSetDescriptionException(message));
484                }
485            }, sessionDescription);
486            return future;
487        }, MoreExecutors.directExecutor());
488    }
489
490    @Nonnull
491    private ListenableFuture<PeerConnection> getPeerConnectionFuture() {
492        final PeerConnection peerConnection = this.peerConnection;
493        if (peerConnection == null) {
494            return Futures.immediateFailedFuture(new PeerConnectionNotInitialized());
495        } else {
496            return Futures.immediateFuture(peerConnection);
497        }
498    }
499
500    private PeerConnection requirePeerConnection() {
501        final PeerConnection peerConnection = this.peerConnection;
502        if (peerConnection == null) {
503            throw new PeerConnectionNotInitialized();
504        }
505        return peerConnection;
506    }
507
508    void addIceCandidate(IceCandidate iceCandidate) {
509        requirePeerConnection().addIceCandidate(iceCandidate);
510    }
511
512    private Optional<CapturerChoice> getVideoCapturer() {
513        final CameraEnumerator enumerator = new Camera2Enumerator(requireContext());
514        final Set<String> deviceNames = ImmutableSet.copyOf(enumerator.getDeviceNames());
515        for (final String deviceName : deviceNames) {
516            if (isFrontFacing(enumerator, deviceName)) {
517                final CapturerChoice capturerChoice = of(enumerator, deviceName, deviceNames);
518                if (capturerChoice == null) {
519                    return Optional.absent();
520                }
521                capturerChoice.isFrontCamera = true;
522                return Optional.of(capturerChoice);
523            }
524        }
525        if (deviceNames.size() == 0) {
526            return Optional.absent();
527        } else {
528            return Optional.fromNullable(of(enumerator, Iterables.get(deviceNames, 0), deviceNames));
529        }
530    }
531
532    PeerConnection.PeerConnectionState getState() {
533        return requirePeerConnection().connectionState();
534    }
535
536    public PeerConnection.SignalingState getSignalingState() {
537        return requirePeerConnection().signalingState();
538    }
539
540
541    EglBase.Context getEglBaseContext() {
542        return this.eglBase.getEglBaseContext();
543    }
544
545    Optional<VideoTrack> getLocalVideoTrack() {
546        return Optional.fromNullable(this.localVideoTrack);
547    }
548
549    Optional<VideoTrack> getRemoteVideoTrack() {
550        return Optional.fromNullable(this.remoteVideoTrack);
551    }
552
553    private Context requireContext() {
554        final Context context = this.context;
555        if (context == null) {
556            throw new IllegalStateException("call setup first");
557        }
558        return context;
559    }
560
561    AppRTCAudioManager getAudioManager() {
562        return appRTCAudioManager;
563    }
564
565    void execute(final Runnable command) {
566        executorService.execute(command);
567    }
568
569    public interface EventCallback {
570        void onIceCandidate(IceCandidate iceCandidate);
571
572        void onConnectionChange(PeerConnection.PeerConnectionState newState);
573
574        void onAudioDeviceChanged(AppRTCAudioManager.AudioDevice selectedAudioDevice, Set<AppRTCAudioManager.AudioDevice> availableAudioDevices);
575
576        void onRenegotiationNeeded();
577    }
578
579    private static abstract class SetSdpObserver implements SdpObserver {
580
581        @Override
582        public void onCreateSuccess(org.webrtc.SessionDescription sessionDescription) {
583            throw new IllegalStateException("Not able to use SetSdpObserver");
584        }
585
586        @Override
587        public void onCreateFailure(String s) {
588            throw new IllegalStateException("Not able to use SetSdpObserver");
589        }
590
591    }
592
593    private static abstract class CreateSdpObserver implements SdpObserver {
594
595
596        @Override
597        public void onSetSuccess() {
598            throw new IllegalStateException("Not able to use CreateSdpObserver");
599        }
600
601
602        @Override
603        public void onSetFailure(String s) {
604            throw new IllegalStateException("Not able to use CreateSdpObserver");
605        }
606    }
607
608    static class InitializationException extends Exception {
609
610        private InitializationException(final String message, final Throwable throwable) {
611            super(message, throwable);
612        }
613
614        private InitializationException(final String message) {
615            super(message);
616        }
617    }
618
619    public static class PeerConnectionNotInitialized extends IllegalStateException {
620
621        private PeerConnectionNotInitialized() {
622            super("initialize PeerConnection first");
623        }
624
625    }
626
627    private static class FailureToSetDescriptionException extends IllegalArgumentException {
628        public FailureToSetDescriptionException(String message) {
629            super(message);
630        }
631    }
632
633    private static class CapturerChoice {
634        private final CameraVideoCapturer cameraVideoCapturer;
635        private final CameraEnumerationAndroid.CaptureFormat captureFormat;
636        private final Set<String> availableCameras;
637        private boolean isFrontCamera = false;
638
639        CapturerChoice(CameraVideoCapturer cameraVideoCapturer, CameraEnumerationAndroid.CaptureFormat captureFormat, Set<String> cameras) {
640            this.cameraVideoCapturer = cameraVideoCapturer;
641            this.captureFormat = captureFormat;
642            this.availableCameras = cameras;
643        }
644
645        int getFrameRate() {
646            return Math.max(captureFormat.framerate.min, Math.min(CAPTURING_MAX_FRAME_RATE, captureFormat.framerate.max));
647        }
648    }
649}