-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
123 lines (88 loc) · 4.31 KB
/
main.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
// credentials - apiKey and apiSecret of application, token for permission, session as kind of room name
var apiKey = '46424572'; //46424572
var apiSecret = ''; //
var sessionId = '1_MX40NjQyNDU3Mn5-MTU2ODkwMTc2NTM1NX5OSVlVNUZYbHlIbmF2ekRuam9IY3NoMlV-fg';
var token = 'T1==cGFydG5lcl9pZD00NjQyNDU3MiZzaWc9NjdkNzA1ZWM3MTc4ODAxM2EyY2Q5ODlkN2IxOTRmMWNmNjJkYWY3YzpzZXNzaW9uX2lkPTFfTVg0ME5qUXlORFUzTW41LU1UVTJPRGt3TVRjMk5UTTFOWDVPU1ZsVk5VWlliSGxJYm1GMmVrUnVhbTlJWTNOb01sVi1mZyZjcmVhdGVfdGltZT0xNTY4OTAxOTMxJm5vbmNlPTAuMDM0OTk0MzQxNjk0MDY2NTY2JnJvbGU9cHVibGlzaGVyJmV4cGlyZV90aW1lPTE1NzE0OTM5MjgmaW5pdGlhbF9sYXlvdXRfY2xhc3NfbGlzdD0=';
// (optional) add server code here
// initializeSession();
/**
* connectionEventsSuppressed: true
*
*/
var props = {};
var session = OT.initSession(apiKey, sessionId, props);
const broadcastToOriginal = document.querySelector('#broadcastToOriginal');
if (broadcastToOriginal) {
broadcastToOriginal.addEventListener('click', () => {
console.log('clicked to broadcastToOriginal');
// create publisher
var publisher = OT.initPublisher('publisher',
{insertMode: 'append', width: '100%', height: '100px'},
(error) => handleError(error)
);
/**
* publish to publisher
* max connection = 3000 per publisher
* max stream = 3000 per publisher
*/
session.connect(token, (error) => {
if (error) {
handleError(error);
}
else {
// publish publisher
console.log('session connected');
session.publish(publisher,
(error) => handleError(error))
.on('streamCreated',
(event) => { console.log('publisher streaming')});
}
});
});
}
const subscribeToOriginal = document.querySelector('#subscribeToOriginal');
if (subscribeToOriginal) {
subscribeToOriginal.addEventListener('click', () => {
console.log(`subscribeToOriginal ${session.sessionId}`);
// create subscriber
session.on('streamCreated', (event) => {
console.log('subscribing to original stream');
session.subscribe(event.stream,
'subscriber',
{insertMode: 'append', width: '100%', height: '250px'}
);
})
.connect(token, (error) => handleError(error));
});
}
const selectChannels = document.querySelector('#selectChannels')
if (selectChannels) {
selectChannels.addEventListener('click', () => {
console.log('selectChannels')
});
}
const broadcastToNewChannel = document.querySelector('#broadcastToNewChannel');
if (broadcastToNewChannel) {
broadcastToNewChannel.addEventListener('click', () => {
console.log('broadcastToNewChannel')
});
}
function handleError(error) {
if (error) {
console.log(error.message);
if (error.name === "OT_MEDIA_ERR_NETWORK") { console.log('Network error'); }
else if (error.name === "OT_MEDIA_ERR_ABORTED") { console.log('Stream fetching is aborted'); }
else if (error.name === "OT_HARDWARE_UNAVAILABLE") { console.log('Camera or microphone is not acquired. May be using by another application.'); }
else if (error.name === "OT_NOT_CONNECTED") { console.log('You are not connected to internet'); }
else if (error.name === "OT_AUTHENTICATION_ERROR") { console.log(''); }
else if (error.name === "OT_CONNECT_FAILED") { console.log(''); }
else if (error.name === "OT_CONNECTION_LIMIT_EXCEEDED") { console.log('max connection limit 3000 exceeded.'); } //stream.connect - Consider broadcasting ?
else if (error.name === "OT_STREAM_LIMIT_EXCEEDED") { console.log('max stream limit 3000 exceeded.'); } //stream.subscribe - Consider broadcasting ?
else if (error.name === "OT_INVALID_SESSION_ID") { console.log(''); }
else if (error.name === "OT_CHROME_MICROPHONE_ACQUISITION_ERROR") { console.log('Chrome fails to get access to the microphone, due to a Chrome err. Please Restart Chrome browser!'); }
else if (error.name === "OT_CREATE_PEER_CONNECTION_FAILED") { console.log('Connection error between client and subscriber or media router'); }
else if (error.name === "OT_HARDWARE_UNAVAILABLE") { console.log('Camera or microphone is not acquired. May be using by another application.'); }
else if (error.name === "OT_ICE_WORKFLOW_FAILED") { console.log('Something wrong while establishing webRTC connectivity'); }
else if (error.name === "OT_USER_MEDIA_ACCESS_DENIED") { console.log('You denied permisson for microphone/camera'); }
}
}