forked from lynckia/licode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleNativeConnection.js
175 lines (150 loc) · 5.55 KB
/
simpleNativeConnection.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
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; // We need this for testing with self signed certs
const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // eslint-disable-line import/no-extraneous-dependencies
const newIo = require('socket.io-client'); // eslint-disable-line import/no-extraneous-dependencies
const nodeUrl = require('url');
const Erizo = require('./erizofc');
const NativeStream = require('./NativeStream.js');
const nativeConnectionManager = require('./NativeConnectionManager.js');
const nativeConnectionHelpers = require('./NativeConnectionHelpers.js');
const logger = require('./logger').logger;
const log = logger.getLogger('ErizoSimpleNativeConnection');
exports.ErizoSimpleNativeConnection = (spec, callback, error) => {
const that = {};
let localStream = {};
const availableStreams = new Map();
let room = '';
that.isActive = false;
localStream.getID = () => 0;
const createToken = (userName, role, tokenCallback) => {
const req = new XMLHttpRequest();
const theUrl = nodeUrl.parse(spec.serverUrl, true);
if (theUrl.protocol !== 'https') {
log.warn('Protocol is not https');
}
if (theUrl.query.room) {
room = theUrl.query.room;
log.info('will Connect to Room', room);
}
theUrl.pathname += 'createToken/';
const url = nodeUrl.format(theUrl);
log.info('Url to createToken', url);
const body = { username: userName, role, room };
req.onreadystatechange = () => {
if (req.readyState === 4) {
if (req.status === 200) {
tokenCallback(req.responseText);
} else {
log.error('Could not get token');
tokenCallback('error');
}
}
};
req.open('POST', url, true);
req.setRequestHeader('Content-Type', 'application/json');
req.send(JSON.stringify(body));
};
const subscribeToStreams = (streams) => {
streams.forEach((stream) => {
if (spec.subscribeConfig) {
log.info('Should subscribe', spec.subscribeConfig);
room.subscribe(stream, spec.subscribeConfig);
}
});
};
const createConnection = () => {
createToken('name', 'presenter', (token) => {
log.info('Getting token', token);
if (token === 'error') {
error(`Could not get token from nuve in ${spec.serverUrl}`);
return;
}
room = Erizo.Room(newIo, nativeConnectionHelpers, nativeConnectionManager, { token });
room.addEventListener('room-connected', (roomEvent) => {
log.info('Connected to room');
callback('room-connected');
that.isActive = true;
subscribeToStreams(roomEvent.streams);
if (spec.publishConfig) {
log.info('Will publish with config', spec.publishConfig);
localStream = NativeStream.Stream(nativeConnectionHelpers, spec.publishConfig);
room.publish(localStream, spec.publishConfig, (id, message) => {
if (id === undefined) {
log.error('ERROR when publishing', message);
error(message);
}
});
}
});
room.addEventListener('stream-added', (roomEvent) => {
log.info('stream added', roomEvent.stream.getID());
callback('stream-added');
if (roomEvent.stream.getID() !== localStream.getID()) {
const streams = [];
streams.push(roomEvent.stream);
subscribeToStreams(streams);
} else {
log.error('Adding localStream, id', localStream.getID());
availableStreams.set(localStream.getID(), localStream);
}
});
room.addEventListener('stream-removed', (roomEvent) => {
callback('stream-removed');
const eventStreamId = roomEvent.stream.getID();
if (eventStreamId !== localStream.getID()) {
room.unsubscribe(roomEvent.stream);
log.info('stream removed', eventStreamId);
}
availableStreams.delete(eventStreamId);
});
room.addEventListener('stream-subscribed', (streamEvent) => {
log.info('stream-subscribed');
callback('stream-subscribed');
availableStreams.set(streamEvent.stream.getID(), streamEvent.stream);
});
room.addEventListener('room-error', (roomEvent) => {
log.error('Room Error', roomEvent.type, roomEvent.message);
error(roomEvent.message);
});
room.addEventListener('room-disconnected', (roomEvent) => {
log.info('Room Disconnected', roomEvent.type, roomEvent.message);
callback(roomEvent.type + roomEvent.message);
});
room.connect();
});
that.sendData = () => {
localStream.sendData({ msg: 'Testing Data Connection' });
};
};
that.close = () => {
log.info('Close');
room.disconnect();
};
that.getStats = () => {
const promises = [];
availableStreams.forEach((stream, streamId) => {
if (stream.pc && stream.pc.peerConnection && stream.pc.peerConnection.connected) {
promises.push(stream.pc.peerConnection.getStats());
} else {
log.warn('No stream to ask for stats: ', streamId);
promises.push({});
}
});
return Promise.all(promises);
};
that.getStatus = () => {
if (availableStreams.length === 0) {
return ['disconnected'];
}
const connectionStatus = [];
availableStreams.forEach((stream) => {
if (!stream.pc || !stream.pc.peerConnection || !stream.pc.peerConnection.connected) {
connectionStatus.push('disconnected');
} else {
connectionStatus.push('connected');
}
});
return connectionStatus;
};
createConnection();
return that;
};