Unverified 提交 1624393e authored 作者: Nazar Mokrynskyi's avatar Nazar Mokrynskyi 提交者: GitHub

Merge pull request #643 from nazar-pc/rust-examples

New Rust videoroom example, echo example comments
...@@ -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)
......
module.exports = {
env : {
browser : true,
es6 : true
},
parserOptions : {
project : 'tsconfig.json',
sourceType : 'module'
}
};
<!doctype html>
<style>
body {
margin: 0;
}
#container {
display: flex;
}
figure {
margin: 0;
width: 30vw;
}
video {
max-width: 100%;
}
</style>
<div id="container">
<figure>
<video id="preview-send" muted controls></video>
<figcaption>You</figcaption>
</figure>
</div>
<script src="dist/bundle.js">
</script>
{
"version": "0.0.0",
"private": true,
"engines": {
"node": ">=14.8"
},
"scripts": {
"start": "webpack serve"
},
"author": "Nazar Mokrynskyi <nazar@mokrynskyi.com>",
"dependencies": {
"mediasoup-client": "^3.6.36"
},
"devDependencies": {
"ts-loader": "^9.2.4",
"typescript": "^4.3.5",
"webpack": "^5.46.0",
"webpack-cli": "^4.7.2",
"webpack-dev-server": "^3.11.2"
}
}
差异被折叠。
{
"compilerOptions": {
"module": "es6",
"target": "es2018",
"sourceMap": true,
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": false,
"sourceRoot": "src",
"baseUrl": "src",
"outDir": "dist",
"strict": true,
"noUnusedParameters": true,
"noUnusedLocals": true,
"lib": [
"es5",
"es2015",
"es2019",
"dom"
]
},
"include": [
"src",
".eslintrc.js",
"webpack.config.js"
]
}
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path');
module.exports = {
mode : 'development',
entry : './src/index.ts',
module : {
rules : [
{
test : /\.ts$/,
use : 'ts-loader',
exclude : /node_modules/
}
]
},
resolve : {
extensions : [ '.ts', '.js' ]
},
output : {
filename : 'dist/bundle.js',
path : path.resolve(__dirname, 'dist')
},
devServer : {
liveReload : false,
port : 3001
}
};
差异被折叠。
...@@ -10,4 +10,13 @@ In order to run server-side part of examples (this directory) use `cargo run --e ...@@ -10,4 +10,13 @@ In order to run server-side part of examples (this directory) use `cargo run --e
Simple echo example that receives audio+video from the client and sends audio+video back to the client via different Simple echo example that receives audio+video from the client and sends audio+video back to the client via different
transport. Frontend part of this example is in `examples-frontend/echo` directory. transport. Frontend part of this example is in `examples-frontend/echo` directory.
Check WebSocket messages in browser DevTools for better understanding of what is happening under the hood. Check WebSocket messages in browser DevTools for better understanding of what is happening under the hood, also source
code has a bunch of comments about what is happening and where changes would be needed for production use.
# Video room
A bit more advanced example that allow multiple participants to join the same virtual room and receive audio+video from
other participants, resulting in a simple video conferencing setup. Frontend part of this example is in
`examples-frontend/videoroom` directory.
Check WebSocket messages in browser DevTools for better understanding of what is happening under the hood, also source
code has a bunch of comments about what is happening and where changes would be needed for production use.
差异被折叠。
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论