generated from akshatvg/template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
233 lines (221 loc) · 7.5 KB
/
index.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
223
224
225
226
227
228
229
230
231
232
233
// Create Agora RTC client
var client = AgoraRTC.createClient({ mode: "rtc", codec: "vp8" });
// JavaScript Speech Recognition Init
var SpeechRecognition = window.webkitSpeechRecognition || window.speechRecognition;
var recognition = new webkitSpeechRecognition() || new SpeechRecognition();
var transContent = "";
var noteContent = "";
recognition.continuous = true;
// RTM Global Vars
var isLoggedIn = false;
// Local Tracks
var localTracks = {
videoTrack: null,
audioTrack: null
};
var remoteUsers = {};
// Agora client options
var options = {
appid: null,
channel: null,
uid: null,
token: null,
accountName: null
};
// Join Channel
$("#join-form").submit(async function (e) {
e.preventDefault();
$("#join").attr("disabled", true);
try {
options.appid = $("#appid").val();
options.token = $("#token").val();
options.channel = $("#channel").val();
options.accountName = $('#accountName').val();
await join();
} catch (error) {
console.error(error);
} finally {
$("#leave").attr("disabled", false);
$("#transcribe").attr("disabled", false);
$("#note").attr("disabled", false);
}
})
// Leave Channel
$("#leave").click(function (e) {
leave();
})
// Join Function
async function join() {
// Add event listener to play remote tracks when remote user publishes
client.on("user-published", handleUserPublished);
client.on("user-unpublished", handleUserUnpublished);
// Join a channel and create local tracks, we can use Promise.all to run them concurrently
[options.uid, localTracks.audioTrack, localTracks.videoTrack] = await Promise.all([
// Join the channel
client.join(options.appid, options.channel, options.token || null),
// Create local tracks, using microphone and camera
AgoraRTC.createMicrophoneAudioTrack(),
AgoraRTC.createCameraVideoTrack()
]);
// Play local video track
localTracks.videoTrack.play("local-player");
$("#local-player-name").text(`localVideo(${options.uid})`);
// Publish local tracks to channel
await client.publish(Object.values(localTracks));
console.log("Publish success");
// Create Agora RTM client
const clientRTM = AgoraRTM.createInstance($("#appid").val(), { enableLogUpload: false });
var accountName = $('#accountName').val();
// Login
clientRTM.login({ uid: accountName }).then(() => {
console.log('AgoraRTM client login success. Username: ' + accountName);
isLoggedIn = true;
// RTM Channel Join
var channelName = $('#channel').val();
channel = clientRTM.createChannel(channelName);
channel.join().then(() => {
console.log('AgoraRTM client channel join success.');
// Start transcription for all (RTM)
$("#transcribe").click(function () {
console.log('Voice recognition is on.');
$("#transcribe").attr("disabled", true);
$("#stop-transcribe").attr("disabled", false);
$("#stop-note").attr("disabled", true);
$("#note").attr("disabled", true);
if (transContent.length) {
transContent += ' ';
}
recognition.start();
});
// Stop transcription for all (RTM)
$("#stop-transcribe").click(function () {
console.log('Voice recognition is off.');
recognition.stop();
recognition.onresult = function (event) {
var current = event.resultIndex;
var transcript = event.results[current][0].transcript;
transContent = transContent + transcript + "<br>";
singleMessage = transContent;
channel.sendMessage({ text: singleMessage }).then(() => {
console.log("Message sent successfully.");
console.log("Your message was: " + singleMessage + " by " + accountName);
$("#actual-text").append("<br> <b>Speaker:</b> " + accountName + "<br> <b>Message:</b> " + singleMessage + "<br>");
transContent = ''
}).catch(error => {
console.log("Message wasn't sent due to an error: ", error);
});
};
$("#note").attr("disabled", false);
$("#stop-note").attr("disabled", true);
$("#stop-transcribe").attr("disabled", true);
$("#transcribe").attr("disabled", false);
});
// Receive RTM Channel Message
channel.on('ChannelMessage', ({ text }, senderId) => {
console.log("Message received successfully.");
console.log("The message is: " + text + " by " + senderId);
$("#actual-text").append("<br> <b>Speaker:</b> " + senderId + "<br> <b>Message:</b> " + text + "<br>");
});
}).catch(error => {
console.log('AgoraRTM client channel join failed: ', error);
}).catch(err => {
console.log('AgoraRTM client login failure: ', err);
});
});
document.getElementById("leave").onclick = async function () {
console.log("Client logged out of RTM.");
await clientRTM.logout();
}
}
// Leave Function
async function leave() {
for (trackName in localTracks) {
var track = localTracks[trackName];
if (track) {
track.stop();
track.close();
localTracks[trackName] = undefined;
}
}
// Remove remote users and player views
remoteUsers = {};
$("#remote-playerlist").html("");
// Leave the channel
await client.leave();
$("#local-player-name").text("");
$("#join").attr("disabled", false);
$("#leave").attr("disabled", true);
$("#note").attr("disabled", true);
$("#transcribe").attr("disabled", true);
$("#stop-transcribe").attr("disabled", true);
$("#stop-note").attr("disabled", true);
console.log("Client leaves channel success");
}
// Subscribe function
async function subscribe(user, mediaType) {
const uid = user.uid;
// Subscribe to a remote user
await client.subscribe(user, mediaType);
console.log("Subscribe success");
if (mediaType === 'video') {
const player = $(`
<div id="player-wrapper-${uid}">
<p class="player-name">remoteUser(${uid})</p>
<div id="player-${uid}" class="player"></div>
</div>
`);
$("#remote-playerlist").append(player);
user.videoTrack.play(`player-${uid}`);
}
if (mediaType === 'audio') {
user.audioTrack.play();
}
}
// User published callback
function handleUserPublished(user, mediaType) {
const id = user.uid;
remoteUsers[id] = user;
subscribe(user, mediaType);
}
// User unpublish callback
function handleUserUnpublished(user) {
const id = user.uid;
delete remoteUsers[id];
$(`#player-wrapper-${id}`).remove();
}
// Start self notes
$("#note").click(function () {
console.log('Voice recognition is on.');
$("#stop-note").attr("disabled", false);
$("#note").attr("disabled", true);
$("#stop-transcribe").attr("disabled", true);
$("#transcribe").attr("disabled", true);
if (noteContent.length) {
noteContent += ' ';
}
recognition.start();
});
// Stop self notes
$("#stop-note").click(function () {
console.log('Voice recognition is off.');
recognition.stop();
recognition.onresult = function (event) {
var current = event.resultIndex;
var transcript = event.results[current][0].transcript;
noteContent = noteContent + transcript + "<br>";
$("#note-text").append("<b><i>You said: </i></b> " + noteContent);
noteContent = '';
};
$("#note").attr("disabled", false);
$("#stop-note").attr("disabled", true);
$("#stop-transcribe").attr("disabled", true);
$("#transcribe").attr("disabled", false);
});
// Can't recognise voice
recognition.onerror = function (event) {
if (event.error == 'no-speech') {
console.log('Could you please repeat? I didn\'t get what you\'re saying.');
recognition.stop();
recognition.start();
}
}