Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import org.webrtc.SessionDescription;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;

public class JanusConnection {
protected final PeerConnection peerConnection;
protected final PeerConnectionFactory peerConnectionFactory;
private List<PeerConnection.IceServer> configuration;
protected PmxKeyStore keyStore;
public final ConnectionType connectionType;
private long sessionId = -1L;
Expand All @@ -27,6 +29,17 @@ public JanusConnection(
ConnectionType connectionType,
TrackObserver trackObserver,
BiConsumer<Long,String> onTrickle
) {
this(pcFactory, keyStore, connectionType, trackObserver, onTrickle, Collections.emptyList());
}

public JanusConnection(
PeerConnectionFactory pcFactory,
PmxKeyStore keyStore,
ConnectionType connectionType,
TrackObserver trackObserver,
BiConsumer<Long, String> onTrickle,
List<PeerConnection.IceServer> configuration
) {
this.peerConnectionFactory = pcFactory;
this.connectionType = connectionType;
Expand All @@ -53,6 +66,7 @@ public JanusConnection(
}
}
);
this.configuration = configuration;
this.peerConnection = createPeerConnection(pcObserver);
}

Expand Down Expand Up @@ -87,7 +101,7 @@ public void onSetFailure(String s) {
//TODO: We need method to pass framecryptorOptions and TrackObserver
private PeerConnection createPeerConnection(PcObserver pcObserver) {
return peerConnectionFactory.createPeerConnection(
new PeerConnection.RTCConfiguration(Collections.emptyList()),
new PeerConnection.RTCConfiguration(configuration),
pcObserver
);
}
Expand Down Expand Up @@ -120,4 +134,9 @@ public void close(){
peerConnection.dispose();
}
}

