提交 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() ...@@ -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} 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({ 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)
......
差异被折叠。
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论