-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMMM-RTSPtoWeb.js
160 lines (141 loc) · 4.82 KB
/
MMM-RTSPtoWeb.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
Module.register("MMM-RTSPtoWeb", {
video: null,
pc: null,
stream: null,
connectTimeout: null,
suspended: false,
suspendedForUserPresence: false,
defaults: {
width: "50%",
token: "",
url: "",
},
start: function () {
if (this.data.hiddenOnStartup) {
// Don't connect if module is going to be hidden
this.suspended = true;
return;
}
this.initializeRTCPeerConnection();
},
suspend: function () {
this.suspended = true;
if (this.stream) {
this.stream.getTracks().forEach(track => {
track.stop();
});
this.pc.close();
this.pc = null;
this.video.srcObject = null;
this.stream = null;
}
},
resume: function () {
this.suspended = false;
this.initializeRTCPeerConnection();
this.updateDom();
},
getStyles: function () {
return [this.name + ".css"];
},
getDom: function () {
if (this.stream) {
this.video = document.createElement("video");
this.video.classList.add("rtw-video");
this.video.autoplay = true;
this.video.controls = false;
this.video.volume = 1;
this.video.muted = true;
this.video.style.maxWidth = this.config.width;
this.video.playsInline = true;
this.video.srcObject = this.stream;
const recover = () => {
this.video.srcObject = this.stream;
this.video.play();
};
this.video.onstalled = recover;
this.video.onerror = recover;
return this.video;
}
const error = document.createElement("div");
error.classList.add("rtw-error", "small");
error.innerHTML = "No data from stream";
return error;
},
notificationReceived: function(notification, payload, sender) {
// Handle USER_PRESENCE events from the MMM-PIR-sensor/similar modules
if (notification === "USER_PRESENCE") {
if (payload) {
this.suspendedForUserPresence = false;
if (this.suspended && !this.hidden) {
this.resume();
}
return;
} else {
this.suspendedForUserPresence = true;
if (!this.suspended) {
this.suspend();
}
}
}
},
socketNotificationReceived: function (notification, payload) {
if (notification === `ANSWER_${this.identifier}`) {
console.log(`${this.name} received answer for ${this.identifier}`);
try {
this.pc.setRemoteDescription(
new RTCSessionDescription({ type: 'answer', sdp: atob(payload) })
)
} catch (e) {
console.warn(e)
}
return;
}
},
async initializeRTCPeerConnection() {
console.log(`${this.name} initializing connection for ${this.identifier}`);
this.stream = new MediaStream();
this.pc = new RTCPeerConnection({
iceServers: [
{
urls: ["stun:stun.l.google.com:19302"],
},
],
sdpSemantics: 'unified-plan'
});
this.pc.onconnectionstatechange = () => {
if (this.pc.connectionState === "failed") {
console.log(`${this.name} connection in failed state, restarting`);
this.pc.close();
this.video.srcObject = null;
this.initializeRTCPeerConnection();
}
};
this.pc.ontrack = (event) => {
this.stream.addTrack(event.track);
};
const pingChannel = this.pc.createDataChannel("ping");
let intervalId;
pingChannel.onopen = () => {
intervalId = setInterval(() => {
try {
pingChannel.send("ping");
} catch (e) {
console.warn(e);
}
}, 1000);
};
pingChannel.onclose = () => {
clearInterval(intervalId);
if (this.suspended) { return; } // Closed due to module being hidden
console.log(`${this.name} ping channel closed; restarting...`);
this.initializeRTCPeerConnection();
};
this.pc.addTransceiver("video", { direction: "recvonly" });
this.pc.onnegotiationneeded = async () => {
const offer = await this.pc.createOffer();
await this.pc.setLocalDescription(offer);
this.sendSocketNotification("OFFER", { url: this.config.url, sdp: btoa(this.pc.localDescription.sdp), identifier: this.identifier })
}
},
});