public void setRTCConfiguration(List<PeerConnection.IceServer> configuration) {
this.configuration = configuration;
this.peerConnection.setConfiguration(new PeerConnection.RTCConfiguration(configuration));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.simplito.java.privmx_endpoint.model.VideoTrackInfo;

import org.webrtc.MediaConstraints;
import org.webrtc.PeerConnection;
import org.webrtc.PeerConnectionFactory;
import org.webrtc.PmxFrameCryptor;
import org.webrtc.PmxFrameCryptorFactory;
Expand All @@ -16,6 +17,7 @@
import org.webrtc.VideoCapturer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;
Expand All @@ -26,9 +28,14 @@ public class JanusPublisher extends JanusConnection{
//TODO: Add videoCapturer to VideoTrackInfo
public final Map<String, VideoCapturer> videoCapturers = new HashMap<>();


public JanusPublisher(PeerConnectionFactory pcFactory, PmxKeyStore keyStore, TrackObserver observer, BiConsumer<Long,String> onTrickle) {
super(pcFactory, keyStore, ConnectionType.Publisher, observer, onTrickle);
public JanusPublisher(
PeerConnectionFactory pcFactory,
PmxKeyStore keyStore,
TrackObserver observer,
BiConsumer<Long, String> onTrickle,
List<PeerConnection.IceServer> configuration
) {
super(pcFactory, keyStore, ConnectionType.Publisher, observer, onTrickle, configuration);
}

public void addAudioTrack(org.webrtc.AudioTrack audioTrack) {
Expand Down Expand Up @@ -82,7 +89,7 @@ public void addVideoTrack(
}

public void addVideoTrack(org.webrtc.VideoTrack videoTrack) {
addVideoTrack(videoTrack,null);
addVideoTrack(videoTrack, null);
}

public void removeAudioTrack(String id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@
import com.simplito.java.privmx_endpoint.model.ConnectionType;

import org.webrtc.MediaConstraints;
import org.webrtc.PeerConnection;
import org.webrtc.PeerConnectionFactory;
import org.webrtc.PmxKeyStore;
import org.webrtc.SessionDescription;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;

public class JanusSubscriber extends JanusConnection{
public JanusSubscriber(PeerConnectionFactory pcFactory, PmxKeyStore keyStore, TrackObserver observer, BiConsumer<Long,String> onTrickle) {
super(pcFactory, keyStore, ConnectionType.Publisher, observer, onTrickle);
public class JanusSubscriber extends JanusConnection {
public JanusSubscriber(
PeerConnectionFactory pcFactory,
PmxKeyStore keyStore,
TrackObserver observer,
BiConsumer<Long, String> onTrickle,
List<PeerConnection.IceServer> configuration
) {
super(pcFactory, keyStore, ConnectionType.Publisher, observer, onTrickle, configuration);
}

public String createAnswer(String offerSdp){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@

import com.simplito.java.privmx_endpoint.model.stream.StreamHandle;

import org.webrtc.PeerConnection;
import org.webrtc.PeerConnectionFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Function;

class PeerConnectionManager {
private final Map<String, RoomJanusSession> sessions = new HashMap<>();
private final Map<Long, String> sessionHandles = new HashMap<>();
protected final PeerConnectionFactory pcFactory;
private final BiConsumer<Long,String> onTrickle;
private List<PeerConnection.IceServer> configuration;

PeerConnectionManager(
PeerConnectionFactory pcFactory,
Expand All @@ -29,9 +31,9 @@ class PeerConnectionManager {
}

@NonNull
public RoomJanusSession createSession(@NonNull String streamRoomId) {
public RoomJanusSession createSession(@NonNull String streamRoomId, List<PeerConnection.IceServer> configuration) {
return Optional.ofNullable(
sessions.putIfAbsent(streamRoomId, new RoomJanusSession(streamRoomId, pcFactory, onTrickle))
sessions.putIfAbsent(streamRoomId, new RoomJanusSession(streamRoomId, pcFactory, onTrickle, configuration))
).orElse(Objects.requireNonNull(sessions.get(streamRoomId)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


import com.simplito.java.privmx_endpoint.model.stream.Key;
import com.simplito.java.privmx_endpoint.model.stream.KeyType;
import org.webrtc.MediaStreamTrack;
Expand Down Expand Up @@ -37,13 +36,20 @@ public class RoomJanusSession {
private final BiConsumer<Long,String> onTrickle;
private final Map<String, TrackObserver> trackObserversByStreamId = new HashMap<>();
private final TrackObserver trackObserver = new TrackObserverImpl();
private List<PeerConnection.IceServer> configuration = Collections.emptyList();

//TODO: Add error listener for catch errors from webrtcInterface
public RoomJanusSession(@NonNull String roomId, @NonNull PeerConnectionFactory pcFactory, BiConsumer<Long,String> onTrickle) {
public RoomJanusSession(
@NonNull String roomId,
@NonNull PeerConnectionFactory pcFactory,
BiConsumer<Long,String> onTrickle,
List<PeerConnection.IceServer> configuration
) {
this.pcFactory = pcFactory;
this.roomID = roomId;
this.keyStore = PmxFrameCryptorFactory.createPmxKeyStore();
this.onTrickle = onTrickle;
this.configuration = configuration;
}

@Nullable
Expand All @@ -62,11 +68,11 @@ public synchronized void createSubscriber() {

public synchronized void createSubscriber(TrackObserver observer) {
if (subscriber == null) {
subscriber = new JanusSubscriber(pcFactory, keyStore, observer, onTrickle);
}else if (subscriber.isEnded()) {
subscriber = new JanusSubscriber(pcFactory, keyStore, observer, onTrickle, configuration);
} else if (subscriber.isEnded()) {
subscriber.close();
subscriber = new JanusSubscriber(pcFactory, keyStore, observer, onTrickle);
}else{
subscriber = new JanusSubscriber(pcFactory, keyStore, observer, onTrickle, configuration);
} else {
throw new IllegalStateException("Subscriber is currently active.");
}
}
Expand All @@ -77,10 +83,10 @@ public synchronized void createPublisher() {

public synchronized void createPublisher(TrackObserver observer) {
if (publisher == null) {
publisher = new JanusPublisher(pcFactory, keyStore, observer, onTrickle);
publisher = new JanusPublisher(pcFactory, keyStore, observer, onTrickle, configuration);
}else if (publisher.isEnded()) {
publisher.close();
publisher = new JanusPublisher(pcFactory, keyStore, observer, onTrickle);
publisher = new JanusPublisher(pcFactory, keyStore, observer, onTrickle, configuration);
}else{
throw new IllegalStateException("Publisher is currently active.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.simplito.java.privmx_endpoint.model.ConnectionType;
import com.simplito.java.privmx_endpoint.model.ContainerPolicy;
import com.simplito.java.privmx_endpoint.model.PagingList;
import com.simplito.java.privmx_endpoint.model.UserWithPubKey;
Expand All @@ -23,6 +24,7 @@
import org.webrtc.DefaultVideoEncoderFactory;
import org.webrtc.EglBase;
import org.webrtc.MediaStreamTrack;
import org.webrtc.PeerConnection;
import org.webrtc.PeerConnectionFactory;
import org.webrtc.PmxFrameCryptor;
import org.webrtc.VideoDecoderFactory;
Expand All @@ -31,7 +33,6 @@
import org.webrtc.audio.AudioDeviceModule;
import org.webrtc.audio.JavaAudioDeviceModule;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -166,7 +167,7 @@ public void joinStreamRoom(
String streamRoomId
) {
//TODO: Rollback this change, it is do only for run test
RoomJanusSession session = pcManager.createSession(streamRoomId);
RoomJanusSession session = pcManager.createSession(streamRoomId, getRTCConfiguration());
api.joinStreamRoom(streamRoomId, session.webrtc);
}

Expand Down Expand Up @@ -262,11 +263,13 @@ public void removeTrack(

public StreamPublishResult publishStream(StreamHandle streamHandle) {
Objects.requireNonNull(streamHandle);
setRTCConfiguration(streamHandle, ConnectionType.Publisher);
return api.publishStream(streamHandle);
}

public StreamPublishResult updateStream(StreamHandle streamHandle) {
Objects.requireNonNull(streamHandle);
setRTCConfiguration(streamHandle, ConnectionType.Publisher);
return api.updateStream(streamHandle);
}

Expand All @@ -292,6 +295,7 @@ public void subscribeToRemoteStreams(
throw new IllegalStateException("No active session to this Stream Room. Join stream room first");
try {
session.createSubscriber();
setRTCConfiguration(streamRoomId, ConnectionType.Subscriber);
} catch (IllegalStateException ignored) {}
api.subscribeToRemoteStreams(streamRoomId, subscriptions, options);
}
Expand All @@ -315,6 +319,7 @@ public void modifyRemoteStreamsSubscriptions(
List<StreamSubscription> subscriptionsToRemove,
Settings options
) {
setRTCConfiguration(streamRoomId, ConnectionType.Subscriber);
api.modifyRemoteStreamsSubscriptions(
streamRoomId,
subscriptionsToAdd,
Expand All @@ -327,6 +332,7 @@ public void unsubscribeFromRemoteStreams(
String streamRoomId,
List<StreamSubscription> subscriptionsToRemove
) {
setRTCConfiguration(streamRoomId,ConnectionType.Subscriber);
api.unsubscribeFromRemoteStreams(
streamRoomId,
subscriptionsToRemove
Expand Down Expand Up @@ -364,4 +370,44 @@ public String buildSubscriptionQuery(
selectorId
);
}

// ----- PRIV
private List<PeerConnection.IceServer> getRTCConfiguration() {
return api.getTurnCredentials().stream().map(item ->
PeerConnection.IceServer.builder(item.url)
.setUsername(item.username)
.setPassword(item.password)
.createIceServer()
).collect(Collectors.toList());
}

private void setRTCConfiguration(Object key, ConnectionType mode) {
RoomJanusSession session = key instanceof StreamHandle
? pcManager.getSession((StreamHandle) key)
: pcManager.getSession((String) key);

if (session == null) {
throw new IllegalStateException(
"No active session to this Stream Room. Join stream room first"
);
}

applyRTCConfiguration(session, mode);
}

private void applyRTCConfiguration(RoomJanusSession session, ConnectionType mode) {
switch (mode) {
case Publisher:
if (session.getPublisher() != null) {
session.getPublisher().setRTCConfiguration(getRTCConfiguration());
}
break;

case Subscriber:
if (session.getSubscriber() != null) {
session.getSubscriber().setRTCConfiguration(getRTCConfiguration());
}
break;
}
}
}