-
Notifications
You must be signed in to change notification settings - Fork 62
/
mediaserver.js
224 lines (162 loc) · 5.75 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const getPort = require('get-port');
const medoozeMediaServer = require('medooze-media-server');
const format = require('string-format');
const execa = require('execa');
const SemanticSDP = require('semantic-sdp');
const SDPInfo = SemanticSDP.SDPInfo;
const MediaInfo = SemanticSDP.MediaInfo;
const CandidateInfo = SemanticSDP.CandidateInfo;
const DTLSInfo = SemanticSDP.DTLSInfo;
const ICEInfo = SemanticSDP.ICEInfo;
const StreamInfo = SemanticSDP.StreamInfo;
const TrackInfo = SemanticSDP.TrackInfo;
const Direction = SemanticSDP.Direction;
const CodecInfo = SemanticSDP.CodecInfo;
const videoPt = 96;
const audioPt = 100;
const videoCodec = 'h264';
const audioCodec = 'opus';
let videoPort = null;
let audioPort = null;
//const RTMP_TO_RTP = 'ffmpeg -fflags nobuffer -i rtmp://ali.wangxiao.eaydu.com/live_bak/x_100_rtc_test -vcodec copy -an -bsf:v h264_mp4toannexb -f rtp -payload_type {pt} rtp://127.0.0.1:{port}'
const RTMP_TO_RTP = "gst-launch-1.0 -v rtmpsrc location=rtmp://ali.wangxiao.eaydu.com/live_bak/x_100_rtc_test ! flvdemux ! h264parse ! rtph264pay config-interval=-1 pt={pt} ! udpsink host=127.0.0.1 port={port}"
class MediaServer
{
constructor(publicIp)
{
this.endpoint = medoozeMediaServer.createEndpoint(publicIp);
medoozeMediaServer.enableDebug(true);
medoozeMediaServer.enableUltraDebug(true);
this.streams = new Map();
}
getStream(streamName)
{
return this.streams.get(streamName)
}
removeStream(streamName)
{
stream = this.streams.get(streamName)
if (stream) {
if (stream.videoStreamer) {
stream.videoStreamer.stop()
}
if (stream.audioStreamer) {
stream.audioStreamer.stop()
}
}
this.streams.delete(streamName)
}
async createStream(streamName,rtmpUrl)
{
const videoStreamer = medoozeMediaServer.createStreamer();
const audioStreamer = medoozeMediaServer.createStreamer();
const video = new MediaInfo(streamName+':video','video');
const audio = new MediaInfo(streamName+':audio','audio');
//Add h264 codec
video.addCodec(new CodecInfo(videoCodec,videoPt));
audio.addCodec(new CodecInfo(audioCodec,audioPt));
if (!videoPort) {
videoPort = await this.getMediaPort();
audioPort = await this.getMediaPort();
}
const videoSession = videoStreamer.createSession(video, {
local : {
port: videoPort
}
});
const audioSession = audioStreamer.createSession(audio, {
local : {
port: audioPort
}
});
this.streams.set(streamName, {
videoPort: videoPort,
audioPort: audioPort,
videoStreamer: videoStreamer,
audioStreamer: audioStreamer,
video:videoSession,
audio:audioSession
});
let rtmp_to_rtp = format(RTMP_TO_RTP, {stream:streamName, pt: videoPt, port: videoPort});
console.log('rtmp_to_rtp ', rtmp_to_rtp);
const gst = execa.shell(rtmp_to_rtp);
gst.on('close', (code, signal) => {
console.log('gst close', code, signal)
})
gst.on('exit', (code, signal) => {
console.log(code, signal)
})
videoPort = null;
audioPort = null;
}
async getMediaPort()
{
let port;
while(true)
{
port = await getPort();
if(port%2 == 0){
break;
}
}
return port;
}
async offerStream(streamName, offerStr)
{
let offer = SDPInfo.process(offerStr);
const transport = this.endpoint.createTransport({
dtls : offer.getDTLS(),
ice : offer.getICE()
});
transport.setRemoteProperties({
audio : offer.getMedia('audio'),
video : offer.getMedia('video')
});
//Get local DTLS and ICE info
const dtls = transport.getLocalDTLSInfo();
const ice = transport.getLocalICEInfo();
//Get local candidates
const candidates = this.endpoint.getLocalCandidates();
let answer = new SDPInfo();
answer.setDTLS(dtls);
answer.setICE(ice);
for (let i=0;i<candidates.length;++i)
{
answer.addCandidate(candidates[i]);
}
let audioOffer = offer.getMedia('audio');
if (audioOffer)
{
let audio = new MediaInfo(audioOffer.getId(), 'audio');
//Set recv only
audio.setDirection(Direction.SENDONLY);
//Add it to answer
//answer.addMedia(audio);
}
let videoOffer = offer.getMedia('video');
let video = new MediaInfo(videoOffer.getId(), 'video');
let videocodec = videoOffer.getCodec(videoCodec);
video.addCodec(videocodec);
video.setDirection(Direction.SENDONLY);
answer.addMedia(video);
console.log('answer', answer);
transport.setLocalProperties({
audio : answer.getMedia('audio'),
video : answer.getMedia('video')
});
const outgoingStream = transport.createOutgoingStream({
video: true,
audio: false
});
let videoSession = this.streams.get(streamName).video
videoSession.on('stopped', () => {
transport.stop()
})
// now we only attach video
outgoingStream.getVideoTracks()[0].attachTo(videoSession.getIncomingStreamTrack());
const info = outgoingStream.getStreamInfo();
answer.addStream(info);
return answer.toString();
}
}
module.exports = MediaServer;