This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmediaServer.js
183 lines (148 loc) · 6.08 KB
/
mediaServer.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const SemanticSDP = require('semantic-sdp');
const Medooze = require('medooze-media-server');
const uuidV4 = require('uuid/v4');
const AUDIO_CAPABILITIES = {
audio: {
codecs: ['opus'],
extensions: [
'urn:ietf:params:rtp-hdrext:ssrc-audio-level',
'http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01',
],
},
};
const DEFAULT_IP = '127.0.0.1';
class MediaServer {
constructor() {
this._producers = {};
Medooze.setPortRange(1024, 20480);
this.publicIp = DEFAULT_IP;
}
async createProducer(offer) {
const newUuid = uuidV4();
console.log(`Producer[${newUuid}]: Creating`);
// We don't reuse endpoints because it has CPU implications
const endpoint = Medooze.createEndpoint(this.publicIp);
// Process the sdp
const offerObj = SemanticSDP.SDPInfo.parse(offer);
// Create an DTLS ICE transport
const transport = endpoint.createTransport(offerObj);
transport.once('stopped', (event) => {
console.log(`Producer[${newUuid}]: Transport stopped`);
});
transport.once('incomingtrack', (track) => {
console.log(`Producer[${newUuid}]: Incoming track`);
});
transport.on('dtlsstate', (dtlsState) => {
console.log(`Producer[${newUuid}]: DTLS state '${dtlsState}'`);
});
// Set RTP remote properties
const audio = offerObj.getMedia('audio');
transport.setRemoteProperties({
audio,
});
// Create local SDP info
const answerObj = offerObj.answer({
dtls: transport.getLocalDTLSInfo(),
ice: transport.getLocalICEInfo(),
candidates: endpoint.getLocalCandidates(),
capabilities: AUDIO_CAPABILITIES,
});
// Use DTX for bandwidth saving
const audioOffer = answerObj.getMedia('audio');
const opus = audioOffer.getCodec('opus');
// Set RTP local properties
transport.setLocalProperties({
audio: answerObj.getMedia('audio'),
});
// Get offered stream info
const offered = offerObj.getFirstStream();
// Create the remote stream into the transport
const incomingStream = transport.createIncomingStream(offered);
// Store this stream mapped to the UUID so we can fetch it for consumers later
this._producers[newUuid] = {
stream: incomingStream,
transport,
endpoint,
mediaInfo: audio,
consumers: {},
remoteConsumers: {},
};
console.log(`Producer[${newUuid}]: Created`);
// Return UUID and SDP answer
return {
uuid: newUuid,
sdp: answerObj.toString(),
};
}
createConsumer(producerUuid, offer, internal, externalIdMap) {
const producer = this._producers[producerUuid];
if (producer) {
const newUuid = uuidV4();
console.log(`Consumer[${newUuid}]: Creating for producer '${producerUuid}'`);
// Match to same endpoint (more efficient)
const endpoint = producer.endpoint;
const offerObj = SemanticSDP.SDPInfo.parse(offer);
// Create an DTLS ICE transport in that endpoint
const transport = endpoint.createTransport(offerObj, null);
// Create local SDP info & add local ice and dtls info
const answerObj = new SemanticSDP.SDPInfo();
answerObj.setDTLS(transport.getLocalDTLSInfo());
answerObj.setICE(transport.getLocalICEInfo());
// For each local candidate - add candidate to media info
for (const candidate of endpoint.getLocalCandidates()) {
answerObj.addCandidate(candidate);
}
// Get audio m-line info
const audioOffer = offerObj.getMedia('audio');
let audio;
if (audioOffer) {
// Create audio media
audio = new SemanticSDP.MediaInfo(audioOffer.getId(), 'audio');
const opus = audioOffer.getCodec('opus');
audio.addCodec(opus);
audio.setDirection(SemanticSDP.Direction.SENDONLY);
// Add it to answer
answerObj.addMedia(audio);
} else {
throw new Error(`Consumer[${newUuid}]: No audio offer was given`);
}
// Set RTP local properties
transport.setLocalProperties({
audio: answerObj.getMedia('audio'),
});
// Create new local stream with only video
const outgoingStream = transport.createOutgoingStream({
audio: true,
});
// Copy incoming data from the broadcast stream to the local one
const stream = producer.stream;
if (!stream) {
throw new Error(`Consumer[${newUuid}]: '${producerUuid}' has no stream information`);
}
outgoingStream.attachTo(stream);
// Get local stream info and add local stream info it to the answer
answerObj.addStream(outgoingStream.getStreamInfo());
transport.on('dtlsstate', (dtlsState) => {
console.log(`Consumer[${newUuid}]: DTLS state '${dtlsState}'`);
if (dtlsState === 'closed') {
this.destroyConsumer(producerUuid, newUuid, true);
}
});
// Store this consumer as part of the producer object
producer.consumers[newUuid] = {
uuid: newUuid,
transport,
stream: outgoingStream,
};
console.log(`Consumer[${newUuid}]: Created for producer '${producerUuid}'`);
// Return UUID and SDP answer
return {
uuid: newUuid,
sdp: answerObj.toString(),
};
}
console.log(`Producer '${producerUuid}' doesn't exist to create consumer for`);
return null;
}
}
module.exports = new MediaServer();