/
PlayerExperience.js
201 lines (160 loc) · 5.88 KB
/
PlayerExperience.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
import { Experience, View, client } from 'soundworks/client';
const template = `
<div class="video-wrapper">
<video id="video" controls></video>
</div>
<% if (!isEnv) { %>
<button class="btn" id="reload">Reload</button>
<p id="infos">
<span id="part"><%= part %></span>
-
<span id="section-label"><%= sectionLabel %></span>
</p>
<% } %>
`;
class PlayerExperience extends Experience {
constructor(score) {
super();
this.score = score;
this.platform = this.require('platform', { features: [] });
this.placer = this.require('placer');
this.sharedParams = this.require('shared-params');
this.syncScheduler = this.require('sync-scheduler');
this.videoLoader = this.require('video-loader');
this.onTransportChange = this.onTransportChange.bind(this);
this.onUpdateTime = this.onUpdateTime.bind(this);
this.updateLabel = this.updateLabel.bind(this);
this.onFirstPlay = this.onFirstPlay.bind(this);
this.reload = this.reload.bind(this);
this.isEnv = false;
this.isReady = false; // don't listen controls if not ready
}
start() {
super.start();
this.part = this.score.parts[client.label];
if (this.part.type === 'env')
this.isEnv = true;
// initialize the view
const model = {
sectionLabel: `<span class="orange soft-blink">start the video and wait for the beginning</span>`,
part: client.label,
isEnv: this.isEnv,
};
const events = {
'touchstart #reload': () => this.reload(true),
};
this.view = new View(template, model, events, {});
Promise.all([this.show(), this.videoLoader.load(this.part.file)])
.then(([empty, objectUrl]) => {
this.$video = this.view.$el.querySelector('#video');
this.$video.src = objectUrl;
this.receive('transport', this.onTransportChange);
this.receive('updateTime', this.onUpdateTime);
// this.sharedParams.addParamListener('playbackRate', (value) => this.$video.playbackRate = value);
// this.sharedParams.addParamListener('seek', (value) => this.$video.currentTime = value);
this.sharedParams.addParamListener('reload', () => this.reload(false));
if (this.part.type !== 'env')
this.sharedParams.addParamListener('volume:performers', (value) => this.$video.volume = value);
else
this.sharedParams.addParamListener(`volume:env:${client.label}`, (value) => this.$video.volume = value);
// update label according to video current time
this.$video.addEventListener('timeupdate', this.updateLabel);
this.$video.addEventListener('play', this.onFirstPlay);
});
}
// @todo - remove that, use platform hook...
onFirstPlay() {
setTimeout(() => {
this.$video.pause()
// remove controls
this.$video.removeAttribute('controls');
if (!this.isEnv)
alert('click "ok" and wait for the beginning...');
this.isReady = true; // don't listen controls if not ready
// feedback for the controller
this.send('ready');
this.$video.removeEventListener('play', this.onFirstPlay);
}, 0);
}
onTransportChange(state, transportTime, triggerSyncTime) {
if (!this.isReady)
return;
// console.log(state, transportTime, triggerSyncTime);
const currentSyncTime = this.syncScheduler.currentTime;
// message received to late execute now and compensate if state is Start
if (triggerSyncTime < currentSyncTime) {
if (state === 'Start' ) {
const decay = currentSyncTime - triggerSyncTime;
this.$video.currentTime = transportTime + decay;
this.$video.play();
} else {
this.$video.pause();
this.$video.currentTime = transportTime;
}
} else {
// defer execution to triggerSyncTime
this.syncScheduler.defer(() => {
if (state === 'Start') {
this.$video.currentTime = transportTime;
this.$video.play();
} else {
this.$video.pause();
this.$video.currentTime = transportTime;
}
}, triggerSyncTime);
}
}
// this is triggered every tickPeriod by the server to maintain every client
// into an acceptable state, or recover if a problem occured
onUpdateTime(transportTime, triggerSyncTime) {
if (!this.isReady)
return;
// console.log(transportTime, triggerSyncTime);
const syncTime = this.syncScheduler.currentTime;
// just wait for the next message if received too late
if (triggerSyncTime > syncTime) {
this.syncScheduler.defer(() => {
const videoCurrentTime = this.$video.currentTime;
const jit = Math.abs(transportTime - videoCurrentTime);
// we assume a large possible jitter as we have no f****** idea of the
// video clock resolution... this should be explored
if (jit > 0.5) {
this.$video.currentTime = transportTime;
}
}, triggerSyncTime);
}
}
reload(confirm = false) {
if (confirm === true) {
const isConfirmed = window.confirm('are you sure you want to reload?');
if (!isConfirmed) return;
}
window.location.reload();
}
updateLabel() {
if (this.isEnv)
return;
const sections = this.score.sections;
const currentTime = this.$video.currentTime;
const names = Object.keys(sections);
let label = null;
for (let i = 0; i < names.length; i++) {
const section = sections[names[i]];
const next = sections[names[i + 1]];
if (next) {
if (!label && currentTime >= section.time && currentTime < next.time) {
label = section.label;
break;
}
} else {
if (!label && currentTime >= section.time)
label = section.label;
}
};
if (this.view.model.sectionLabel !== label) {
this.view.model.sectionLabel = label;
this.view.render('#section-label');
}
}
}
export default PlayerExperience;