2020-04-06 11:01:17 +00:00
|
|
|
package eu.siacs.conversations.xmpp.jingle;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2020-04-14 17:06:39 +00:00
|
|
|
import android.os.Build;
|
2020-04-13 10:02:34 +00:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
2020-04-06 13:45:06 +00:00
|
|
|
import android.util.Log;
|
2020-04-06 11:01:17 +00:00
|
|
|
|
2020-04-14 17:06:39 +00:00
|
|
|
import com.google.common.base.Optional;
|
|
|
|
import com.google.common.base.Preconditions;
|
2020-05-02 15:15:46 +00:00
|
|
|
import com.google.common.collect.ImmutableSet;
|
2020-04-06 11:01:17 +00:00
|
|
|
import com.google.common.util.concurrent.Futures;
|
|
|
|
import com.google.common.util.concurrent.ListenableFuture;
|
|
|
|
import com.google.common.util.concurrent.MoreExecutors;
|
|
|
|
import com.google.common.util.concurrent.SettableFuture;
|
|
|
|
|
|
|
|
import org.webrtc.AudioSource;
|
|
|
|
import org.webrtc.AudioTrack;
|
2020-04-08 11:30:12 +00:00
|
|
|
import org.webrtc.CandidatePairChangeEvent;
|
2020-04-06 11:01:17 +00:00
|
|
|
import org.webrtc.DataChannel;
|
2020-04-14 17:06:39 +00:00
|
|
|
import org.webrtc.DefaultVideoDecoderFactory;
|
|
|
|
import org.webrtc.DefaultVideoEncoderFactory;
|
|
|
|
import org.webrtc.EglBase;
|
2020-04-06 11:01:17 +00:00
|
|
|
import org.webrtc.IceCandidate;
|
|
|
|
import org.webrtc.MediaConstraints;
|
|
|
|
import org.webrtc.MediaStream;
|
2020-04-20 15:05:27 +00:00
|
|
|
import org.webrtc.MediaStreamTrack;
|
2020-04-06 11:01:17 +00:00
|
|
|
import org.webrtc.PeerConnection;
|
|
|
|
import org.webrtc.PeerConnectionFactory;
|
|
|
|
import org.webrtc.RtpReceiver;
|
2020-04-20 15:05:27 +00:00
|
|
|
import org.webrtc.RtpTransceiver;
|
2020-04-06 11:01:17 +00:00
|
|
|
import org.webrtc.SdpObserver;
|
|
|
|
import org.webrtc.SessionDescription;
|
2020-04-07 09:36:28 +00:00
|
|
|
import org.webrtc.VideoTrack;
|
2020-05-21 09:13:46 +00:00
|
|
|
import org.webrtc.audio.JavaAudioDeviceModule;
|
|
|
|
import org.webrtc.voiceengine.WebRtcAudioEffects;
|
2020-04-06 11:01:17 +00:00
|
|
|
|
2021-11-11 20:02:15 +00:00
|
|
|
import java.util.LinkedList;
|
2020-04-06 11:01:17 +00:00
|
|
|
import java.util.List;
|
2021-11-11 20:02:15 +00:00
|
|
|
import java.util.Queue;
|
2020-04-13 10:02:34 +00:00
|
|
|
import java.util.Set;
|
2021-11-11 20:02:15 +00:00
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
2020-04-06 11:01:17 +00:00
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2020-04-06 13:45:06 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2020-04-13 10:02:34 +00:00
|
|
|
import eu.siacs.conversations.services.AppRTCAudioManager;
|
2020-05-21 13:39:59 +00:00
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
2020-04-06 13:45:06 +00:00
|
|
|
|
2022-11-12 12:37:18 +00:00
|
|
|
@SuppressWarnings("UnstableApiUsage")
|
2020-04-06 11:01:17 +00:00
|
|
|
public class WebRTCWrapper {
|
|
|
|
|
2020-04-20 09:38:02 +00:00
|
|
|
private static final String EXTENDED_LOGGING_TAG = WebRTCWrapper.class.getSimpleName();
|
|
|
|
|
2021-11-11 20:02:15 +00:00
|
|
|
private final ExecutorService executorService = Executors.newSingleThreadExecutor();
|
2022-11-12 12:37:18 +00:00
|
|
|
|
|
|
|
private static final Set<String> HARDWARE_AEC_BLACKLIST =
|
|
|
|
new ImmutableSet.Builder<String>()
|
|
|
|
.add("Pixel")
|
|
|
|
.add("Pixel XL")
|
|
|
|
.add("Moto G5")
|
|
|
|
.add("Moto G (5S) Plus")
|
|
|
|
.add("Moto G4")
|
|
|
|
.add("TA-1053")
|
|
|
|
.add("Mi A1")
|
|
|
|
.add("Mi A2")
|
|
|
|
.add("E5823") // Sony z5 compact
|
|
|
|
.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();
|
2020-04-16 07:03:39 +00:00
|
|
|
|
2020-04-14 17:06:39 +00:00
|
|
|
private final EventCallback eventCallback;
|
2021-11-11 20:02:15 +00:00
|
|
|
private final AtomicBoolean readyToReceivedIceCandidates = new AtomicBoolean(false);
|
|
|
|
private final Queue<IceCandidate> iceCandidates = new LinkedList<>();
|
2022-11-12 12:37:18 +00:00
|
|
|
private final AppRTCAudioManager.AudioManagerEvents audioManagerEvents =
|
|
|
|
new AppRTCAudioManager.AudioManagerEvents() {
|
|
|
|
@Override
|
|
|
|
public void onAudioDeviceChanged(
|
|
|
|
AppRTCAudioManager.AudioDevice selectedAudioDevice,
|
|
|
|
Set<AppRTCAudioManager.AudioDevice> availableAudioDevices) {
|
|
|
|
eventCallback.onAudioDeviceChanged(selectedAudioDevice, availableAudioDevices);
|
|
|
|
}
|
|
|
|
};
|
2020-04-14 17:06:39 +00:00
|
|
|
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
2022-11-12 12:37:18 +00:00
|
|
|
private TrackWrapper<AudioTrack> localAudioTrack = null;
|
|
|
|
private TrackWrapper<VideoTrack> localVideoTrack = null;
|
2020-04-07 09:36:28 +00:00
|
|
|
private VideoTrack remoteVideoTrack = null;
|
2022-11-12 12:37:18 +00:00
|
|
|
private final PeerConnection.Observer peerConnectionObserver =
|
|
|
|
new PeerConnection.Observer() {
|
|
|
|
@Override
|
|
|
|
public void onSignalingChange(PeerConnection.SignalingState signalingState) {
|
|
|
|
Log.d(EXTENDED_LOGGING_TAG, "onSignalingChange(" + signalingState + ")");
|
|
|
|
// this is called after removeTrack or addTrack
|
|
|
|
// and should then trigger a content-add or content-remove or something
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/removeTrack
|
|
|
|
}
|
2020-04-06 11:01:17 +00:00
|
|
|
|
2022-11-12 12:37:18 +00:00
|
|
|
@Override
|
|
|
|
public void onConnectionChange(final PeerConnection.PeerConnectionState newState) {
|
|
|
|
eventCallback.onConnectionChange(newState);
|
|
|
|
}
|
2020-04-06 11:01:17 +00:00
|
|
|
|
2022-11-12 12:37:18 +00:00
|
|
|
@Override
|
|
|
|
public void onIceConnectionChange(
|
|
|
|
PeerConnection.IceConnectionState iceConnectionState) {
|
|
|
|
Log.d(
|
|
|
|
EXTENDED_LOGGING_TAG,
|
|
|
|
"onIceConnectionChange(" + iceConnectionState + ")");
|
|
|
|
}
|
2020-04-06 11:01:17 +00:00
|
|
|
|
2022-11-12 12:37:18 +00:00
|
|
|
@Override
|
|
|
|
public void onSelectedCandidatePairChanged(CandidatePairChangeEvent event) {
|
|
|
|
Log.d(Config.LOGTAG, "remote candidate selected: " + event.remote);
|
|
|
|
Log.d(Config.LOGTAG, "local candidate selected: " + event.local);
|
|
|
|
}
|
2020-04-06 11:01:17 +00:00
|
|
|
|
2022-11-12 12:37:18 +00:00
|
|
|
@Override
|
|
|
|
public void onIceConnectionReceivingChange(boolean b) {}
|
2020-04-06 11:01:17 +00:00
|
|
|
|
2022-11-12 12:37:18 +00:00
|
|
|
@Override
|
|
|
|
public void onIceGatheringChange(
|
|
|
|
PeerConnection.IceGatheringState iceGatheringState) {
|
|
|
|
Log.d(EXTENDED_LOGGING_TAG, "onIceGatheringChange(" + iceGatheringState + ")");
|
|
|
|
}
|
2020-04-06 11:01:17 +00:00
|
|
|
|
2022-11-12 12:37:18 +00:00
|
|
|
@Override
|
|
|
|
public void onIceCandidate(IceCandidate iceCandidate) {
|
|
|
|
if (readyToReceivedIceCandidates.get()) {
|
|
|
|
eventCallback.onIceCandidate(iceCandidate);
|
|
|
|
} else {
|
|
|
|
iceCandidates.add(iceCandidate);
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 11:01:17 +00:00
|
|
|
|
2022-11-12 12:37:18 +00:00
|
|
|
@Override
|
|
|
|
public void onIceCandidatesRemoved(IceCandidate[] iceCandidates) {}
|
2020-04-06 11:01:17 +00:00
|
|
|
|
2022-11-12 12:37:18 +00:00
|
|
|
@Override
|
|
|
|
public void onAddStream(MediaStream mediaStream) {
|
|
|
|
Log.d(
|
|
|
|
EXTENDED_LOGGING_TAG,
|
|
|
|
"onAddStream(numAudioTracks="
|
|
|
|
+ mediaStream.audioTracks.size()
|
|
|
|
+ ",numVideoTracks="
|
|
|
|
+ mediaStream.videoTracks.size()
|
|
|
|
+ ")");
|
|
|
|
}
|
2020-04-06 11:01:17 +00:00
|
|
|
|
2022-11-12 12:37:18 +00:00
|
|
|
@Override
|
|
|
|
public void onRemoveStream(MediaStream mediaStream) {}
|
2020-04-06 11:01:17 +00:00
|
|
|
|
2022-11-12 12:37:18 +00:00
|
|
|
@Override
|
|
|
|
public void onDataChannel(DataChannel dataChannel) {}
|
2020-04-06 11:01:17 +00:00
|
|
|
|
2022-11-12 12:37:18 +00:00
|
|
|
@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();
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 11:01:17 +00:00
|
|
|
|
2022-11-12 12:37:18 +00:00
|
|
|
@Override
|
|
|
|
public void onAddTrack(RtpReceiver rtpReceiver, MediaStream[] mediaStreams) {
|
|
|
|
final MediaStreamTrack track = rtpReceiver.track();
|
|
|
|
Log.d(
|
|
|
|
EXTENDED_LOGGING_TAG,
|
|
|
|
"onAddTrack(kind="
|
|
|
|
+ (track == null ? "null" : track.kind())
|
|
|
|
+ ",numMediaStreams="
|
|
|
|
+ mediaStreams.length
|
|
|
|
+ ")");
|
|
|
|
if (track instanceof VideoTrack) {
|
|
|
|
remoteVideoTrack = (VideoTrack) track;
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 11:01:17 +00:00
|
|
|
|
2022-11-12 12:37:18 +00:00
|
|
|
@Override
|
2022-11-22 09:13:48 +00:00
|
|
|
public void onTrack(final RtpTransceiver transceiver) {
|
2022-11-12 12:37:18 +00:00
|
|
|
Log.d(
|
|
|
|
EXTENDED_LOGGING_TAG,
|
|
|
|
"onTrack(mid="
|
|
|
|
+ transceiver.getMid()
|
|
|
|
+ ",media="
|
|
|
|
+ transceiver.getMediaType()
|
2022-11-22 09:13:48 +00:00
|
|
|
+ ",direction="
|
|
|
|
+ transceiver.getDirection()
|
2022-11-12 12:37:18 +00:00
|
|
|
+ ")");
|
|
|
|
}
|
2022-11-22 09:13:48 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRemoveTrack(final RtpReceiver receiver) {
|
|
|
|
Log.d(EXTENDED_LOGGING_TAG, "onRemoveTrack(" + receiver.id() + ")");
|
|
|
|
}
|
2022-11-12 12:37:18 +00:00
|
|
|
};
|
2022-11-19 12:03:34 +00:00
|
|
|
@Nullable private PeerConnectionFactory peerConnectionFactory = null;
|
2022-11-12 12:37:18 +00:00
|
|
|
@Nullable private PeerConnection peerConnection = null;
|
2020-04-13 10:02:34 +00:00
|
|
|
private AppRTCAudioManager appRTCAudioManager = null;
|
2020-05-21 13:39:59 +00:00
|
|
|
private ToneManager toneManager = null;
|
2020-04-14 17:06:39 +00:00
|
|
|
private Context context = null;
|
|
|
|
private EglBase eglBase = null;
|
2022-11-12 12:37:18 +00:00
|
|
|
private VideoSourceWrapper videoSourceWrapper;
|
2020-04-06 11:01:17 +00:00
|
|
|
|
2020-04-29 07:10:15 +00:00
|
|
|
WebRTCWrapper(final EventCallback eventCallback) {
|
2020-04-06 11:01:17 +00:00
|
|
|
this.eventCallback = eventCallback;
|
|
|
|
}
|
|
|
|
|
2020-05-21 09:13:46 +00:00
|
|
|
private static void dispose(final PeerConnection peerConnection) {
|
|
|
|
try {
|
|
|
|
peerConnection.dispose();
|
|
|
|
} catch (final IllegalStateException e) {
|
|
|
|
Log.e(Config.LOGTAG, "unable to dispose of peer connection", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-12 12:37:18 +00:00
|
|
|
public void setup(
|
|
|
|
final XmppConnectionService service,
|
|
|
|
final AppRTCAudioManager.SpeakerPhonePreference speakerPhonePreference)
|
|
|
|
throws InitializationException {
|
2020-05-09 17:48:54 +00:00
|
|
|
try {
|
|
|
|
PeerConnectionFactory.initialize(
|
2022-03-08 20:14:08 +00:00
|
|
|
PeerConnectionFactory.InitializationOptions.builder(service)
|
2022-11-12 12:37:18 +00:00
|
|
|
.setFieldTrials("WebRTC-BindUsingInterfaceName/Enabled/")
|
|
|
|
.createInitializationOptions());
|
2020-05-09 17:48:54 +00:00
|
|
|
} catch (final UnsatisfiedLinkError e) {
|
2020-06-18 18:37:56 +00:00
|
|
|
throw new InitializationException("Unable to initialize PeerConnectionFactory", e);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
this.eglBase = EglBase.create();
|
|
|
|
} catch (final RuntimeException e) {
|
|
|
|
throw new InitializationException("Unable to create EGL base", e);
|
2020-05-09 17:48:54 +00:00
|
|
|
}
|
2020-05-21 13:39:59 +00:00
|
|
|
this.context = service;
|
|
|
|
this.toneManager = service.getJingleConnectionManager().toneManager;
|
2022-11-12 12:37:18 +00:00
|
|
|
mainHandler.post(
|
|
|
|
() -> {
|
|
|
|
appRTCAudioManager = AppRTCAudioManager.create(service, speakerPhonePreference);
|
|
|
|
toneManager.setAppRtcAudioManagerHasControl(true);
|
|
|
|
appRTCAudioManager.start(audioManagerEvents);
|
|
|
|
eventCallback.onAudioDeviceChanged(
|
|
|
|
appRTCAudioManager.getSelectedAudioDevice(),
|
|
|
|
appRTCAudioManager.getAudioDevices());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
synchronized void initializePeerConnection(
|
|
|
|
final Set<Media> media, final List<PeerConnection.IceServer> iceServers)
|
|
|
|
throws InitializationException {
|
2020-04-14 17:06:39 +00:00
|
|
|
Preconditions.checkState(this.eglBase != null);
|
2020-04-15 10:07:19 +00:00
|
|
|
Preconditions.checkNotNull(media);
|
2022-11-12 12:37:18 +00:00
|
|
|
Preconditions.checkArgument(
|
|
|
|
media.size() > 0, "media can not be empty when initializing peer connection");
|
|
|
|
final boolean setUseHardwareAcousticEchoCanceler =
|
|
|
|
WebRtcAudioEffects.canUseAcousticEchoCanceler()
|
|
|
|
&& !HARDWARE_AEC_BLACKLIST.contains(Build.MODEL);
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
String.format(
|
|
|
|
"setUseHardwareAcousticEchoCanceler(%s) model=%s",
|
|
|
|
setUseHardwareAcousticEchoCanceler, Build.MODEL));
|
2022-11-19 12:03:34 +00:00
|
|
|
this.peerConnectionFactory =
|
2022-11-12 12:37:18 +00:00
|
|
|
PeerConnectionFactory.builder()
|
|
|
|
.setVideoDecoderFactory(
|
|
|
|
new DefaultVideoDecoderFactory(eglBase.getEglBaseContext()))
|
|
|
|
.setVideoEncoderFactory(
|
|
|
|
new DefaultVideoEncoderFactory(
|
|
|
|
eglBase.getEglBaseContext(), true, true))
|
|
|
|
.setAudioDeviceModule(
|
2022-11-19 12:03:34 +00:00
|
|
|
JavaAudioDeviceModule.builder(requireContext())
|
2022-11-12 12:37:18 +00:00
|
|
|
.setUseHardwareAcousticEchoCanceler(
|
|
|
|
setUseHardwareAcousticEchoCanceler)
|
|
|
|
.createAudioDeviceModule())
|
|
|
|
.createPeerConnectionFactory();
|
2020-04-07 09:36:28 +00:00
|
|
|
|
2021-11-17 10:33:15 +00:00
|
|
|
final PeerConnection.RTCConfiguration rtcConfig = buildConfiguration(iceServers);
|
2022-11-12 12:37:18 +00:00
|
|
|
final PeerConnection peerConnection =
|
2022-11-19 12:03:34 +00:00
|
|
|
requirePeerConnectionFactory()
|
|
|
|
.createPeerConnection(rtcConfig, peerConnectionObserver);
|
2021-03-16 14:21:01 +00:00
|
|
|
if (peerConnection == null) {
|
|
|
|
throw new InitializationException("Unable to create PeerConnection");
|
|
|
|
}
|
2020-04-07 09:36:28 +00:00
|
|
|
|
2022-11-19 12:03:34 +00:00
|
|
|
if (media.contains(Media.VIDEO)) {
|
|
|
|
addVideoTrack(peerConnection);
|
2020-04-07 09:36:28 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 10:07:19 +00:00
|
|
|
if (media.contains(Media.AUDIO)) {
|
2022-11-19 12:03:34 +00:00
|
|
|
addAudioTrack(peerConnection);
|
2020-04-06 11:01:17 +00:00
|
|
|
}
|
2020-04-06 13:45:06 +00:00
|
|
|
peerConnection.setAudioPlayout(true);
|
|
|
|
peerConnection.setAudioRecording(true);
|
2022-11-12 12:37:18 +00:00
|
|
|
|
2020-04-06 11:01:17 +00:00
|
|
|
this.peerConnection = peerConnection;
|
|
|
|
}
|
2020-04-13 12:55:07 +00:00
|
|
|
|
2022-11-19 12:03:34 +00:00
|
|
|
private VideoSourceWrapper initializeVideoSourceWrapper() {
|
|
|
|
final VideoSourceWrapper existingVideoSourceWrapper = this.videoSourceWrapper;
|
|
|
|
if (existingVideoSourceWrapper != null) {
|
|
|
|
existingVideoSourceWrapper.startCapture();
|
|
|
|
return existingVideoSourceWrapper;
|
|
|
|
}
|
|
|
|
final VideoSourceWrapper videoSourceWrapper =
|
|
|
|
new VideoSourceWrapper.Factory(requireContext()).create();
|
|
|
|
if (videoSourceWrapper == null) {
|
|
|
|
throw new IllegalStateException("Could not instantiate VideoSourceWrapper");
|
|
|
|
}
|
|
|
|
videoSourceWrapper.initialize(
|
|
|
|
requirePeerConnectionFactory(), requireContext(), eglBase.getEglBaseContext());
|
|
|
|
videoSourceWrapper.startCapture();
|
2022-11-19 13:19:07 +00:00
|
|
|
this.videoSourceWrapper = videoSourceWrapper;
|
2022-11-19 12:03:34 +00:00
|
|
|
return videoSourceWrapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized boolean addTrack(final Media media) {
|
|
|
|
if (media == Media.VIDEO) {
|
|
|
|
return addVideoTrack(requirePeerConnection());
|
|
|
|
} else if (media == Media.AUDIO) {
|
|
|
|
return addAudioTrack(requirePeerConnection());
|
|
|
|
}
|
|
|
|
throw new IllegalStateException(String.format("Could not add track for %s", media));
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean addAudioTrack(final PeerConnection peerConnection) {
|
|
|
|
final AudioSource audioSource =
|
|
|
|
requirePeerConnectionFactory().createAudioSource(new MediaConstraints());
|
|
|
|
final AudioTrack audioTrack =
|
|
|
|
requirePeerConnectionFactory().createAudioTrack("my-audio-track", audioSource);
|
|
|
|
this.localAudioTrack = TrackWrapper.addTrack(peerConnection, audioTrack);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean addVideoTrack(final PeerConnection peerConnection) {
|
|
|
|
Preconditions.checkState(
|
|
|
|
this.localVideoTrack == null, "A local video track already exists");
|
|
|
|
final VideoSourceWrapper videoSourceWrapper;
|
|
|
|
try {
|
|
|
|
videoSourceWrapper = initializeVideoSourceWrapper();
|
|
|
|
} catch (final IllegalStateException e) {
|
|
|
|
Log.d(Config.LOGTAG, "could not add video track", e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
final VideoTrack videoTrack =
|
|
|
|
requirePeerConnectionFactory()
|
|
|
|
.createVideoTrack("my-video-track", videoSourceWrapper.getVideoSource());
|
|
|
|
this.localVideoTrack = TrackWrapper.addTrack(peerConnection, videoTrack);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-12 12:37:18 +00:00
|
|
|
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;
|
2021-11-17 10:33:15 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2021-11-11 13:40:15 +00:00
|
|
|
void restartIce() {
|
2021-11-14 17:22:18 +00:00
|
|
|
executorService.execute(() -> requirePeerConnection().restartIce());
|
2021-11-11 20:02:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setIsReadyToReceiveIceCandidates(final boolean ready) {
|
|
|
|
readyToReceivedIceCandidates.set(ready);
|
2021-11-14 17:22:18 +00:00
|
|
|
while (ready && iceCandidates.peek() != null) {
|
2021-11-11 20:02:15 +00:00
|
|
|
eventCallback.onIceCandidate(iceCandidates.poll());
|
|
|
|
}
|
2021-11-11 13:40:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 07:10:15 +00:00
|
|
|
synchronized void close() {
|
2020-04-08 13:27:17 +00:00
|
|
|
final PeerConnection peerConnection = this.peerConnection;
|
2022-11-19 12:03:34 +00:00
|
|
|
final PeerConnectionFactory peerConnectionFactory = this.peerConnectionFactory;
|
2022-11-12 12:37:18 +00:00
|
|
|
final VideoSourceWrapper videoSourceWrapper = this.videoSourceWrapper;
|
2020-04-15 10:07:19 +00:00
|
|
|
final AppRTCAudioManager audioManager = this.appRTCAudioManager;
|
|
|
|
final EglBase eglBase = this.eglBase;
|
2020-04-08 13:27:17 +00:00
|
|
|
if (peerConnection != null) {
|
2020-04-28 18:15:23 +00:00
|
|
|
dispose(peerConnection);
|
2020-04-18 16:22:10 +00:00
|
|
|
this.peerConnection = null;
|
2020-04-08 13:27:17 +00:00
|
|
|
}
|
2020-04-13 10:02:34 +00:00
|
|
|
if (audioManager != null) {
|
2020-05-21 13:39:59 +00:00
|
|
|
toneManager.setAppRtcAudioManagerHasControl(false);
|
2020-04-13 10:02:34 +00:00
|
|
|
mainHandler.post(audioManager::stop);
|
|
|
|
}
|
2020-04-14 19:06:26 +00:00
|
|
|
this.localVideoTrack = null;
|
|
|
|
this.remoteVideoTrack = null;
|
2022-11-12 12:37:18 +00:00
|
|
|
if (videoSourceWrapper != null) {
|
2020-04-14 19:06:26 +00:00
|
|
|
try {
|
2022-11-12 12:37:18 +00:00
|
|
|
videoSourceWrapper.stopCapture();
|
|
|
|
} catch (final InterruptedException e) {
|
2020-04-15 10:07:19 +00:00
|
|
|
Log.e(Config.LOGTAG, "unable to stop capturing");
|
2020-04-14 19:06:26 +00:00
|
|
|
}
|
2022-11-19 12:03:34 +00:00
|
|
|
videoSourceWrapper.dispose();
|
2020-04-14 19:06:26 +00:00
|
|
|
}
|
2020-04-15 10:07:19 +00:00
|
|
|
if (eglBase != null) {
|
|
|
|
eglBase.release();
|
2020-04-18 16:22:10 +00:00
|
|
|
this.eglBase = null;
|
|
|
|
}
|
2022-11-19 12:03:34 +00:00
|
|
|
if (peerConnectionFactory != null) {
|
|
|
|
peerConnectionFactory.dispose();
|
|
|
|
}
|
2020-04-18 16:22:10 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 07:10:15 +00:00
|
|
|
synchronized void verifyClosed() {
|
2020-04-18 16:22:10 +00:00
|
|
|
if (this.peerConnection != null
|
|
|
|
|| this.eglBase != null
|
|
|
|
|| this.localVideoTrack != null
|
|
|
|
|| this.remoteVideoTrack != null) {
|
2022-11-12 12:37:18 +00:00
|
|
|
final IllegalStateException e =
|
|
|
|
new IllegalStateException("WebRTCWrapper hasn't been closed properly");
|
2020-04-21 10:00:13 +00:00
|
|
|
Log.e(Config.LOGTAG, "verifyClosed() failed. Going to throw", e);
|
|
|
|
throw e;
|
2020-04-15 10:07:19 +00:00
|
|
|
}
|
2020-04-08 13:27:17 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 15:15:46 +00:00
|
|
|
boolean isCameraSwitchable() {
|
2022-11-12 12:37:18 +00:00
|
|
|
final VideoSourceWrapper videoSourceWrapper = this.videoSourceWrapper;
|
|
|
|
return videoSourceWrapper != null && videoSourceWrapper.isCameraSwitchable();
|
2020-05-02 15:15:46 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 09:08:11 +00:00
|
|
|
boolean isFrontCamera() {
|
2022-11-12 12:37:18 +00:00
|
|
|
final VideoSourceWrapper videoSourceWrapper = this.videoSourceWrapper;
|
|
|
|
return videoSourceWrapper == null || videoSourceWrapper.isFrontCamera();
|
2020-05-03 09:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ListenableFuture<Boolean> switchCamera() {
|
2022-11-12 12:37:18 +00:00
|
|
|
final VideoSourceWrapper videoSourceWrapper = this.videoSourceWrapper;
|
|
|
|
if (videoSourceWrapper == null) {
|
|
|
|
return Futures.immediateFailedFuture(
|
|
|
|
new IllegalStateException("VideoSourceWrapper has not been initialized"));
|
2020-05-02 15:15:46 +00:00
|
|
|
}
|
2022-11-12 12:37:18 +00:00
|
|
|
return videoSourceWrapper.switchCamera();
|
2020-05-02 15:15:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 17:16:47 +00:00
|
|
|
boolean isMicrophoneEnabled() {
|
2022-11-12 12:37:18 +00:00
|
|
|
final Optional<AudioTrack> audioTrack = TrackWrapper.get(this.localAudioTrack);
|
|
|
|
if (audioTrack.isPresent()) {
|
|
|
|
try {
|
|
|
|
return audioTrack.get().enabled();
|
|
|
|
} catch (final IllegalStateException e) {
|
|
|
|
// sometimes UI might still be rendering the buttons when a background thread has
|
|
|
|
// already ended the call
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2020-04-13 10:53:23 +00:00
|
|
|
throw new IllegalStateException("Local audio track does not exist (yet)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 17:14:28 +00:00
|
|
|
boolean setMicrophoneEnabled(final boolean enabled) {
|
2022-11-12 12:37:18 +00:00
|
|
|
final Optional<AudioTrack> audioTrack = TrackWrapper.get(this.localAudioTrack);
|
|
|
|
if (audioTrack.isPresent()) {
|
|
|
|
try {
|
|
|
|
audioTrack.get().setEnabled(enabled);
|
|
|
|
return true;
|
|
|
|
} catch (final IllegalStateException e) {
|
|
|
|
Log.d(Config.LOGTAG, "unable to toggle microphone", e);
|
|
|
|
// ignoring race condition in case MediaStreamTrack has been disposed
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2020-04-13 10:53:23 +00:00
|
|
|
throw new IllegalStateException("Local audio track does not exist (yet)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 07:10:15 +00:00
|
|
|
boolean isVideoEnabled() {
|
2022-11-12 12:37:18 +00:00
|
|
|
final Optional<VideoTrack> videoTrack = TrackWrapper.get(this.localVideoTrack);
|
|
|
|
if (videoTrack.isPresent()) {
|
|
|
|
return videoTrack.get().enabled();
|
2020-04-15 17:16:47 +00:00
|
|
|
}
|
2022-11-12 12:37:18 +00:00
|
|
|
return false;
|
2020-04-15 17:16:47 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 07:10:15 +00:00
|
|
|
void setVideoEnabled(final boolean enabled) {
|
2022-11-12 12:37:18 +00:00
|
|
|
final Optional<VideoTrack> videoTrack = TrackWrapper.get(this.localVideoTrack);
|
|
|
|
if (videoTrack.isPresent()) {
|
|
|
|
videoTrack.get().setEnabled(enabled);
|
|
|
|
return;
|
2020-04-15 17:16:47 +00:00
|
|
|
}
|
2022-11-12 12:37:18 +00:00
|
|
|
throw new IllegalStateException("Local video track does not exist");
|
2020-04-15 17:16:47 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 14:17:12 +00:00
|
|
|
synchronized ListenableFuture<SessionDescription> setLocalDescription() {
|
2022-11-12 12:37:18 +00:00
|
|
|
return Futures.transformAsync(
|
|
|
|
getPeerConnectionFuture(),
|
|
|
|
peerConnection -> {
|
|
|
|
if (peerConnection == null) {
|
|
|
|
return Futures.immediateFailedFuture(
|
|
|
|
new IllegalStateException("PeerConnection was null"));
|
|
|
|
}
|
|
|
|
final SettableFuture<SessionDescription> future = SettableFuture.create();
|
|
|
|
peerConnection.setLocalDescription(
|
|
|
|
new SetSdpObserver() {
|
|
|
|
@Override
|
|
|
|
public void onSetSuccess() {
|
|
|
|
final SessionDescription description =
|
|
|
|
peerConnection.getLocalDescription();
|
|
|
|
Log.d(EXTENDED_LOGGING_TAG, "set local description:");
|
|
|
|
logDescription(description);
|
|
|
|
future.set(description);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSetFailure(final String message) {
|
|
|
|
future.setException(
|
|
|
|
new FailureToSetDescriptionException(message));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return future;
|
|
|
|
},
|
|
|
|
MoreExecutors.directExecutor());
|
2020-04-06 11:01:17 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 15:40:16 +00:00
|
|
|
private static void logDescription(final SessionDescription sessionDescription) {
|
2022-11-12 12:37:18 +00:00
|
|
|
for (final String line :
|
|
|
|
sessionDescription.description.split(
|
|
|
|
eu.siacs.conversations.xmpp.jingle.SessionDescription.LINE_DIVIDER)) {
|
2020-04-20 09:38:02 +00:00
|
|
|
Log.d(EXTENDED_LOGGING_TAG, line);
|
|
|
|
}
|
2020-04-06 11:01:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-12 12:37:18 +00:00
|
|
|
synchronized ListenableFuture<Void> setRemoteDescription(
|
|
|
|
final SessionDescription sessionDescription) {
|
2020-04-20 09:38:02 +00:00
|
|
|
Log.d(EXTENDED_LOGGING_TAG, "setting remote description:");
|
2021-11-10 15:40:16 +00:00
|
|
|
logDescription(sessionDescription);
|
2022-11-12 12:37:18 +00:00
|
|
|
return Futures.transformAsync(
|
|
|
|
getPeerConnectionFuture(),
|
|
|
|
peerConnection -> {
|
|
|
|
if (peerConnection == null) {
|
|
|
|
return Futures.immediateFailedFuture(
|
|
|
|
new IllegalStateException("PeerConnection was null"));
|
|
|
|
}
|
|
|
|
final SettableFuture<Void> future = SettableFuture.create();
|
|
|
|
peerConnection.setRemoteDescription(
|
|
|
|
new SetSdpObserver() {
|
|
|
|
@Override
|
|
|
|
public void onSetSuccess() {
|
|
|
|
future.set(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSetFailure(final String message) {
|
|
|
|
future.setException(
|
|
|
|
new FailureToSetDescriptionException(message));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
sessionDescription);
|
|
|
|
return future;
|
|
|
|
},
|
|
|
|
MoreExecutors.directExecutor());
|
2020-04-06 11:01:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
private ListenableFuture<PeerConnection> getPeerConnectionFuture() {
|
|
|
|
final PeerConnection peerConnection = this.peerConnection;
|
|
|
|
if (peerConnection == null) {
|
2021-11-16 14:17:12 +00:00
|
|
|
return Futures.immediateFailedFuture(new PeerConnectionNotInitialized());
|
2020-04-06 11:01:17 +00:00
|
|
|
} else {
|
|
|
|
return Futures.immediateFuture(peerConnection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 12:03:34 +00:00
|
|
|
@Nonnull
|
2021-11-16 14:17:12 +00:00
|
|
|
private PeerConnection requirePeerConnection() {
|
|
|
|
final PeerConnection peerConnection = this.peerConnection;
|
|
|
|
if (peerConnection == null) {
|
|
|
|
throw new PeerConnectionNotInitialized();
|
|
|
|
}
|
|
|
|
return peerConnection;
|
|
|
|
}
|
|
|
|
|
2022-11-19 12:03:34 +00:00
|
|
|
@Nonnull
|
|
|
|
private PeerConnectionFactory requirePeerConnectionFactory() {
|
|
|
|
final PeerConnectionFactory peerConnectionFactory = this.peerConnectionFactory;
|
|
|
|
if (peerConnectionFactory == null) {
|
|
|
|
throw new IllegalStateException("Make sure PeerConnectionFactory is initialized");
|
|
|
|
}
|
|
|
|
return peerConnectionFactory;
|
|
|
|
}
|
|
|
|
|
2020-04-29 07:10:15 +00:00
|
|
|
void addIceCandidate(IceCandidate iceCandidate) {
|
2020-04-07 12:22:12 +00:00
|
|
|
requirePeerConnection().addIceCandidate(iceCandidate);
|
|
|
|
}
|
|
|
|
|
2021-11-16 14:17:12 +00:00
|
|
|
PeerConnection.PeerConnectionState getState() {
|
2020-04-07 12:22:12 +00:00
|
|
|
return requirePeerConnection().connectionState();
|
|
|
|
}
|
|
|
|
|
2021-11-16 14:17:12 +00:00
|
|
|
public PeerConnection.SignalingState getSignalingState() {
|
|
|
|
return requirePeerConnection().signalingState();
|
|
|
|
}
|
|
|
|
|
2020-04-16 07:03:39 +00:00
|
|
|
EglBase.Context getEglBaseContext() {
|
2020-04-14 17:06:39 +00:00
|
|
|
return this.eglBase.getEglBaseContext();
|
|
|
|
}
|
|
|
|
|
2020-04-29 07:10:15 +00:00
|
|
|
Optional<VideoTrack> getLocalVideoTrack() {
|
2022-11-12 12:37:18 +00:00
|
|
|
return TrackWrapper.get(this.localVideoTrack);
|
2020-04-14 17:06:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 07:10:15 +00:00
|
|
|
Optional<VideoTrack> getRemoteVideoTrack() {
|
2020-04-14 17:06:39 +00:00
|
|
|
return Optional.fromNullable(this.remoteVideoTrack);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Context requireContext() {
|
|
|
|
final Context context = this.context;
|
|
|
|
if (context == null) {
|
|
|
|
throw new IllegalStateException("call setup first");
|
|
|
|
}
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2020-04-29 07:10:15 +00:00
|
|
|
AppRTCAudioManager getAudioManager() {
|
2020-04-13 10:53:23 +00:00
|
|
|
return appRTCAudioManager;
|
|
|
|
}
|
|
|
|
|
2021-11-11 20:02:15 +00:00
|
|
|
void execute(final Runnable command) {
|
|
|
|
executorService.execute(command);
|
|
|
|
}
|
|
|
|
|
2020-04-14 17:06:39 +00:00
|
|
|
public interface EventCallback {
|
|
|
|
void onIceCandidate(IceCandidate iceCandidate);
|
|
|
|
|
2021-11-11 16:05:32 +00:00
|
|
|
void onConnectionChange(PeerConnection.PeerConnectionState newState);
|
2020-04-14 17:06:39 +00:00
|
|
|
|
2022-11-12 12:37:18 +00:00
|
|
|
void onAudioDeviceChanged(
|
|
|
|
AppRTCAudioManager.AudioDevice selectedAudioDevice,
|
|
|
|
Set<AppRTCAudioManager.AudioDevice> availableAudioDevices);
|
2021-11-11 13:40:15 +00:00
|
|
|
|
|
|
|
void onRenegotiationNeeded();
|
2020-04-14 17:06:39 +00:00
|
|
|
}
|
|
|
|
|
2022-11-12 12:37:18 +00:00
|
|
|
private abstract static class SetSdpObserver implements SdpObserver {
|
2020-04-06 11:01:17 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreateSuccess(org.webrtc.SessionDescription sessionDescription) {
|
|
|
|
throw new IllegalStateException("Not able to use SetSdpObserver");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreateFailure(String s) {
|
|
|
|
throw new IllegalStateException("Not able to use SetSdpObserver");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 07:10:15 +00:00
|
|
|
static class InitializationException extends Exception {
|
2020-04-09 13:22:03 +00:00
|
|
|
|
2020-06-18 18:37:56 +00:00
|
|
|
private InitializationException(final String message, final Throwable throwable) {
|
|
|
|
super(message, throwable);
|
2020-05-09 17:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private InitializationException(final String message) {
|
2020-04-09 13:22:03 +00:00
|
|
|
super(message);
|
|
|
|
}
|
|
|
|
}
|
2020-04-16 07:03:39 +00:00
|
|
|
|
2020-08-01 12:18:00 +00:00
|
|
|
public static class PeerConnectionNotInitialized extends IllegalStateException {
|
|
|
|
|
|
|
|
private PeerConnectionNotInitialized() {
|
|
|
|
super("initialize PeerConnection first");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-10 15:40:16 +00:00
|
|
|
private static class FailureToSetDescriptionException extends IllegalArgumentException {
|
|
|
|
public FailureToSetDescriptionException(String message) {
|
|
|
|
super(message);
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 11:01:17 +00:00
|
|
|
}
|