Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
M
mediasoup
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
聂卓夫
mediasoup
Commits
6d71f5f5
提交
6d71f5f5
authored
12月 12, 2015
作者:
Iñaki Baz Castillo
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Node: Add Worker class.
上级
64248dd1
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
103 行增加
和
54 行删除
+103
-54
Server.js
lib/Server.js
+18
-54
Worker.js
lib/Worker.js
+85
-0
没有找到文件。
lib/Server.js
浏览文件 @
6d71f5f5
'use strict'
;
'use strict'
;
const
path
=
require
(
'path'
);
const
spawn
=
require
(
'child_process'
).
spawn
;
const
process
=
require
(
'process'
);
const
os
=
require
(
'os'
);
const
debug
=
require
(
'debug'
)(
'mediasoup:Server'
);
const
debug
=
require
(
'debug'
)(
'mediasoup:Server'
);
const
debugerror
=
require
(
'debug'
)(
'mediasoup:ERROR:Server'
);
const
debugerror
=
require
(
'debug'
)(
'mediasoup:ERROR:Server'
);
debugerror
.
log
=
console
.
error
.
bind
(
console
);
debugerror
.
log
=
console
.
error
.
bind
(
console
);
const
process
=
require
(
'process'
);
const
os
=
require
(
'os'
);
const
check
=
require
(
'check-types'
);
const
check
=
require
(
'check-types'
);
const
randomString
=
require
(
'random-string'
);
const
randomString
=
require
(
'random-string'
);
const
WORKER_BIN_PATH
=
path
.
join
(
__dirname
,
'..'
,
'worker'
,
'bin'
,
'mediasoup-worker'
);
const
Worker
=
require
(
'./Worker'
);
const
DEFAULT_NUM_WORKERS
=
Object
.
keys
(
os
.
cpus
()).
length
;
const
DEFAULT_NUM_WORKERS
=
Object
.
keys
(
os
.
cpus
()).
length
;
const
VALID_PARAMETERS
=
const
VALID_PARAMETERS
=
[
[
...
@@ -21,6 +19,7 @@ const VALID_PARAMETERS =
...
@@ -21,6 +19,7 @@ const VALID_PARAMETERS =
'dtlsCertificateFile'
,
'dtlsPrivateKeyFile'
'dtlsCertificateFile'
,
'dtlsPrivateKeyFile'
];
];
// Set of Server instances
var
servers
=
new
Set
();
var
servers
=
new
Set
();
process
.
on
(
'exit'
,
()
=>
process
.
on
(
'exit'
,
()
=>
...
@@ -48,9 +47,12 @@ class Server
...
@@ -48,9 +47,12 @@ class Server
numWorkers
=
options
.
numWorkers
;
numWorkers
=
options
.
numWorkers
;
}
}
// Map of
mediasoup-worker child process
es
// Map of
Worker instanc
es
this
.
_workers
=
new
Map
();
this
.
_workers
=
new
Map
();
// Add the server to the set
servers
.
add
(
this
);
for
(
let
key
of
Object
.
keys
(
options
))
for
(
let
key
of
Object
.
keys
(
options
))
{
{
if
(
check
.
includes
(
VALID_PARAMETERS
,
key
))
if
(
check
.
includes
(
VALID_PARAMETERS
,
key
))
...
@@ -59,63 +61,25 @@ class Server
...
@@ -59,63 +61,25 @@ class Server
}
}
}
}
debug
(
'constructor() | [worker parameters:"%s"]'
,
parameters
.
join
(
' '
));
debug
(
'constructor() [worker parameters:"%s"]'
,
parameters
.
join
(
' '
));
// Add the server to the set
servers
.
add
(
this
);
// Create mediasoup-worker child processes
// Create mediasoup-worker child processes
for
(
let
i
=
1
;
i
<=
numWorkers
;
i
++
)
for
(
let
i
=
1
;
i
<=
numWorkers
;
i
++
)
{
{
let
worker
;
let
worker
;
let
workerId
=
serverId
+
'#'
+
i
;
let
workerId
=
serverId
+
'#'
+
i
;
let
spawnArgs
=
[
workerId
].
concat
(
parameters
);
let
spawnOptions
=
{
detached
:
false
,
stdio
:
[
'pipe'
,
'pipe'
,
'pipe'
,
'ipc'
]
};
debug
(
'constructor() | creating worker [workerId:%s]'
,
workerId
);
// Create the worker child process
worker
=
spawn
(
WORKER_BIN_PATH
,
spawnArgs
,
spawnOptions
);
//
Store worker id within the worker process
//
Create a Worker instance
worker
.
workerId
=
workerId
;
worker
=
new
Worker
(
workerId
,
parameters
)
;
worker
.
stdout
.
on
(
'data'
,
(
buffer
)
=>
worker
.
on
(
'exit'
,
(
)
=>
{
{
buffer
.
toString
(
'utf8'
).
split
(
'
\
n'
).
forEach
((
line
)
=>
this
.
_workers
.
delete
(
workerId
);
{
if
(
line
)
{
debug
(
line
);
}
});
});
worker
.
stderr
.
on
(
'data'
,
(
buffer
)
=>
{
buffer
.
toString
(
'utf8'
).
split
(
'
\
n'
).
forEach
((
line
)
=>
{
if
(
line
)
{
debugerror
(
line
);
}
});
});
worker
.
on
(
'exit'
,
(
code
,
signal
)
=>
{
debug
(
'worker process closed [workerId:%s, code:%s, signal:%s]'
,
worker
.
workerId
,
code
,
signal
);
this
.
_workers
.
delete
(
worker
.
workerId
);
});
});
worker
.
on
(
'error'
,
(
error
)
=>
worker
.
on
(
'error'
,
()
=>
{
{
debugerror
(
'worker process error [workerId:%s]: %s'
,
worker
.
workerId
,
error
);
this
.
_workers
.
delete
(
workerId
);
});
});
// Add the worker to the map
// Add the worker to the map
...
@@ -130,10 +94,10 @@ class Server
...
@@ -130,10 +94,10 @@ class Server
// Remove this server from the set
// Remove this server from the set
servers
.
delete
(
this
);
servers
.
delete
(
this
);
//
Kill every worker process
//
Close every worker
for
(
let
worker
of
this
.
_workers
.
values
())
for
(
let
worker
of
this
.
_workers
.
values
())
{
{
worker
.
kill
(
'SIGTERM'
);
worker
.
close
(
);
}
}
// Clear the workers map
// Clear the workers map
...
...
lib/Worker.js
0 → 100644
浏览文件 @
6d71f5f5
'use strict'
;
const
debug
=
require
(
'debug'
)(
'mediasoup:Worker'
);
const
debugerror
=
require
(
'debug'
)(
'mediasoup:ERROR:Worker'
);
debugerror
.
log
=
console
.
error
.
bind
(
console
);
const
path
=
require
(
'path'
);
const
spawn
=
require
(
'child_process'
).
spawn
;
const
process
=
require
(
'process'
);
const
EventEmitter
=
require
(
'events'
).
EventEmitter
;
const
WORKER_BIN_PATH
=
path
.
join
(
__dirname
,
'..'
,
'worker'
,
'bin'
,
'mediasoup-worker'
);
// Set an env variable to tell workers which the Control FD is
process
.
env
.
MEDIASOUP_CONTROL_FD
=
'3'
;
class
Worker
extends
EventEmitter
{
constructor
(
id
,
parameters
)
{
super
();
debug
(
'constructor() [id:%s]'
,
id
);
let
spawnArgs
=
[
id
].
concat
(
parameters
);
let
spawnOptions
=
{
detached
:
false
,
stdio
:
[
'pipe'
,
'pipe'
,
'pipe'
,
'pipe'
]
};
// Create the worker child process
this
.
_child
=
spawn
(
WORKER_BIN_PATH
,
spawnArgs
,
spawnOptions
);
// TODO: TMP
global
.
p
=
this
.
_child
.
stdio
[
3
];
this
.
_child
.
stdout
.
on
(
'data'
,
(
buffer
)
=>
{
buffer
.
toString
(
'utf8'
).
split
(
'
\
n'
).
forEach
((
line
)
=>
{
if
(
line
)
{
debug
(
line
);
}
});
});
this
.
_child
.
stderr
.
on
(
'data'
,
(
buffer
)
=>
{
buffer
.
toString
(
'utf8'
).
split
(
'
\
n'
).
forEach
((
line
)
=>
{
if
(
line
)
{
debugerror
(
line
);
}
});
});
this
.
_child
.
on
(
'exit'
,
(
code
,
signal
)
=>
{
debug
(
'worker process closed [id:%s, code:%s, signal:%s]'
,
id
,
code
,
signal
);
this
.
emit
(
'exit'
,
code
,
signal
);
});
this
.
_child
.
on
(
'error'
,
(
error
)
=>
{
debugerror
(
'worker process error [id:%s]: %s'
,
id
,
error
);
this
.
emit
(
'error'
,
error
);
});
}
close
()
{
debug
(
'close()'
);
// Kill worker child
this
.
_child
.
kill
(
'SIGTERM'
);
}
}
module
.
exports
=
Worker
;
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论