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