openvidu-server: GET /sessions simple response

pull/88/merge
pabloFuente 2018-07-04 14:58:05 +02:00
parent a0e50d1022
commit fa52c1e76a
6 changed files with 190 additions and 118 deletions

View File

@ -40,23 +40,6 @@ public class MediaOptions {
this.videoDimensions = videoDimensions;
}
@SuppressWarnings("unchecked")
public JSONObject toJson() {
JSONObject json = new JSONObject();
json.put("hasAudio", this.hasAudio);
if (hasAudio)
json.put("audioActive", this.audioActive);
json.put("hasVideo", this.hasVideo);
if (hasVideo)
json.put("videoActive", this.videoActive);
if (this.hasVideo && this.videoActive) {
json.put("typeOfVideo", this.typeOfVideo);
json.put("frameRate", this.frameRate);
json.put("videoDimensions", this.videoDimensions);
}
return json;
}
public boolean hasAudio() {
return this.hasAudio;
}
@ -85,4 +68,20 @@ public class MediaOptions {
return this.videoDimensions;
}
@SuppressWarnings("unchecked")
public JSONObject toJSON() {
JSONObject json = new JSONObject();
json.put("hasAudio", this.hasAudio);
if (hasAudio)
json.put("audioActive", this.audioActive);
json.put("hasVideo", this.hasVideo);
if (hasVideo) {
json.put("videoActive", this.videoActive);
json.put("typeOfVideo", this.typeOfVideo);
json.put("frameRate", this.frameRate);
json.put("videoDimensions", this.videoDimensions);
}
return json;
}
}

View File

