-
Notifications
You must be signed in to change notification settings - Fork 0
/
opentok-ports.js
154 lines (131 loc) · 4.38 KB
/
opentok-ports.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
window.opentokPorts = ports => {
const sessions = {};
ports.connect.subscribe(credentials => {
const { apiKey, sessionId, token } = credentials;
if (sessions[sessionId]) {
ports.connectCallback.send({
sessionId,
success: false,
message: `Already called connect once on session with ID: ${sessionId}`
});
return;
}
const session = OT.initSession(apiKey, sessionId);
const streams = {};
sessions[sessionId] = { session, streams, subscribers: {} };
const streamCreated = event => {
const { stream } = event;
const { streamId } = stream;
streams[streamId] = stream;
ports.onStreamCreated.send({ sessionId, streamId });
};
const streamDestroyed = event => {
const { streamId } = event.stream;
delete streams[streamId];
ports.onStreamDestroyed.send({ sessionId, streamId });
};
const eventHandlers = { streamCreated, streamDestroyed };
session.on(eventHandlers);
sessions[sessionId].eventHandlers = eventHandlers;
session.connect(token, err => {
const success = !Boolean(err);
const message = err ? String(err.message) : '';
ports.connectCallback.send({ sessionId, success, message });
});
});
ports.disconnect.subscribe(options => {
const { sessionId } = options;
if (sessions[sessionId]) {
const { session, subscribers, publisher, eventHandlers } = sessions[sessionId];
session.off(eventHandlers);
Object.keys(subscribers).forEach(streamId => {
session.unsubscribe(subscribers[streamId]);
});
session.unpublish(publisher);
session.disconnect();
session.destroy();
delete sessions[sessionId];
}
});
ports.publish.subscribe(options => {
const { sessionId, containerId } = options;
const properties = {}; // FIXME
if (!sessions[sessionId]) {
ports.publishCallback.send({
sessionId,
success: false,
message: `Could not publish to unknown session with ID: ${sessionId}`
});
return;
}
const { session } = sessions[sessionId];
const container = document.createElement('div');
const publisher = session.publish(container, properties, err => {
const success = !Boolean(err);
const message = err ? String(err.message) : '';
const streamId = success ? publisher.stream.streamId : '';
ports.publishCallback.send({ sessionId, streamId, success, message });
});
const appendContainer = () => {
const el = document.getElementById(containerId);
if (el) {
el.appendChild(container);
} else {
setTimeout(appendContainer, 100);
}
};
publisher.once('videoElementCreated', appendContainer);
sessions[sessionId].publisher = publisher;
});
ports.subscribe.subscribe(options => {
const { sessionId, streamId, containerId } = options;
const properties = {}; // FIXME
if (!sessions[sessionId]) {
ports.subscribeCallback.send({
sessionId,
streamId,
success: false,
message: `Could not subscribe to unknown session with ID: ${sessionId}`
});
return;
}
const { session, streams, subscribers } = sessions[sessionId];
const stream = streams[streamId];
if (!stream) {
ports.subscribeCallback.send({
sessionId,
streamId,
success: false,
message: `Could not subscribe to unknown stream with ID: ${streamId}`
});
return;
}
const container = document.createElement('div');
const subscriber = session.subscribe(stream, container, properties, err => {
const success = !Boolean(err);
const message = err ? String(err.message) : '';
ports.subscribeCallback.send({ sessionId, streamId, success, message });
});
const appendContainer = () => {
const el = document.getElementById(containerId);
if (el) {
el.appendChild(container);
} else {
setTimeout(appendContainer, 100);
}
};
subscriber.once('videoElementCreated', appendContainer);
subscribers[streamId] = subscriber;
});
ports.unsubscribe.subscribe(options => {
const { sessionId, streamId } = options;
if (!sessions[sessionId]) {
return;
}
const { session, subscribers } = sessions[sessionId];
if (!subscribers[streamId]) {
return;
}
session.unsubscribe(subscribers[streamId]);
});
};