提交 afd1c95f authored 作者: Nazar Mokrynskyi's avatar Nazar Mokrynskyi

Add inline documentation for both client-side and server-side code of `echo` Rust example

上级 d5672c22
......@@ -114,15 +114,19 @@ async function init()
switch (decodedMessage.action)
{
case 'Init': {
// It is expected that server will send initialization message right after
// WebSocket connection is established
await device.load({
routerRtpCapabilities : decodedMessage.routerRtpCapabilities
});
// Send client-side initialization message back right away
send({
action : 'Init',
rtpCapabilities : device.rtpCapabilities
});
// Producer transport is needed to send audio and video to SFU
producerTransport = device.createSendTransport(
decodedMessage.producerTransportOptions
);
......@@ -130,10 +134,13 @@ async function init()
producerTransport
.on('connect', ({ dtlsParameters }, success) =>
{
// Send request to establish producer transport connection
send({
action : 'ConnectProducerTransport',
dtlsParameters
});
// And wait for confirmation, but, obviously, no error handling,
// which you should definitely have in real-world applications
waitingForResponse.set('ConnectedProducerTransport', () =>
{
success();
......@@ -142,17 +149,24 @@ async function init()
})
.on('produce', ({ kind, rtpParameters }, success) =>
{
// Once connection is established, send request to produce
// audio or video track
send({
action : 'Produce',
kind,
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}) =>
{
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({
audio : true,
video : {
......@@ -172,6 +186,7 @@ async function init()
const producers = [];
// And create producers for all tracks that were previously requested
for (const track of mediaStream.getTracks())
{
const producer = await producerTransport.produce({ track });
......@@ -180,6 +195,8 @@ async function init()
console.log(`${track.kind} producer created:`, producer);
}
// Producer transport is now needed to receive previously produced
// tracks back
consumerTransport = device.createRecvTransport(
decodedMessage.consumerTransportOptions
);
......@@ -187,10 +204,13 @@ async function init()
consumerTransport
.on('connect', ({ dtlsParameters }, success) =>
{
// Send request to establish consumer transport connection
send({
action : 'ConnectConsumerTransport',
dtlsParameters
});
// And wait for confirmation, but, obviously, no error handling,
// which you should definitely have in real-world applications
waitingForResponse.set('ConnectedConsumerTransport', () =>
{
success();
......@@ -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)
{
await new Promise((resolve) =>
{
// Send request to consume producer
send({
action : 'Consume',
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) =>
{
// Once confirmation is received, corresponding consumer
// can be created client-side
const consumer = await (consumerTransport as Transport).consume(
consumerOptions
);
console.log(`${consumer.kind} producer created:`, consumer);
console.log(`${consumer.kind} consumer 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({
action : 'ConsumerResume',
id : consumer.id
......@@ -237,6 +267,8 @@ async function init()
break;
}
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);
if (callback)
......
差异被折叠。
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论