openvidu-server: refactor unnecessary isOffer param

pull/621/head
pabloFuente 2021-03-12 10:16:17 +01:00
parent 6d5bca0762
commit 17ea55ad07
6 changed files with 179 additions and 198 deletions

View File

@ -24,7 +24,6 @@ import io.openvidu.server.kurento.endpoint.KurentoFilter;
public class KurentoMediaOptions extends MediaOptions {
public boolean isOffer;
public String sdpOffer;
public boolean doLoopback;
@ -34,21 +33,19 @@ public class KurentoMediaOptions extends MediaOptions {
public Boolean onlyPlayWithSubscribers;
public Integer networkCache;
public KurentoMediaOptions(boolean isOffer, String sdpOffer, Boolean hasAudio, Boolean hasVideo,
Boolean audioActive, Boolean videoActive, String typeOfVideo, Integer frameRate, String videoDimensions,
KurentoFilter filter, boolean doLoopback) {
public KurentoMediaOptions(String sdpOffer, Boolean hasAudio, Boolean hasVideo, Boolean audioActive,
Boolean videoActive, String typeOfVideo, Integer frameRate, String videoDimensions, KurentoFilter filter,
boolean doLoopback) {
super(hasAudio, hasVideo, audioActive, videoActive, typeOfVideo, frameRate, videoDimensions, filter);
this.isOffer = isOffer;
this.sdpOffer = sdpOffer;
this.doLoopback = doLoopback;
}
public KurentoMediaOptions(boolean isOffer, String sdpOffer, Boolean hasAudio, Boolean hasVideo,
Boolean audioActive, Boolean videoActive, String typeOfVideo, Integer frameRate, String videoDimensions,
KurentoFilter filter, boolean doLoopback, String rtspUri, Boolean adaptativeBitrate,
Boolean onlyPlayWithSubscribers, Integer networkCache) {
public KurentoMediaOptions(String sdpOffer, Boolean hasAudio, Boolean hasVideo, Boolean audioActive,
Boolean videoActive, String typeOfVideo, Integer frameRate, String videoDimensions, KurentoFilter filter,
boolean doLoopback, String rtspUri, Boolean adaptativeBitrate, Boolean onlyPlayWithSubscribers,
Integer networkCache) {
super(hasAudio, hasVideo, audioActive, videoActive, typeOfVideo, frameRate, videoDimensions, filter);
this.isOffer = isOffer;
this.sdpOffer = sdpOffer;
this.doLoopback = doLoopback;
this.rtspUri = rtspUri;
@ -61,7 +58,6 @@ public class KurentoMediaOptions extends MediaOptions {
String typeOfVideo, Integer frameRate, String videoDimensions, KurentoFilter filter,
KurentoMediaOptions streamProperties) {
super(hasAudio, hasVideo, audioActive, videoActive, typeOfVideo, frameRate, videoDimensions, filter);
this.isOffer = streamProperties.isOffer;
this.sdpOffer = streamProperties.sdpOffer;
this.doLoopback = streamProperties.doLoopback;
this.rtspUri = streamProperties.rtspUri;

View File

@ -55,7 +55,6 @@ import io.openvidu.server.core.MediaOptions;
import io.openvidu.server.core.Participant;
import io.openvidu.server.kurento.endpoint.MediaEndpoint;
import io.openvidu.server.kurento.endpoint.PublisherEndpoint;
import io.openvidu.server.kurento.endpoint.SdpType;
import io.openvidu.server.kurento.endpoint.SubscriberEndpoint;
import io.openvidu.server.recording.service.RecordingManager;
@ -172,15 +171,15 @@ public class KurentoParticipant extends Participant {
return session;
}
public String publishToRoom(SdpType sdpType, String sdpString, boolean doLoopback, boolean silent) {
log.info("PARTICIPANT {}: Request to publish video in room {} (sdp type {})", this.getParticipantPublicId(),
this.session.getSessionId(), sdpType);
log.trace("PARTICIPANT {}: Publishing Sdp ({}) is {}", this.getParticipantPublicId(), sdpType, sdpString);
public String publishToRoom(String sdpOffer, boolean doLoopback, boolean silent) {
log.info("PARTICIPANT {}: Request to publish video in room {})", this.getParticipantPublicId(),
this.session.getSessionId());
log.trace("PARTICIPANT {}: Publishing SDPOffer is {}", this.getParticipantPublicId(), sdpOffer);
String sdpResponse = this.getPublisher().publish(sdpType, sdpString, doLoopback);
String sdpResponse = this.getPublisher().publish(sdpOffer, doLoopback);
this.streaming = true;
log.trace("PARTICIPANT {}: Publishing Sdp ({}) is {}", this.getParticipantPublicId(), sdpType, sdpResponse);
log.trace("PARTICIPANT {}: Publishing Sdp is {}", this.getParticipantPublicId(), sdpResponse);
log.info("PARTICIPANT {}: Is now publishing video in room {}", this.getParticipantPublicId(),
this.session.getSessionId());

View File

@ -72,7 +72,6 @@ import io.openvidu.server.core.SessionManager;
import io.openvidu.server.core.Token;
import io.openvidu.server.kurento.endpoint.KurentoFilter;
import io.openvidu.server.kurento.endpoint.PublisherEndpoint;
import io.openvidu.server.kurento.endpoint.SdpType;
import io.openvidu.server.kurento.kms.Kms;
import io.openvidu.server.kurento.kms.KmsManager;
import io.openvidu.server.rpc.RpcHandler;
@ -380,7 +379,6 @@ public class KurentoSessionManager extends SessionManager {
KurentoMediaOptions kurentoOptions = (KurentoMediaOptions) mediaOptions;
KurentoParticipant kParticipant = (KurentoParticipant) participant;
KurentoSession kSession = kParticipant.getSession();
SdpType sdpType = kurentoOptions.isOffer ? SdpType.OFFER : SdpType.ANSWER;
boolean isTranscodingAllowed = kSession.getSessionProperties().isTranscodingAllowed();
VideoCodec forcedVideoCodec = kSession.getSessionProperties().forcedVideoCodec();
@ -391,15 +389,13 @@ public class KurentoSessionManager extends SessionManager {
// Modify sdp if forced codec is defined
if (forcedVideoCodec != VideoCodec.NONE && !participant.isIpcam()) {
kurentoOptions.sdpOffer = sdpMunging.forceCodec(participant, kurentoOptions.sdpOffer,
kurentoOptions.isOffer, kSession, true, false, isTranscodingAllowed, forcedVideoCodec);
kurentoOptions.sdpOffer = sdpMunging.forceCodec(participant, kurentoOptions.sdpOffer, kSession, true, false,
isTranscodingAllowed, forcedVideoCodec);
CDR.log(new WebrtcDebugEvent(participant, streamId, WebrtcDebugEventIssuer.client,
WebrtcDebugEventOperation.publish, WebrtcDebugEventType.sdpOfferMunged, kurentoOptions.sdpOffer));
}
log.debug(
"Request [PUBLISH_MEDIA] isOffer={} sdp={} "
+ "loopbackAltSrc={} lpbkConnType={} doLoopback={} rtspUri={} ({})",
kurentoOptions.isOffer, kurentoOptions.sdpOffer, kurentoOptions.doLoopback, kurentoOptions.rtspUri,
log.debug("Request [PUBLISH_MEDIA] sdpOffer={} loopbackAltSrc={} lpbkConnType={} doLoopback={} rtspUri={} ({})",
kurentoOptions.sdpOffer, kurentoOptions.doLoopback, kurentoOptions.rtspUri,
participant.getParticipantPublicId());
kParticipant.createPublishingEndpoint(mediaOptions, streamId);
@ -426,7 +422,7 @@ public class KurentoSessionManager extends SessionManager {
}
}
sdpAnswer = kParticipant.publishToRoom(sdpType, kurentoOptions.sdpOffer, kurentoOptions.doLoopback, false);
sdpAnswer = kParticipant.publishToRoom(kurentoOptions.sdpOffer, kurentoOptions.doLoopback, false);
if (sdpAnswer == null) {
OpenViduException e = new OpenViduException(Code.MEDIA_SDP_ERROR_CODE,
@ -580,8 +576,8 @@ public class KurentoSessionManager extends SessionManager {
// Modify sdp if forced codec is defined
if (forcedVideoCodec != VideoCodec.NONE && !participant.isIpcam()) {
sdpOffer = sdpMunging.forceCodec(participant, sdpOffer, true, session, false, false,
isTranscodingAllowed, forcedVideoCodec);
sdpOffer = sdpMunging.forceCodec(participant, sdpOffer, session, false, false, isTranscodingAllowed,
forcedVideoCodec);
CDR.log(new WebrtcDebugEvent(participant, subscriberEndpointName, WebrtcDebugEventIssuer.client,
WebrtcDebugEventOperation.subscribe, WebrtcDebugEventType.sdpOfferMunged, sdpOffer));
@ -788,8 +784,8 @@ public class KurentoSessionManager extends SessionManager {
boolean doLoopback = RpcHandler.getBooleanParam(request, ProtocolElements.PUBLISHVIDEO_DOLOOPBACK_PARAM);
return new KurentoMediaOptions(true, sdpOffer, hasAudio, hasVideo, audioActive, videoActive, typeOfVideo,
frameRate, videoDimensions, kurentoFilter, doLoopback);
return new KurentoMediaOptions(sdpOffer, hasAudio, hasVideo, audioActive, videoActive, typeOfVideo, frameRate,
videoDimensions, kurentoFilter, doLoopback);
}
@Override
@ -1123,8 +1119,8 @@ public class KurentoSessionManager extends SessionManager {
// Modify sdp if forced codec is defined
if (forcedVideoCodec != VideoCodec.NONE && !participant.isIpcam()) {
sdpOfferHasBeenMunged = true;
sdpOffer = sdpMunging.forceCodec(participant, sdpOffer, true, kSession, isPublisher, true,
isTranscodingAllowed, forcedVideoCodec);
sdpOffer = sdpMunging.forceCodec(participant, sdpOffer, kSession, isPublisher, true, isTranscodingAllowed,
forcedVideoCodec);
}
if (isPublisher) {
@ -1151,8 +1147,7 @@ public class KurentoSessionManager extends SessionManager {
// 3) Create a new PublisherEndpoint connecting it to the previous PassThrough
kParticipant.resetPublisherEndpoint(kurentoOptions, passThru);
kParticipant.createPublishingEndpoint(kurentoOptions, streamId);
SdpType sdpType = kurentoOptions.isOffer ? SdpType.OFFER : SdpType.ANSWER;
String sdpAnswer = kParticipant.publishToRoom(sdpType, sdpOffer, kurentoOptions.doLoopback, true);
String sdpAnswer = kParticipant.publishToRoom(sdpOffer, kurentoOptions.doLoopback, true);
log.debug("SDP Answer for publishing reconnection PARTICIPANT {}: {}", participant.getParticipantPublicId(),
sdpAnswer);

View File

@ -181,16 +181,14 @@ public class PublisherEndpoint extends MediaEndpoint {
* itself (after applying the intermediate media elements and the
* {@link PassThrough}) to allow loopback of the media stream.
*
* @param sdpType indicates the type of the sdpString (offer or
* answer)
* @param sdpString offer or answer from the remote peer
* @param sdpOffer offer from the remote peer
* @param doLoopback loopback flag
* @param loopbackAlternativeSrc alternative loopback source
* @param loopbackConnectionType how to connect the loopback source
* @return the SDP response (the answer if processing an offer SDP, otherwise is
* the updated offer generated previously by this endpoint)
*/
public synchronized String publish(SdpType sdpType, String sdpString, boolean doLoopback) {
public synchronized String publish(String sdpOffer, boolean doLoopback) {
registerOnIceCandidateEventListener(this.getOwner().getParticipantPublicId());
if (doLoopback) {
connect(this.getEndpoint());
@ -198,21 +196,7 @@ public class PublisherEndpoint extends MediaEndpoint {
innerConnect();
}
this.createdAt = System.currentTimeMillis();
String sdpResponse = null;
switch (sdpType) {
case ANSWER:
/** THIS IS CURRENTLY NEVER CALLED **/
sdpResponse = processAnswer(sdpString);
/** THIS IS CURRENTLY NEVER CALLED **/
break;
case OFFER:
sdpResponse = processOffer(sdpString);
break;
default:
throw new OpenViduException(Code.MEDIA_SDP_ERROR_CODE, "Sdp type not supported: " + sdpType);
}
String sdpResponse = processOffer(sdpOffer);
gatherCandidates();
return sdpResponse;
}

View File

@ -686,8 +686,8 @@ public class SessionRestController {
String typeOfVideo = ConnectionType.IPCAM.name();
Integer frameRate = null;
String videoDimensions = null;
KurentoMediaOptions mediaOptions = new KurentoMediaOptions(true, null, hasAudio, hasVideo, audioActive,
videoActive, typeOfVideo, frameRate, videoDimensions, null, false, connectionProperties.getRtspUri(),
KurentoMediaOptions mediaOptions = new KurentoMediaOptions(null, hasAudio, hasVideo, audioActive, videoActive,
typeOfVideo, frameRate, videoDimensions, null, false, connectionProperties.getRtspUri(),
connectionProperties.adaptativeBitrate(), connectionProperties.onlyPlayWithSubscribers(),
connectionProperties.getNetworkCache());

View File

@ -16,32 +16,34 @@
*/
package io.openvidu.server.utils;
import io.openvidu.client.OpenViduException;
import io.openvidu.java.client.VideoCodec;
import io.openvidu.client.OpenViduException.Code;
import io.openvidu.server.core.Participant;
import io.openvidu.server.core.Session;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import io.openvidu.client.OpenViduException;
import io.openvidu.client.OpenViduException.Code;
import io.openvidu.java.client.VideoCodec;
import io.openvidu.server.core.Participant;
import io.openvidu.server.core.Session;
public class SDPMunging {
private static final Logger log = LoggerFactory.getLogger(SDPMunging.class);
private Set<VideoCodec> supportedVideoCodecs = new HashSet<>(Arrays.asList(
VideoCodec.VP8,
VideoCodec.H264
));
private Set<VideoCodec> supportedVideoCodecs = new HashSet<>(Arrays.asList(VideoCodec.VP8, VideoCodec.H264));
/**
* `codec` is a uppercase SDP-style codec name: "VP8", "H264".
*
* This looks for all video m-sections (lines starting with "m=video"),
* then searches all of its related PayloadTypes trying to find those which
* This looks for all video m-sections (lines starting with "m=video"), then
* searches all of its related PayloadTypes trying to find those which
* correspond to the preferred codec. If any is found, they are moved to the
* front of the PayloadTypes list in the m= line, without removing the other
* codecs that might be present.
@ -52,10 +54,10 @@ public class SDPMunging {
* 6.1 "Unicast Streams" allows the answerer to list media formats in a
* different order of preference from what it got in the offer:
*
* > Although the answerer MAY list the formats in their desired order of
* > preference, it is RECOMMENDED that unless there is a specific reason,
* > the answerer list formats in the same relative order they were
* > present in the offer.
* > Although the answerer MAY list the formats in their desired order of >
* preference, it is RECOMMENDED that unless there is a specific reason, > the
* answerer list formats in the same relative order they were > present in the
* offer.
*
* Here we have a specific reason, thus we use this allowance to change the
* ordering of formats. Browsers (tested with Chrome 84) honor this change and
@ -116,7 +118,8 @@ public class SDPMunging {
}
if (codecPts.isEmpty()) {
throw new OpenViduException(Code.FORCED_CODEC_NOT_FOUND_IN_SDPOFFER, "The specified forced codec " + codecStr + " is not present in the SDP");
throw new OpenViduException(Code.FORCED_CODEC_NOT_FOUND_IN_SDPOFFER,
"The specified forced codec " + codecStr + " is not present in the SDP");
}
// Build a new m= line where any PayloadTypes found have been moved
@ -150,27 +153,31 @@ public class SDPMunging {
/**
* Return a SDP modified to force a specific codec
*/
public String forceCodec(Participant participant, String sdp, boolean isOffer, Session session, boolean isPublisher,
boolean isReconnecting, boolean isTranscodingAllowed, VideoCodec forcedVideoCodec) throws OpenViduException {
public String forceCodec(Participant participant, String sdp, Session session, boolean isPublisher,
boolean isReconnecting, boolean isTranscodingAllowed, VideoCodec forcedVideoCodec)
throws OpenViduException {
try {
if (supportedVideoCodecs.contains(forcedVideoCodec)) {
String mungedSdpOffer;
log.debug("PARTICIPANT '{}' in Session '{}'. Is Publisher: '{}'. Is Subscriber: '{}'."
+ " Is Offer SDP: '{}'. Is Answer SDP: '{}'. Is Reconnecting '{}'."
+ " SDP before munging: \n {}", participant.getParticipantPublicId(),
session.getSessionId(), isPublisher, !isPublisher, isOffer, !isOffer, isReconnecting, sdp);
log.debug(
"PARTICIPANT '{}' in Session '{}'. Is Publisher: '{}'. Is Subscriber: '{}'. Is Reconnecting '{}'."
+ " SDP before munging: \n {}",
participant.getParticipantPublicId(), session.getSessionId(), isPublisher, !isPublisher,
isReconnecting, sdp);
mungedSdpOffer = this.setCodecPreference(forcedVideoCodec, sdp);
log.debug("PARTICIPANT '{}' in Session '{}'. Is Publisher: '{}'. Is Subscriber: '{}'."
+ " Is Offer SDP: '{}'. Is Answer SDP: '{}'. Is Reconnecting '{}'."
+ " SDP after munging: \n {}", participant.getParticipantPublicId(),
session.getSessionId(), isPublisher, !isPublisher, isOffer, !isOffer, isReconnecting, mungedSdpOffer);
log.debug(
"PARTICIPANT '{}' in Session '{}'. Is Publisher: '{}'. Is Subscriber: '{}'."
+ " Is Reconnecting '{}'." + " SDP after munging: \n {}",
participant.getParticipantPublicId(), session.getSessionId(), isPublisher, !isPublisher,
isReconnecting, mungedSdpOffer);
return mungedSdpOffer;
} else {
throw new OpenViduException(Code.FORCED_CODEC_NOT_FOUND_IN_SDPOFFER, "Codec not supported by Media Server");
throw new OpenViduException(Code.FORCED_CODEC_NOT_FOUND_IN_SDPOFFER,
"Codec not supported by Media Server");
}
} catch (OpenViduException e) {
@ -178,17 +185,17 @@ public class SDPMunging {
String errorMessage = "Error forcing codec: '" + forcedVideoCodec + "', for PARTICIPANT: '"
+ participant.getParticipantPublicId() + "' in Session: '" + session.getSessionId()
+ "'. Is publishing: '" + isPublisher + "'. Is Subscriber: '" + !isPublisher
+ "'. Is Offer: '" + isOffer + "'. Is Answer: '" + !isOffer + "'. Is Reconnecting: '"
+ isReconnecting + "'.\nException: " + e.getMessage() + "\nSDP:\n" + sdp;
+ "'. Is Reconnecting: '" + isReconnecting + "'.\nException: " + e.getMessage() + "\nSDP:\n" + sdp;
if(!isTranscodingAllowed) {
if (!isTranscodingAllowed) {
throw new OpenViduException(Code.FORCED_CODEC_NOT_FOUND_IN_SDPOFFER, errorMessage);
}
log.info("Codec: '{}' is not supported for PARTICIPANT: '{}' in Session: '{}'. Is publishing: '{}'. "
+ "Is Subscriber: '{}' Is Offer SDP: '{}'. Is Answer SDP: '{}'. Is Reconnecting: '{}'."
+ " Transcoding will be allowed", forcedVideoCodec, participant.getParticipantPublicId(),
session.getSessionId(), isPublisher, !isPublisher, isOffer, !isOffer, isReconnecting);
log.info(
"Codec: '{}' is not supported for PARTICIPANT: '{}' in Session: '{}'. Is publishing: '{}'. "
+ "Is Subscriber: '{}'. Is Reconnecting: '{}'." + " Transcoding will be allowed",
forcedVideoCodec, participant.getParticipantPublicId(), session.getSessionId(), isPublisher,
!isPublisher, isReconnecting);
return sdp;
}