1

我已经让 Janus videoroom 正常工作,现在我想实现 DataChannel 在设备之间相互发送消息

我设置了数据:当发布者发送配置和订阅者加入时为真

void sendLocalDescription(BigInteger handleId, JSONObject sdpObj) {
try {
JSONObject msg = new JSONObject();
JSONObject body = new JSONObject();
body.put(REQUEST, BodyRequestMessageType.CONFIGURE.getType());
body.put(AUDIO, options.audio);
body.put(VIDEO, options.video);
body.put(DATA, true);

msg.put(JANUS, SendMessageType.MESSAGE.getType());
msg.put(TRANSACTION, RandomUtil.randomString());
msg.put(SESSION_ID, sessionId);
msg.put(HANDLE_ID, handleId);

msg.put(BODY, body);
msg.put(JESP, sdpObj);
connection.sendMessage(msg.toString());
Log.e(SERVER_TAG, "publisher sendLocalDescription: " + body.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}

这是给订户的

private void sendSubscribeMessage() {
try {
JSONObject object = new JSONObject();
object.put(JANUS, SendMessageType.MESSAGE.getType());
object.put(PLUGIN, PLUGIN_VALUE);
object.put(TRANSACTION, RandomUtil.randomString());
object.put(SESSION_ID, sessionId);
object.put(HANDLE_ID, handleId);

JSONObject body = new JSONObject();
body.put(REQUEST, BodyRequestMessageType.JOIN.getType());
body.put(PTYPE, PType.SUBSCRIBER.getType());
body.put(ROOM, options.meetNum);
body.put(PIN, options.pin);
body.put(PRIVATE_ID, privateId);
body.put(FEED, publisherBean.getId());
body.put(DATA, true);
body.put(OFFER_DATA, true);
object.put(BODY, body);
connection.sendMessage(object.toString());
if(publisherBean.isSumulcast()){
sendConfigureSimulcastMessage();
}
} catch (JSONException e) {
e.printStackTrace();
}
}

为发布者创建对等连接后,我创建了数据通道:

peerConnection = PCFactoryProxy.instance().createPeerConnection(configuration, this);
dataChannel = peerConnection.createDataChannel("1", new DataChannel.Init());
dataChannel.registerObserver(new DataChannel.Observer() {
@Override
public void onBufferedAmountChange(long l) {

}

@Override
public void onStateChange() {
Log.e(SERVER_TAG, "dataChannel state: " + dataChannel.state().toString());
}

@Override
public void onMessage(DataChannel.Buffer buffer) {

}
});

但我从来没有看到 onDataChannel 被触发,有人可以帮忙吗?

@Override
public void onDataChannel(DataChannel dataChannel) {
LogUtil.e(SERVER_TAG, "PeerConnectionChannelV3 -> onDataChannel: " + dataChannel.label());
this.dataChannel = dataChannel;
// this.dataChannel.registerObserver(this);
// sendDataChannelMessage("test");
}

据我所知DataChannel,当 Peer A 已经拥有 datachannel 对象时,Peer B 将在触发 onDataChannel 时接收 datachannel

4

0 回答 0