Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
M
mediasoup
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
聂卓夫
mediasoup
Commits
afd1c95f
提交
afd1c95f
authored
8月 19, 2021
作者:
Nazar Mokrynskyi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add inline documentation for both client-side and server-side code of `echo` Rust example
上级
d5672c22
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
33 行增加
和
1 行删除
+33
-1
index.ts
rust/examples-frontend/echo/src/index.ts
+33
-1
echo.rs
rust/examples/echo.rs
+0
-0
没有找到文件。
rust/examples-frontend/echo/src/index.ts
浏览文件 @
afd1c95f
...
@@ -114,15 +114,19 @@ async function init()
...
@@ -114,15 +114,19 @@ async function init()
switch
(
decodedMessage
.
action
)
switch
(
decodedMessage
.
action
)
{
{
case
'Init'
:
{
case
'Init'
:
{
// It is expected that server will send initialization message right after
// WebSocket connection is established
await
device
.
load
({
await
device
.
load
({
routerRtpCapabilities
:
decodedMessage
.
routerRtpCapabilities
routerRtpCapabilities
:
decodedMessage
.
routerRtpCapabilities
});
});
// Send client-side initialization message back right away
send
({
send
({
action
:
'Init'
,
action
:
'Init'
,
rtpCapabilities
:
device
.
rtpCapabilities
rtpCapabilities
:
device
.
rtpCapabilities
});
});
// Producer transport is needed to send audio and video to SFU
producerTransport
=
device
.
createSendTransport
(
producerTransport
=
device
.
createSendTransport
(
decodedMessage
.
producerTransportOptions
decodedMessage
.
producerTransportOptions
);
);
...
@@ -130,10 +134,13 @@ async function init()
...
@@ -130,10 +134,13 @@ async function init()
producerTransport
producerTransport
.
on
(
'connect'
,
({
dtlsParameters
},
success
)
=>
.
on
(
'connect'
,
({
dtlsParameters
},
success
)
=>
{
{
// Send request to establish producer transport connection
send
({
send
({
action
:
'ConnectProducerTransport'
,
action
:
'ConnectProducerTransport'
,
dtlsParameters
dtlsParameters
});
});
// And wait for confirmation, but, obviously, no error handling,
// which you should definitely have in real-world applications
waitingForResponse
.
set
(
'ConnectedProducerTransport'
,
()
=>
waitingForResponse
.
set
(
'ConnectedProducerTransport'
,
()
=>
{
{
success
();
success
();
...
@@ -142,17 +149,24 @@ async function init()
...
@@ -142,17 +149,24 @@ async function init()
})
})
.
on
(
'produce'
,
({
kind
,
rtpParameters
},
success
)
=>
.
on
(
'produce'
,
({
kind
,
rtpParameters
},
success
)
=>
{
{
// Once connection is established, send request to produce
// audio or video track
send
({
send
({
action
:
'Produce'
,
action
:
'Produce'
,
kind
,
kind
,
rtpParameters
rtpParameters
});
});
// And wait for confirmation, but, obviously, no error handling,
// which you should definitely have in real-world applications
waitingForResponse
.
set
(
'Produced'
,
({
id
}:
{
id
:
string
})
=>
waitingForResponse
.
set
(
'Produced'
,
({
id
}:
{
id
:
string
})
=>
{
{
success
({
id
});
success
({
id
});
});
});
});
});
// Request microphone and camera access, in real-world apps you may want
// to do this separately so that audio-only and video-only cases are
// handled nicely instead of failing completely
const
mediaStream
=
await
navigator
.
mediaDevices
.
getUserMedia
({
const
mediaStream
=
await
navigator
.
mediaDevices
.
getUserMedia
({
audio
:
true
,
audio
:
true
,
video
:
{
video
:
{
...
@@ -172,6 +186,7 @@ async function init()
...
@@ -172,6 +186,7 @@ async function init()
const
producers
=
[];
const
producers
=
[];
// And create producers for all tracks that were previously requested
for
(
const
track
of
mediaStream
.
getTracks
())
for
(
const
track
of
mediaStream
.
getTracks
())
{
{
const
producer
=
await
producerTransport
.
produce
({
track
});
const
producer
=
await
producerTransport
.
produce
({
track
});
...
@@ -180,6 +195,8 @@ async function init()
...
@@ -180,6 +195,8 @@ async function init()
console
.
log
(
`
${
track
.
kind
}
producer created:`
,
producer
);
console
.
log
(
`
${
track
.
kind
}
producer created:`
,
producer
);
}
}
// Producer transport is now needed to receive previously produced
// tracks back
consumerTransport
=
device
.
createRecvTransport
(
consumerTransport
=
device
.
createRecvTransport
(
decodedMessage
.
consumerTransportOptions
decodedMessage
.
consumerTransportOptions
);
);
...
@@ -187,10 +204,13 @@ async function init()
...
@@ -187,10 +204,13 @@ async function init()
consumerTransport
consumerTransport
.
on
(
'connect'
,
({
dtlsParameters
},
success
)
=>
.
on
(
'connect'
,
({
dtlsParameters
},
success
)
=>
{
{
// Send request to establish consumer transport connection
send
({
send
({
action
:
'ConnectConsumerTransport'
,
action
:
'ConnectConsumerTransport'
,
dtlsParameters
dtlsParameters
});
});
// And wait for confirmation, but, obviously, no error handling,
// which you should definitely have in real-world applications
waitingForResponse
.
set
(
'ConnectedConsumerTransport'
,
()
=>
waitingForResponse
.
set
(
'ConnectedConsumerTransport'
,
()
=>
{
{
success
();
success
();
...
@@ -198,22 +218,32 @@ async function init()
...
@@ -198,22 +218,32 @@ async function init()
});
});
});
});
// For simplicity of this example producers were stored in an array
// and are now all consumed one at a time
for
(
const
producer
of
producers
)
for
(
const
producer
of
producers
)
{
{
await
new
Promise
((
resolve
)
=>
await
new
Promise
((
resolve
)
=>
{
{
// Send request to consume producer
send
({
send
({
action
:
'Consume'
,
action
:
'Consume'
,
producerId
:
producer
.
id
producerId
:
producer
.
id
});
});
// And wait for confirmation, but, obviously, no error handling,
// which you should definitely have in real-world applications
waitingForResponse
.
set
(
'Consumed'
,
async
(
consumerOptions
:
ConsumerOptions
)
=>
waitingForResponse
.
set
(
'Consumed'
,
async
(
consumerOptions
:
ConsumerOptions
)
=>
{
{
// Once confirmation is received, corresponding consumer
// can be created client-side
const
consumer
=
await
(
consumerTransport
as
Transport
).
consume
(
const
consumer
=
await
(
consumerTransport
as
Transport
).
consume
(
consumerOptions
consumerOptions
);
);
console
.
log
(
`
${
consumer
.
kind
}
produc
er created:`
,
consumer
);
console
.
log
(
`
${
consumer
.
kind
}
consum
er created:`
,
consumer
);
// Consumer needs to be resumed after being created in
// paused state (see official documentation about why:
// https://mediasoup.org/documentation/v3/mediasoup/api/#transport-consume)
send
({
send
({
action
:
'ConsumerResume'
,
action
:
'ConsumerResume'
,
id
:
consumer
.
id
id
:
consumer
.
id
...
@@ -237,6 +267,8 @@ async function init()
...
@@ -237,6 +267,8 @@ async function init()
break
;
break
;
}
}
default
:
{
default
:
{
// All messages other than initialization go here and are assumed
// to be notifications that correspond to previously sent requests
const
callback
=
waitingForResponse
.
get
(
decodedMessage
.
action
);
const
callback
=
waitingForResponse
.
get
(
decodedMessage
.
action
);
if
(
callback
)
if
(
callback
)
...
...
rust/examples/echo.rs
浏览文件 @
afd1c95f
差异被折叠。
点击展开。
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论