|
|
@@ -109,7 +109,12 @@ public class PlayServiceImpl implements IPlayService {
|
|
|
@EventListener
|
|
|
public void onApplicationEvent(MediaDepartureEvent event) {
|
|
|
if (!"rtp".equals(event.getApp())) return;
|
|
|
+ // The adapter owns adopted teardown; a departure hook must not enter channel-wide legacy stop.
|
|
|
+ SsrcTransaction departing = sessionManager.getSsrcTransactionByStream("rtp", event.getStream());
|
|
|
+ if (sessionManager.isAdopted(departing)) return;
|
|
|
InviteInfo info = inviteStreamService.getInviteInfoByStream(null, event.getStream());
|
|
|
+ if (info != null && info.getStream() != null && info.getStream().startsWith("managed_")
|
|
|
+ && (event.getMediaServer() == null || !java.util.Objects.equals(info.getMediaServerId(), event.getMediaServer().getId()))) return;
|
|
|
if (info != null && info.getStatus() == InviteSessionStatus.ok
|
|
|
&& info.getStreamInfo() != null && info.getSsrcInfo() != null) stop(info);
|
|
|
}
|
|
|
@@ -123,6 +128,8 @@ public class PlayServiceImpl implements IPlayService {
|
|
|
if (!"rtp".equals(event.getApp())) {
|
|
|
return;
|
|
|
}
|
|
|
+ // The managed namespace is never a legacy device/channel identifier.
|
|
|
+ if (event.getStream() == null || event.getStream().startsWith("managed_")) return;
|
|
|
if (videoLifecycleStore != null && videoLifecycleStore.isStreamCancelled(event.getStream())) {
|
|
|
log.info("[ZLM HOOK] 该流已被取消,不自动点播:{}", event.getStream());
|
|
|
return;
|
|
|
@@ -164,6 +171,361 @@ public class PlayServiceImpl implements IPlayService {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ /** Retains termination evidence independently of the legacy channel cache/ready TTL. */
|
|
|
+ private final java.util.Map<String, ManagedPlay> managedPlays = new java.util.concurrent.ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ private static final class ManagedPlay {
|
|
|
+ private final MediaServer node;
|
|
|
+ private final Device device;
|
|
|
+ private final DeviceChannel channel;
|
|
|
+ private final String stream;
|
|
|
+ private SSRCInfo ssrc;
|
|
|
+ private boolean inviteSent;
|
|
|
+ private boolean byeConfirmed;
|
|
|
+ private String confirmedCallId;
|
|
|
+ private boolean stopped;
|
|
|
+ private boolean answered;
|
|
|
+ private ManagedPlay(MediaServer node, Device device, DeviceChannel channel, String stream) {
|
|
|
+ this.node = node; this.device = device; this.channel = channel; this.stream = stream;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean managedCancelled(String stream) {
|
|
|
+ return videoLifecycleStore == null || videoLifecycleStore.isStreamCancelled(stream);
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean managedMatches(ManagedPlay play, MediaServer node, Device device, DeviceChannel channel) {
|
|
|
+ return java.util.Objects.equals(play.node.getId(), node.getId())
|
|
|
+ && java.util.Objects.equals(play.device.getDeviceId(), device.getDeviceId())
|
|
|
+ && java.util.Objects.equals(play.channel.getId(), channel.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public synchronized SSRCInfo playManaged(MediaServer node, String deviceId, String channelId, String stream,
|
|
|
+ ErrorCallback<StreamInfo> callback) {
|
|
|
+ if (node == null || node.getId() == null || !node.isRtpEnable() || stream == null
|
|
|
+ || !stream.matches("managed_[0-9a-fA-F]{32}") || managedCancelled(stream)) {
|
|
|
+ if (callback != null) callback.run(InviteErrorCode.FAIL.getCode(), "Managed generation cancelled or unsupported node/stream", null);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Device device = redisCatchStorage.getDevice(deviceId);
|
|
|
+ DeviceChannel channel = deviceChannelService.getOneForSource(deviceId, channelId);
|
|
|
+ if (device == null || channel == null) {
|
|
|
+ if (callback != null) callback.run(InviteErrorCode.FAIL.getCode(), "Managed device/channel missing", null);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // Never take over or stop a different stream, even when its media list is empty.
|
|
|
+ for (InviteInfo existing : inviteStreamService.getAllInviteInfo()) {
|
|
|
+ if (existing.getType() == InviteSessionType.PLAY && java.util.Objects.equals(channel.getId(), existing.getChannelId())
|
|
|
+ && !stream.equals(existing.getStream())) {
|
|
|
+ if (callback != null) callback.run(InviteErrorCode.FAIL.getCode(), "Channel already belongs to another stream", null);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ InviteInfo existing = inviteStreamService.getInviteInfo(InviteSessionType.PLAY, channel.getId(), stream);
|
|
|
+ if (existing != null) {
|
|
|
+ if (!node.getId().equals(existing.getMediaServerId()) || !deviceId.equals(existing.getDeviceId())) return null;
|
|
|
+ ManagedPlay retained = managedPlays.get(stream);
|
|
|
+ if (retained == null) {
|
|
|
+ retained = new ManagedPlay(node, device, channel, stream); retained.ssrc = existing.getSsrcInfo();
|
|
|
+ retained.inviteSent = true; managedPlays.put(stream, retained);
|
|
|
+ }
|
|
|
+ if (managedCancelled(stream)) return null;
|
|
|
+ if (existing.getStreamInfo() != null) {
|
|
|
+ if (callback != null) callback.run(InviteErrorCode.SUCCESS.getCode(), InviteErrorCode.SUCCESS.getMsg(), existing.getStreamInfo());
|
|
|
+ } else if (callback != null) {
|
|
|
+ inviteStreamService.once(InviteSessionType.PLAY, channel.getId(), stream, callback);
|
|
|
+ }
|
|
|
+ return existing.getSsrcInfo();
|
|
|
+ }
|
|
|
+ // Same UUID is single-use even after a failure or a cache eviction.
|
|
|
+ if (managedPlays.containsKey(stream)) return null;
|
|
|
+ ManagedPlay play = new ManagedPlay(node, device, channel, stream);
|
|
|
+ managedPlays.put(stream, play);
|
|
|
+ RTPServerParam param = new RTPServerParam(); param.setMediaServerItem(node); param.setStreamId(stream);
|
|
|
+ param.setSsrcCheck(device.isSsrcCheck()); param.setDisableAudio(!channel.isHasAudio());
|
|
|
+ param.setTcpMode("TCP-ACTIVE".equalsIgnoreCase(device.getStreamMode()) ? 2 : "TCP-PASSIVE".equalsIgnoreCase(device.getStreamMode()) ? 1 : 0);
|
|
|
+ try {
|
|
|
+ if (managedCancelled(stream)) return null;
|
|
|
+ play.ssrc = receiveRtpServerService.openRTPServer(param, (code, msg, result) -> managedArrival(play, callback, code, msg, result));
|
|
|
+ if (play.ssrc == null || play.ssrc.getPort() <= 0 || managedCancelled(stream)) {
|
|
|
+ failManaged(play, callback, InviteErrorCode.FAIL.getCode(), "Managed allocation failed/cancelled");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ InviteInfo invite = InviteInfo.getInviteInfo(deviceId, channel.getId(), stream, play.ssrc, node.getId(), node.getSdpIp(),
|
|
|
+ play.ssrc.getPort(), device.getStreamMode(), InviteSessionType.PLAY, InviteSessionStatus.ready, userSetting.getRecordSip());
|
|
|
+ inviteStreamService.updateInviteInfo(invite);
|
|
|
+ if (managedCancelled(stream)) {
|
|
|
+ failManaged(play, callback, InviteErrorCode.FAIL.getCode(), "Managed generation cancelled");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // Set BEFORE dispatch: timeout/throw does not prove that no INVITE left the process.
|
|
|
+ play.inviteSent = true;
|
|
|
+ cmder.playStreamCmd(node, play.ssrc, device, channel,
|
|
|
+ event -> managedInviteOk(play, callback, event),
|
|
|
+ event -> failManaged(play, callback, event.statusCode, event.msg), userSetting.getPlayTimeout().longValue());
|
|
|
+ return play.ssrc;
|
|
|
+ } catch (InvalidArgumentException | SipException | ParseException | RuntimeException ex) {
|
|
|
+ failManaged(play, callback, InviteErrorCode.ERROR_FOR_SIP_SENDING_FAILED.getCode(), ex.getMessage());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private synchronized void managedArrival(ManagedPlay play, ErrorCallback<StreamInfo> callback,
|
|
|
+ int code, String msg, OpenRTPServerResult result) {
|
|
|
+ if (result != null && result.getSsrcInfo() != null
|
|
|
+ && play.stream.equals(result.getSsrcInfo().getStream()) && "rtp".equals(result.getSsrcInfo().getApp())) {
|
|
|
+ play.ssrc = result.getSsrcInfo();
|
|
|
+ }
|
|
|
+ if (managedCancelled(play.stream) || code != InviteErrorCode.SUCCESS.getCode()) {
|
|
|
+ failManaged(play, callback, code == InviteErrorCode.SUCCESS.getCode() ? InviteErrorCode.FAIL.getCode() : code, msg);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (result == null || result.getHookData() == null || result.getHookData().getMediaServer() == null
|
|
|
+ || !play.node.getId().equals(result.getHookData().getMediaServer().getId())
|
|
|
+ || !play.stream.equals(result.getHookData().getStream())
|
|
|
+ || result.getHookData().getMediaInfo() == null
|
|
|
+ || !play.stream.equals(result.getHookData().getMediaInfo().getStream())
|
|
|
+ || !"rtp".equals(result.getHookData().getMediaInfo().getApp())) return;
|
|
|
+ StreamInfo info = onPublishHandlerForPlay(play.node, result.getHookData().getMediaInfo(), play.device, play.channel);
|
|
|
+ if (info == null || managedCancelled(play.stream)) {
|
|
|
+ failManaged(play, callback, InviteErrorCode.FAIL.getCode(), "Managed arrival no longer owned");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!play.answered) {
|
|
|
+ play.answered = true;
|
|
|
+ try { if (callback != null) callback.run(InviteErrorCode.SUCCESS.getCode(), InviteErrorCode.SUCCESS.getMsg(), info); }
|
|
|
+ finally { inviteStreamService.call(InviteSessionType.PLAY, play.channel.getId(), play.stream,
|
|
|
+ InviteErrorCode.SUCCESS.getCode(), InviteErrorCode.SUCCESS.getMsg(), info); }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private synchronized void managedInviteOk(ManagedPlay play, ErrorCallback<StreamInfo> callback, SipSubscribe.EventResult event) {
|
|
|
+ // SIPCommander has retained the exact late dialog for ACK/BYE, but never republishes it.
|
|
|
+ if (managedCancelled(play.stream)) {
|
|
|
+ stopManaged(play.node, play.device, play.channel, play.stream);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ InviteInfo invite = inviteStreamService.getInviteInfo(InviteSessionType.PLAY, play.channel.getId(), play.stream);
|
|
|
+ if (invite == null || !play.node.getId().equals(invite.getMediaServerId())) {
|
|
|
+ failManaged(play, callback, InviteErrorCode.FAIL.getCode(), "Managed invite no longer owned");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ ResponseEvent response = (ResponseEvent) event.event;
|
|
|
+ String sdp = new String(response.getResponse().getRawContent(), java.nio.charset.StandardCharsets.UTF_8);
|
|
|
+ String responseSsrc = SipUtils.getSsrcFromSdp(sdp);
|
|
|
+ if (responseSsrc != null && !responseSsrc.equals(play.ssrc.getSsrc()) && play.device.isSsrcCheck()) {
|
|
|
+ if (!Boolean.TRUE.equals(mediaServerService.updateRtpServerSSRC(play.node, play.stream, responseSsrc))) {
|
|
|
+ failManaged(play, callback, InviteErrorCode.ERROR_FOR_RESET_SSRC.getCode(), "Managed SSRC update failed");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (managedCancelled(play.stream)) { stopManaged(play.node, play.device, play.channel, play.stream); return; }
|
|
|
+ if ("TCP-ACTIVE".equalsIgnoreCase(play.device.getStreamMode())) {
|
|
|
+ String body = sdp.contains("y=") ? sdp.substring(0, sdp.indexOf("y=")) : sdp;
|
|
|
+ SessionDescription description = SdpFactory.getInstance().createSessionDescription(body);
|
|
|
+ int port = -1;
|
|
|
+ for (Object value : description.getMediaDescriptions(true)) {
|
|
|
+ Media media = ((MediaDescription)value).getMedia();
|
|
|
+ if (media.getMediaFormats(false).contains("96")) { port = media.getMediaPort(); break; }
|
|
|
+ }
|
|
|
+ if (port <= 0 || !Boolean.TRUE.equals(mediaServerService.connectRtpServer(play.node, description.getConnection().getAddress(), port, play.stream))) {
|
|
|
+ failManaged(play, callback, InviteErrorCode.FAIL.getCode(), "Managed TCP connect failed"); return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (managedCancelled(play.stream)) { stopManaged(play.node, play.device, play.channel, play.stream); return; }
|
|
|
+ // Arrival, not SIP 200, publishes StreamInfo. Only this precise Invite can be updated.
|
|
|
+ invite.setStatus(InviteSessionStatus.ok); inviteStreamService.updateInviteInfo(invite);
|
|
|
+ } catch (SdpException | RuntimeException ex) {
|
|
|
+ failManaged(play, callback, InviteErrorCode.ERROR_FOR_SDP_PARSING_EXCEPTIONS.getCode(), ex.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private synchronized void failManaged(ManagedPlay play, ErrorCallback<StreamInfo> callback, int code, String msg) {
|
|
|
+ try { stopManaged(play.node, play.device, play.channel, play.stream); }
|
|
|
+ finally {
|
|
|
+ if (!play.answered) {
|
|
|
+ play.answered = true;
|
|
|
+ try { if (callback != null) callback.run(code, msg, null); }
|
|
|
+ finally { inviteStreamService.call(InviteSessionType.PLAY, play.channel.getId(), play.stream, code, msg, null); }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public synchronized boolean stopManaged(MediaServer node, Device device, DeviceChannel channel, String stream) {
|
|
|
+ if (node == null || node.getId() == null || device == null || channel == null || stream == null
|
|
|
+ || !stream.matches("managed_[0-9a-fA-F]{32}")) return false;
|
|
|
+ ManagedPlay play = managedPlays.get(stream);
|
|
|
+ InviteInfo invite = inviteStreamService.getInviteInfo(InviteSessionType.PLAY, channel.getId(), stream);
|
|
|
+ SsrcTransaction transaction = sessionManager.getSsrcTransactionByStream("rtp", stream);
|
|
|
+ if ((play != null && !managedMatches(play, node, device, channel))
|
|
|
+ || (invite != null && (!node.getId().equals(invite.getMediaServerId()) || !device.getDeviceId().equals(invite.getDeviceId())))
|
|
|
+ || (transaction != null && (!node.getId().equals(transaction.getMediaServerId())
|
|
|
+ || !device.getDeviceId().equals(transaction.getDeviceId()) || !java.util.Objects.equals(channel.getId(), transaction.getChannelId())))) return false;
|
|
|
+ if (videoLifecycleStore == null) return false;
|
|
|
+ videoLifecycleStore.markStreamCancelled(stream);
|
|
|
+ try {
|
|
|
+ inviteStreamService.call(InviteSessionType.PLAY, channel.getId(), stream,
|
|
|
+ InviteErrorCode.FAIL.getCode(), "Managed generation cancelled", null);
|
|
|
+ } catch (RuntimeException ex) {
|
|
|
+ log.warn("[Managed callback close] {}: {}", stream, ex.getMessage());
|
|
|
+ }
|
|
|
+ if (play == null) {
|
|
|
+ // Missing transaction is NOT evidence that an INVITE was never sent.
|
|
|
+ if (invite == null && transaction == null) return false;
|
|
|
+ play = new ManagedPlay(node, device, channel, stream); play.inviteSent = true;
|
|
|
+ play.ssrc = invite == null ? new SSRCInfo(0, transaction.getSsrc(), "rtp", stream, null) : invite.getSsrcInfo();
|
|
|
+ managedPlays.put(stream, play);
|
|
|
+ }
|
|
|
+ if (play.stopped && transaction == null) return true;
|
|
|
+ final ManagedPlay owned = play;
|
|
|
+ if (transaction != null && owned.byeConfirmed && !java.util.Objects.equals(owned.confirmedCallId, transaction.getCallId())) {
|
|
|
+ owned.byeConfirmed = false;
|
|
|
+ owned.stopped = false;
|
|
|
+ }
|
|
|
+ boolean rtpClosed = false;
|
|
|
+ try {
|
|
|
+ if (transaction != null && !owned.byeConfirmed) {
|
|
|
+ String callId = transaction.getCallId();
|
|
|
+ cmder.streamByeCmd(device, channel.getDeviceId(), "rtp", stream, callId, event -> {
|
|
|
+ synchronized (PlayServiceImpl.this) {
|
|
|
+ if (event.statusCode >= 200 && event.statusCode < 300
|
|
|
+ && (event.callId == null || java.util.Objects.equals(callId, event.callId))) {
|
|
|
+ owned.byeConfirmed = true;
|
|
|
+ owned.confirmedCallId = callId;
|
|
|
+ sessionManager.removeManagedIfSame(transaction);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } catch (InvalidArgumentException | SipException | ParseException | SsrcTransactionNotFoundException | RuntimeException ex) {
|
|
|
+ log.warn("[Managed BYE] {}/{} remains pending: {}", node.getId(), stream, ex.getMessage());
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ SSRCInfo info = owned.ssrc == null ? new SSRCInfo(0, null, "rtp", stream, null) : owned.ssrc;
|
|
|
+ rtpClosed = receiveRtpServerService.closeManagedRTPServer(node, info);
|
|
|
+ } catch (RuntimeException ex) {
|
|
|
+ log.warn("[Managed close] {}/{} remains pending: {}", node.getId(), stream, ex.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (rtpClosed && (!owned.inviteSent || owned.byeConfirmed)) {
|
|
|
+ if (transaction != null && java.util.Objects.equals(owned.confirmedCallId, transaction.getCallId())) {
|
|
|
+ sessionManager.removeManagedIfSame(transaction);
|
|
|
+ }
|
|
|
+ inviteStreamService.removeInviteInfo(InviteSessionType.PLAY, channel.getId(), stream);
|
|
|
+ owned.stopped = true;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** Adoption is a capability for one observed dialog, NOT a reusable legacy stream name. */
|
|
|
+ private final java.util.Map<String, AdoptedPlay> adoptedPlays = new java.util.concurrent.ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ private static final class AdoptedPlay {
|
|
|
+ private final SsrcTransaction dialog;
|
|
|
+ private final SSRCInfo info;
|
|
|
+ private final String channelCode;
|
|
|
+ private boolean byeConfirmed;
|
|
|
+ private boolean cancelled;
|
|
|
+ private AdoptedPlay(SsrcTransaction current, SSRCInfo ssrc, String channelCode) {
|
|
|
+ this.channelCode = channelCode;
|
|
|
+ info = new SSRCInfo(ssrc.getPort(), ssrc.getSsrc(), ssrc.getApp(), ssrc.getStream(), ssrc.getTimeOutTaskKey());
|
|
|
+ dialog = new SsrcTransaction();
|
|
|
+ dialog.setApp(current.getApp()); dialog.setStream(current.getStream()); dialog.setCallId(current.getCallId());
|
|
|
+ dialog.setSsrc(current.getSsrc()); dialog.setMediaServerId(current.getMediaServerId());
|
|
|
+ dialog.setDeviceId(current.getDeviceId()); dialog.setChannelId(current.getChannelId()); dialog.setType(current.getType());
|
|
|
+ SipTransactionInfo sip = new SipTransactionInfo(), source = current.getSipTransactionInfo();
|
|
|
+ sip.setCallId(source.getCallId()); sip.setFromTag(source.getFromTag()); sip.setToTag(source.getToTag());
|
|
|
+ sip.setViaBranch(source.getViaBranch()); sip.setAsSender(source.isAsSender()); dialog.setSipTransactionInfo(sip);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean nonblank(String value) { return value != null && !value.isBlank(); }
|
|
|
+
|
|
|
+ private boolean adoptedIdentity(AdoptedPlay owned, MediaServer node, Device device, DeviceChannel channel, String stream) {
|
|
|
+ return owned != null && node != null && device != null && channel != null
|
|
|
+ && java.util.Objects.equals(owned.dialog.getMediaServerId(), node.getId())
|
|
|
+ && java.util.Objects.equals(owned.dialog.getDeviceId(), device.getDeviceId())
|
|
|
+ && java.util.Objects.equals(owned.dialog.getChannelId(), channel.getId())
|
|
|
+ && java.util.Objects.equals(owned.channelCode, channel.getDeviceId())
|
|
|
+ && java.util.Objects.equals(owned.dialog.getStream(), stream);
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean adoptedInvite(AdoptedPlay owned, InviteInfo invite) {
|
|
|
+ return invite != null && invite.getSsrcInfo() != null && invite.getType() == InviteSessionType.PLAY
|
|
|
+ && java.util.Objects.equals(owned.dialog.getMediaServerId(), invite.getMediaServerId())
|
|
|
+ && java.util.Objects.equals(owned.dialog.getDeviceId(), invite.getDeviceId())
|
|
|
+ && java.util.Objects.equals(owned.dialog.getChannelId(), invite.getChannelId())
|
|
|
+ && java.util.Objects.equals(owned.dialog.getStream(), invite.getStream())
|
|
|
+ && owned.info.equals(invite.getSsrcInfo());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public synchronized String adoptStream(MediaServer node, Device device, DeviceChannel channel, String stream, String handle) {
|
|
|
+ if (node == null || !nonblank(node.getId()) || !node.isRtpEnable() || device == null || channel == null
|
|
|
+ || !nonblank(stream) || stream.startsWith("managed_")) return null;
|
|
|
+ InviteInfo invite = inviteStreamService.getInviteInfo(InviteSessionType.PLAY, channel.getId(), stream);
|
|
|
+ SsrcTransaction current = sessionManager.getSsrcTransactionByStream("rtp", stream);
|
|
|
+ if (handle != null) {
|
|
|
+ AdoptedPlay owned = adoptedPlays.get(handle);
|
|
|
+ return adoptedIdentity(owned, node, device, channel, stream) && !owned.cancelled
|
|
|
+ && adoptedInvite(owned, invite) && SipInviteSessionManager.sameDialog(owned.dialog, current)
|
|
|
+ ? handle : null;
|
|
|
+ }
|
|
|
+ if (invite == null || invite.getSsrcInfo() == null || current == null || current.getType() != InviteSessionType.PLAY
|
|
|
+ || !nonblank(current.getCallId()) || !nonblank(current.getSsrc()) || !"rtp".equals(current.getApp())
|
|
|
+ || current.getSipTransactionInfo() == null
|
|
|
+ || !current.getCallId().equals(current.getSipTransactionInfo().getCallId())
|
|
|
+ || !nonblank(current.getSipTransactionInfo().getFromTag()) || !nonblank(current.getSipTransactionInfo().getToTag())
|
|
|
+ || !nonblank(invite.getSsrcInfo().getTimeOutTaskKey())
|
|
|
+ || !"rtp".equals(invite.getSsrcInfo().getApp()) || !stream.equals(invite.getSsrcInfo().getStream())
|
|
|
+ || !current.getSsrc().equals(invite.getSsrcInfo().getSsrc())) return null;
|
|
|
+ AdoptedPlay owned = new AdoptedPlay(current, invite.getSsrcInfo(), channel.getDeviceId());
|
|
|
+ if (!adoptedIdentity(owned, node, device, channel, stream) || !adoptedInvite(owned, invite)
|
|
|
+ || !sessionManager.retainAdopted(owned.dialog, owned.info)) return null;
|
|
|
+ String token = java.util.UUID.randomUUID().toString(); adoptedPlays.put(token, owned); return token;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public synchronized boolean stopAdopted(MediaServer node, Device device, DeviceChannel channel, String stream, String handle) {
|
|
|
+ AdoptedPlay owned = handle == null ? null : adoptedPlays.get(handle);
|
|
|
+ if (!adoptedIdentity(owned, node, device, channel, stream)) return false;
|
|
|
+ InviteInfo invite = inviteStreamService.getInviteInfo(InviteSessionType.PLAY, channel.getId(), stream);
|
|
|
+ SsrcTransaction current = sessionManager.getSsrcTransactionByStream("rtp", stream);
|
|
|
+ if ((invite != null && !adoptedInvite(owned, invite))
|
|
|
+ || (current != null && !SipInviteSessionManager.sameDialog(owned.dialog, current))
|
|
|
+ || (current == null && !owned.byeConfirmed)) return false;
|
|
|
+ owned.cancelled = true;
|
|
|
+ try {
|
|
|
+ if (!owned.byeConfirmed) {
|
|
|
+ // Explicit dialog overload: never invokes the legacy remove-before-send path.
|
|
|
+ cmder.streamByeCmd(device, owned.channelCode, owned.dialog.getSipTransactionInfo(), event -> {
|
|
|
+ synchronized (PlayServiceImpl.this) {
|
|
|
+ if (event.statusCode >= 200 && event.statusCode < 300
|
|
|
+ && owned.dialog.getCallId().equals(event.callId)) {
|
|
|
+ owned.byeConfirmed = true;
|
|
|
+ sessionManager.removeAdoptedIfSame(owned.dialog);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } catch (InvalidArgumentException | SipException | ParseException | SsrcTransactionNotFoundException | RuntimeException ex) {
|
|
|
+ log.warn("[Adopted BYE] {}/{} remains pending: {}", node.getId(), stream, ex.getMessage());
|
|
|
+ }
|
|
|
+ // A retained receipt can survive departure/Invite eviction; a different allocation cannot.
|
|
|
+ boolean closed = receiveRtpServerService.closeAdoptedRTPServer(node, owned.info, owned.dialog.getCallId());
|
|
|
+ current = sessionManager.getSsrcTransactionByStream("rtp", stream);
|
|
|
+ invite = inviteStreamService.getInviteInfo(InviteSessionType.PLAY, channel.getId(), stream);
|
|
|
+ if (!closed || !owned.byeConfirmed || (current != null && !SipInviteSessionManager.sameDialog(owned.dialog, current))
|
|
|
+ || (invite != null && !adoptedInvite(owned, invite))) return false;
|
|
|
+ sessionManager.removeAdoptedIfSame(owned.dialog);
|
|
|
+ if (invite != null) inviteStreamService.removeInviteInfo(InviteSessionType.PLAY, channel.getId(), stream);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public SSRCInfo play(MediaServer mediaServerItem, String deviceId, String channelId, String ssrc, ErrorCallback<StreamInfo> callback) {
|
|
|
if (mediaServerItem == null) {
|
|
|
@@ -405,7 +767,23 @@ public class PlayServiceImpl implements IPlayService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public StreamInfo onPublishHandlerForPlay(MediaServer mediaServerItem, MediaInfo mediaInfo, Device device, DeviceChannel channel) {
|
|
|
+ public synchronized StreamInfo onPublishHandlerForPlay(MediaServer mediaServerItem, MediaInfo mediaInfo, Device device, DeviceChannel channel) {
|
|
|
+ if (mediaInfo != null && mediaInfo.getStream() != null && mediaInfo.getStream().startsWith("managed_")) {
|
|
|
+ String stream = mediaInfo.getStream();
|
|
|
+ if (managedCancelled(stream) || mediaServerItem == null || !"rtp".equals(mediaInfo.getApp())
|
|
|
+ || (mediaInfo.getMediaServer() != null && !java.util.Objects.equals(mediaServerItem.getId(), mediaInfo.getMediaServer().getId()))) return null;
|
|
|
+ InviteInfo exact = inviteStreamService.getInviteInfo(InviteSessionType.PLAY, channel.getId(), stream);
|
|
|
+ if (exact == null || !java.util.Objects.equals(exact.getMediaServerId(), mediaServerItem.getId())
|
|
|
+ || !java.util.Objects.equals(exact.getDeviceId(), device.getDeviceId())) return null;
|
|
|
+ StreamInfo info = mediaServerService.getStreamInfoByAppAndStream(mediaServerItem, "rtp", stream, mediaInfo, null);
|
|
|
+ if (info == null || !stream.equals(info.getStream()) || info.getMediaServer() == null
|
|
|
+ || !mediaServerItem.getId().equals(info.getMediaServer().getId()) || managedCancelled(stream)) return null;
|
|
|
+ info.setDeviceId(device.getDeviceId()); info.setChannelId(channel.getId());
|
|
|
+ exact.setStatus(InviteSessionStatus.ok); exact.setStreamInfo(info);
|
|
|
+ inviteStreamService.updateInviteInfo(exact);
|
|
|
+ // Deliberately no channel-wide startPlay/stopPlay: the managed stream owns only its exact Invite.
|
|
|
+ return managedCancelled(stream) ? null : info;
|
|
|
+ }
|
|
|
StreamInfo streamInfo = null;
|
|
|
streamInfo = onPublishHandler(mediaServerItem, mediaInfo, device, channel);
|
|
|
if (streamInfo != null) {
|
|
|
@@ -975,6 +1353,13 @@ public class PlayServiceImpl implements IPlayService {
|
|
|
|
|
|
@Override
|
|
|
public void stop(InviteSessionType type, Device device, DeviceChannel channel, String stream) {
|
|
|
+ if (stream != null && stream.startsWith("managed_")) {
|
|
|
+ InviteInfo exact = inviteStreamService.getInviteInfo(type, channel.getId(), stream);
|
|
|
+ ManagedPlay retained = managedPlays.get(stream);
|
|
|
+ MediaServer node = retained != null ? retained.node : exact == null ? null : mediaServerService.getOne(exact.getMediaServerId());
|
|
|
+ stopManaged(node, device, channel, stream);
|
|
|
+ return;
|
|
|
+ }
|
|
|
InviteInfo inviteInfo = inviteStreamService.getInviteInfo(type, channel.getId(), stream);
|
|
|
if (inviteInfo == null) {
|
|
|
if (type == InviteSessionType.PLAY) {
|
|
|
@@ -1019,6 +1404,10 @@ public class PlayServiceImpl implements IPlayService {
|
|
|
log.warn("[停止点播] 发现设备不存在");
|
|
|
return;
|
|
|
}
|
|
|
+ if (inviteInfo.getStream() != null && inviteInfo.getStream().startsWith("managed_")) {
|
|
|
+ stopManaged(mediaServerService.getOne(inviteInfo.getMediaServerId()), device, channel, inviteInfo.getStream());
|
|
|
+ return;
|
|
|
+ }
|
|
|
inviteStreamService.removeInviteInfo(inviteInfo);
|
|
|
if (InviteSessionStatus.ok == inviteInfo.getStatus()) {
|
|
|
try {
|