-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.html
197 lines (161 loc) · 7.96 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="assets/p2p-media-loader-core.min.js"></script>
<script src="assets/p2p-media-loader-shaka.min.js"></script>
<script src="https://github.com/videojs/mux.js/releases/download/v4.4.0/mux.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/shaka-player/2.4.4/shaka-player.compiled.js"></script>
</head>
<body>
<video id="video" width="640" controls autoplay muted></video>
<script>
function initIndexedDb() {
return new Promise((resolve, reject) => {
const request = window.indexedDB.open("assets", 1);
request.onupgradeneeded = (event) => {
console.info("Upgrading from", event.oldVersion, "to", event.newVersion);
const db = event.target.result;
switch (event.oldVersion) {
case 0: { // create DB
db.createObjectStore("assets", { keyPath: ["requestUri", "requestRange", "masterSwarmId"] });
db.createObjectStore("segmentsData", { keyPath: ["id", "masterSwarmId"] });
const segments = db.createObjectStore("segments", { keyPath: ["id", "masterSwarmId"] });
segments.createIndex("masterSwarmId", "masterSwarmId", { multiEntry: true });
} break;
default:
throw new Error("unknown database version:", event.oldVersion);
}
};
request.onerror = (event) => reject(event);
request.onsuccess = (event) => resolve(event.target.result);
});
}
class IdbAssetsStorage {
constructor(db) {
this.db = db;
}
storeAsset(asset) {
return new Promise((resolve, reject) => {
const request = this.db.transaction(["assets"], "readwrite").objectStore("assets")
.put(asset.requestRange === undefined ? {...asset, requestRange: ""} : asset);
request.onerror = (event) => reject(event);
request.onsuccess = (event) => resolve();
});
}
getAsset(requestUri, requestRange, masterSwarmId) {
return new Promise((resolve, reject) => {
const request = this.db.transaction(["assets"]).objectStore("assets")
.get([requestUri, requestRange === undefined ? "" : requestRange, masterSwarmId]);
request.onerror = (event) => reject(event);
request.onsuccess = (event) => resolve(event.target.result);
});
}
destroy() {
}
}
class IdbSegmentsStorage {
constructor(db) {
this.db = db;
}
storeSegment(segment) {
return new Promise((resolve, reject) => {
const segmentWithoutData = {...segment};
delete segmentWithoutData.data;
const transaction = this.db.transaction(["segments", "segmentsData"], "readwrite")
transaction.objectStore("segments").put(segmentWithoutData).onsuccess = () => {
transaction.objectStore("segmentsData").put({
id: segment.id,
masterSwarmId: segment.masterSwarmId,
data: segment.data
});
}
transaction.onerror = (event) => reject(event);
transaction.oncomplete = () => resolve();
});
}
getSegmentsMap(masterSwarmId) {
return new Promise((resolve, reject) => {
const cursor = this.db.transaction(["segments"]).objectStore("segments").index("masterSwarmId").openCursor(IDBKeyRange.only(masterSwarmId));
const result = new Map();
cursor.onerror = (event) => reject(event);
cursor.onsuccess = (event) => {
const cursor = event.target.result;
if (cursor) {
result.set(cursor.value.id, {segment: cursor.value});
cursor.continue();
} else {
resolve(result);
}
};
});
}
getSegment(id, masterSwarmId) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction(["segments", "segmentsData"]);
let segment;
transaction.objectStore("segments").get([id, masterSwarmId]).onsuccess = (event) => {
segment = event.target.result;
if (segment === undefined) {
return;
}
transaction.objectStore("segmentsData").get([id, masterSwarmId]).onsuccess = (event) => {
segment.data = event.target.result.data;
};
};
transaction.onerror = (event) => reject(event);
transaction.oncomplete = () => resolve(segment);
});
}
clean() {}
destroy() {}
}
(async () => {
let registration = await navigator.serviceWorker.register("sw.js");
registration.addEventListener("updatefound", (event) => {
registration.installing.addEventListener("statechange", (event) => {
if (event.target.state === "installed") {
location.reload();
}
});
});
if (registration.active) {
registration.update();
}
const db = await initIndexedDb();
const assetsStorage = new IdbAssetsStorage(db);
const segmentsStorage = new IdbSegmentsStorage(db);
shaka.polyfill.installAll();
if (shaka.Player.isBrowserSupported() && p2pml.shaka.Engine.isSupported()) {
const settings = {
segments: {
assetsStorage: assetsStorage,
},
loader: {
segmentsStorage: segmentsStorage,
trackerAnnounce: ["wss://tracker.novage.com.ua"]
}
};
const engine = new p2pml.shaka.Engine(settings);
function log(message) {
const p = document.createElement("p");
p.innerHTML = message;
document.body.appendChild(p);
}
engine.on(p2pml.core.Events.SegmentLoaded, (segment, peerId) => log(`segment ${segment.url} ${segment.range} loaded via ${peerId ? "P2P" : "HTTP"}`));
engine.on(p2pml.core.Events.PeerConnect, (peer) => log(`peer connected ${peer.id}`));
engine.on(p2pml.core.Events.PeerClose, (peer) => log(`peer disconnected ${peer.id}`));
const video = document.getElementById("video");
const player = new shaka.Player(video);
const onError = function(error) { console.error("Error code", error.code, "object", error); }
player.addEventListener("error", function(event) { onError(event.detail); });
engine.initShakaPlayer(player);
player.configure({ abr: { enabled: false }}); // disable ABR
player.load("https://storage.googleapis.com/shaka-demo-assets/angel-one/dash.mpd").catch(onError);
} else {
document.write("Not supported :(");
}
})();
</script>
</body>
</html>