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