forked from AgoraIO/API-Examples-Web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geoFencing.js
229 lines (203 loc) · 7.25 KB
/
geoFencing.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
/*
* These procedures use Agora Video Call SDK for Web to enable local and remote
* users to join and leave a Video Call channel managed by Agora Platform.
*/
/*
* Create an {@link https://docs.agora.io/en/Video/API%20Reference/web_ng/interfaces/iagorartcclient.html|AgoraRTCClient} instance.
*
* @param {string} mode - The {@link https://docs.agora.io/en/Voice/API%20Reference/web_ng/interfaces/clientconfig.html#mode| streaming algorithm} used by Agora SDK.
* @param {string} codec - The {@link https://docs.agora.io/en/Voice/API%20Reference/web_ng/interfaces/clientconfig.html#codec| client codec} used by the browser.
*/
var client = AgoraRTC.createClient({ mode: "rtc", codec: "vp8" });
AgoraRTC.enableLogUpload();
/*
* Clear the video and audio tracks used by `client` on initiation.
*/
var localTracks = {
videoTrack: null,
audioTrack: null
};
/*
* On initiation no users are connected.
*/
var remoteUsers = {};
/*
* On initiation. `client` is not attached to any project or channel for any specific user.
*/
var options = {
appid: null,
channel: null,
uid: null,
token: null
};
var areas = [
{ label: "GLOBAL", detail: "Global", value: "GLOBAL" },
{ label: "ASIA", detail: "Asia, excluding Mainland China", value: "ASIA" },
{ label: "CHINA", detail: "China", value: "CHINA" },
{ label: "EUROPE", detail: "Europe", value: "EUROPE" },
{ label: "INDIA", detail: "India", value: "INDIA" },
{ label: "JAPAN", detail: "Japan", value: "JAPAN" },
{ label: "NORTH_AMERICA", detail: "North America", value: "NORTH_AMERICA" }
]
var area;
/*
* When this page is called with parameters in the URL, this procedure
* attempts to join a Video Call channel using those parameters.
*/
$(() => {
initAreas();
$(".profile-list").delegate("a", "click", function(e){
changeArea(this.getAttribute("label"));
});
var urlParams = new URL(location.href).searchParams;
options.appid = urlParams.get("appid");
options.channel = urlParams.get("channel");
options.token = urlParams.get("token");
options.uid = urlParams.get("uid");
if (options.appid && options.channel) {
$("#uid").val(options.uid);
$("#appid").val(options.appid);
$("#token").val(options.token);
$("#channel").val(options.channel);
$("#join-form").submit();
}
})
/*
* When a user clicks Join or Leave in the HTML form, this procedure gathers the information
* entered in the form and calls join asynchronously. The UI is updated to match the options entered
* by the user.
*/
$("#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.uid = Number($("#uid").val());
await join();
if(options.token) {
$("#success-alert-with-token").css("display", "block");
} else {
$("#success-alert a").attr("href", `index.html?appid=${options.appid}&channel=${options.channel}&token=${options.token}`);
$("#success-alert").css("display", "block");
}
} catch (error) {
console.error(error);
} finally {
$("#leave").attr("disabled", false);
}
})
/*
* Called when a user clicks Leave in order to exit a channel.
*/
$("#leave").click(function (e) {
leave();
})
/*
* Join a channel, then create local video and audio tracks and publish them to the channel.
*/
async function join() {
// Add an 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. Best practice is to use Promise.all and run them concurrently.
[ options.uid, localTracks.audioTrack, localTracks.videoTrack ] = await Promise.all([
// Join the channel.
client.join(options.appid, options.channel, options.token || null, options.uid || null),
// Create tracks to the local microphone and camera.
AgoraRTC.createMicrophoneAudioTrack(),
AgoraRTC.createCameraVideoTrack()
]);
// Play the local video track to the local browser and update the UI with the user ID.
localTracks.videoTrack.play("local-player");
$("#local-player-name").text(`localVideo(${options.uid})`);
// Publish the local video and audio tracks to the channel.
await client.publish(Object.values(localTracks));
console.log("publish success");
}
/*
* Stop all local and remote tracks then leave the channel.
*/
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);
console.log("client leaves channel success");
}
/*
* Add the local use to a remote channel.
*
* @param {IAgoraRTCRemoteUser} user - The {@link https://docs.agora.io/en/Voice/API%20Reference/web_ng/interfaces/iagorartcremoteuser.html| remote user} to add.
* @param {trackMediaType - The {@link https://docs.agora.io/en/Voice/API%20Reference/web_ng/interfaces/itrack.html#trackmediatype | media type} to add.
*/
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();
}
}
/*
* Add a user who has subscribed to the live channel to the local interface.
*
* @param {IAgoraRTCRemoteUser} user - The {@link https://docs.agora.io/en/Voice/API%20Reference/web_ng/interfaces/iagorartcremoteuser.html| remote user} to add.
* @param {trackMediaType - The {@link https://docs.agora.io/en/Voice/API%20Reference/web_ng/interfaces/itrack.html#trackmediatype | media type} to add.
*/
function handleUserPublished(user, mediaType) {
const id = user.uid;
remoteUsers[id] = user;
subscribe(user, mediaType);
}
/*
* Remove the user specified from the channel in the local interface.
*
* @param {string} user - The {@link https://docs.agora.io/en/Voice/API%20Reference/web_ng/interfaces/iagorartcremoteuser.html| remote user} to remove.
*/
function handleUserUnpublished(user, mediaType) {
if (mediaType === 'video') {
const id = user.uid;
delete remoteUsers[id];
$(`#player-wrapper-${id}`).remove();
}
}
async function changeArea (label) {
area = areas.find(profile => profile.label === label);
$(".profile-input").val(`${area.detail}`);
// Specify the region for connection as North America
AgoraRTC.setArea({
areaCode:area.value
})
}
function initAreas () {
areas.forEach(profile => {
$(".profile-list").append(`<a class="dropdown-item" label="${profile.label}" href="#">${profile.label}: ${profile.detail}</a>`)
});
area = areas[0];
$(".profile-input").val(`${area.detail}`);
}