提交 0c0b0d30 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Work in Worker, Room, Channel

上级 2915e191
......@@ -20,11 +20,9 @@ Flexible and powerful Node.js library handling a C++ multi-stream media server
* SDP unaware. Really, MediaSoup will not parse a SDP for you.
* Extremely powerful. MediaSoup is coded in C++ on top of the awesome [libuv](https://github.com/libuv/libuv) asychronous I/O library. MediaSoup is a single process application with multiple *Worker* threads for media handling. MediaSoup takes full advantage of your CPU capabilities.
## Worker Building
## Building mediasoup-worker
```bash
$ cd worker
$ make submodules
$ make
```
......
'use strict';
const debug = require('debug')('mediasoup:Channel');
const debugerror = require('debug')('mediasoup:ERROR:Channel');
debugerror.log = console.error.bind(console);
class Channel
{
constructor(socket)
{
debug('constructor()');
this._socket = socket;
}
sendRequest(req)
{
debug('sendRequest() [request:%o]', req);
this._socket.write(JSON.stringify(req));
}
}
module.exports = Channel;
'use strict';
const debug = require('debug')('mediasoup:Room');
const debugerror = require('debug')('mediasoup:ERROR:Room');
debugerror.log = console.error.bind(console);
const EventEmitter = require('events').EventEmitter;
class Room extends EventEmitter
{
constructor(options, channel)
{
super();
this._channel = channel;
let req =
{
type : 'request',
id : 12345678,
method : 'createRoom'
};
this._channel.sendRequest(req);
}
close()
{
debug('close()');
this.emit('close');
}
}
module.exports = Room;
......@@ -5,9 +5,9 @@ const debugerror = require('debug')('mediasoup:ERROR:Server');
debugerror.log = console.error.bind(console);
const process = require('process');
const os = require('os');
const path = require('path');
const EventEmitter = require('events').EventEmitter;
const check = require('check-types');
const randomString = require('random-string');
......@@ -20,22 +20,12 @@ const VALID_PARAMETERS =
'dtlsCertificateFile', 'dtlsPrivateKeyFile'
];
// Set of Server instances
var servers = new Set();
// On process exit close all the Servers
process.on('exit', () =>
{
for (let server of servers)
{
server.close();
}
});
class Server
class Server extends EventEmitter
{
constructor(options)
{
super();
debug('constructor() [options:%o]', options);
let serverId = randomString({ numeric: false, length: 6 }).toLowerCase();
......@@ -44,11 +34,8 @@ class Server
options = options || {};
// Map of Worker instances
this._workers = new Map();
// Map of Room instances
this._rooms = new Map();
// Set of Worker instances
this._workers = new Set();
if (check.integer(options.numWorkers) && check.positive(options.numWorkers))
{
......@@ -65,9 +52,6 @@ class Server
options.dtlsPrivateKeyFile = path.resolve(options.dtlsPrivateKeyFile);
}
// Add this Server to the Set
servers.add(this);
for (let key of Object.keys(options))
{
if (check.includes(VALID_PARAMETERS, key))
......@@ -88,16 +72,16 @@ class Server
worker.on('exit', () =>
{
this._workers.delete(workerId);
this._workers.delete(worker);
});
worker.on('error', () =>
{
this._workers.delete(workerId);
this._workers.delete(worker);
});
// Add the Worker to the Map
this._workers.set(workerId, worker);
// Add the Worker to the Set
this._workers.add(worker);
}
}
......@@ -105,15 +89,14 @@ class Server
{
debug('close()');
// Remove this Server from the Set
servers.delete(this);
// Close every Worker
for (let worker of this._workers.values())
for (let worker of this._workers)
{
worker.close();
}
this._workers.clear();
this.emit('close');
}
createRoom(options)
......@@ -127,9 +110,9 @@ class Server
_getRandomWorker()
{
let workerIds = Array.from(this._workers.keys());
let array = Array.from(this._workers);
return this._workers.get(workerIds[workerIds.length * Math.random() << 0]);
return array[array.length * Math.random() << 0];
}
}
......
......@@ -9,13 +9,15 @@ const path = require('path');
const spawn = require('child_process').spawn;
const process = require('process');
const EventEmitter = require('events').EventEmitter;
const randomString = require('random-string');
const Channel = require('./Channel');
const Room = require('./Room');
const WORKER_BIN_PATH = path.join(__dirname, '..', 'worker', 'bin', 'mediasoup-worker');
const CONTROL_FD = 3;
const CHANNEL_FD = 3;
// Set an env variable to tell childs which the Control FD is
process.env.MEDIASOUP_CONTROL_FD = String(CONTROL_FD);
// Set an env variable to tell childs which the Channel FD is
process.env.MEDIASOUP_CHANNEL_FD = String(CHANNEL_FD);
class Worker extends EventEmitter
{
......@@ -23,9 +25,6 @@ class Worker extends EventEmitter
{
super();
// TODO: TMP
this.id = id;
debug('constructor() [id:%s]', id);
let spawnArgs = [ id ].concat(parameters);
......@@ -38,8 +37,11 @@ class Worker extends EventEmitter
// Create the mediasoup-worker child process
this._child = spawn(WORKER_BIN_PATH, spawnArgs, spawnOptions);
// Control unix socket
this._control = this._child.stdio[CONTROL_FD];
// Channel instance
this._channel = new Channel(this._child.stdio[CHANNEL_FD]);
// Set of Room instances
this._rooms = new Set();
// Set child process events
......@@ -76,22 +78,17 @@ class Worker extends EventEmitter
this._child.kill('SIGTERM');
}
createRoom()
createRoom(options)
{
debug('createRoom()');
let roomId = randomString({ numeric: false, length: 8 }).toLowerCase();
let req =
{
type : 'request',
method : 'createRoom',
room :
{
id : roomId
}
};
let room = new Room(options, this._channel);
// Store the Room instance and remove it when closed
this._rooms.add(room);
room.on('close', () => this._rooms.delete(room));
this._control.write(JSON.stringify(req));
return room;
}
}
......
......@@ -5,14 +5,34 @@ const debugerror = require('debug')('mediasoup:ERROR');
debugerror.log = console.error.bind(console);
const process = require('process');
const Server = require('./Server');
// Set of Server instances
var servers = new Set();
module.exports =
{
createServer : function(options)
{
debug('createServer()');
return new Server(options);
var server = new Server(options);
// Store the Server instance and remove it when closed
servers.add(server);
server.on('close', () => servers.delete(server));
return server;
}
};
// On process exit close all the Servers
process.on('exit', () =>
{
for (let server of servers)
{
server.close();
}
});
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论