Kurento-one2many-call这是官方的一个例子,一个发送视频,N个人来观看,预览者可以随时离开通讯,当发布者完成会话时,每个连接的预览者都会收到一个停止的消息并终止其会话,类似直播。
cd kurento-tutorial-java/kurento-one2many-call
mvn -U clean spring-boot:run \
-Dspring-boot.run.jvmArguments="-Dkms.url=ws://{KMS_HOST}:8888/kurento"
功能说明:
- 发布者创建offer,并设置本地描述;
- 发布者将offer发送presenter到服务端;
- 发布者接收到presenterResponse,获取到answer并设置远程描述;
- 预览者创建offer,并设置本地描述;
- 预览者将offer,发送viewer到服务端;
- 预览者收到viewerResponse,获取到answer并设置远程描述;
- 一对N端的视频完成,预览者可以随时离开
- 当发布者完成会话时,每个连接的预览者都会收到一个stopCommunication消息并终止其会话。
以下是官方流程图:
发布者:
//创建Offer
val mediaConstraints = MediaConstraints()
peerConnection.createOffer(this, mediaConstraints)
...
//设置本地描述
override fun onCreateSuccess(sd: SessionDescription?) {
peerConnection.setLocalDescription(this, sd)
}
...
//设置本地描述成功后发送到服务端
override fun onSetSuccess() {
peerConnection.localDescription?.let {
sendPresenter(it.description)
}
}
接收到presenterResponse后将answer设置为远程描述:
if (message["response"].asString == "accepted") {
val sdpAnswer = message["sdpAnswer"].asString
peerConnection.setRemoteDescription(
this,
SessionDescription(SessionDescription.Type.ANSWER, sdpAnswer)
)
}
预览者
//创建Offer
val mediaConstraints = MediaConstraints()
peerConnection.createOffer(this, mediaConstraints)
...
//设置本地描述
override fun onCreateSuccess(sd: SessionDescription?) {
peerConnection.setLocalDescription(this, sd)
}
//设置本地描述成功后发送到服务端
override fun onSetSuccess() {
peerConnection.localDescription?.let {
sendViewer(it.description)
}
}
预览者接收到viewerResponse后将answer设置为远程描述:
if (message["response"].asString == "accepted") {
val sdpAnswer = message["sdpAnswer"].asString
peerConnection!!.setRemoteDescription(
this,
SessionDescription(SessionDescription.Type.ANSWER, sdpAnswer)
)
}
评论