Skip to content

Commit

Permalink
Merge pull request #21 from lunasaw/dev_1.1.3
Browse files Browse the repository at this point in the history
Dev 1.1.3
  • Loading branch information
lunasaw authored Dec 7, 2023
2 parents 2d1deb0 + edf9d94 commit b68724a
Show file tree
Hide file tree
Showing 16 changed files with 213 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.github.lunasaw.sip.common.enums.CmdTypeEnum;
import io.github.lunasaw.sip.common.subscribe.SubscribeInfo;
import io.github.lunasaw.sip.common.transmit.SipSender;
import io.github.lunasaw.sip.common.transmit.event.Event;

/**
* @author luna
Expand Down Expand Up @@ -56,12 +57,20 @@ public static String deviceAlarmNotify(FromDevice fromDevice, ToDevice toDevice,
* @return
*/
public static String deviceKeepLiveNotify(FromDevice fromDevice, ToDevice toDevice, String status) {
return deviceKeepLiveNotify(fromDevice, toDevice, status, null, null);
}

public static String deviceKeepLiveNotify(FromDevice fromDevice, ToDevice toDevice, String status, Event errorEvent) {
return deviceKeepLiveNotify(fromDevice, toDevice, status, errorEvent, null);
}

public static String deviceKeepLiveNotify(FromDevice fromDevice, ToDevice toDevice, String status, Event errorEvent, Event okEvent) {
DeviceKeepLiveNotify deviceKeepLiveNotify =
new DeviceKeepLiveNotify(CmdTypeEnum.KEEPALIVE.getType(), RandomStrUtil.getValidationCode(), fromDevice.getUserId());
new DeviceKeepLiveNotify(CmdTypeEnum.KEEPALIVE.getType(), RandomStrUtil.getValidationCode(), fromDevice.getUserId());

deviceKeepLiveNotify.setStatus(status);

return SipSender.doMessageRequest(fromDevice, toDevice, deviceKeepLiveNotify);
return SipSender.doMessageRequest(fromDevice, toDevice, deviceKeepLiveNotify, errorEvent, okEvent);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package io.github.lunasaw.gbproxy.server.transimit.request.message.notify;

import javax.sip.RequestEvent;
import javax.sip.message.Response;

import gov.nist.javax.sip.message.SIPRequest;
import io.github.lunasaw.sip.common.entity.FromDevice;
import io.github.lunasaw.sip.common.entity.RemoteAddressInfo;
import io.github.lunasaw.sip.common.entity.ToDevice;
import io.github.lunasaw.sip.common.entity.base.DeviceSession;
import io.github.lunasaw.sip.common.entity.notify.DeviceKeepLiveNotify;
import io.github.lunasaw.sip.common.transmit.ResponseCmd;
import io.github.lunasaw.sip.common.utils.SipUtils;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -47,7 +49,9 @@ public void handForEvt(RequestEvent event) {
// 设备查询
ToDevice toDevice = (ToDevice) messageProcessorServer.getToDevice(userId);
if (toDevice == null) {
// 未注册的设备不做处理
// 未注册的设备回复失败
log.warn("device not register, userId: {}", userId);
ResponseCmd.doResponseCmd(Response.NOT_FOUND, event);
return;
}
DeviceKeepLiveNotify deviceKeepLiveNotify = parseXml(DeviceKeepLiveNotify.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*/
@Getter
@Setter
@Component("serverRegisterRequestProcessor")
@Component
@Slf4j
public class ServerRegisterRequestProcessor extends SipRequestProcessorAbstract {

Expand All @@ -63,10 +63,9 @@ public void process(RequestEvent evt) {
boolean registerFlag = expires > 0;

String userId = SipUtils.getUserIdFromFromHeader(request);
String sipUserId = SipUtils.getUser(request);

FromDevice fromDevice = (FromDevice)registerProcessorServer.getFromDevice();
if (fromDevice == null || !sipUserId.equals(fromDevice.getUserId())) {
if (fromDevice == null) {
return;
}
// 设备接收到的IP地址,有可能是Nat之后的, 本地回复直接使用这个地址即可
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.luna.common.os.SystemInfoUtil;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -12,6 +13,7 @@
import io.github.lunasaw.sip.common.entity.Device;
import io.github.lunasaw.sip.common.entity.FromDevice;
import io.github.lunasaw.sip.common.entity.ToDevice;
import oshi.SystemInfo;

/**
* @author luna
Expand All @@ -20,7 +22,7 @@
@Configuration
public class DeviceConfig {

public static final String LOOP_IP = "10.143.118.70";
public static final String LOOP_IP = SystemInfoUtil.getIpv4();

public static final String LOOP_IP_LOCAL = "0.0.0.0";

Expand Down Expand Up @@ -48,11 +50,6 @@ public class DeviceConfig {

ToDevice serverTo = ToDevice.getInstance("33010602011187000001", LOOP_IP, 8118);
DEVICE_MAP.put("server_to", serverTo);


DEVICE_CLIENT_VIEW_MAP.put("41010500002000000001", clientTo);

DEVICE_SERVER_VIEW_MAP.put("33010602011187000001", serverTo);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
import io.github.lunasaw.sip.common.entity.Device;
import io.github.lunasaw.sip.common.entity.GbSessionDescription;
import io.github.lunasaw.sip.common.entity.SdpSessionDescription;
import io.github.lunasaw.sip.common.enums.InviteSessionNameEnum;
import io.github.lunasaw.sip.common.transmit.event.SipSubscribe;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import javax.sdp.SessionDescription;
import javax.sdp.SessionName;
import java.io.FileNotFoundException;

/**
Expand All @@ -28,8 +32,8 @@ public class DefaultInviteProcessorClient implements InviteProcessorClient {

static {
try {
VIDEO_FILE = ResourceUtils.getFile("classpath:device/videofile.h264").getAbsolutePath();
RECORD_VIDEO_FILE = ResourceUtils.getFile("classpath:device/record.h264").getAbsolutePath();
VIDEO_FILE = ResourceUtils.getFile("classpath:file/invite.mp4").getAbsolutePath();
RECORD_VIDEO_FILE = ResourceUtils.getFile("classpath:file/record.mp4").getAbsolutePath();
} catch (FileNotFoundException e) {

}
Expand All @@ -42,14 +46,28 @@ public class DefaultInviteProcessorClient implements InviteProcessorClient {
private FfmpegCommander ffmpegCommander;

@Override
@SneakyThrows
public void inviteSession(String callId, SdpSessionDescription sessionDescription) {
GbSessionDescription gbSessionDescription = (GbSessionDescription) sessionDescription;
log.info("点播请求 inviteSession::sessionDescription = {}", JSON.toJSONString(sessionDescription));
SessionDescription descriptionBaseSdb = gbSessionDescription.getBaseSdb();
SessionName sessionName = descriptionBaseSdb.getSessionName();

SipSubscribe.addOkSubscribe(callId, eventResult -> {
ffmpegCommander.closeAllStream();
ffmpegCommander.pushStream(eventResult.callId, VIDEO_FILE, gbSessionDescription.getAddress(), gbSessionDescription.getPort());
});
if (InviteSessionNameEnum.PLAY.getType().equals(sessionName.getValue())) {
log.info("点播请求 inviteSession::sessionDescription = {}", JSON.toJSONString(sessionDescription));

SipSubscribe.addOkSubscribe(callId, eventResult -> {
ffmpegCommander.closeAllStream();
ffmpegCommander.pushStream(eventResult.callId, VIDEO_FILE, gbSessionDescription.getAddress(), gbSessionDescription.getPort());
});
} else if (InviteSessionNameEnum.PLAY_BACK.getType().equals(sessionName.getValue())) {

log.info("回放请求 inviteSession::sessionDescription = {}", JSON.toJSONString(sessionDescription));

SipSubscribe.addOkSubscribe(callId, eventResult -> {
ffmpegCommander.closeAllStream();
ffmpegCommander.pushStream(eventResult.callId, RECORD_VIDEO_FILE, gbSessionDescription.getAddress(), gbSessionDescription.getPort());
});
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
Expand All @@ -20,11 +21,15 @@
* @author luna
* @date 2023/10/17
*/
@Slf4j
@Component
public class DefaultRegisterProcessorClient implements RegisterProcessorClient {

public static Boolean isRegister = true;
ScheduledExecutorService taskExecutor = Executors.newScheduledThreadPool(1);
/**
* 心跳定时任务线程池
*/
private static final ScheduledExecutorService taskExecutor = Executors.newScheduledThreadPool(1);
@Autowired
@Qualifier("clientFrom")
private Device fromDevice;
Expand All @@ -42,8 +47,13 @@ public void registerSuccess(String toUserId) {
if (!isRegister) {
return;
}
ClientSendCmd.deviceKeepLiveNotify((FromDevice) fromDevice, (ToDevice) DeviceConfig.DEVICE_CLIENT_VIEW_MAP.get(toUserId), "OK");
}, 60, 90, TimeUnit.SECONDS);
ClientSendCmd.deviceKeepLiveNotify((FromDevice)fromDevice, (ToDevice)DeviceConfig.DEVICE_CLIENT_VIEW_MAP.get(toUserId), "OK",
eventResult -> {
// 注册
log.error("心跳失败 发起注册 registerSuccess::toUserId = {} ", toUserId);
ClientSendCmd.deviceRegister((FromDevice)fromDevice, (ToDevice)DeviceConfig.DEVICE_CLIENT_VIEW_MAP.get(toUserId), 300);
});
}, 30, 60, TimeUnit.SECONDS);

if (!isRegister) {
// 注销
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class FfmpegCommander {

private static final String path = "/usr/local/bin/ffmpeg";
private static final String cmd =
"-re -i {filePath} -vcodec h264 -acodec aac -f rtsp -rtsp_transport tcp rtsp://{ip}:{port}/rtp/33010602011187000001_33010602011187000001?sign=41db35390ddad33f83944f44b8b75ded";
"-re -stream_loop -1 -i {filePath} -vcodec h264 -acodec aac -f rtsp -rtsp_transport tcp rtsp://{ip}:{port}/rtsp/live?sign=41db35390ddad33f83944f44b8b75ded";

private static final Map<String, Process> processMap = new ConcurrentHashMap<>();
private final Logger logger = LoggerFactory.getLogger(FfmpegCommander.class);

Expand Down
Binary file removed gb28181-test/src/main/resources/device/record.h264
Binary file not shown.
Binary file not shown.
Binary file added gb28181-test/src/main/resources/file/invite.mp4
Binary file not shown.
Binary file added gb28181-test/src/main/resources/file/record.mp4
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.github.lunasaw.gbproxy.test.invite;

import javax.sip.message.Request;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;

import io.github.lunasaw.gbproxy.client.transmit.cmd.ClientSendCmd;
import io.github.lunasaw.gbproxy.test.Gb28181ApplicationTest;
import io.github.lunasaw.gbproxy.test.config.DeviceConfig;
import io.github.lunasaw.gbproxy.test.user.client.DefaultRegisterProcessorClient;
import io.github.lunasaw.sip.common.entity.Device;
import io.github.lunasaw.sip.common.entity.FromDevice;
import io.github.lunasaw.sip.common.entity.ToDevice;
import io.github.lunasaw.sip.common.layer.SipLayer;
import io.github.lunasaw.sip.common.transmit.SipSender;
import io.github.lunasaw.sip.common.transmit.request.SipRequestProvider;
import io.github.lunasaw.sip.common.utils.SipRequestUtils;
import lombok.extern.slf4j.Slf4j;

/**
* @author luna
* @date 2023/10/12
*/
@Slf4j
@SpringBootTest(classes = Gb28181ApplicationTest.class)
public class ClientInviteTest {

@Autowired
@Qualifier("clientFrom")
private Device fromDevice;

@Autowired
@Qualifier("clientTo")
private Device toDevice;

@AfterAll
public static void after() {
while (true) {

}
}

@BeforeEach
public void before() {
// 本地端口监听
log.info("before::客户端初始化 fromDevice.ip : {} , fromDevice.port : {}", fromDevice.getIp(), fromDevice.getPort());
SipLayer.addListeningPoint(DeviceConfig.LOOP_IP, fromDevice.getPort());
// 模拟平台添加
DeviceConfig.DEVICE_CLIENT_VIEW_MAP.put(toDevice.getUserId(), toDevice);
}

@Test
public void test_register_client() {
String callId = SipRequestUtils.getNewCallId();
Request registerRequest = SipRequestProvider.createRegisterRequest((FromDevice)fromDevice, (ToDevice)toDevice, 300, callId);

SipSender.transmitRequest(fromDevice.getIp(), registerRequest);
}

@Test
public void b_test_un_register_client_custom() {
Device instance = DeviceConfig.DEVICE_CLIENT_VIEW_MAP.get("41010500002000000001");
DefaultRegisterProcessorClient.isRegister = false;
ClientSendCmd.deviceUnRegister((FromDevice)fromDevice, (ToDevice)instance);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package io.github.lunasaw.gbproxy.test.invite;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;

import io.github.lunasaw.gbproxy.server.transimit.cmd.ServerSendCmd;
import io.github.lunasaw.gbproxy.test.Gb28181ApplicationTest;
import io.github.lunasaw.gbproxy.test.config.DeviceConfig;
import io.github.lunasaw.sip.common.entity.Device;
import io.github.lunasaw.sip.common.entity.FromDevice;
import io.github.lunasaw.sip.common.entity.ToDevice;
import io.github.lunasaw.sip.common.layer.SipLayer;
import io.github.lunasaw.sip.common.utils.DynamicTask;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

/**
* @author luna
* @date 2023/10/12
*/
@Slf4j
@SpringBootTest(classes = Gb28181ApplicationTest.class)
public class ServerInviteTest {

@Autowired
@Qualifier("serverFrom")
private Device fromDevice;

@Autowired
private DynamicTask dynamicTask;

@BeforeEach
public void before() {
// 本地端口监听
SipLayer.addListeningPoint(DeviceConfig.LOOP_IP, 8117, true);
}

@Test
@SneakyThrows
public void test_invite_server() {
dynamicTask.startDelay("play_test", () -> {
Device device = DeviceConfig.DEVICE_SERVER_VIEW_MAP.get("33010602011187000001");
if (device == null) {
test_invite_server();
return;
}
String invitePlay = ServerSendCmd.deviceInvitePlay((FromDevice)fromDevice, (ToDevice)device, "127.0.0.1", 1554);
}, 10 * 1000);
}

@Test
@SneakyThrows
public void test_invite_play_back_server() {
dynamicTask.startDelay("play_back_test", () -> {
Device device = DeviceConfig.DEVICE_SERVER_VIEW_MAP.get("34020000001320000001");
if (device == null) {
test_invite_play_back_server();
return;
}
String invitePlay =
ServerSendCmd.deviceInvitePlayBack((FromDevice)fromDevice, (ToDevice)device, "127.0.0.1", 10000, "2023-11-29 00:00:00");
System.out.println(invitePlay);
}, 30 * 1000);
}

@SneakyThrows
@AfterEach
public void after() {
while (true) {

}
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring-boot.version>2.7.10</spring-boot.version>
<luna-common.version>2.4.8</luna-common.version>
<luna-common.version>2.5.2</luna-common.version>
<github.username>lunasaw</github.username>
<app.profiles>${project.name}</app.profiles>
<sip.version>1.3.0-91</sip.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class ThreadPoolTaskConfig {
/**
* 缓冲队列大小
*/
private static final int queueCapacity = 10000;
private static final int queueCapacity = 100;
/**
* 线程池名前缀
*/
Expand Down
Loading

0 comments on commit b68724a

Please sign in to comment.