-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
264 lines (201 loc) · 7.36 KB
/
test.html
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<!DOCTYPE html>
<html>
<head>
<title>Eluvio Video Sample</title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
<script src="https://cdn.jsdelivr.net/npm/@eluvio/[email protected]/dist/ElvClient-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
<script src="https://cdn.dashjs.org/latest/dash.all.min.js"></script>
</head>
<body>
<div id="app" class="app">
<div class="videoContainer">
<video
id="video0"
muted="muted"
controls
/>
</div>
<!-- More Video Holders -->
<div class="videoContainer">
<video
id="video1"
muted="muted"
controls
/>
</div>
<div class="videoContainer">
<video
id="video2"
muted="muted"
controls
/>
</div>
<div class="videoContainer">
<video
id="video3"
muted="muted"
controls
/>
</div>
</div>
<script type="text/javascript">
/* index.js */
const privateKey = "0xa9ef73043561d10056e07c67546c1e321e2f89055eb5a7459fd9ae44d75b55bf";
const mnemonic = "ritual render safe tongue destroy evoke original walk fog kick video pipe";
const videoIdList = ["video0", "video1", "video2", "video3"];
/* Utils */
const funcStartTag = (tag) => console.log('######### ' + tag + '() #########');
const funcEndTag = () => console.log('###############################' + '\n');
const objectTag = (name, object, type) => {
if (type) {
console.log(name + ': ');
//console.log(util.inspect(object, {showHidden: false, depth: null})); // too lazy to requre
console.log(object); // Doesn't show depth
console.log();
} else {
console.log(name + ': ' + object + '\n');
}
}
/* Initialize ElvClient with given privateKey */
const init_client = async () => {
funcStartTag('init_client');
const configUrl = "https://demo.net955210.contentfabric.io/config";
const client = await ElvClient.FromConfigurationUrl({configUrl}).catch(err => console.log(err));
const wallet = client.GenerateWallet();
const signer = wallet.AddAccount({"accountName": "main" ,"privateKey": privateKey});
//const signer = wallet.AddAccountFromMnemonic({mnemonic}); // does the same as above
client.SetSigner({signer}); // "main" becomes the current account
objectTag('Wallet', wallet, true);
objectTag('Signer', signer, true);
// Shows how to get balance without keeping Wallet
let balance = await wallet.GetAccountBalance({signer});
objectTag("Account Balance (wallet)", balance, false);
const accountAddress = client.CurrentAccountAddress();
objectTag('Account Address', accountAddress, false);
balance = await client.GetBalance({"address": accountAddress});
objectTag("Account Balance (client)", balance, false);
funcEndTag();
return client;
};
/* Helper: Return a list of all libraries accessible by user */
const get_content_libraries = async (client) => {
const lib_list = await client.ContentLibraries();
objectTag("Content Library List", lib_list, true);
return lib_list;
}
/* Helper: get all contents from one library */
// No await; returns a Promise
const get_content_from_library = async (client, libraryID) => {
const resp = client.ContentObjects({
"libraryId": libraryID,
"filterOptions": {
"select": "name",
"limit": 1,
"latestOnly": true,
}
});
return resp;
}
/* Returns a list of lists of contents */
const get_all_contents_from_all_libraries = async (client) => {
funcStartTag("get_all_contents_from_all_libraries");
const lib_list = await get_content_libraries(client);
var content_list = [];
for (let i = 0; i < lib_list.length; i++) {
content_list[i] = get_content_from_library(client, lib_list[i]);
}
let list = await Promise.all(content_list);
//.then(() => objectTag("Content List", content_list, true))
//.catch(err => console.log(err));// Parallizes content_list
objectTag("Content List", list, true);
funcEndTag();
return list;
}
/* init video players and load */
const PlayHLS = (video, playoutOptions) => {
const playoutUrl =`${playoutOptions.hls.playoutUrl}&player_profile=hls-js`;
const player = new Hls();
player.loadSource(playoutUrl);
player.attachMedia(video);
// Autoplay
player.on(Hls.Events.MANIFEST_PARSED, () => {
video.play();
});
};
const PlayDash = (authToken, video, playoutOptions) => {
const player = dashjs.MediaPlayer().create();
if(playoutOptions.dash.drms) {
const widevineUrl = playoutOptions.dash.drms.widevine.licenseServers
.find(server => server.startsWith("https"));
player.setProtectionData({
"com.widevine.alpha": {
"serverURL": widevineUrl,
"httpRequestHeaders": {
"Authorization": `Bearer ${authToken}`
},
"withCredentials": false
}
});
}
player.initialize(video, playoutOptions.dash.playoutUrl, true);
};
const Load = async (client, objectId, video) => {
funcStartTag("Load");
const availableDRMs = await client.AvailableDRMs();
const playoutOptions = await client.PlayoutOptions({
"objectId": objectId,
protocols: ["dash", "hls"],
drms: availableDRMs
});
console.log('playout');
if(playoutOptions.hls) {
PlayHLS(video, playoutOptions);
} else {
console.log("DASH");
const authToken = await client.GenerateStateChannelToken({"objectId": objectId});
PlayDash(authToken, video, playoutOptions);
}
};
/* Main function */
const main = async() => {
console.log("privateKey : " + privateKey + '\n');
const client = await init_client(); // See init_client to get current Wallet
const content_list = await get_all_contents_from_all_libraries(client);
let video;
let content;
funcStartTag("Starting Loop");
for (let i = 0; i < 4; i++) {
video = document.getElementById(videoIdList[i]);
console.log('video: ' + video);
content = content_list[i]["contents"][0]["id"];
console.log("id: " + content);
Load(client, content, video);
}
console.log("done with loop");
// const video = document.getElementById("video");
}
main().then(console.log("*********DONE*********"));
</script>
<style>
html,
body,
#app {
display: contents;
height: 100%;
width: 100%;
padding: 0;
margin: 0;
}
#app {
display: block;
justify-content: center;
align-items: center;
}
video {
height: 50vh;
margin-bottom: 25vh;
}
</style>
</body>
</html>