-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhoast360.js
299 lines (242 loc) · 10.6 KB
/
hoast360.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
==============================================================================
This file is part of hoast360, the open-source, higher-order Ambisonics, 360
degree audio/video player.
https://github.com/thomasdeppisch/hoast360
Authors: Thomas Deppisch, Nils Meyer-Kahlen
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
==============================================================================
*/
import * as dashjs from 'dashjs';
import videojs from 'video.js';
import 'videojs-contrib-dash'
import 'videojs-http-source-selector';
import 'videojs-contrib-quality-levels';
import 'videojs-xr';
import MatrixMultiplier from './dependencies/MatrixMultiplier.js';
import { zoomMtx, stepsize, minZoomfactor, maxZoomfactor } from './dependencies/HoastZoom.js';
import PlaybackEventHandler from './dependencies/PlaybackEventHandler.js';
import HOASTloader from './dependencies/HoastLoader.js';
import HOASTBinDecoder from './dependencies/HoastBinauralDecoder.js';
import HOASTRotator from './dependencies/HoastRotator.js';
import { isMobileTabletVRDevice } from './dependencies/UserAgentChecker.js';
import './css/video-js.css';
import './css/hoast360.css';
"use strict";
export class HOAST360 {
constructor() {
this.order = 0;
this.irs = '';
this.mediaUrl = '';
this.irUrl = '';
this.audioPlayer = null;
this.sourceNode = null;
this.audioSetupComplete = false;
this.videoSetupComplete = false;
this.xrActive = false;
this.context = null;
this.rotator = null;
this.multiplier = null;
this.decoder = null;
this.masterGain = 0;
this.numCh = 0;
this.videoPlayer = null;
this.maxOrder = 4;
this.opusSupport = true;
this.zoomIndex = 1;
this.zoomEnabled = true;
var AudioContext = window.AudioContext || window.webkitAudioContext;
this.context = new AudioContext;
console.log(this.context);
if (isMobileTabletVRDevice()) {
this.zoomEnabled = false; // disable zoom on mobile and VR devices to improve efficiency
console.log('detected mobile device: zoom disabled');
}
this.playbackEventHandler = new PlaybackEventHandler(this.context);
// create as many audio players as we need for max order
this.audioElement = new Audio();
if (this.audioElement.canPlayType('audio/ogg; codecs="opus"') === '') {
this.opusSupport = false;
}
this.videoPlayer = videojs('hoast360-player', {
html5: { nativeCaptions: false },
liveui: true,
plugins: {
httpSourceSelector: { default: 'auto' }
}
});
}
initialize(newMediaUrl, newIrUrl, newOrder) {
if (!this.opusSupport) {
this.videoPlayer.error('Error: Your browser does not support the OPUS audio codec. Please use Firefox or Chrome-based browsers.');
return;
}
this.videoPlayer.xr();
console.log(this.videoPlayer);
console.log(this.videoPlayer.xr());
this.audioSetupComplete = false;
this.videoSetupComplete = false;
if (this.order > this.maxOrder)
console.error('Ambisonic orders greater than 4 not supported!');
this.order = newOrder;
this.mediaUrl = newMediaUrl;
this.irUrl = newIrUrl;
this._setOrderDependentVariables();
if (this.mediaUrl.includes(".mpd")) { // in this case audio and video are inside the same mpd
if (!this.sourceNode)
this.sourceNode = this.context.createMediaElementSource(this.videoPlayer.tech({ IWillNotUseThisInPlugins: true }).el());
this.videoPlayer.src({ type: 'application/dash+xml', src: this.mediaUrl });
this.audioPlayer = null;
} else { // load audio and video from separate mpds
this.audioPlayer = dashjs.MediaPlayer().create();
if (!this.sourceNode)
this.sourceNode = this.context.createMediaElementSource(this.audioElement);
this.videoPlayer.src({ type: 'application/dash+xml', src: this.mediaUrl + 'video.mpd' });
this.audioPlayer.initialize(this.audioElement);
this.audioPlayer.setAutoPlay(false);
this.audioPlayer.attachSource(this.mediaUrl + "audio.mpd");
}
let scope = this;
this.videoPlayer.xr().on("initialized", function () {
console.log("xr initialized");
scope._startSetup();
// playback event handler is only needed if we have separate audio and video players
if (scope.audioPlayer)
scope.playbackEventHandler.initialize(scope.videoPlayer, scope.audioPlayer);
});
}
reset() {
if (!this.opusSupport) {
this.videoPlayer.reset();
return;
}
if (this.audioPlayer)
this.playbackEventHandler.reset();
this.videoPlayer.pause();
this._disconnectAudio();
this.videoPlayer.xr().reset();
this.videoPlayer.dash.mediaPlayer.reset();
this.videoPlayer.reset(); // this triggers an error "failed to remove source buffer from media source", but seems to work anyway
if (this.audioPlayer)
this.audioPlayer.reset();
}
_disconnectAudio() {
this.sourceNode.disconnect();
this.rotator.out.disconnect();
this.multiplier.out.disconnect();
this.decoder.out.disconnect();
this.masterGain.disconnect();
}
_startSetup() {
if (!this.audioSetupComplete && !this.videoSetupComplete) {
this._setupAudio();
this._setupVideo();
}
}
_setupAudio() {
let scope = this;
// initialize ambisonic rotator
this.rotator = new HOASTRotator(this.context, this.order);
console.log(this.rotator);
// initialize matrix multiplier (for now use always 4th order as zoom matrix is in 4th order format)
this.multiplier = new MatrixMultiplier(this.context, 4);
console.log(this.multiplier);
this.decoder = new HOASTBinDecoder(this.context, this.order);
console.log(this.decoder);
var loader_filters = new HOASTloader(this.context, this.order, this.irs, (foaBuffer, hoaBuffer) => {
this.decoder.updateFilters(foaBuffer, hoaBuffer);
if (this.audioPlayer)
this.playbackEventHandler.setAllBuffersLoaded(true);
});
loader_filters.load();
this.masterGain = this.context.createGain();
this.masterGain.gain.value = 1.0;
this.videoPlayer.on("volumechange", function () {
if (!scope.masterGain)
return;
if (this.muted())
scope.masterGain.gain.value = 0;
else
scope.masterGain.gain.value = this.volume();
});
this.sourceNode.channelCount = this.numCh;
this.sourceNode.connect(this.rotator.in);
if (this.zoomEnabled) {
this.rotator.out.connect(this.multiplier.in);
this.multiplier.out.connect(this.decoder.in);
}
else {
this.rotator.out.connect(this.decoder.in);
}
this.decoder.out.connect(this.masterGain);
this.masterGain.connect(this.context.destination);
this.audioSetupComplete = true;
}
_setupVideo() {
this.videoPlayer.xr().camera.rotation.order = 'YZX'; // in THREE Y is vertical axis! -> set to yaw-pitch-roll
let vidControls = this.videoPlayer.xr().controls3d;
vidControls.orbit.minDistance = -700;
vidControls.orbit.maxDistance = 200;
let scope = this;
// this.controls3d.orbit.on( .. ) does not work for custom events!
// view change
vidControls.orbit.addEventListener("change", function () {
if (scope.xrActive)
return;
scope.rotator.updateRotationFromCamera(scope.videoPlayer.xr().camera.matrixWorld.elements);
});
// view change if HMD is used
this.videoPlayer.xr().on("xrCameraUpdate", function () {
if (!scope.xrActive)
return;
scope.rotator.updateRotationFromCamera(this.xrPose.views[0].transform.matrix);
});
if (this.zoomEnabled) {
vidControls.orbit.addEventListener("zoom", function () { // zoom change
scope._updateZoom();
});
}
this.videoPlayer.xr().on("xrSessionActivated", function () {
scope.xrActive = true;
scope.multiplier.bypass(true);
});
this.videoPlayer.xr().on("xrSessionDeactivated", function () {
scope.xrActive = false;
scope.multiplier.bypass(false);
if (scope.zoomEnabled)
scope._updateZoom();
scope.rotator.updateRotationFromCamera(this.camera.matrixWorld.elements);
});
this.videoSetupComplete = true;
}
_updateZoom() {
let currentDistance = this.videoPlayer.xr().controls3d.orbit.currentDistance;
let minDistance = this.videoPlayer.xr().controls3d.orbit.minDistance;
let zoomFactor = (minDistance + currentDistance) / minDistance;
if (zoomFactor >= minZoomfactor && zoomFactor <= maxZoomfactor) {
let newZoomIndex = Math.round((zoomFactor - minZoomfactor) / stepsize);
if (newZoomIndex != this.zoomIndex) {
this.multiplier.updateMtx(zoomMtx[newZoomIndex]);
this.zoomIndex = newZoomIndex;
}
}
}
_setOrderDependentVariables() {
let getUrl = window.location;
let base_url = getUrl.protocol + "//" + getUrl.host + "/"
this.numCh = (this.order + 1) * (this.order + 1);
if (this.irUrl.includes("://")) // protocol already included
this.irs = this.irUrl + 'hoast_o' + this.order + '.wav';
else
this.irs = base_url + this.irUrl + 'hoast_o' + this.order + '.wav';
}
}