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