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