@ -193,20 +193,21 @@ public class KurentoParticipant extends Participant {
return session;
}
public boolean isSubscribed() {
public Set<SubscriberEndpoint> getAllConnectedSubscribedEndpoints() {
Set<SubscriberEndpoint> subscribedToSet = new HashSet<>();
for (SubscriberEndpoint se : subscribers.values()) {
if (se.isConnectedToPublisher()) {
return true;
subscribedToSet.add(se);
}
}
return false;
return subscribedToSet;
}
public Set<String> getConnectedSubscribedEndpoints() {
Set<String> subscribedToSet = new HashSet<String>();
public Set<SubscriberEndpoint> getConnectedSubscribedEndpoints(PublisherEndpoint publisher) {
Set<SubscriberEndpoint> subscribedToSet = new HashSet<>();
for (SubscriberEndpoint se : subscribers.values()) {
if (se.isConnectedToPublisher()) {
subscribedToSet.add(se.getEndpointName());
if (se.isConnectedToPublisher() && se.getPublisher().equals(publisher)) {
subscribedToSet.add(se);
}
}
return subscribedToSet;
@ -431,7 +432,6 @@ public class KurentoParticipant extends Participant {
*/
public SubscriberEndpoint getNewOrExistingSubscriber(String senderPublicId) {
KurentoParticipant kSender = (KurentoParticipant) this.session.getParticipantByPublicId(senderPublicId);
SubscriberEndpoint sendingEndpoint = new SubscriberEndpoint(webParticipant, this, senderPublicId, pipeline);
SubscriberEndpoint existingSendingEndpoint = this.subscribers.putIfAbsent(senderPublicId, sendingEndpoint);
@ -444,8 +444,6 @@ public class KurentoParticipant extends Participant {
senderPublicId);
}
sendingEndpoint.setMediaOptions(kSender.getPublisherMediaOptions());
return sendingEndpoint;
}
@ -645,7 +643,8 @@ public class KurentoParticipant extends Participant {
+ " | MEDIATYPE: " + event.getMediaType() + " | TIMESTAMP: " + System.currentTimeMillis();
endpoint.flowInMedia.put(event.getSource().getName(), event.getMediaType());
if (endpoint.getMediaOptions().hasAudio() && endpoint.getMediaOptions().hasVideo()
if (endpoint.getPublisher().getMediaOptions().hasAudio()
&& endpoint.getPublisher().getMediaOptions().hasVideo()
&& endpoint.flowInMedia.values().size() == 2) {
endpoint.kmsEvents.add(new KmsEvent(event));
} else if (endpoint.flowInMedia.values().size() == 1) {
@ -662,7 +661,8 @@ public class KurentoParticipant extends Participant {
+ " | MEDIATYPE: " + event.getMediaType() + " | TIMESTAMP: " + System.currentTimeMillis();
endpoint.flowOutMedia.put(event.getSource().getName(), event.getMediaType());
if (endpoint.getMediaOptions().hasAudio() && endpoint.getMediaOptions().hasVideo()
if (endpoint.getPublisher().getMediaOptions().hasAudio()
&& endpoint.getPublisher().getMediaOptions().hasVideo()
&& endpoint.flowOutMedia.values().size() == 2) {
endpoint.kmsEvents.add(new KmsEvent(event));
} else if (endpoint.flowOutMedia.values().size() == 1) {

View File

@ -24,6 +24,7 @@ import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
@ -43,6 +44,8 @@ import io.openvidu.java.client.SessionProperties;
import io.openvidu.server.cdr.CallDetailRecord;
import io.openvidu.server.core.Participant;
import io.openvidu.server.core.Session;
import io.openvidu.server.kurento.endpoint.PublisherEndpoint;
import io.openvidu.server.kurento.endpoint.SubscriberEndpoint;
/**
* @author Pablo Fuente (pablofuenteperez@gmail.com)
@ -74,8 +77,8 @@ public class KurentoSession implements Session {
private CallDetailRecord CDR;
public KurentoSession(String sessionId, SessionProperties sessionProperties, KurentoClient kurentoClient, KurentoSessionEventsHandler kurentoSessionHandler,
boolean destroyKurentoClient, CallDetailRecord CDR) {
public KurentoSession(String sessionId, SessionProperties sessionProperties, KurentoClient kurentoClient,
KurentoSessionEventsHandler kurentoSessionHandler, boolean destroyKurentoClient, CallDetailRecord CDR) {
this.sessionId = sessionId;
this.sessionProperties = sessionProperties;
this.kurentoClient = kurentoClient;
@ -100,7 +103,8 @@ public class KurentoSession implements Session {
checkClosed();
createPipeline();
KurentoParticipant kurentoParticipant = new KurentoParticipant(participant, this, getPipeline(), kurentoSessionHandler.getInfoHandler(), this.CDR);
KurentoParticipant kurentoParticipant = new KurentoParticipant(participant, this, getPipeline(),
kurentoSessionHandler.getInfoHandler(), this.CDR);
participants.put(participant.getParticipantPrivateId(), kurentoParticipant);
filterStates.forEach((filterId, state) -> {
@ -142,8 +146,8 @@ public class KurentoSession implements Session {
}
log.debug("SESSION {}: Unsubscribed other participants {} from the publisher {}", sessionId, participants.values(),
participant.getParticipantPublicId());
log.debug("SESSION {}: Unsubscribed other participants {} from the publisher {}", sessionId,
participants.values(), participant.getParticipantPublicId());
}
@ -154,8 +158,8 @@ public class KurentoSession implements Session {
KurentoParticipant participant = participants.get(participantPrivateId);
if (participant == null) {
throw new OpenViduException(Code.USER_NOT_FOUND_ERROR_CODE,
"Participant with private id " + participantPrivateId + " not found in session '" + sessionId + "'");
throw new OpenViduException(Code.USER_NOT_FOUND_ERROR_CODE, "Participant with private id "
+ participantPrivateId + " not found in session '" + sessionId + "'");
}
participant.releaseAllFilters();
@ -194,6 +198,11 @@ public class KurentoSession implements Session {
return null;
}
public Set<SubscriberEndpoint> getAllSubscribersForPublisher(PublisherEndpoint publisher) {
return this.participants.values().stream().flatMap(kp -> kp.getConnectedSubscribedEndpoints(publisher).stream())
.collect(Collectors.toSet());
}
@Override
public boolean close(String reason) {
if (!closed) {
@ -246,7 +255,8 @@ public class KurentoSession implements Session {
participants.remove(participant.getParticipantPrivateId());
log.debug("SESSION {}: Cancel receiving media from participant '{}' for other participant", this.sessionId, participant.getParticipantPublicId());
log.debug("SESSION {}: Cancel receiving media from participant '{}' for other participant", this.sessionId,
participant.getParticipantPublicId());
for (KurentoParticipant other : participants.values()) {
other.cancelReceivingMedia(participant.getParticipantPublicId(), reason);
}
@ -357,14 +367,20 @@ public class KurentoSession implements Session {
json.put("mediaMode", this.sessionProperties.mediaMode().name());
json.put("recordingMode", this.sessionProperties.recordingMode().name());
json.put("defaultRecordingLayout", this.sessionProperties.defaultRecordingLayout().name());
if (this.sessionProperties.defaultCustomLayout() != null && !this.sessionProperties.defaultCustomLayout().isEmpty()) {
if (this.sessionProperties.defaultCustomLayout() != null
&& !this.sessionProperties.defaultCustomLayout().isEmpty()) {
json.put("defaultCustomLayout", this.sessionProperties.defaultCustomLayout());
}
JSONArray participants = new JSONArray();
this.participants.values().forEach(p -> {
participants.add(p.toJSON());
});
json.put("connections", participants);
JSONObject connections = new JSONObject();
connections.put("count", participants.size());
connections.put("items", participants);
json.put("connections", connections);
return json;
}
@ -376,14 +392,20 @@ public class KurentoSession implements Session {
json.put("mediaMode", this.sessionProperties.mediaMode().name());
json.put("recordingMode", this.sessionProperties.recordingMode().name());
json.put("defaultRecordingLayout", this.sessionProperties.defaultRecordingLayout().name());
if (this.sessionProperties.defaultCustomLayout() != null && !this.sessionProperties.defaultCustomLayout().isEmpty()) {
if (this.sessionProperties.defaultCustomLayout() != null
&& !this.sessionProperties.defaultCustomLayout().isEmpty()) {
json.put("defaultCustomLayout", this.sessionProperties.defaultCustomLayout());
}
JSONArray participants = new JSONArray();
this.participants.values().forEach(p -> {
participants.add(p.withStatsToJSON());
});
json.put("connections", participants);
JSONObject connections = new JSONObject();
connections.put("count", participants.size());
connections.put("items", participants);
json.put("connections", connections);
return json;
}

View File

@ -18,6 +18,7 @@
package io.openvidu.server.kurento.endpoint;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
@ -43,7 +44,6 @@ import org.slf4j.LoggerFactory;
import io.openvidu.client.OpenViduException;
import io.openvidu.client.OpenViduException.Code;
import io.openvidu.server.core.MediaOptions;
import io.openvidu.server.core.Participant;
import io.openvidu.server.kurento.MutedMediaType;
import io.openvidu.server.kurento.core.KurentoParticipant;
@ -70,11 +70,11 @@ public abstract class MediaEndpoint {
private MediaPipeline pipeline = null;
private ListenerSubscription endpointSubscription = null;
private final List<IceCandidate> receivedCandidateList = new LinkedList<IceCandidate>();
private LinkedList<IceCandidate> candidates = new LinkedList<IceCandidate>();
private MutedMediaType muteType;
private MediaOptions mediaOptions;
public Map<String, MediaType> flowInMedia = new ConcurrentHashMap<>();
public Map<String, MediaType> flowOutMedia = new ConcurrentHashMap<>();
@ -104,14 +104,6 @@ public abstract class MediaEndpoint {
this.setMediaPipeline(pipeline);
}
public MediaOptions getMediaOptions() {
return mediaOptions;
}
public void setMediaOptions(MediaOptions mediaOptions) {
this.mediaOptions = mediaOptions;
}
public boolean isWeb() {
return web;
}
@ -490,6 +482,7 @@ public abstract class MediaEndpoint {
throw new OpenViduException(Code.MEDIA_WEBRTC_ENDPOINT_ERROR_CODE,
"Can't add existing ICE candidates to null WebRtcEndpoint (ep: " + endpointName + ")");
}
this.receivedCandidateList.add(candidate);
this.webEndpoint.addIceCandidate(candidate, new Continuation<Void>() {
@Override
public void onSuccess(Void result) throws Exception {
@ -503,18 +496,18 @@ public abstract class MediaEndpoint {
});
}
@SuppressWarnings("unchecked")
public abstract PublisherEndpoint getPublisher();
public JSONObject toJSON() {
JSONObject json = new JSONObject();
json.put("mediaOptions", this.mediaOptions);
return json;
}
@SuppressWarnings("unchecked")
public JSONObject withStatsToJSON() {
JSONObject json = new JSONObject();
json.put("mediaOptions", this.mediaOptions);
json.put("webrtcTagName", this.getEndpoint().getTag("name"));
json.put("receivedCandidates", this.receivedCandidateList);
json.put("localCandidate", this.selectedLocalIceCandidate);
json.put("remoteCandidate", this.selectedRemoteIceCandidate);

View File

@ -23,6 +23,7 @@ import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import org.json.simple.JSONObject;
import org.kurento.client.Continuation;
import org.kurento.client.ListenerSubscription;
import org.kurento.client.MediaElement;
@ -35,6 +36,7 @@ import org.slf4j.LoggerFactory;
import io.openvidu.client.OpenViduException;
import io.openvidu.client.OpenViduException.Code;
import io.openvidu.server.core.MediaOptions;
import io.openvidu.server.kurento.MutedMediaType;
import io.openvidu.server.kurento.core.KurentoParticipant;
@ -46,6 +48,8 @@ import io.openvidu.server.kurento.core.KurentoParticipant;
public class PublisherEndpoint extends MediaEndpoint {
private final static Logger log = LoggerFactory.getLogger(PublisherEndpoint.class);
protected MediaOptions mediaOptions;
private PassThrough passThru = null;
private ListenerSubscription passThruSubscription = null;
@ -462,4 +466,37 @@ public class PublisherEndpoint extends MediaEndpoint {
});
}
}
@Override
public PublisherEndpoint getPublisher() {
return this;
}
public MediaOptions getMediaOptions() {
return mediaOptions;
}
public void setMediaOptions(MediaOptions mediaOptions) {
this.mediaOptions = mediaOptions;
}
@SuppressWarnings("unchecked")
@Override
public JSONObject toJSON() {
JSONObject json = super.toJSON();
json.put("streamId", this.getEndpoint().getTag("name"));
json.put("mediaOptions", this.mediaOptions.toJSON());
return json;
}
@SuppressWarnings("unchecked")
@Override
public JSONObject withStatsToJSON() {
JSONObject json = super.withStatsToJSON();
JSONObject toJSON = this.toJSON();
for (Object key : toJSON.keySet()) {
json.put(key, toJSON.get(key));
}
return json;
}
}

View File

@ -17,6 +17,7 @@
package io.openvidu.server.kurento.endpoint;
import org.json.simple.JSONObject;
import org.kurento.client.MediaPipeline;
import org.kurento.client.MediaType;
import org.slf4j.Logger;
@ -32,65 +33,85 @@ import io.openvidu.server.kurento.core.KurentoParticipant;
* @author <a href="mailto:rvlad@naevatec.com">Radu Tom Vlad</a>
*/
public class SubscriberEndpoint extends MediaEndpoint {
private final static Logger log = LoggerFactory.getLogger(SubscriberEndpoint.class);
private final static Logger log = LoggerFactory.getLogger(SubscriberEndpoint.class);
private boolean connectedToPublisher = false;
private boolean connectedToPublisher = false;
private PublisherEndpoint publisher = null;
private PublisherEndpoint publisher = null;
public SubscriberEndpoint(boolean web, KurentoParticipant owner, String endpointName,
MediaPipeline pipeline) {
super(web, owner, endpointName, pipeline, log);
}
public SubscriberEndpoint(boolean web, KurentoParticipant owner, String endpointName, MediaPipeline pipeline) {
super(web, owner, endpointName, pipeline, log);
}
public synchronized String subscribe(String sdpOffer, PublisherEndpoint publisher) {
registerOnIceCandidateEventListener();
String sdpAnswer = processOffer(sdpOffer);
gatherCandidates();
publisher.connect(this.getEndpoint());
setConnectedToPublisher(true);
setPublisher(publisher);
return sdpAnswer;
}
public synchronized String subscribe(String sdpOffer, PublisherEndpoint publisher) {
registerOnIceCandidateEventListener();
String sdpAnswer = processOffer(sdpOffer);
gatherCandidates();
publisher.connect(this.getEndpoint());
setConnectedToPublisher(true);
setPublisher(publisher);
return sdpAnswer;
}
public boolean isConnectedToPublisher() {
return connectedToPublisher;
}
public boolean isConnectedToPublisher() {
return connectedToPublisher;
}
public void setConnectedToPublisher(boolean connectedToPublisher) {
this.connectedToPublisher = connectedToPublisher;
}
public void setConnectedToPublisher(boolean connectedToPublisher) {
this.connectedToPublisher = connectedToPublisher;
}
public PublisherEndpoint getPublisher() {
return publisher;
}
@Override
public PublisherEndpoint getPublisher() {
return this.publisher;
}
public void setPublisher(PublisherEndpoint publisher) {
this.publisher = publisher;
}
public void setPublisher(PublisherEndpoint publisher) {
this.publisher = publisher;
}
@Override
public synchronized void mute(io.openvidu.server.kurento.MutedMediaType muteType) {
if (this.publisher == null) {
throw new OpenViduException(Code.MEDIA_MUTE_ERROR_CODE, "Publisher endpoint not found");
}
switch (muteType) {
case ALL :
this.publisher.disconnectFrom(this.getEndpoint());
break;
case AUDIO :
this.publisher.disconnectFrom(this.getEndpoint(), MediaType.AUDIO);
break;
case VIDEO :
this.publisher.disconnectFrom(this.getEndpoint(), MediaType.VIDEO);
break;
}
resolveCurrentMuteType(muteType);
}
@Override
public synchronized void mute(io.openvidu.server.kurento.MutedMediaType muteType) {
if (this.publisher == null) {
throw new OpenViduException(Code.MEDIA_MUTE_ERROR_CODE, "Publisher endpoint not found");
}
switch (muteType) {
case ALL:
this.publisher.disconnectFrom(this.getEndpoint());
break;
case AUDIO:
this.publisher.disconnectFrom(this.getEndpoint(), MediaType.AUDIO);
break;
case VIDEO:
this.publisher.disconnectFrom(this.getEndpoint(), MediaType.VIDEO);
break;
}
resolveCurrentMuteType(muteType);
}
@Override
public synchronized void unmute() {
this.publisher.connect(this.getEndpoint());
setMuteType(null);
}
@Override
public synchronized void unmute() {
this.publisher.connect(this.getEndpoint());
setMuteType(null);
}
@SuppressWarnings("unchecked")
@Override
public JSONObject toJSON() {
JSONObject json = super.toJSON();
json.put("streamId", this.publisher.getEndpoint().getTag("name"));
json.put("publisher", this.publisher.getEndpointName());
return json;
}
@SuppressWarnings("unchecked")
@Override
public JSONObject withStatsToJSON() {
JSONObject json = super.withStatsToJSON();
JSONObject toJSON = this.toJSON();
for (Object key : toJSON.keySet()) {
json.put(key, toJSON.get(key));
}
return json;
}
}