整合 closeStream 方法

This commit is contained in:
shikong 2023-09-02 17:54:16 +08:00
parent 0420227ccc
commit 67b90c7341

View File

@ -71,8 +71,47 @@ public class PlayService {
return result;
}
private int openRtpServer(DeferredResult<JsonResponse<String>> result, String streamId, int streamMode) {
GetRtpInfoResp rtpInfo = zlmMediaService.getRtpInfo(streamId);
if (rtpInfo.getExist()) {
result.setResult(JsonResponse.error(MessageFormat.format("流 {0} 已存在", streamId)));
return -1;
}
OpenRtpServer openRtpServer = new OpenRtpServer();
openRtpServer.setPort(0);
openRtpServer.setStreamId(streamId);
openRtpServer.setTcpMode(streamMode);
OpenRtpServerResp openRtpServerResp = zlmMediaService.openRtpServer(openRtpServer);
log.info("openRtpServerResp => {}", openRtpServerResp);
if (!openRtpServerResp.getCode().equals(ResponseStatus.Success)) {
result.setResult(JsonResponse.error(openRtpServerResp.getCode().getMsg()));
return -1;
}
return openRtpServerResp.getPort();
}
@SneakyThrows
private JsonResponse<Void> closeStream(String streamId, MediaSdpHelper.Action action, DockingDevice device, String channelId) {
zlmMediaService.closeRtpServer(new CloseRtpServer(streamId));
String key = CacheUtil.getKey(action.getAction(), device.getDeviceId(), channelId);
SipTransactionInfo transactionInfo = JsonUtils.parse(RedisUtil.StringOps.get(key), SipTransactionInfo.class);
if (transactionInfo == null) {
return JsonResponse.error("未找到连接信息");
}
Request request = SipRequestBuilder.createByeRequest(device, channelId, transactionInfo);
String senderIp = device.getLocalIp();
sender.send(senderIp, request);
String ssrc = transactionInfo.getSsrc();
ssrcService.releaseSsrc(zlmMediaConfig.getId(), ssrc);
RedisUtil.KeyOps.delete(key);
return JsonResponse.success(null);
}
/**
* 实时视频点播
*
* @param deviceId 设备id
* @param channelId 通道id
*/
@ -91,26 +130,14 @@ public class PlayService {
return result;
}
GetRtpInfoResp rtpInfo = zlmMediaService.getRtpInfo(streamId);
if(rtpInfo.getExist()){
result.setResult(JsonResponse.error(MessageFormat.format("流 {0} 已存在", streamId)));
return result;
}
int streamMode = device.getStreamMode() == null || device.getStreamMode().equalsIgnoreCase(ListeningPoint.UDP) ? 0 : 1;
OpenRtpServer openRtpServer = new OpenRtpServer();
openRtpServer.setPort(0);
openRtpServer.setStreamId(streamId);
openRtpServer.setTcpMode(streamMode);
OpenRtpServerResp openRtpServerResp = zlmMediaService.openRtpServer(openRtpServer);
log.info("openRtpServerResp => {}", openRtpServerResp);
if(!openRtpServerResp.getCode().equals(ResponseStatus.Success)){
result.setResult(JsonResponse.error(openRtpServerResp.getCode().getMsg()));
String ip = zlmMediaConfig.getIp();
int port = openRtpServer(result, streamId, streamMode);
if (result.hasResult()) {
return result;
}
String ip = zlmMediaConfig.getIp();
int port = openRtpServerResp.getPort();
String ssrc = ssrcService.getPlaySsrc();
GB28181Description description = MediaSdpHelper.play(deviceId, channelId, Connection.IP4, ip, port, ssrc, StreamMode.of(device.getStreamMode()));
@ -123,6 +150,7 @@ public class PlayService {
subscribe.getInviteSubscribe().addPublisher(subscribeKey);
Flow.Subscriber<SIPResponse> subscriber = new Flow.Subscriber<>() {
private Flow.Subscription subscription;
@Override
public void onSubscribe(Flow.Subscription subscription) {
this.subscription = subscription;
@ -179,20 +207,7 @@ public class PlayService {
}
String streamId = MediaSdpHelper.getStreamId(deviceId, channelId);
String key = CacheUtil.getKey(MediaSdpHelper.Action.PLAY.getAction(), deviceId, channelId);
zlmMediaService.closeRtpServer(new CloseRtpServer(streamId));
SipTransactionInfo transactionInfo = JsonUtils.parse(RedisUtil.StringOps.get(key), SipTransactionInfo.class);
if(transactionInfo == null){
return JsonResponse.error("未找到连接信息");
}
Request request = SipRequestBuilder.createByeRequest(device, channelId, transactionInfo);
String senderIp = device.getLocalIp();
sender.send(senderIp, request);
String ssrc = transactionInfo.getSsrc();
ssrcService.releaseSsrc(zlmMediaConfig.getId(),ssrc);
RedisUtil.KeyOps.delete(key);
return JsonResponse.success(null);
return closeStream(streamId, MediaSdpHelper.Action.PLAY, device, channelId);
}
@SneakyThrows
@ -212,26 +227,14 @@ public class PlayService {
return result;
}
GetRtpInfoResp rtpInfo = zlmMediaService.getRtpInfo(streamId);
if(rtpInfo.getExist()){
result.setResult(JsonResponse.error(MessageFormat.format("流 {0} 已存在", streamId)));
return result;
}
int streamMode = device.getStreamMode() == null || device.getStreamMode().equalsIgnoreCase(ListeningPoint.UDP) ? 0 : 1;
OpenRtpServer openRtpServer = new OpenRtpServer();
openRtpServer.setPort(0);
openRtpServer.setStreamId(streamId);
openRtpServer.setTcpMode(streamMode);
OpenRtpServerResp openRtpServerResp = zlmMediaService.openRtpServer(openRtpServer);
log.info("openRtpServerResp => {}", openRtpServerResp);
if(!openRtpServerResp.getCode().equals(ResponseStatus.Success)){
result.setResult(JsonResponse.error(openRtpServerResp.getCode().getMsg()));
String ip = zlmMediaConfig.getIp();
int port = openRtpServer(result, streamId, streamMode);
if (result.hasResult()) {
return result;
}
String ip = zlmMediaConfig.getIp();
int port = openRtpServerResp.getPort();
String ssrc = ssrcService.getPlaySsrc();
GB28181Description description = MediaSdpHelper.playback(deviceId, channelId, Connection.IP4, ip, port, ssrc, StreamMode.of(device.getStreamMode()), startTime, endTime);
@ -245,6 +248,7 @@ public class PlayService {
subscribe.getInviteSubscribe().addPublisher(subscribeKey);
Flow.Subscriber<SIPResponse> subscriber = new Flow.Subscriber<>() {
private Flow.Subscription subscription;
@Override
public void onSubscribe(Flow.Subscription subscription) {
this.subscription = subscription;
@ -303,20 +307,7 @@ public class PlayService {
long start = startTime.toInstant().getEpochSecond();
long end = endTime.toInstant().getEpochSecond();
String streamId = MediaSdpHelper.getStreamId(deviceId, channelId, String.valueOf(start), String.valueOf(end));
String key = CacheUtil.getKey(MediaSdpHelper.Action.PLAY_BACK.getAction(), deviceId, channelId);
zlmMediaService.closeRtpServer(new CloseRtpServer(streamId));
SipTransactionInfo transactionInfo = JsonUtils.parse(RedisUtil.StringOps.get(key), SipTransactionInfo.class);
if(transactionInfo == null){
return JsonResponse.error("未找到连接信息");
}
Request request = SipRequestBuilder.createByeRequest(device, channelId, transactionInfo);
String senderIp = device.getLocalIp();
sender.send(senderIp, request);
String ssrc = transactionInfo.getSsrc();
ssrcService.releaseSsrc(zlmMediaConfig.getId(),ssrc);
RedisUtil.KeyOps.delete(key);
return JsonResponse.success(null);
return closeStream(streamId, MediaSdpHelper.Action.PLAY_BACK, device, channelId);
}
}