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