4
4
5
5
'use strict' ;
6
6
7
- const { EventEmitter } = require ( 'events' ) ;
8
- const log = require ( './logger' ) . logger . getLogger ( 'AnalyticsController' ) ;
9
- const {
10
- Publication,
11
- Subscription,
12
- Processor,
13
- } = require ( './session' ) ;
7
+ const log = require ( '../logger' ) . logger . getLogger ( 'AnalyticsController' ) ;
8
+ const { TypeController} = require ( './typeController' ) ;
9
+ const { Publication, Subscription, Processor} = require ( '../stateTypes' )
14
10
15
11
function AnalyticsSession ( config , direction ) {
16
12
const session = Object . assign ( { } , config ) ;
@@ -24,14 +20,9 @@ function AnalyticsSession(config, direction) {
24
20
* 'session-updated': (id, Publication|Subscription)
25
21
* 'session-aborted': (id, reason)
26
22
*/
27
- class AnalyticsController extends EventEmitter {
28
-
29
- constructor ( rpcChannel , selfRpcId , clusterRpcId ) {
30
- log . debug ( `constructor ${ selfRpcId } , ${ clusterRpcId } ` ) ;
31
- super ( ) ;
32
- this . rpcChannel = rpcChannel ;
33
- this . selfRpcId = selfRpcId ;
34
- this . clusterRpcId = clusterRpcId ;
23
+ class AnalyticsController extends TypeController {
24
+ constructor ( rpc ) {
25
+ super ( rpc ) ;
35
26
// Map {processorId => Processor}
36
27
this . processors = new Map ( ) ;
37
28
this . sessions = new Map ( ) ;
@@ -48,12 +39,10 @@ class AnalyticsController extends EventEmitter {
48
39
*/
49
40
async addProcessor ( procConfig ) {
50
41
log . debug ( 'addProcessor:' , procConfig ) ;
51
- const rpcChannel = this . rpcChannel ;
52
- const taskConfig = { room : procConfig . domain , task : procConfig . id } ;
53
42
// Unused media preference for analytics
54
43
const mediaPreference = { audio : { encode :[ ] , decode :[ ] } } ;
55
- const locality = await this . _getWorkerNode (
56
- this . clusterRpcId , 'analytics ', taskConfig , mediaPreference ) ;
44
+ const locality = await this . getWorkerNode (
45
+ 'audio ', procConfig . domain , procConfig . id , mediaPreference ) ;
57
46
log . debug ( 'locality:' , locality ) ;
58
47
const analyzer = new Processor ( procConfig . id , 'analyzer' , procConfig ) ;
59
48
analyzer . locality = locality ;
@@ -64,11 +53,9 @@ class AnalyticsController extends EventEmitter {
64
53
}
65
54
66
55
async removeProcessor ( id ) {
67
- const rpcChannel = this . rpcChannel ;
68
56
const processor = this . processors . get ( id ) ;
69
57
if ( processor ) {
70
58
const procConfig = this . processors . get ( id ) . info ;
71
- const taskId = procConfig . analytics . id ;
72
59
const locality = processor . locality ;
73
60
// Add clean up method for analytics agent
74
61
@@ -80,9 +67,8 @@ class AnalyticsController extends EventEmitter {
80
67
this . emit ( 'session-aborted' , videoTrack . id , 'Processor removed' ) ;
81
68
} ) ;
82
69
this . processors . delete ( id ) ;
83
- const taskConfig = { room : procConfig . domain , task : procConfig . id } ;
84
70
// Recycle node
85
- this . _recycleWorkerNode ( locality . agent , locality . node , taskConfig )
71
+ this . recycleWorkerNode ( locality , procConfig . domain , procConfig . id )
86
72
. catch ( ( err ) => log . debug ( 'Failed to recycleNode:' , err ) ) ;
87
73
} else {
88
74
return Promise . reject ( new Error ( 'Invalid processor ID' ) ) ;
@@ -127,7 +113,6 @@ class AnalyticsController extends EventEmitter {
127
113
return Promise . reject ( new Error ( 'Invalid processor ID' ) ) ;
128
114
}
129
115
130
- const rpcChannel = this . rpcChannel ;
131
116
const session = AnalyticsSession ( sessionConfig , direction ) ;
132
117
if ( direction === 'in' ) {
133
118
// Generate video stream for analyzer
@@ -152,9 +137,9 @@ class AnalyticsController extends EventEmitter {
152
137
const options = {
153
138
media : sessionConfig . media ,
154
139
connection : sessionConfig . connection ,
155
- controller : this . selfRpcId ,
140
+ controller : this . selfId ,
156
141
} ;
157
- await rpcChannel . makeRPC ( processor . locality . node , 'subscribe' ,
142
+ await this . makeRPC ( processor . locality . node , 'subscribe' ,
158
143
[ inputId , 'analytics' , options ] ) ;
159
144
this . sessions . set ( inputId , session ) ;
160
145
// Create subscription
@@ -177,31 +162,31 @@ class AnalyticsController extends EventEmitter {
177
162
178
163
async removeSession ( id , direction , reason ) {
179
164
if ( this . sessions . has ( id ) ) {
180
- const rpcChannel = this . rpcChannel ;
181
165
const session = this . sessions . get ( id ) ;
182
166
const processor = this . processors . get ( session . processor ) ;
183
167
if ( ! processor ) {
184
168
throw new Error ( `Processor for ${ id } not found` ) ;
185
169
}
186
170
187
- if ( session . direction === 'in' ) {
171
+ reason = reason || 'Participant terminate' ;
172
+ if ( direction === 'in' ) {
188
173
// Degenerate
189
174
const idx = processor . outputs . video . findIndex ( ( track ) => track . id === id ) ;
190
175
if ( idx >= 0 ) {
191
176
processor . outputs . audio . splice ( idx , 1 ) ;
192
- this . emit ( 'session-aborted' , id , 'Participant terminate' ) ;
177
+ this . emit ( 'session-aborted' , id , reason ) ;
193
178
}
194
179
} else {
195
180
// Remove input
196
181
log . debug ( 'session:' , session ) ;
197
182
// Let cutoff do remove-input
198
183
const inputId = session . media ?. audio ?. from ;
199
- rpcChannel . makeRPC ( processor . locality . node , 'unsubscribe' , [ inputId ] )
184
+ this . makeRPC ( processor . locality . node , 'unsubscribe' , [ inputId ] )
200
185
. catch ( ( e ) => log . debug ( 'ignore unpublish callback' ) ) ;
201
186
const idx = processor . inputs . audio . findIndex ( ( track ) => track . id === id ) ;
202
187
if ( idx >= 0 ) {
203
188
processor . inputs . video . splice ( idx , 1 ) ;
204
- this . emit ( 'session-aborted' , id , 'Participant terminate' ) ;
189
+ this . emit ( 'session-aborted' , id , reason ) ;
205
190
}
206
191
}
207
192
} else {
@@ -262,27 +247,6 @@ class AnalyticsController extends EventEmitter {
262
247
//
263
248
} ) ;
264
249
}
265
-
266
- _getWorkerNode ( clusterManager , purpose , forWhom , preference ) {
267
- const rpcChannel = this . rpcChannel ;
268
- return rpcChannel . makeRPC ( clusterManager , 'schedule' , [ purpose , forWhom . task , preference , 30 * 1000 ] )
269
- . then ( function ( workerAgent ) {
270
- return rpcChannel . makeRPC ( workerAgent . id , 'getNode' , [ forWhom ] )
271
- . then ( function ( workerNode ) {
272
- return { agent : workerAgent . id , node : workerNode } ;
273
- } ) ;
274
- } ) ;
275
- }
276
-
277
- _recycleWorkerNode ( workerAgent , workerNode , forWhom ) {
278
- return this . rpcChannel . makeRPC ( workerAgent , 'recycleNode' , [ workerNode , forWhom ] )
279
- . catch ( ( result ) => {
280
- return 'ok' ;
281
- } , ( err ) => {
282
- return 'ok' ;
283
- } ) ;
284
- }
285
-
286
250
}
287
251
288
252
exports . AnalyticsController = AnalyticsController ;
0 commit comments