[*] 日常优化

This commit is contained in:
acgist
2023-08-28 07:52:17 +08:00
parent be691965a6
commit 9d2c183fcc
9 changed files with 170 additions and 134 deletions

View File

@@ -994,7 +994,7 @@ public final class Taoyao implements ITaoyao {
final String roomId = MapUtils.get(body, "roomId"); final String roomId = MapUtils.get(body, "roomId");
final Room room = this.rooms.get(roomId); final Room room = this.rooms.get(roomId);
if(room == null) { if(room == null) {
Log.w(Taoyao.class.getSimpleName(), "无效房间:" + roomId); Log.w(Taoyao.class.getSimpleName(), "进入房间(无效房间" + roomId);
return; return;
} }
room.newRemoteClientFromRoomEnter(body); room.newRemoteClientFromRoomEnter(body);
@@ -1009,13 +1009,17 @@ public final class Taoyao implements ITaoyao {
* @return 房间 * @return 房间
*/ */
public Room roomEnter(String roomId, String password) { public Room roomEnter(String roomId, String password) {
if(this.rooms.containsKey(roomId)) {
Log.w(Taoyao.class.getSimpleName(), "终端已经进入房间:" + roomId);
return null;
}
final Resources resources = this.context.getResources(); final Resources resources = this.context.getResources();
final Room room = this.rooms.computeIfAbsent( final Room room = this.rooms.computeIfAbsent(
roomId, roomId,
key -> new Room( key -> new Room(
roomId, this.name, roomId, this.name,
password, this.clientId, password, this.clientId,
this, this.mainHandler, this, this.mainHandler,
resources.getBoolean(R.bool.preview), resources.getBoolean(R.bool.preview),
resources.getBoolean(R.bool.playAudio), resources.getBoolean(R.bool.playAudio),
resources.getBoolean(R.bool.playVideo), resources.getBoolean(R.bool.playVideo),
@@ -1035,7 +1039,7 @@ public final class Taoyao implements ITaoyao {
room.mediaProduce(); room.mediaProduce();
return room; return room;
} else { } else {
Log.i(Taoyao.class.getSimpleName(), "进入房间失败:" + roomId); Log.i(Taoyao.class.getSimpleName(), "终端进入房间失败:" + roomId);
this.rooms.remove(roomId); this.rooms.remove(roomId);
return null; return null;
} }
@@ -1073,7 +1077,7 @@ public final class Taoyao implements ITaoyao {
public void roomLeave(String roomId) { public void roomLeave(String roomId) {
final Room room = this.rooms.remove(roomId); final Room room = this.rooms.remove(roomId);
if(room == null) { if(room == null) {
Log.w(Taoyao.class.getSimpleName(), "无效房间:" + roomId); Log.w(Taoyao.class.getSimpleName(), "离开房间(无效房间" + roomId);
return; return;
} }
this.push(this.buildMessage( this.push(this.buildMessage(

View File

@@ -11,4 +11,4 @@ MEDIASOUP_MIN_PORT=40000
MEDIASOUP_MAX_PORT=49999 MEDIASOUP_MAX_PORT=49999
MEDIASOUP_LISTEN_IP=0.0.0.0 MEDIASOUP_LISTEN_IP=0.0.0.0
MEDIASOUP_LISTEN_PORT=44444 MEDIASOUP_LISTEN_PORT=44444
MEDIASOUP_ANNOUNCED_IP=192.168.8.244 MEDIASOUP_ANNOUNCED_IP=192.168.8.100

View File

@@ -9,6 +9,15 @@ import {
defaultRTCPeerConnectionConfig, defaultRTCPeerConnectionConfig,
} from "./Config.js"; } from "./Config.js";
/**
* 成功编码
*/
const SUCCESS_CODE = "0000";
/**
* 成功描述
*/
const SUCCESS_MESSAGE = "成功";
/** /**
* 信令协议 * 信令协议
*/ */
@@ -2333,64 +2342,6 @@ class Taoyao extends RemoteClient {
return response.body; return response.body;
} }
/**
* 进入房间信令
*
* @param {*} roomId 房间ID
* @param {*} password 房间密码
*/
async roomEnter(roomId, password) {
const me = this;
if(me.roomId) {
return {
code : 9999,
message: "已经进入房间",
};
}
me.roomId = roomId;
let response = await me.request(protocol.buildMessage("media::router::rtp::capabilities", {
roomId: me.roomId,
}));
if(response.code !== '0000') {
me.roomId = null;
this.callbackError(response.message);
return response;
}
const routerRtpCapabilities = response.body.rtpCapabilities;
me.mediasoupDevice = new mediasoupClient.Device();
await me.mediasoupDevice.load({ routerRtpCapabilities });
response = await me.request(protocol.buildMessage("room::enter", {
roomId : roomId,
password : password,
rtpCapabilities : me.audioConsume || me.videoConsume || me.audioProduce || me.videoProduce ? me.mediasoupDevice.rtpCapabilities : undefined,
sctpCapabilities: me.dataConsume || me.dataProduce ? me.mediasoupDevice.sctpCapabilities : undefined,
}));
if(response.code !== '0000') {
me.roomId = null;
this.callbackError(response.message);
return response;
}
return response;
}
/**
* 进入房间信令
*
* @param {*} message 信令消息
*/
defaultRoomEnter(message) {
const me = this;
const { roomId, clientId, status } = message.body;
if (clientId === me.clientId) {
// 忽略自己
} else if(me.remoteClients.has(clientId)) {
console.debug("房间已经存在远程终端", clientId);
} else {
console.debug("远程终端进入房间", clientId);
me.remoteClients.set(clientId, new RemoteClient(status));
}
}
/** /**
* 媒体回调 * 媒体回调
* *
@@ -2403,8 +2354,8 @@ class Taoyao extends RemoteClient {
clientId, clientId,
track, track,
}); });
callbackMessage.code = "0000"; callbackMessage.code = SUCCESS_CODE;
callbackMessage.message = "媒体回调"; callbackMessage.message = SUCCESS_MESSAGE;
me.callback(callbackMessage); me.callback(callbackMessage);
} }
@@ -2934,6 +2885,67 @@ class Taoyao extends RemoteClient {
} }
} }
/**
* 进入房间信令
*
* @param {*} roomId 房间ID
* @param {*} password 房间密码
*/
async roomEnter(roomId, password) {
if(this.roomId) {
this.callbackError("终端已经进入房间");
return {
code : 9999,
message: "终端已经进入房间",
};
}
this.roomId = roomId;
let response = await this.request(protocol.buildMessage("media::router::rtp::capabilities", {
roomId: this.roomId,
}));
if(response.code !== SUCCESS_CODE) {
this.roomId = null;
this.callbackError(response.message);
return response;
}
const routerRtpCapabilities = response.body.rtpCapabilities;
this.mediasoupDevice = new mediasoupClient.Device();
await this.mediasoupDevice.load({ routerRtpCapabilities });
response = await this.request(protocol.buildMessage("room::enter", {
roomId : roomId,
password : password,
rtpCapabilities : this.audioConsume || this.videoConsume || this.audioProduce || this.videoProduce ? this.mediasoupDevice.rtpCapabilities : undefined,
sctpCapabilities: this.dataConsume || this.dataProduce ? this.mediasoupDevice.sctpCapabilities : undefined,
}));
if(response.code !== SUCCESS_CODE) {
this.roomId = null;
this.callbackError(response.message);
return response;
}
return response;
}
/**
* 进入房间信令
*
* @param {*} message 信令消息
*/
defaultRoomEnter(message) {
const {
roomId,
clientId,
status
} = message.body;
if (clientId === this.clientId) {
// 忽略自己
} else if(this.remoteClients.has(clientId)) {
console.debug("终端已经进入房间", clientId);
} else {
console.debug("远程终端进入房间", clientId);
this.remoteClients.set(clientId, new RemoteClient(status));
}
}
/** /**
* 踢出房间信令 * 踢出房间信令
* *
@@ -3064,7 +3076,7 @@ class Taoyao extends RemoteClient {
body, body,
message message
} = response; } = response;
if(code !== "0000") { if(code !== SUCCESS_CODE) {
this.callbackError(message); this.callbackError(message);
return; return;
} }

View File

@@ -56,7 +56,7 @@ public class RoomEnterProtocol extends ProtocolRoomAdapter {
@Override @Override
public boolean authenticate(Message message) { public boolean authenticate(Message message) {
final Map<String, Object> body = message.body(); final Map<String, Object> body = message.body();
final String roomId = MapUtils.get(body, Constant.ROOM_ID); final String roomId = MapUtils.get(body, Constant.ROOM_ID);
final String password = MapUtils.get(body, Constant.PASSWORD); final String password = MapUtils.get(body, Constant.PASSWORD);
final Room room = this.roomManager.getRoom(roomId); final Room room = this.roomManager.getRoom(roomId);
if(room == null) { if(room == null) {
@@ -84,17 +84,17 @@ public class RoomEnterProtocol extends ProtocolRoomAdapter {
} }
/** /**
* 终端进入 * 终端进入房间
* *
* @param clientId 终端ID * @param clientId 终端ID
* @param room 房间 * @param room 房间
* @param client 终端 * @param client 终端
* @param message 消息 * @param message 信令消息
* @param body 消息主体 * @param body 消息主体
*/ */
private void enter(String clientId, Room room, Client client, Message message, Map<String, Object> body) { private void enter(String clientId, Room room, Client client, Message message, Map<String, Object> body) {
final String subscribeType = MapUtils.get(body, Constant.SUBSCRIBE_TYPE); final String subscribeType = MapUtils.get(body, Constant.SUBSCRIBE_TYPE);
final Object rtpCapabilities = MapUtils.get(body, Constant.RTP_CAPABILITIES); final Object rtpCapabilities = MapUtils.get(body, Constant.RTP_CAPABILITIES);
final Object sctpCapabilities = MapUtils.get(body, Constant.SCTP_CAPABILITIES); final Object sctpCapabilities = MapUtils.get(body, Constant.SCTP_CAPABILITIES);
// 进入房间 // 进入房间
final ClientWrapper clientWrapper = room.enter(client); final ClientWrapper clientWrapper = room.enter(client);
@@ -104,9 +104,9 @@ public class RoomEnterProtocol extends ProtocolRoomAdapter {
clientWrapper.setSctpCapabilities(sctpCapabilities); clientWrapper.setSctpCapabilities(sctpCapabilities);
// 发送通知 // 发送通知
message.setBody(Map.of( message.setBody(Map.of(
Constant.ROOM_ID, room.getRoomId(), Constant.ROOM_ID, room.getRoomId(),
Constant.CLIENT_ID, clientId, Constant.CLIENT_ID, clientId,
Constant.STATUS, client.getStatus() Constant.STATUS, client.getStatus()
)); ));
room.broadcast(message); room.broadcast(message);
// 进入房间事件 // 进入房间事件

View File

@@ -22,15 +22,19 @@ import com.acgist.taoyao.signal.protocol.ProtocolRoomAdapter;
*/ */
@Protocol @Protocol
@Description( @Description(
body = """ body = {
{ """
"roomId": "房间ID" {
} "roomId": "房间ID"
{ }
"roomId" : "房间ID" """,
"clientId": "离开终端ID" """
} {
""", "roomId" : "房间ID"
"clientId": "离开终端ID"
}
"""
},
flow = { flow = {
"终端->信令服务-)终端", "终端->信令服务-)终端",
"终端-[关闭终端]>信令服务-)终端", "终端-[关闭终端]>信令服务-)终端",

View File

@@ -16,20 +16,24 @@ import com.acgist.taoyao.signal.protocol.ProtocolClientAdapter;
*/ */
@Protocol @Protocol
@Description( @Description(
body = """ body = {
{ """
"roomId": "房间ID"
}
[
{ {
"name" : "房间名称", "roomId": "房间ID"
"passowrd" : "房间密码", }
"clientSize" : "终端数量", """,
"mediaClientId": "媒体服务标识" """
}, [
... {
] "name" : "房间名称",
""", "passowrd" : "房间密码",
"clientSize" : "终端数量",
"mediaClientId": "媒体服务标识"
},
...
]
"""
},
flow = "终端=>信令服务" flow = "终端=>信令服务"
) )
public class RoomListProtocol extends ProtocolClientAdapter { public class RoomListProtocol extends ProtocolClientAdapter {

View File

@@ -17,17 +17,21 @@ import com.acgist.taoyao.signal.protocol.ProtocolRoomAdapter;
*/ */
@Protocol @Protocol
@Description( @Description(
body = """ body = {
{ """
"roomId": "房间ID" {
} "roomId": "房间ID"
{ }
"name" : "房间名称", """,
"passowrd" : "房间密码", """
"clientSize" : "终端数量", {
"mediaClientId": "媒体服务标识" "name" : "房间名称",
} "passowrd" : "房间密码",
""", "clientSize" : "终端数量",
"mediaClientId": "媒体服务标识"
}
"""
},
flow = "终端=>信令服务" flow = "终端=>信令服务"
) )
public class RoomStatusProtocol extends ProtocolRoomAdapter { public class RoomStatusProtocol extends ProtocolRoomAdapter {

View File

@@ -21,18 +21,22 @@ import com.acgist.taoyao.signal.protocol.ProtocolSessionAdapter;
*/ */
@Protocol @Protocol
@Description( @Description(
body = """ body = {
{ """
"clientId": "目标ID", {
"audio" : 是否需要声音true|false "clientId": "目标ID",
"video" : 是否需要视频true|false "audio" : 是否需要声音true|false
} "video" : 是否需要视频true|false
{ }
"name" : "终端名称", """,
"clientId" : "终端ID", """
"sessionId": "会话ID" {
} "name" : "终端名称",
""", "clientId" : "终端ID",
"sessionId": "会话ID"
}
"""
},
flow = "终端=>信令服务->终端" flow = "终端=>信令服务->终端"
) )
public class SessionCallProtocol extends ProtocolSessionAdapter { public class SessionCallProtocol extends ProtocolSessionAdapter {

View File

@@ -23,22 +23,26 @@ import com.acgist.taoyao.signal.protocol.ProtocolSessionAdapter;
1. 交换类型大小写 1. 交换类型大小写
2. candidate内容默认名称sdp 2. candidate内容默认名称sdp
""", """,
body = """ body = {
{ """
"sdp" : "sdp" {
"type" : "offer|answer", "sdp" : "sdp"
"sessionId": "会话ID" "type" : "offer|answer",
} "sessionId": "会话ID"
{
"type" : "candidate",
"sessionId": "会话ID",
"candidate": {
"sdpMid" : "sdpMid",
"candidate" : "candidate",
"sdpMLineIndex": sdpMLineIndex
} }
} """,
""", """
{
"type" : "candidate",
"sessionId": "会话ID",
"candidate": {
"sdpMid" : "sdpMid",
"candidate" : "candidate",
"sdpMLineIndex": sdpMLineIndex
}
}
"""
},
flow = "终端->信令服务->终端" flow = "终端->信令服务->终端"
) )
public class SessionExchangeProtocol extends ProtocolSessionAdapter { public class SessionExchangeProtocol extends ProtocolSessionAdapter {