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.LinkedList;
49import java.util.List;
50import java.util.Queue;
51import java.util.Set;
52import java.util.concurrent.ExecutorService;
53import java.util.concurrent.Executors;
54import java.util.concurrent.atomic.AtomicBoolean;
55
56import javax.annotation.Nonnull;
57import javax.annotation.Nullable;
58
59import eu.siacs.conversations.Config;
60import eu.siacs.conversations.services.AppRTCAudioManager;
61import eu.siacs.conversations.services.XmppConnectionService;
62
63public class WebRTCWrapper {
64
65 private static final String EXTENDED_LOGGING_TAG = WebRTCWrapper.class.getSimpleName();
66
67 private final ExecutorService executorService = Executors.newSingleThreadExecutor();
68
69 //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
70 private static final Set<String> HARDWARE_AEC_BLACKLIST = new ImmutableSet.Builder<String>()
71 .add("Pixel")
72 .add("Pixel XL")
73 .add("Moto G5")
74 .add("Moto G (5S) Plus")
75 .add("Moto G4")
76 .add("TA-1053")
77 .add("Mi A1")
78 .add("Mi A2")
79 .add("E5823") // Sony z5 compact
80 .add("Redmi Note 5")
81 .add("FP2") // Fairphone FP2
82 .add("MI 5")
83 .build();
84
85 private static final int CAPTURING_RESOLUTION = 1920;
86 private static final int CAPTURING_MAX_FRAME_RATE = 30;
87
88 private final EventCallback eventCallback;
89 private final AtomicBoolean readyToReceivedIceCandidates = 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 Log.d(EXTENDED_LOGGING_TAG, "onRenegotiationNeeded()");
167 final PeerConnection.PeerConnectionState currentState = peerConnection == null ? null : peerConnection.connectionState();
168 if (currentState != null && 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(EXTENDED_LOGGING_TAG, "onAddTrack(kind=" + (track == null ? "null" : track.kind()) + ",numMediaStreams=" + mediaStreams.length + ")");
177 if (track instanceof VideoTrack) {
178 remoteVideoTrack = (VideoTrack) track;
179 }
180 }
181
182 @Override
183 public void onTrack(RtpTransceiver transceiver) {
184 Log.d(EXTENDED_LOGGING_TAG, "onTrack(mid=" + transceiver.getMid() + ",media=" + transceiver.getMediaType() + ")");
185 }
186 };
187 @Nullable
188 private PeerConnection peerConnection = null;
189 private AudioTrack localAudioTrack = null;
190 private AppRTCAudioManager appRTCAudioManager = null;
191 private ToneManager toneManager = null;
192 private Context context = null;
193 private EglBase eglBase = null;
194 private CapturerChoice capturerChoice;
195
196 WebRTCWrapper(final EventCallback eventCallback) {
197 this.eventCallback = eventCallback;
198 }
199
200 private static void dispose(final PeerConnection peerConnection) {
201 try {
202 peerConnection.dispose();
203 } catch (final IllegalStateException e) {
204 Log.e(Config.LOGTAG, "unable to dispose of peer connection", e);
205 }
206 }
207
208 @Nullable
209 private static CapturerChoice of(CameraEnumerator enumerator, final String deviceName, Set<String> availableCameras) {
210 final CameraVideoCapturer capturer = enumerator.createCapturer(deviceName, null);
211 if (capturer == null) {
212 return null;
213 }
214 final ArrayList<CameraEnumerationAndroid.CaptureFormat> choices = new ArrayList<>(enumerator.getSupportedFormats(deviceName));
215 Collections.sort(choices, (a, b) -> b.width - a.width);
216 for (final CameraEnumerationAndroid.CaptureFormat captureFormat : choices) {
217 if (captureFormat.width <= CAPTURING_RESOLUTION) {
218 return new CapturerChoice(capturer, captureFormat, availableCameras);
219 }
220 }
221 return null;
222 }
223
224 private static boolean isFrontFacing(final CameraEnumerator cameraEnumerator, final String deviceName) {
225 try {
226 return cameraEnumerator.isFrontFacing(deviceName);
227 } catch (final NullPointerException e) {
228 return false;
229 }
230 }
231
232 public void setup(final XmppConnectionService service, final AppRTCAudioManager.SpeakerPhonePreference speakerPhonePreference) throws InitializationException {
233 try {
234 PeerConnectionFactory.initialize(
235 PeerConnectionFactory.InitializationOptions.builder(service).createInitializationOptions()
236 );
237 } catch (final UnsatisfiedLinkError e) {
238 throw new InitializationException("Unable to initialize PeerConnectionFactory", e);
239 }
240 try {
241 this.eglBase = EglBase.create();
242 } catch (final RuntimeException e) {
243 throw new InitializationException("Unable to create EGL base", e);
244 }
245 this.context = service;
246 this.toneManager = service.getJingleConnectionManager().toneManager;
247 mainHandler.post(() -> {
248 appRTCAudioManager = AppRTCAudioManager.create(service, speakerPhonePreference);
249 toneManager.setAppRtcAudioManagerHasControl(true);
250 appRTCAudioManager.start(audioManagerEvents);
251 eventCallback.onAudioDeviceChanged(appRTCAudioManager.getSelectedAudioDevice(), appRTCAudioManager.getAudioDevices());
252 });
253 }
254
255 synchronized void initializePeerConnection(final Set<Media> media, final List<PeerConnection.IceServer> iceServers) throws InitializationException {
256 Preconditions.checkState(this.eglBase != null);
257 Preconditions.checkNotNull(media);
258 Preconditions.checkArgument(media.size() > 0, "media can not be empty when initializing peer connection");
259 final boolean setUseHardwareAcousticEchoCanceler = WebRtcAudioEffects.canUseAcousticEchoCanceler() && !HARDWARE_AEC_BLACKLIST.contains(Build.MODEL);
260 Log.d(Config.LOGTAG, String.format("setUseHardwareAcousticEchoCanceler(%s) model=%s", setUseHardwareAcousticEchoCanceler, Build.MODEL));
261 PeerConnectionFactory peerConnectionFactory = PeerConnectionFactory.builder()
262 .setVideoDecoderFactory(new DefaultVideoDecoderFactory(eglBase.getEglBaseContext()))
263 .setVideoEncoderFactory(new DefaultVideoEncoderFactory(eglBase.getEglBaseContext(), true, true))
264 .setAudioDeviceModule(JavaAudioDeviceModule.builder(context)
265 .setUseHardwareAcousticEchoCanceler(setUseHardwareAcousticEchoCanceler)
266 .createAudioDeviceModule()
267 )
268 .createPeerConnectionFactory();
269
270
271 final PeerConnection.RTCConfiguration rtcConfig = new PeerConnection.RTCConfiguration(iceServers);
272 rtcConfig.tcpCandidatePolicy = PeerConnection.TcpCandidatePolicy.DISABLED; //XEP-0176 doesn't support tcp
273 rtcConfig.continualGatheringPolicy = PeerConnection.ContinualGatheringPolicy.GATHER_CONTINUALLY;
274 rtcConfig.sdpSemantics = PeerConnection.SdpSemantics.UNIFIED_PLAN;
275 rtcConfig.rtcpMuxPolicy = PeerConnection.RtcpMuxPolicy.NEGOTIATE;
276 final PeerConnection peerConnection = peerConnectionFactory.createPeerConnection(rtcConfig, peerConnectionObserver);
277 if (peerConnection == null) {
278 throw new InitializationException("Unable to create PeerConnection");
279 }
280
281 final Optional<CapturerChoice> optionalCapturerChoice = media.contains(Media.VIDEO) ? getVideoCapturer() : Optional.absent();
282
283 if (optionalCapturerChoice.isPresent()) {
284 this.capturerChoice = optionalCapturerChoice.get();
285 final CameraVideoCapturer capturer = this.capturerChoice.cameraVideoCapturer;
286 final VideoSource videoSource = peerConnectionFactory.createVideoSource(false);
287 SurfaceTextureHelper surfaceTextureHelper = SurfaceTextureHelper.create("webrtc", eglBase.getEglBaseContext());
288 capturer.initialize(surfaceTextureHelper, requireContext(), videoSource.getCapturerObserver());
289 Log.d(Config.LOGTAG, String.format("start capturing at %dx%d@%d", capturerChoice.captureFormat.width, capturerChoice.captureFormat.height, capturerChoice.getFrameRate()));
290 capturer.startCapture(capturerChoice.captureFormat.width, capturerChoice.captureFormat.height, capturerChoice.getFrameRate());
291
292 this.localVideoTrack = peerConnectionFactory.createVideoTrack("my-video-track", videoSource);
293
294 peerConnection.addTrack(this.localVideoTrack);
295 }
296
297
298 if (media.contains(Media.AUDIO)) {
299 //set up audio track
300 final AudioSource audioSource = peerConnectionFactory.createAudioSource(new MediaConstraints());
301 this.localAudioTrack = peerConnectionFactory.createAudioTrack("my-audio-track", audioSource);
302 peerConnection.addTrack(this.localAudioTrack);
303 }
304 peerConnection.setAudioPlayout(true);
305 peerConnection.setAudioRecording(true);
306 this.peerConnection = peerConnection;
307 }
308
309 void restartIce() {
310 executorService.execute(()-> requirePeerConnection().restartIce());
311 }
312
313 public void setIsReadyToReceiveIceCandidates(final boolean ready) {
314 readyToReceivedIceCandidates.set(ready);
315 while(ready && iceCandidates.peek() != null) {
316 eventCallback.onIceCandidate(iceCandidates.poll());
317 }
318 }
319
320 synchronized void close() {
321 final PeerConnection peerConnection = this.peerConnection;
322 final CapturerChoice capturerChoice = this.capturerChoice;
323 final AppRTCAudioManager audioManager = this.appRTCAudioManager;
324 final EglBase eglBase = this.eglBase;
325 if (peerConnection != null) {
326 dispose(peerConnection);
327 this.peerConnection = null;
328 }
329 if (audioManager != null) {
330 toneManager.setAppRtcAudioManagerHasControl(false);
331 mainHandler.post(audioManager::stop);
332 }
333 this.localVideoTrack = null;
334 this.remoteVideoTrack = null;
335 if (capturerChoice != null) {
336 try {
337 capturerChoice.cameraVideoCapturer.stopCapture();
338 } catch (InterruptedException e) {
339 Log.e(Config.LOGTAG, "unable to stop capturing");
340 }
341 }
342 if (eglBase != null) {
343 eglBase.release();
344 this.eglBase = null;
345 }
346 }
347
348 synchronized void verifyClosed() {
349 if (this.peerConnection != null
350 || this.eglBase != null
351 || this.localVideoTrack != null
352 || this.remoteVideoTrack != null) {
353 final IllegalStateException e = new IllegalStateException("WebRTCWrapper hasn't been closed properly");
354 Log.e(Config.LOGTAG, "verifyClosed() failed. Going to throw", e);
355 throw e;
356 }
357 }
358
359 boolean isCameraSwitchable() {
360 final CapturerChoice capturerChoice = this.capturerChoice;
361 return capturerChoice != null && capturerChoice.availableCameras.size() > 1;
362 }
363
364 boolean isFrontCamera() {
365 final CapturerChoice capturerChoice = this.capturerChoice;
366 return capturerChoice == null || capturerChoice.isFrontCamera;
367 }
368
369 ListenableFuture<Boolean> switchCamera() {
370 final CapturerChoice capturerChoice = this.capturerChoice;
371 if (capturerChoice == null) {
372 return Futures.immediateFailedFuture(new IllegalStateException("CameraCapturer has not been initialized"));
373 }
374 final SettableFuture<Boolean> future = SettableFuture.create();
375 capturerChoice.cameraVideoCapturer.switchCamera(new CameraVideoCapturer.CameraSwitchHandler() {
376 @Override
377 public void onCameraSwitchDone(boolean isFrontCamera) {
378 capturerChoice.isFrontCamera = isFrontCamera;
379 future.set(isFrontCamera);
380 }
381
382 @Override
383 public void onCameraSwitchError(final String message) {
384 future.setException(new IllegalStateException(String.format("Unable to switch camera %s", message)));
385 }
386 });
387 return future;
388 }
389
390 boolean isMicrophoneEnabled() {
391 final AudioTrack audioTrack = this.localAudioTrack;
392 if (audioTrack == null) {
393 throw new IllegalStateException("Local audio track does not exist (yet)");
394 }
395 try {
396 return audioTrack.enabled();
397 } catch (final IllegalStateException e) {
398 //sometimes UI might still be rendering the buttons when a background thread has already ended the call
399 return false;
400 }
401 }
402
403 boolean setMicrophoneEnabled(final boolean enabled) {
404 final AudioTrack audioTrack = this.localAudioTrack;
405 if (audioTrack == null) {
406 throw new IllegalStateException("Local audio track does not exist (yet)");
407 }
408 try {
409 audioTrack.setEnabled(enabled);
410 return true;
411 } catch (final IllegalStateException e) {
412 Log.d(Config.LOGTAG, "unable to toggle microphone", e);
413 //ignoring race condition in case MediaStreamTrack has been disposed
414 return false;
415 }
416 }
417
418 boolean isVideoEnabled() {
419 final VideoTrack videoTrack = this.localVideoTrack;
420 if (videoTrack == null) {
421 return false;
422 }
423 return videoTrack.enabled();
424 }
425
426 void setVideoEnabled(final boolean enabled) {
427 final VideoTrack videoTrack = this.localVideoTrack;
428 if (videoTrack == null) {
429 throw new IllegalStateException("Local video track does not exist");
430 }
431 videoTrack.setEnabled(enabled);
432 }
433
434 ListenableFuture<SessionDescription> setLocalDescription() {
435 return Futures.transformAsync(getPeerConnectionFuture(), peerConnection -> {
436 final SettableFuture<SessionDescription> future = SettableFuture.create();
437 peerConnection.setLocalDescription(new SetSdpObserver() {
438 @Override
439 public void onSetSuccess() {
440 final SessionDescription description = peerConnection.getLocalDescription();
441 Log.d(EXTENDED_LOGGING_TAG, "set local description:");
442 logDescription(description);
443 future.set(description);
444 }
445
446 @Override
447 public void onSetFailure(final String message) {
448 future.setException(new FailureToSetDescriptionException(message));
449 }
450 });
451 return future;
452 }, MoreExecutors.directExecutor());
453 }
454
455 private static void logDescription(final SessionDescription sessionDescription) {
456 for (final String line : sessionDescription.description.split(eu.siacs.conversations.xmpp.jingle.SessionDescription.LINE_DIVIDER)) {
457 Log.d(EXTENDED_LOGGING_TAG, line);
458 }
459 }
460
461 ListenableFuture<Void> setRemoteDescription(final SessionDescription sessionDescription) {
462 Log.d(EXTENDED_LOGGING_TAG, "setting remote description:");
463 logDescription(sessionDescription);
464 return Futures.transformAsync(getPeerConnectionFuture(), peerConnection -> {
465 final SettableFuture<Void> future = SettableFuture.create();
466 peerConnection.setRemoteDescription(new SetSdpObserver() {
467 @Override
468 public void onSetSuccess() {
469 future.set(null);
470 }
471
472 @Override
473 public void onSetFailure(final String message) {
474 future.setException(new FailureToSetDescriptionException(message));
475 }
476 }, sessionDescription);
477 return future;
478 }, MoreExecutors.directExecutor());
479 }
480
481 @Nonnull
482 private ListenableFuture<PeerConnection> getPeerConnectionFuture() {
483 final PeerConnection peerConnection = this.peerConnection;
484 if (peerConnection == null) {
485 return Futures.immediateFailedFuture(new IllegalStateException("initialize PeerConnection first"));
486 } else {
487 return Futures.immediateFuture(peerConnection);
488 }
489 }
490
491 void addIceCandidate(IceCandidate iceCandidate) {
492 requirePeerConnection().addIceCandidate(iceCandidate);
493 }
494
495 private Optional<CapturerChoice> getVideoCapturer() {
496 final CameraEnumerator enumerator = new Camera2Enumerator(requireContext());
497 final Set<String> deviceNames = ImmutableSet.copyOf(enumerator.getDeviceNames());
498 for (final String deviceName : deviceNames) {
499 if (isFrontFacing(enumerator, deviceName)) {
500 final CapturerChoice capturerChoice = of(enumerator, deviceName, deviceNames);
501 if (capturerChoice == null) {
502 return Optional.absent();
503 }
504 capturerChoice.isFrontCamera = true;
505 return Optional.of(capturerChoice);
506 }
507 }
508 if (deviceNames.size() == 0) {
509 return Optional.absent();
510 } else {
511 return Optional.fromNullable(of(enumerator, Iterables.get(deviceNames, 0), deviceNames));
512 }
513 }
514
515 public PeerConnection.PeerConnectionState getState() {
516 return requirePeerConnection().connectionState();
517 }
518
519 EglBase.Context getEglBaseContext() {
520 return this.eglBase.getEglBaseContext();
521 }
522
523 Optional<VideoTrack> getLocalVideoTrack() {
524 return Optional.fromNullable(this.localVideoTrack);
525 }
526
527 Optional<VideoTrack> getRemoteVideoTrack() {
528 return Optional.fromNullable(this.remoteVideoTrack);
529 }
530
531 private PeerConnection requirePeerConnection() {
532 final PeerConnection peerConnection = this.peerConnection;
533 if (peerConnection == null) {
534 throw new PeerConnectionNotInitialized();
535 }
536 return peerConnection;
537 }
538
539 private Context requireContext() {
540 final Context context = this.context;
541 if (context == null) {
542 throw new IllegalStateException("call setup first");
543 }
544 return context;
545 }
546
547 AppRTCAudioManager getAudioManager() {
548 return appRTCAudioManager;
549 }
550
551 void execute(final Runnable command) {
552 executorService.execute(command);
553 }
554
555 public interface EventCallback {
556 void onIceCandidate(IceCandidate iceCandidate);
557
558 void onConnectionChange(PeerConnection.PeerConnectionState newState);
559
560 void onAudioDeviceChanged(AppRTCAudioManager.AudioDevice selectedAudioDevice, Set<AppRTCAudioManager.AudioDevice> availableAudioDevices);
561
562 void onRenegotiationNeeded();
563 }
564
565 private static abstract class SetSdpObserver implements SdpObserver {
566
567 @Override
568 public void onCreateSuccess(org.webrtc.SessionDescription sessionDescription) {
569 throw new IllegalStateException("Not able to use SetSdpObserver");
570 }
571
572 @Override
573 public void onCreateFailure(String s) {
574 throw new IllegalStateException("Not able to use SetSdpObserver");
575 }
576
577 }
578
579 private static abstract class CreateSdpObserver implements SdpObserver {
580
581
582 @Override
583 public void onSetSuccess() {
584 throw new IllegalStateException("Not able to use CreateSdpObserver");
585 }
586
587
588 @Override
589 public void onSetFailure(String s) {
590 throw new IllegalStateException("Not able to use CreateSdpObserver");
591 }
592 }
593
594 static class InitializationException extends Exception {
595
596 private InitializationException(final String message, final Throwable throwable) {
597 super(message, throwable);
598 }
599
600 private InitializationException(final String message) {
601 super(message);
602 }
603 }
604
605 public static class PeerConnectionNotInitialized extends IllegalStateException {
606
607 private PeerConnectionNotInitialized() {
608 super("initialize PeerConnection first");
609 }
610
611 }
612
613 private static class FailureToSetDescriptionException extends IllegalArgumentException {
614 public FailureToSetDescriptionException(String message) {
615 super(message);
616 }
617 }
618
619 private static class CapturerChoice {
620 private final CameraVideoCapturer cameraVideoCapturer;
621 private final CameraEnumerationAndroid.CaptureFormat captureFormat;
622 private final Set<String> availableCameras;
623 private boolean isFrontCamera = false;
624
625 CapturerChoice(CameraVideoCapturer cameraVideoCapturer, CameraEnumerationAndroid.CaptureFormat captureFormat, Set<String> cameras) {
626 this.cameraVideoCapturer = cameraVideoCapturer;
627 this.captureFormat = captureFormat;
628 this.availableCameras = cameras;
629 }
630
631 int getFrameRate() {
632 return Math.max(captureFormat.framerate.min, Math.min(CAPTURING_MAX_FRAME_RATE, captureFormat.framerate.max));
633 }
634 }
635}