From 0908996fb08681819782eb8f82e43c52fd138445 Mon Sep 17 00:00:00 2001
From: acgist <289547414@qq.com>
Date: Sun, 11 Jun 2023 08:12:29 +0800
Subject: [PATCH] =?UTF-8?q?[*]=20=E6=97=A5=E5=B8=B8=E4=BC=98=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../acgist/taoyao/client/signal/Taoyao.java | 3 +-
.../client/src/main/res/values/settings.xml | 2 +
.../com/acgist/taoyao/media/client/Room.java | 431 +++++++++++++++---
taoyao-client-media/src/Config.js | 7 +
taoyao-client-media/src/Server.js | 2 +-
taoyao-client-media/src/Taoyao.js | 16 +-
6 files changed, 390 insertions(+), 71 deletions(-)
diff --git a/taoyao-client-android/taoyao/client/src/main/java/com/acgist/taoyao/client/signal/Taoyao.java b/taoyao-client-android/taoyao/client/src/main/java/com/acgist/taoyao/client/signal/Taoyao.java
index 035d9fa..a9b0fa6 100644
--- a/taoyao-client-android/taoyao/client/src/main/java/com/acgist/taoyao/client/signal/Taoyao.java
+++ b/taoyao-client-android/taoyao/client/src/main/java/com/acgist/taoyao/client/signal/Taoyao.java
@@ -1032,7 +1032,7 @@ public final class Taoyao implements ITaoyao {
roomId,
key -> new Room(
roomId, this.name,
- this.clientId, password,
+ password, this.clientId,
this, this.mainHandler,
resources.getBoolean(R.bool.preview),
resources.getBoolean(R.bool.playAudio),
@@ -1043,6 +1043,7 @@ public final class Taoyao implements ITaoyao {
resources.getBoolean(R.bool.dataProduce),
resources.getBoolean(R.bool.audioProduce),
resources.getBoolean(R.bool.videoProduce),
+ resources.getBoolean(R.bool.roomUseIceServer),
this.mediaManager.getMediaProperties(),
this.mediaManager.getWebrtcProperties()
)
diff --git a/taoyao-client-android/taoyao/client/src/main/res/values/settings.xml b/taoyao-client-android/taoyao/client/src/main/res/values/settings.xml
index ce3ea1a..0eeab60 100644
--- a/taoyao-client-android/taoyao/client/src/main/res/values/settings.xml
+++ b/taoyao-client-android/taoyao/client/src/main/res/values/settings.xml
@@ -40,6 +40,8 @@
1
1
+
+ false
/taoyao
diff --git a/taoyao-client-android/taoyao/media/src/main/java/com/acgist/taoyao/media/client/Room.java b/taoyao-client-android/taoyao/media/src/main/java/com/acgist/taoyao/media/client/Room.java
index fbfb99b..36a3bf6 100644
--- a/taoyao-client-android/taoyao/media/src/main/java/com/acgist/taoyao/media/client/Room.java
+++ b/taoyao-client-android/taoyao/media/src/main/java/com/acgist/taoyao/media/client/Room.java
@@ -33,64 +33,139 @@ import java.util.concurrent.ConcurrentHashMap;
*/
public class Room extends CloseableClient implements RouterCallback {
- private final String name;
+ /**
+ * 房间ID
+ */
private final String roomId;
- private final String clientId;
+ /**
+ * 房间名称
+ */
+ private final String name;
+ /**
+ * 房间密码
+ */
private final String password;
+ /**
+ * 当前终端ID
+ */
+ private final String clientId;
+ /**
+ * 是否预览
+ */
private final boolean preview;
+ /**
+ * 是否播放音频
+ */
private final boolean playAudio;
+ /**
+ * 是否播放视频
+ */
private final boolean playVideo;
+ /**
+ * 是否消费数据
+ */
private final boolean dataConsume;
+ /**
+ * 是否消费音频
+ */
private final boolean audioConsume;
+ /**
+ * 是否消费视频
+ */
private final boolean videoConsume;
+ /**
+ * 是否生产数据
+ */
private final boolean dataProduce;
+ /**
+ * 是否生产音频
+ */
private final boolean audioProduce;
+ /**
+ * 是否生产视频
+ */
private final boolean videoProduce;
+ /**
+ * 是否使用IceServer
+ */
+ private final boolean useIceServer;
+ /**
+ * 是否已经开始生产
+ */
private boolean produce;
+ /**
+ * 媒体配置
+ */
private final MediaProperties mediaProperties;
+ /**
+ * WebRTC配置
+ */
private final WebrtcProperties webrtcProperties;
- private final Map remoteClients;
+ /**
+ * 房间指针
+ */
private final long nativeRoomPointer;
+ /**
+ * 本地终端
+ */
private LocalClient localClient;
- private PeerConnection.RTCConfiguration rtcConfiguration;
- private PeerConnectionFactory peerConnectionFactory;
+ /**
+ * 远程终端
+ */
+ private final Map remoteClients;
+ /**
+ * RTC能力
+ */
private Object rtpCapabilities;
+ /**
+ * SCTP能力
+ */
private Object sctpCapabilities;
+ /**
+ * RTC配置
+ */
+ private PeerConnection.RTCConfiguration rtcConfiguration;
+ /**
+ * PeerConnectionFactory
+ */
+ private PeerConnectionFactory peerConnectionFactory;
/**
- * @param roomId 房间ID
- * @param name 终端名称
- * @param clientId 当前终端ID
- * @param password 房间密码
- * @param taoyao 信令
- * @param mainHandler MainHandler
- * @param preview 是否预览视频
- * @param playAudio 是否播放音频
- * @param playVideo 是否播放视频
- * @param dataConsume 是否消费数据
- * @param audioConsume 是否消费音频
- * @param videoConsume 是否消费视频
- * @param dataProduce 是否生产数据
- * @param audioProduce 是否生产音频
- * @param videoProduce 是否生产视频
+ * @param roomId 房间ID
+ * @param name 终端名称
+ * @param clientId 当前终端ID
+ * @param password 房间密码
+ * @param taoyao 信令
+ * @param mainHandler MainHandler
+ * @param preview 是否预览视频
+ * @param playAudio 是否播放音频
+ * @param playVideo 是否播放视频
+ * @param dataConsume 是否消费数据
+ * @param audioConsume 是否消费音频
+ * @param videoConsume 是否消费视频
+ * @param dataProduce 是否生产数据
+ * @param audioProduce 是否生产音频
+ * @param videoProduce 是否生产视频
+ * @param useIceServer 是否使用IceServer
* @param mediaProperties 媒体配置
* @param webrtcProperties WebRTC配置
*/
public Room(
- String roomId, String name,
- String clientId, String password,
- ITaoyao taoyao, Handler mainHandler,
+ String roomId, String name,
+ String password, String clientId,
+ ITaoyao taoyao, Handler mainHandler,
boolean preview, boolean playAudio, boolean playVideo,
boolean dataConsume, boolean audioConsume, boolean videoConsume,
boolean dataProduce, boolean audioProduce, boolean videoProduce,
+ boolean useIceServer,
MediaProperties mediaProperties, WebrtcProperties webrtcProperties
) {
super(taoyao, mainHandler);
- this.roomId = roomId;
- this.name = name;
- this.clientId = clientId;
- this.password = password;
- this.preview = preview;
+ this.roomId = roomId;
+ this.name = name;
+ this.password = password;
+ this.clientId = clientId;
+ this.preview = preview;
this.playAudio = playAudio;
this.playVideo = playVideo;
this.dataConsume = dataConsume;
@@ -99,13 +174,17 @@ public class Room extends CloseableClient implements RouterCallback {
this.dataProduce = dataProduce;
this.audioProduce = audioProduce;
this.videoProduce = videoProduce;
+ this.useIceServer = useIceServer;
this.produce = false;
- this.mediaProperties = mediaProperties;
- this.webrtcProperties = webrtcProperties;
+ this.mediaProperties = mediaProperties;
+ this.webrtcProperties = webrtcProperties;
this.remoteClients = new ConcurrentHashMap<>();
this.nativeRoomPointer = this.nativeNewRoom(roomId, this);
}
+ /**
+ * @return 是否成功进入房间
+ */
public boolean enter() {
synchronized (this) {
if (this.init) {
@@ -120,12 +199,18 @@ public class Room extends CloseableClient implements RouterCallback {
this.localClient.playVideo();
}
// STUN | TURN
- final List iceServers = new ArrayList<>();
- // 不用配置
-// final List iceServers = this.webrtcProperties.getIceServers();
+ final List iceServers;
+ if(this.useIceServer) {
+ // 不用配置:正常情况都是能够直接访问媒体服务
+ iceServers = this.webrtcProperties.getIceServers();
+ } else {
+ iceServers = new ArrayList<>();
+ }
this.rtcConfiguration = new PeerConnection.RTCConfiguration(iceServers);
+ // 开始协商
return this.taoyao.requestFuture(
this.taoyao.buildMessage("media::router::rtp::capabilities", "roomId", this.roomId),
+ // 成功加载Mediasoup房间
response -> {
this.nativeEnterRoom(
this.nativeRoomPointer,
@@ -135,6 +220,7 @@ public class Room extends CloseableClient implements RouterCallback {
);
return true;
},
+ // 失败关闭资源
response -> {
this.close();
return false;
@@ -143,11 +229,16 @@ public class Room extends CloseableClient implements RouterCallback {
}
}
+ /**
+ * 生产媒体
+ */
public void mediaProduce() {
- if(this.produce) {
- return;
+ synchronized(this) {
+ if(this.produce) {
+ return;
+ }
+ this.produce = true;
}
- this.produce = true;
if (this.audioProduce || this.videoProduce) {
this.createSendTransport();
}
@@ -164,6 +255,9 @@ public class Room extends CloseableClient implements RouterCallback {
}
}
+ /**
+ * 创建发送媒体通道
+ */
private void createSendTransport() {
this.taoyao.requestFuture(
this.taoyao.buildMessage(
@@ -183,6 +277,9 @@ public class Room extends CloseableClient implements RouterCallback {
);
}
+ /**
+ * 创建接收媒体通道
+ */
private void createRecvTransport() {
this.taoyao.requestFuture(
this.taoyao.buildMessage(
@@ -202,6 +299,12 @@ public class Room extends CloseableClient implements RouterCallback {
);
}
+ /**
+ * 媒体消费
+ *
+ * @param message 信令消息
+ * @param body 消息主体
+ */
public void mediaConsume(Message message, Map body) {
this.nativeMediaConsume(this.nativeRoomPointer, JSONUtils.toJSON(message));
}
@@ -213,41 +316,63 @@ public class Room extends CloseableClient implements RouterCallback {
*/
public void newRemoteClientFromRoomEnter(Map body) {
final String clientId = MapUtils.get(body, "clientId");
+ // 忽略自己
if(this.clientId.equals(clientId)) {
return;
}
- final Map status = MapUtils.get(body, "status");
- final String name = MapUtils.get(status, "name");
- final RemoteClient remoteClient = new RemoteClient(name, clientId, this.taoyao, this.mainHandler);
- final RemoteClient old = this.remoteClients.put(clientId, remoteClient);
- if(old != null) {
+ final Map status = MapUtils.get(body, "status");
+ final String name = MapUtils.get(status, "name");
+ final RemoteClient remoteClient = new RemoteClient(name, clientId, this.taoyao, this.mainHandler);
+ final RemoteClient oldRemoteClient = this.remoteClients.put(clientId, remoteClient);
+ if(oldRemoteClient != null) {
// 关闭旧的资源
- old.close();
+ this.closeRemoteClient(oldRemoteClient);
}
}
+ /**
+ * 新增远程终端
+ *
+ * @param body 消息主体
+ */
public void newRemoteClientFromRoomClientList(Map body) {
final List