forked from robreys/media-server-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamer.js
120 lines (110 loc) · 2.74 KB
/
Streamer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const EventEmitter = require('events').EventEmitter;
const StreamerSession = require("./StreamerSession");
/**
* An streamer allows to send and receive plain RTP over udp sockets.
* This allows both to bridge legacy enpoints or integrate streaming/broadcasting services.
*/
class Streamer
{
/**
* @ignore
* @hideconstructor
* private constructor
*/
constructor(ip)
{
//Store ip address of the endpoint
this.ip = ip;
//Sessions set
this.sessions = new Set();
//Create event emitter
this.emitter = new EventEmitter();
}
/**
* Creates a new streaming session from a media description
* @param {MediaInfo} media - Media codec description info
* @param {Object} params - Network parameters [Optional]
* @param {Object} params.local - Local parameters
* @param {Number} params.local.port - receiving port
* @param {Object} params.remote - Remote parameters
* @param {String} params.remote.ip - Sending ip address
* @param {Number} params.remote.port - Sending port
* @param {Number} params.noRTCP - Disable sending rtcp
* @returns {StreamerSession} The new streaming session
*/
createSession(media,params)
{
//Create session
const session = new StreamerSession(media,params);
//Add listener
session.once("stopped",()=>{
//Remove from set
this.sessions.delete(session);
});
//Store it
this.sessions.add(session);
//Return it
return session;
}
/**
* Add event listener
* @param {String} event - Event name
* @param {function} listeener - Event listener
* @returns {Endpoint}
*/
on()
{
//Delegate event listeners to event emitter
this.emitter.on.apply(this.emitter, arguments);
//Return object so it can be chained
return this;
}
/**
* Add event listener once
* @param {String} event - Event name
* @param {function} listener - Event listener
* @returns {Endpoint}
*/
once()
{
//Delegate event listeners to event emitter
this.emitter.once.apply(this.emitter, arguments);
//Return object so it can be chained
return this;
}
/**
* Remove event listener
* @param {String} event - Event name
* @param {function} listener - Event listener
* @returns {Endpoint}
*/
off()
{
//Delegate event listeners to event emitter
this.emitter.removeListener.apply(this.emitter, arguments);
//Return object so it can be chained
return this;
}
/**
* Stop all streaming sessions and frees resources
*/
stop()
{
//Stop all sessions
for (let session of this.sessions.values())
//stop
session.stop();
/**
* Streamer stopped event
*
* @name stopped
* @memberof Streamer
* @kind event
* @argument {Streamer} streamer
*/
this.emitter.emit("stopped",this);
//Clear set jic
this.sessions.clear();
}
}
module.exports = Streamer;