[*] 精简流程

This commit is contained in:
acgist
2023-04-22 14:31:05 +08:00
parent 4da70b5216
commit d6ff6ec271
3 changed files with 31 additions and 37 deletions

View File

@@ -35,7 +35,9 @@ import java.util.function.Consumer;
* P2P终端
* 使用安卓SDK + WebRTC实现P2P会话
*
* https://zhuanlan.zhihu.com/p/82446482
* 注意:
* 2. offer/answer/candidate枚举大小
* 1. candidate格式安卓和浏览器格式不同
*
* @author acgist
*/
@@ -56,10 +58,6 @@ public class SessionClient extends Client {
private final boolean videoProduce;
private final MediaProperties mediaProperties;
private final WebrtcProperties webrtcProperties;
/**
* 是否已经提供本地媒体
*/
private volatile boolean offerLocal;
/**
* 本地媒体
*/
@@ -168,10 +166,6 @@ public class SessionClient extends Client {
* 提供媒体服务
*/
public synchronized void offer() {
if(this.offerLocal) {
return;
}
this.offerLocal = true;
final MediaConstraints mediaConstraints = this.mediaManager.buildMediaConstraints();
this.peerConnection.createOffer(this.sdpObserver(
"主动Offer",
@@ -179,9 +173,7 @@ public class SessionClient extends Client {
this.peerConnection.setLocalDescription(this.sdpObserver(
"主动OfferExchange",
null,
() -> {
this.exchangeSessionDescription(sessionDescription);
}
() -> this.exchangeSessionDescription(sessionDescription)
), sessionDescription);
},
null
@@ -202,10 +194,7 @@ public class SessionClient extends Client {
this.peerConnection.setLocalDescription(this.sdpObserver(
"主动AnswerExchange",
null,
() -> {
this.exchangeSessionDescription(sessionDescription);
this.offer();
}
() -> this.exchangeSessionDescription(sessionDescription)
), sessionDescription);
},
null
@@ -221,15 +210,14 @@ public class SessionClient extends Client {
"被动Answer",
null,
null
// () -> this.offer()
), new SessionDescription(sdpType, sdp));
}
private void candidate(Message message, Map<String, Object> body) {
final Map<String, Object> candidate = MapUtils.get(body, "candidate");
final String sdp = MapUtils.get(candidate, "candidate");
final String sdpMid = MapUtils.get(candidate, "sdpMid");
final Integer sdpMLineIndex = MapUtils.getInteger(candidate, "sdpMLineIndex");
final String sdp = MapUtils.get(candidate, "candidate");
final String sdpMid = MapUtils.get(candidate, "sdpMid");
final Integer sdpMLineIndex = MapUtils.getInteger(candidate, "sdpMLineIndex");
if(sdp == null || sdpMid == null || sdpMLineIndex == null) {
Log.w(SessionClient.class.getSimpleName(), "无效媒体协商:" + body);
} else {

View File

@@ -241,8 +241,6 @@ class Session {
clientId;
// 会话ID
sessionId;
// 是否已经提供本地媒体
offerLocal;
// 本地媒体流
localStream;
// 本地音频
@@ -266,7 +264,6 @@ class Session {
this.closed = false;
this.clientId = clientId;
this.sessionId = sessionId;
this.offerLocal = false;
}
async pause() {
@@ -2151,7 +2148,6 @@ class Taoyao extends RemoteClient {
this.sessionClients.set(sessionId, session);
await me.buildPeerConnection(session, sessionId);
session.peerConnection.createOffer().then(async description => {
session.offerLocal = true;
await session.peerConnection.setLocalDescription(description);
me.push(
protocol.buildMessage("session::exchange", {
@@ -2199,18 +2195,6 @@ class Taoyao extends RemoteClient {
sessionId: sessionId
})
);
if(!session.offerLocal) {
session.peerConnection.createOffer().then(async description => {
await session.peerConnection.setLocalDescription(description);
me.push(
protocol.buildMessage("session::exchange", {
sdp : description.sdp,
type : description.type,
sessionId: sessionId
})
);
});
}
});
} else if (type === "answer") {
await session.peerConnection.setRemoteDescription(new RTCSessionDescription(message.body));

View File

@@ -17,9 +17,31 @@ import com.acgist.taoyao.signal.protocol.ProtocolSessionAdapter;
*/
@Protocol
@Description(
memo = "媒体交换协商offer/answer/candidate",
memo = """
媒体交换协商offer/answer/candidate
安卓需要注意:
1. 交换类型大小写
2. candidate内容默认名称sdp
""",
body = """
{
"sdp": "sdp"
"type": "offer",
"sessionId": "会话ID"
}
{
"sdp": "sdp"
"type": "answer",
"sessionId": "会话ID"
}
{
"type": "candidate",
"sessionId": "会话ID",
"candidate": {
"sdpMid": "sdpMid",
"candidate": "candidate信息",
"sdpMLineIndex":sdpMLineIndex
}
}
""",
flow = "终端->信令服务->终端"