wrplayer 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,392 @@
1
+ import WRPlayer from '../../core/WRPlayer.js';
2
+
3
+ /**
4
+ * Vanilla JavaScript Wrapper for WRPlayer with PTZ and Voice Intercom support
5
+ * Provides a more convenient API for vanilla JS usage
6
+ */
7
+ export class WRPlayerWrapper {
8
+ /**
9
+ * Create a new WRPlayer wrapper instance
10
+ * @param {HTMLVideoElement|string} element - Video element or selector
11
+ * @param {Object} config - Player configuration
12
+ * @param {Object} [callbacks] - Event callbacks
13
+ */
14
+ constructor(element, config, callbacks = {}) {
15
+ // Get element reference
16
+ this.element = typeof element === 'string'
17
+ ? document.querySelector(element)
18
+ : element;
19
+
20
+ if (!this.element) {
21
+ throw new Error('WRPlayerWrapper: Video element not found');
22
+ }
23
+
24
+ this.config = config;
25
+ this.callbacks = callbacks;
26
+ this.player = null;
27
+ this.isReady = false;
28
+ this.error = null;
29
+ this.ptzAvailable = false;
30
+ this.voiceIntercomAvailable = false;
31
+
32
+ this._init();
33
+ }
34
+
35
+ /**
36
+ * Initialize the player
37
+ * @private
38
+ */
39
+ _init() {
40
+ try {
41
+ this.player = new WRPlayer({
42
+ element: this.element,
43
+ ...this.config
44
+ });
45
+
46
+ this.ptzAvailable = !!this.player.ptz;
47
+ this.voiceIntercomAvailable = !!this.player.voiceIntercom;
48
+
49
+ // Set up event listeners
50
+ this._setupEventListeners();
51
+
52
+ } catch (err) {
53
+ this.error = { type: 'init_error', details: err };
54
+ if (this.callbacks.onError) {
55
+ this.callbacks.onError(this.error);
56
+ }
57
+ }
58
+ }
59
+
60
+ /**
61
+ * Set up event listeners
62
+ * @private
63
+ */
64
+ _setupEventListeners() {
65
+ if (!this.player) return;
66
+
67
+ // Player ready event
68
+ this.player.on('ready', () => {
69
+ this.isReady = true;
70
+ this.error = null;
71
+ if (this.callbacks.onReady) {
72
+ this.callbacks.onReady(this);
73
+ }
74
+ });
75
+
76
+ // Player error event
77
+ this.player.on('error', (errorData) => {
78
+ this.error = errorData;
79
+ this.isReady = false;
80
+ if (this.callbacks.onError) {
81
+ this.callbacks.onError(errorData);
82
+ }
83
+ });
84
+
85
+ // Player state events
86
+ const stateEvents = ['play', 'pause', 'ended', 'timeupdate'];
87
+ stateEvents.forEach(event => {
88
+ this.player.on(event, (data) => {
89
+ const callbackName = `on${event.charAt(0).toUpperCase()}${event.slice(1)}`;
90
+ if (this.callbacks[callbackName]) {
91
+ this.callbacks[callbackName](data);
92
+ }
93
+ });
94
+ });
95
+
96
+ // PTZ events
97
+ if (this.ptzAvailable && this.callbacks.onPtzEvent) {
98
+ const ptzEvents = [
99
+ 'ptz:move', 'ptz:zoom', 'ptz:focus', 'ptz:iris', 'ptz:speed:change',
100
+ 'ptz:preset:list', 'ptz:preset:saved', 'ptz:preset:called', 'ptz:preset:deleted',
101
+ 'ptz:cruise:started', 'ptz:cruise:stopped', 'ptz:cruise:speed:set',
102
+ 'ptz:scan:started', 'ptz:scan:stopped', 'ptz:scan:boundary:left', 'ptz:scan:boundary:right',
103
+ 'ptz:request:success', 'ptz:request:error'
104
+ ];
105
+
106
+ ptzEvents.forEach(event => {
107
+ this.player.on(event, (data) => {
108
+ this.callbacks.onPtzEvent(event, data);
109
+ });
110
+ });
111
+ }
112
+ }
113
+
114
+ /**
115
+ * Start playing the video
116
+ */
117
+ play() {
118
+ if (this.player) {
119
+ this.player.play();
120
+ }
121
+ }
122
+
123
+ /**
124
+ * Pause the video
125
+ */
126
+ pause() {
127
+ if (this.player) {
128
+ this.player.pause();
129
+ }
130
+ }
131
+
132
+ /**
133
+ * Stop the video
134
+ */
135
+ stop() {
136
+ if (this.player) {
137
+ this.player.stop();
138
+ }
139
+ }
140
+
141
+ /**
142
+ * Destroy the player and clean up resources
143
+ */
144
+ destroy() {
145
+ if (this.player) {
146
+ this.player.stop();
147
+ this.player = null;
148
+ }
149
+ this.isReady = false;
150
+ this.error = null;
151
+ this.ptzAvailable = false;
152
+ }
153
+
154
+ /**
155
+ * Get the underlying WRPlayer instance
156
+ * @returns {WRPlayer|null}
157
+ */
158
+ getPlayer() {
159
+ return this.player;
160
+ }
161
+
162
+ /**
163
+ * Check if PTZ control is available
164
+ * @returns {boolean}
165
+ */
166
+ isPtzAvailable() {
167
+ return this.ptzAvailable;
168
+ }
169
+
170
+ /**
171
+ * Get PTZ controller
172
+ * @returns {PTZController|null}
173
+ */
174
+ getPtz() {
175
+ return this.player?.ptz || null;
176
+ }
177
+
178
+ /**
179
+ * PTZ movement control
180
+ * @param {string} direction - Direction ('up', 'down', 'left', 'right', 'stop')
181
+ * @param {number} [speed] - Speed (1-100)
182
+ * @returns {Promise}
183
+ */
184
+ ptzMove(direction, speed) {
185
+ if (this.player?.ptz) {
186
+ return this.player.ptz.move(direction, speed);
187
+ }
188
+ return Promise.reject(new Error('PTZ not available'));
189
+ }
190
+
191
+ /**
192
+ * PTZ zoom control
193
+ * @param {string} direction - Direction ('in', 'out')
194
+ * @param {number} [speed] - Speed (1-100)
195
+ * @returns {Promise}
196
+ */
197
+ ptzZoom(direction, speed) {
198
+ if (this.player?.ptz) {
199
+ return this.player.ptz.zoom(direction, speed);
200
+ }
201
+ return Promise.reject(new Error('PTZ not available'));
202
+ }
203
+
204
+ /**
205
+ * PTZ focus control
206
+ * @param {string} direction - Direction ('near', 'far')
207
+ * @param {number} [speed] - Speed (1-100)
208
+ * @returns {Promise}
209
+ */
210
+ ptzFocus(direction, speed) {
211
+ if (this.player?.ptz) {
212
+ return this.player.ptz.focus(direction, speed);
213
+ }
214
+ return Promise.reject(new Error('PTZ not available'));
215
+ }
216
+
217
+ /**
218
+ * PTZ iris control
219
+ * @param {string} direction - Direction ('in', 'out')
220
+ * @param {number} [speed] - Speed (1-100)
221
+ * @returns {Promise}
222
+ */
223
+ ptzIris(direction, speed) {
224
+ if (this.player?.ptz) {
225
+ return this.player.ptz.iris(direction, speed);
226
+ }
227
+ return Promise.reject(new Error('PTZ not available'));
228
+ }
229
+
230
+ /**
231
+ * Stop all PTZ movements
232
+ * @returns {Promise}
233
+ */
234
+ ptzStop() {
235
+ if (this.player?.ptz) {
236
+ return this.player.ptz.stop();
237
+ }
238
+ return Promise.reject(new Error('PTZ not available'));
239
+ }
240
+
241
+ /**
242
+ * Set PTZ control speed
243
+ * @param {number} speed - Speed (1-100)
244
+ * @returns {WRPlayerWrapper}
245
+ */
246
+ ptzSetSpeed(speed) {
247
+ if (this.player?.ptz) {
248
+ this.player.ptz.setSpeed(speed);
249
+ }
250
+ return this;
251
+ }
252
+
253
+ /**
254
+ * Get current PTZ speed
255
+ * @returns {number}
256
+ */
257
+ ptzGetSpeed() {
258
+ return this.player?.ptz?.getSpeed() || 50;
259
+ }
260
+
261
+ /**
262
+ * Save current position as preset
263
+ * @param {number} presetId - Preset ID (1-255)
264
+ * @returns {Promise}
265
+ */
266
+ ptzSavePreset(presetId) {
267
+ if (this.player?.ptz?.preset) {
268
+ return this.player.ptz.preset.save(presetId);
269
+ }
270
+ return Promise.reject(new Error('PTZ preset not available'));
271
+ }
272
+
273
+ /**
274
+ * Call a preset
275
+ * @param {number} presetId - Preset ID
276
+ * @returns {Promise}
277
+ */
278
+ ptzCallPreset(presetId) {
279
+ if (this.player?.ptz?.preset) {
280
+ return this.player.ptz.preset.call(presetId);
281
+ }
282
+ return Promise.reject(new Error('PTZ preset not available'));
283
+ }
284
+
285
+ /**
286
+ * Delete a preset
287
+ * @param {number} presetId - Preset ID
288
+ * @returns {Promise}
289
+ */
290
+ ptzDeletePreset(presetId) {
291
+ if (this.player?.ptz?.preset) {
292
+ return this.player.ptz.preset.delete(presetId);
293
+ }
294
+ return Promise.reject(new Error('PTZ preset not available'));
295
+ }
296
+
297
+ /**
298
+ * List all presets
299
+ * @returns {Promise<Array>}
300
+ */
301
+ ptzListPresets() {
302
+ if (this.player?.ptz?.preset) {
303
+ return this.player.ptz.preset.list();
304
+ }
305
+ return Promise.reject(new Error('PTZ preset not available'));
306
+ }
307
+
308
+ /**
309
+ * Start cruise
310
+ * @param {number} [cruiseId=1] - Cruise ID
311
+ * @returns {Promise}
312
+ */
313
+ ptzStartCruise(cruiseId = 1) {
314
+ if (this.player?.ptz?.cruise) {
315
+ return this.player.ptz.cruise.start(cruiseId);
316
+ }
317
+ return Promise.reject(new Error('PTZ cruise not available'));
318
+ }
319
+
320
+ /**
321
+ * Stop cruise
322
+ * @param {number} [cruiseId=1] - Cruise ID
323
+ * @returns {Promise}
324
+ */
325
+ ptzStopCruise(cruiseId = 1) {
326
+ if (this.player?.ptz?.cruise) {
327
+ return this.player.ptz.cruise.stop(cruiseId);
328
+ }
329
+ return Promise.reject(new Error('PTZ cruise not available'));
330
+ }
331
+
332
+ /**
333
+ * Start scan
334
+ * @param {number} [scanId=1] - Scan ID
335
+ * @returns {Promise}
336
+ */
337
+ ptzStartScan(scanId = 1) {
338
+ if (this.player?.ptz?.scan) {
339
+ return this.player.ptz.scan.start(scanId);
340
+ }
341
+ return Promise.reject(new Error('PTZ scan not available'));
342
+ }
343
+
344
+ /**
345
+ * Stop scan
346
+ * @param {number} [scanId=1] - Scan ID
347
+ * @returns {Promise}
348
+ */
349
+ ptzStopScan(scanId = 1) {
350
+ if (this.player?.ptz?.scan) {
351
+ return this.player.ptz.scan.stop(scanId);
352
+ }
353
+ return Promise.reject(new Error('PTZ scan not available'));
354
+ }
355
+
356
+ /**
357
+ * Set scan left boundary
358
+ * @param {number} [scanId=1] - Scan ID
359
+ * @returns {Promise}
360
+ */
361
+ ptzSetScanLeftBoundary(scanId = 1) {
362
+ if (this.player?.ptz?.scan) {
363
+ return this.player.ptz.scan.setLeftBoundary(scanId);
364
+ }
365
+ return Promise.reject(new Error('PTZ scan not available'));
366
+ }
367
+
368
+ /**
369
+ * Set scan right boundary
370
+ * @param {number} [scanId=1] - Scan ID
371
+ * @returns {Promise}
372
+ */
373
+ ptzSetScanRightBoundary(scanId = 1) {
374
+ if (this.player?.ptz?.scan) {
375
+ return this.player.ptz.scan.setRightBoundary(scanId);
376
+ }
377
+ return Promise.reject(new Error('PTZ scan not available'));
378
+ }
379
+ }
380
+
381
+ /**
382
+ * Factory function to create WRPlayer instances
383
+ * @param {HTMLVideoElement|string} element - Video element or selector
384
+ * @param {Object} config - Player configuration
385
+ * @param {Object} [callbacks] - Event callbacks
386
+ * @returns {WRPlayerWrapper}
387
+ */
388
+ export function createWRPlayer(element, config, callbacks) {
389
+ return new WRPlayerWrapper(element, config, callbacks);
390
+ }
391
+
392
+ export default { WRPlayerWrapper, createWRPlayer };