revert webrtc for f-droid
This commit is contained in:
parent
6d8e70ead1
commit
664007a28c
1 changed files with 87 additions and 86 deletions
|
@ -17,7 +17,6 @@ import com.google.common.util.concurrent.SettableFuture;
|
|||
|
||||
import org.webrtc.AudioSource;
|
||||
import org.webrtc.AudioTrack;
|
||||
import org.webrtc.Camera1Enumerator;
|
||||
import org.webrtc.Camera2Enumerator;
|
||||
import org.webrtc.CameraEnumerationAndroid;
|
||||
import org.webrtc.CameraEnumerator;
|
||||
|
@ -45,8 +44,13 @@ import org.webrtc.voiceengine.WebRtcAudioEffects;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
@ -59,7 +63,8 @@ public class WebRTCWrapper {
|
|||
|
||||
private static final String EXTENDED_LOGGING_TAG = WebRTCWrapper.class.getSimpleName();
|
||||
|
||||
//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
|
||||
private final ExecutorService executorService = Executors.newSingleThreadExecutor();
|
||||
|
||||
private static final Set<String> HARDWARE_AEC_BLACKLIST = new ImmutableSet.Builder<String>()
|
||||
.add("Pixel")
|
||||
.add("Pixel XL")
|
||||
|
@ -73,12 +78,17 @@ public class WebRTCWrapper {
|
|||
.add("Redmi Note 5")
|
||||
.add("FP2") // Fairphone FP2
|
||||
.add("MI 5")
|
||||
.add("GT-I9515") // Samsung Galaxy S4 Value Edition (jfvelte)
|
||||
.add("GT-I9515L") // Samsung Galaxy S4 Value Edition (jfvelte)
|
||||
.add("GT-I9505") // Samsung Galaxy S4 (jfltexx)
|
||||
.build();
|
||||
|
||||
private static final int CAPTURING_RESOLUTION = 1920;
|
||||
private static final int CAPTURING_MAX_FRAME_RATE = 30;
|
||||
|
||||
private final EventCallback eventCallback;
|
||||
private final AtomicBoolean readyToReceivedIceCandidates = new AtomicBoolean(false);
|
||||
private final Queue<IceCandidate> iceCandidates = new LinkedList<>();
|
||||
private final AppRTCAudioManager.AudioManagerEvents audioManagerEvents = new AppRTCAudioManager.AudioManagerEvents() {
|
||||
@Override
|
||||
public void onAudioDeviceChanged(AppRTCAudioManager.AudioDevice selectedAudioDevice, Set<AppRTCAudioManager.AudioDevice> availableAudioDevices) {
|
||||
|
@ -98,13 +108,13 @@ public class WebRTCWrapper {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onConnectionChange(PeerConnection.PeerConnectionState newState) {
|
||||
public void onConnectionChange(final PeerConnection.PeerConnectionState newState) {
|
||||
eventCallback.onConnectionChange(newState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onIceConnectionChange(PeerConnection.IceConnectionState iceConnectionState) {
|
||||
|
||||
Log.d(EXTENDED_LOGGING_TAG, "onIceConnectionChange(" + iceConnectionState + ")");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -125,7 +135,11 @@ public class WebRTCWrapper {
|
|||
|
||||
@Override
|
||||
public void onIceCandidate(IceCandidate iceCandidate) {
|
||||
eventCallback.onIceCandidate(iceCandidate);
|
||||
if (readyToReceivedIceCandidates.get()) {
|
||||
eventCallback.onIceCandidate(iceCandidate);
|
||||
} else {
|
||||
iceCandidates.add(iceCandidate);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -136,7 +150,6 @@ public class WebRTCWrapper {
|
|||
@Override
|
||||
public void onAddStream(MediaStream mediaStream) {
|
||||
Log.d(EXTENDED_LOGGING_TAG, "onAddStream(numAudioTracks=" + mediaStream.audioTracks.size() + ",numVideoTracks=" + mediaStream.videoTracks.size() + ")");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -151,7 +164,11 @@ public class WebRTCWrapper {
|
|||
|
||||
@Override
|
||||
public void onRenegotiationNeeded() {
|
||||
|
||||
Log.d(EXTENDED_LOGGING_TAG, "onRenegotiationNeeded()");
|
||||
final PeerConnection.PeerConnectionState currentState = peerConnection == null ? null : peerConnection.connectionState();
|
||||
if (currentState != null && currentState != PeerConnection.PeerConnectionState.NEW) {
|
||||
eventCallback.onRenegotiationNeeded();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -161,7 +178,6 @@ public class WebRTCWrapper {
|
|||
if (track instanceof VideoTrack) {
|
||||
remoteVideoTrack = (VideoTrack) track;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -217,7 +233,9 @@ public class WebRTCWrapper {
|
|||
public void setup(final XmppConnectionService service, final AppRTCAudioManager.SpeakerPhonePreference speakerPhonePreference) throws InitializationException {
|
||||
try {
|
||||
PeerConnectionFactory.initialize(
|
||||
PeerConnectionFactory.InitializationOptions.builder(service).createInitializationOptions()
|
||||
PeerConnectionFactory.InitializationOptions.builder(service)
|
||||
.setFieldTrials("WebRTC-BindUsingInterfaceName/Enabled/")
|
||||
.createInitializationOptions()
|
||||
);
|
||||
} catch (final UnsatisfiedLinkError e) {
|
||||
throw new InitializationException("Unable to initialize PeerConnectionFactory", e);
|
||||
|
@ -253,10 +271,7 @@ public class WebRTCWrapper {
|
|||
.createPeerConnectionFactory();
|
||||
|
||||
|
||||
final PeerConnection.RTCConfiguration rtcConfig = new PeerConnection.RTCConfiguration(iceServers);
|
||||
rtcConfig.tcpCandidatePolicy = PeerConnection.TcpCandidatePolicy.DISABLED; //XEP-0176 doesn't support tcp
|
||||
rtcConfig.continualGatheringPolicy = PeerConnection.ContinualGatheringPolicy.GATHER_CONTINUALLY;
|
||||
rtcConfig.sdpSemantics = PeerConnection.SdpSemantics.UNIFIED_PLAN;
|
||||
final PeerConnection.RTCConfiguration rtcConfig = buildConfiguration(iceServers);
|
||||
final PeerConnection peerConnection = peerConnectionFactory.createPeerConnection(rtcConfig, peerConnectionObserver);
|
||||
if (peerConnection == null) {
|
||||
throw new InitializationException("Unable to create PeerConnection");
|
||||
|
@ -290,6 +305,31 @@ public class WebRTCWrapper {
|
|||
this.peerConnection = peerConnection;
|
||||
}
|
||||
|
||||
private static PeerConnection.RTCConfiguration buildConfiguration(final List<PeerConnection.IceServer> iceServers) {
|
||||
final PeerConnection.RTCConfiguration rtcConfig = new PeerConnection.RTCConfiguration(iceServers);
|
||||
rtcConfig.tcpCandidatePolicy = PeerConnection.TcpCandidatePolicy.DISABLED; //XEP-0176 doesn't support tcp
|
||||
rtcConfig.continualGatheringPolicy = PeerConnection.ContinualGatheringPolicy.GATHER_CONTINUALLY;
|
||||
rtcConfig.sdpSemantics = PeerConnection.SdpSemantics.UNIFIED_PLAN;
|
||||
rtcConfig.rtcpMuxPolicy = PeerConnection.RtcpMuxPolicy.NEGOTIATE;
|
||||
//rtcConfig.enableImplicitRollback = true;
|
||||
return rtcConfig;
|
||||
}
|
||||
|
||||
void reconfigurePeerConnection(final List<PeerConnection.IceServer> iceServers) {
|
||||
requirePeerConnection().setConfiguration(buildConfiguration(iceServers));
|
||||
}
|
||||
|
||||
void restartIce() {
|
||||
executorService.execute(() -> requirePeerConnection());
|
||||
}
|
||||
|
||||
public void setIsReadyToReceiveIceCandidates(final boolean ready) {
|
||||
readyToReceivedIceCandidates.set(ready);
|
||||
while (ready && iceCandidates.peek() != null) {
|
||||
eventCallback.onIceCandidate(iceCandidates.poll());
|
||||
}
|
||||
}
|
||||
|
||||
synchronized void close() {
|
||||
final PeerConnection peerConnection = this.peerConnection;
|
||||
final CapturerChoice capturerChoice = this.capturerChoice;
|
||||
|
@ -404,70 +444,23 @@ public class WebRTCWrapper {
|
|||
videoTrack.setEnabled(enabled);
|
||||
}
|
||||
|
||||
ListenableFuture<SessionDescription> createOffer() {
|
||||
synchronized ListenableFuture<SessionDescription> setLocalDescription() {
|
||||
return Futures.transformAsync(getPeerConnectionFuture(), peerConnection -> {
|
||||
final SettableFuture<SessionDescription> future = SettableFuture.create();
|
||||
peerConnection.createOffer(new CreateSdpObserver() {
|
||||
@Override
|
||||
public void onCreateSuccess(SessionDescription sessionDescription) {
|
||||
future.set(sessionDescription);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateFailure(String s) {
|
||||
future.setException(new IllegalStateException("Unable to create offer: " + s));
|
||||
}
|
||||
}, new MediaConstraints());
|
||||
return future;
|
||||
}, MoreExecutors.directExecutor());
|
||||
}
|
||||
|
||||
ListenableFuture<SessionDescription> createAnswer() {
|
||||
return Futures.transformAsync(getPeerConnectionFuture(), peerConnection -> {
|
||||
final SettableFuture<SessionDescription> future = SettableFuture.create();
|
||||
peerConnection.createAnswer(new CreateSdpObserver() {
|
||||
@Override
|
||||
public void onCreateSuccess(SessionDescription sessionDescription) {
|
||||
future.set(sessionDescription);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateFailure(String s) {
|
||||
future.setException(new IllegalStateException("Unable to create answer: " + s));
|
||||
}
|
||||
}, new MediaConstraints());
|
||||
return future;
|
||||
}, MoreExecutors.directExecutor());
|
||||
}
|
||||
|
||||
ListenableFuture<Void> setLocalDescription(final SessionDescription sessionDescription) {
|
||||
Log.d(EXTENDED_LOGGING_TAG, "setting local description:");
|
||||
private static void logDescription(final SessionDescription sessionDescription) {
|
||||
for (final String line : sessionDescription.description.split(eu.siacs.conversations.xmpp.jingle.SessionDescription.LINE_DIVIDER)) {
|
||||
Log.d(EXTENDED_LOGGING_TAG, line);
|
||||
}
|
||||
return Futures.transformAsync(getPeerConnectionFuture(), peerConnection -> {
|
||||
final SettableFuture<Void> future = SettableFuture.create();
|
||||
peerConnection.setLocalDescription(new SetSdpObserver() {
|
||||
@Override
|
||||
public void onSetSuccess() {
|
||||
future.set(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSetFailure(final String s) {
|
||||
future.setException(new IllegalArgumentException("unable to set local session description: " + s));
|
||||
|
||||
}
|
||||
}, sessionDescription);
|
||||
return future;
|
||||
}, MoreExecutors.directExecutor());
|
||||
}
|
||||
|
||||
ListenableFuture<Void> setRemoteDescription(final SessionDescription sessionDescription) {
|
||||
synchronized ListenableFuture<Void> setRemoteDescription(final SessionDescription sessionDescription) {
|
||||
Log.d(EXTENDED_LOGGING_TAG, "setting remote description:");
|
||||
for (final String line : sessionDescription.description.split(eu.siacs.conversations.xmpp.jingle.SessionDescription.LINE_DIVIDER)) {
|
||||
Log.d(EXTENDED_LOGGING_TAG, line);
|
||||
}
|
||||
logDescription(sessionDescription);
|
||||
return Futures.transformAsync(getPeerConnectionFuture(), peerConnection -> {
|
||||
final SettableFuture<Void> future = SettableFuture.create();
|
||||
peerConnection.setRemoteDescription(new SetSdpObserver() {
|
||||
|
@ -477,9 +470,8 @@ public class WebRTCWrapper {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onSetFailure(String s) {
|
||||
future.setException(new IllegalArgumentException("unable to set remote session description: " + s));
|
||||
|
||||
public void onSetFailure(final String message) {
|
||||
future.setException(new FailureToSetDescriptionException(message));
|
||||
}
|
||||
}, sessionDescription);
|
||||
return future;
|
||||
|
@ -490,26 +482,26 @@ public class WebRTCWrapper {
|
|||
private ListenableFuture<PeerConnection> getPeerConnectionFuture() {
|
||||
final PeerConnection peerConnection = this.peerConnection;
|
||||
if (peerConnection == null) {
|
||||
return Futures.immediateFailedFuture(new IllegalStateException("initialize PeerConnection first"));
|
||||
return Futures.immediateFailedFuture(new PeerConnectionNotInitialized());
|
||||
} else {
|
||||
return Futures.immediateFuture(peerConnection);
|
||||
}
|
||||
}
|
||||
|
||||
private PeerConnection requirePeerConnection() {
|
||||
final PeerConnection peerConnection = this.peerConnection;
|
||||
if (peerConnection == null) {
|
||||
throw new PeerConnectionNotInitialized();
|
||||
}
|
||||
return peerConnection;
|
||||
}
|
||||
|
||||
void addIceCandidate(IceCandidate iceCandidate) {
|
||||
requirePeerConnection().addIceCandidate(iceCandidate);
|
||||
}
|
||||
|
||||
private CameraEnumerator getCameraEnumerator() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
return new Camera2Enumerator(requireContext());
|
||||
} else {
|
||||
return new Camera1Enumerator();
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<CapturerChoice> getVideoCapturer() {
|
||||
final CameraEnumerator enumerator = getCameraEnumerator();
|
||||
final CameraEnumerator enumerator = new Camera2Enumerator(requireContext());
|
||||
final Set<String> deviceNames = ImmutableSet.copyOf(enumerator.getDeviceNames());
|
||||
for (final String deviceName : deviceNames) {
|
||||
if (isFrontFacing(enumerator, deviceName)) {
|
||||
|
@ -528,10 +520,15 @@ public class WebRTCWrapper {
|
|||
}
|
||||
}
|
||||
|
||||
public PeerConnection.PeerConnectionState getState() {
|
||||
PeerConnection.PeerConnectionState getState() {
|
||||
return requirePeerConnection().connectionState();
|
||||
}
|
||||
|
||||
public PeerConnection.SignalingState getSignalingState() {
|
||||
return requirePeerConnection().signalingState();
|
||||
}
|
||||
|
||||
|
||||
EglBase.Context getEglBaseContext() {
|
||||
return this.eglBase.getEglBaseContext();
|
||||
}
|
||||
|
@ -544,14 +541,6 @@ public class WebRTCWrapper {
|
|||
return Optional.fromNullable(this.remoteVideoTrack);
|
||||
}
|
||||
|
||||
private PeerConnection requirePeerConnection() {
|
||||
final PeerConnection peerConnection = this.peerConnection;
|
||||
if (peerConnection == null) {
|
||||
throw new PeerConnectionNotInitialized();
|
||||
}
|
||||
return peerConnection;
|
||||
}
|
||||
|
||||
private Context requireContext() {
|
||||
final Context context = this.context;
|
||||
if (context == null) {
|
||||
|
@ -564,12 +553,18 @@ public class WebRTCWrapper {
|
|||
return appRTCAudioManager;
|
||||
}
|
||||
|
||||
void execute(final Runnable command) {
|
||||
executorService.execute(command);
|
||||
}
|
||||
|
||||
public interface EventCallback {
|
||||
void onIceCandidate(IceCandidate iceCandidate);
|
||||
|
||||
void onConnectionChange(PeerConnection.PeerConnectionState newState);
|
||||
|
||||
void onAudioDeviceChanged(AppRTCAudioManager.AudioDevice selectedAudioDevice, Set<AppRTCAudioManager.AudioDevice> availableAudioDevices);
|
||||
|
||||
void onRenegotiationNeeded();
|
||||
}
|
||||
|
||||
private static abstract class SetSdpObserver implements SdpObserver {
|
||||
|
@ -620,6 +615,12 @@ public class WebRTCWrapper {
|
|||
|
||||
}
|
||||
|
||||
private static class FailureToSetDescriptionException extends IllegalArgumentException {
|
||||
public FailureToSetDescriptionException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
private static class CapturerChoice {
|
||||
private final CameraVideoCapturer cameraVideoCapturer;
|
||||
private final CameraEnumerationAndroid.CaptureFormat captureFormat;
|
||||
|
|
Loading…
Add table
Reference in a new issue