@ray-js/ray-ipc-player 2.0.18-beta-5 → 2.0.18-beta-7

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.
package/lib/index.web.js DELETED
@@ -1,484 +0,0 @@
1
- import _extends from "@babel/runtime/helpers/esm/extends";
2
- import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3
- import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
4
- import "core-js/modules/es.object.to-string.js";
5
- import "core-js/modules/es.promise.js";
6
- import _regeneratorRuntime from "@babel/runtime/regenerator";
7
- import React, { useEffect, useRef, useCallback } from 'react';
8
- import { useImmer } from 'use-immer';
9
- import { IpcPlayer, View, CoverView } from '@ray-js/components';
10
- import cx from 'classnames';
11
- import { usePageEvent } from 'ray';
12
- import locals from './i18n';
13
- import Styles from './index.module.less';
14
- import { IpcFailCode } from './constant';
15
- import { putDpData } from './utils';
16
-
17
- var Player = function (props) {
18
- var _props$updateLayout = props.updateLayout,
19
- updateLayout = _props$updateLayout === void 0 ? '' : _props$updateLayout,
20
- _props$devId = props.devId,
21
- devId = _props$devId === void 0 ? '' : _props$devId,
22
- onChangeStreamStatus = props.onChangeStreamStatus,
23
- onPlayerTap = props.onPlayerTap,
24
- onInitPreview = props.onInitPreview,
25
- _props$defaultMute = props.defaultMute,
26
- defaultMute = _props$defaultMute === void 0 ? true : _props$defaultMute,
27
- _props$onlineStatus = props.onlineStatus,
28
- onlineStatus = _props$onlineStatus === void 0 ? true : _props$onlineStatus,
29
- _props$loadText = props.loadText,
30
- loadText = _props$loadText === void 0 ? '' : _props$loadText,
31
- _props$rotateZ = props.rotateZ,
32
- rotateZ = _props$rotateZ === void 0 ? 0 : _props$rotateZ,
33
- _props$scalable = props.scalable,
34
- scalable = _props$scalable === void 0 ? true : _props$scalable,
35
- _props$scaleMultiple = props.scaleMultiple,
36
- scaleMultiple = _props$scaleMultiple === void 0 ? 0 : _props$scaleMultiple,
37
- _props$ptzControllabl = props.ptzControllable,
38
- ptzControllable = _props$ptzControllabl === void 0 ? true : _props$ptzControllabl,
39
- _props$clarity = props.clarity,
40
- clarity = _props$clarity === void 0 ? 'normal' : _props$clarity,
41
- onCtx = props.onCtx,
42
- _props$playerStyle = props.playerStyle,
43
- playerStyle = _props$playerStyle === void 0 ? {
44
- borderRadius: 0,
45
- bgColor: '#000000',
46
- borderStyle: 'solid',
47
- borderColor: '',
48
- borderWidth: 0
49
- } : _props$playerStyle,
50
- _props$privateState = props.privateState,
51
- privateState = _props$privateState === void 0 ? false : _props$privateState,
52
- _props$extend = props.extend,
53
- extend = _props$extend === void 0 ? {} : _props$extend;
54
- var borderRadius = playerStyle.borderRadius,
55
- bgColor = playerStyle.bgColor,
56
- borderStyle = playerStyle.borderStyle,
57
- borderColor = playerStyle.borderColor,
58
- borderWidth = playerStyle.borderWidth;
59
-
60
- var _useImmer = useImmer({
61
- connectState: false,
62
- // 连接状态
63
- loadingState: false,
64
- // 加载状态
65
- errorState: false,
66
- // 异常状态
67
- errorMsg: '',
68
- // 异常信息
69
- updateLy: '',
70
- deviceOnlineState: true,
71
- initLy: false
72
- }),
73
- _useImmer2 = _slicedToArray(_useImmer, 2),
74
- playState = _useImmer2[0],
75
- setPlayState = _useImmer2[1];
76
-
77
- var ipcCtx = useRef(null); // ipc实例
78
-
79
- var retryCount = useRef(0); // 重试次数
80
-
81
- var muteRef = useRef(defaultMute); // 静音
82
-
83
- usePageEvent('onShow', useCallback(function () {
84
- // console.log('show');
85
- if (ipcCtx.current && playState.connectState) {
86
- startPreview('');
87
- }
88
- }, [playState.connectState]));
89
- usePageEvent('onHide', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
90
- return _regeneratorRuntime.wrap(function (_context) {
91
- while (1) {
92
- switch (_context.prev = _context.next) {
93
- case 0:
94
- // console.log('hide');
95
- setPlayState(function (d) {
96
- d.loadingState = true;
97
- });
98
- _context.next = 3;
99
- return getCurMute();
100
-
101
- case 3:
102
- stopPreview();
103
-
104
- case 4:
105
- case "end":
106
- return _context.stop();
107
- }
108
- }
109
- }, _callee);
110
- }))); // 隐私模式
111
-
112
- useEffect(function () {
113
- setPlayState(function (d) {
114
- d.errorState = privateState;
115
- });
116
-
117
- if (!privateState) {
118
- retry();
119
- }
120
- }, [privateState]);
121
- useEffect(function () {
122
- muteRef.current = defaultMute;
123
- }, [defaultMute]);
124
- useEffect(function () {
125
- setPlayState(function (d) {
126
- d.updateLy = updateLayout;
127
- });
128
- }, [updateLayout]); // 设备离线
129
-
130
- useEffect(function () {
131
- setPlayState(function (d) {
132
- d.deviceOnlineState = onlineStatus;
133
- });
134
-
135
- if (!onlineStatus) {
136
- setPlayState(function (d) {
137
- d.errorState = true;
138
- d.errorMsg = locals.getLang('ipc_player_no_line');
139
- });
140
- } else if (playState.initLy) {
141
- retry();
142
- }
143
- }, [onlineStatus, playState.initLy]); // 重试
144
-
145
- var _retry = /*#__PURE__*/function () {
146
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
147
- return _regeneratorRuntime.wrap(function (_context2) {
148
- while (1) {
149
- switch (_context2.prev = _context2.next) {
150
- case 0:
151
- setPlayState(function (d) {
152
- d.connectState = false;
153
- d.errorMsg = '';
154
- d.errorState = false;
155
- d.loadingState = true;
156
- });
157
- _context2.t0 = playState.connectState;
158
-
159
- if (!_context2.t0) {
160
- _context2.next = 5;
161
- break;
162
- }
163
-
164
- _context2.next = 5;
165
- return disconnect();
166
-
167
- case 5:
168
- createConnect();
169
-
170
- case 6:
171
- case "end":
172
- return _context2.stop();
173
- }
174
- }
175
- }, _callee2);
176
- }));
177
-
178
- return function () {
179
- return _ref2.apply(this, arguments);
180
- };
181
- }();
182
-
183
- var retry = useCallback(function () {
184
- // console.log('retry:', muteRef.current);
185
- !privateState && _retry();
186
- }, [playState.connectState, privateState]); // 创建ipc实例
187
-
188
- var createIpcCtx = function (id) {
189
- var _ty;
190
-
191
- // @ts-ignore
192
- ipcCtx.current = (_ty = ty) === null || _ty === void 0 ? void 0 : _ty.createIpcPlayerContext(id); // console.log('创建实例:', devId);
193
-
194
- onCtx && onCtx({
195
- ctx: ipcCtx.current,
196
- retry: retry
197
- });
198
- };
199
-
200
- useEffect(function () {
201
- onlineStatus && createIpcCtx(devId);
202
- return function () {
203
- disconnect();
204
- ipcCtx.current = null;
205
- };
206
- }, [devId, onlineStatus]); // 建立连接
207
-
208
- var createConnect = function (params) {
209
- var _ipcCtx$current;
210
-
211
- // console.log('建立连接:', devId);
212
- setPlayState(function (d) {
213
- d.loadingState = true;
214
- });
215
- (_ipcCtx$current = ipcCtx.current) === null || _ipcCtx$current === void 0 ? void 0 : _ipcCtx$current.connect({
216
- success: function success() {
217
- // console.log('建立连接成功:', devId);
218
- setPlayState(function (d) {
219
- d.connectState = true;
220
- d.errorState = false;
221
- d.errorMsg = '';
222
- });
223
- onChangeStreamStatus && onChangeStreamStatus(1001);
224
- startPreview(params);
225
- },
226
- fail: function fail() {
227
- // console.log('建立连接失败:', devId);
228
- setPlayState(function (d) {
229
- d.connectState = false;
230
- d.errorState = true;
231
- d.errorMsg = locals.getLang('ipc_player_connect_fail');
232
- d.loadingState = false;
233
- });
234
- onChangeStreamStatus && onChangeStreamStatus(-1001);
235
- }
236
- });
237
- }; // 视图层准备就绪,开始建立连接
238
-
239
-
240
- // 开启预览
241
- var startPreview = function (params) {
242
- var _ipcCtx$current2;
243
-
244
- (_ipcCtx$current2 = ipcCtx.current) === null || _ipcCtx$current2 === void 0 ? void 0 : _ipcCtx$current2.startPreview({
245
- success: function success() {
246
- // console.log('开启预览:', devId);
247
- setPlayState(function (d) {
248
- d.loadingState = false;
249
- d.errorState = false;
250
- d.errorMsg = '';
251
- });
252
- params && onInitPreview && onInitPreview(devId);
253
- onChangeStreamStatus && onChangeStreamStatus(1002);
254
- },
255
- fail: function fail() {
256
- // console.log('开启失败:', devId);
257
- setPlayState(function (d) {
258
- d.errorState = true;
259
- d.loadingState = false;
260
- d.errorMsg = locals.getLang('ipc_player_preview_fail');
261
- });
262
- onChangeStreamStatus && onChangeStreamStatus(-1002);
263
- },
264
- complete: function complete() {
265
- // console.log('startPreview:', muteRef.current);
266
- setMuted(muteRef.current);
267
- }
268
- });
269
- }; // 暂停预览
270
-
271
-
272
- var stopPreview = function () {
273
- var _ipcCtx$current3;
274
-
275
- (_ipcCtx$current3 = ipcCtx.current) === null || _ipcCtx$current3 === void 0 ? void 0 : _ipcCtx$current3.stopPreview({
276
- success: function success() {
277
- return null;
278
- },
279
- fail: function fail() {
280
- return null;
281
- },
282
- complete: function complete() {
283
- return setMuted(true);
284
- }
285
- });
286
- }; // 设置静音
287
-
288
-
289
- var setMuted = function (val) {
290
- var _ipcCtx$current4;
291
-
292
- (_ipcCtx$current4 = ipcCtx.current) === null || _ipcCtx$current4 === void 0 ? void 0 : _ipcCtx$current4.setMuted({
293
- mute: val,
294
- success: function success() {// console.log('设置声音成功', val);
295
- },
296
- fail: function fail() {// console.log('设置声音失败');
297
- }
298
- });
299
- }; // 获取当前是否静音
300
-
301
-
302
- var getCurMute = function () {
303
- return new Promise(function (resolve, reject) {
304
- var _ipcCtx$current5;
305
-
306
- (_ipcCtx$current5 = ipcCtx.current) === null || _ipcCtx$current5 === void 0 ? void 0 : _ipcCtx$current5.isMuted({
307
- success: function success(res) {
308
- muteRef.current = res;
309
- },
310
- fail: function fail(e) {
311
- reject(e); // console.log(e);
312
- },
313
- complete: function complete(d) {
314
- resolve(d);
315
- }
316
- });
317
- });
318
- }; // 异常事件
319
-
320
-
321
- var onError = useCallback(function (e) {
322
- var _e$detail;
323
-
324
- if (playState.errorState) {
325
- return;
326
- } // console.error('发生错误:', e);
327
-
328
-
329
- var type = e === null || e === void 0 ? void 0 : (_e$detail = e.detail) === null || _e$detail === void 0 ? void 0 : _e$detail.errCode;
330
-
331
- if (IpcFailCode[type]) {
332
- onChangeStreamStatus && onChangeStreamStatus(type);
333
- setPlayState(function (d) {
334
- d.loadingState = false;
335
- d.errorState = true;
336
- d.errorMsg = locals.getLang(IpcFailCode[type]);
337
- d.connectState = false;
338
- });
339
- }
340
- }, [playState.errorState]); // 连接状态发生变化
341
-
342
- // 断开连接
343
- var disconnect = function () {
344
- return new Promise(function (resolve, reject) {
345
- var _ipcCtx$current6;
346
-
347
- (_ipcCtx$current6 = ipcCtx.current) === null || _ipcCtx$current6 === void 0 ? void 0 : _ipcCtx$current6.disconnect({
348
- success: function success(res) {
349
- // console.log('断开连接成功:', devId);
350
- resolve(res);
351
- setPlayState(function (d) {
352
- d.connectState = false;
353
- });
354
- onChangeStreamStatus && onChangeStreamStatus(1009);
355
- },
356
- fail: function fail(e) {
357
- // console.log('断开连接失败:', devId);
358
- reject(e);
359
- onChangeStreamStatus && onChangeStreamStatus(-1009);
360
- }
361
- });
362
- });
363
- };
364
-
365
- // 关闭隐私模式,重新拉流
366
- var handleReWakeCamera = /*#__PURE__*/function () {
367
- var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
368
- return _regeneratorRuntime.wrap(function (_context3) {
369
- while (1) {
370
- switch (_context3.prev = _context3.next) {
371
- case 0:
372
- _context3.next = 2;
373
- return putDpData({
374
- 105: false
375
- }, devId);
376
-
377
- case 2:
378
- case "end":
379
- return _context3.stop();
380
- }
381
- }
382
- }, _callee3);
383
- }));
384
-
385
- return function () {
386
- return _ref3.apply(this, arguments);
387
- };
388
- }();
389
-
390
- return /*#__PURE__*/React.createElement(View, {
391
- className: Styles.ipc_player
392
- }, /*#__PURE__*/React.createElement(IpcPlayer, _extends({
393
- className: Styles.player,
394
- onVideotap: function handlePlayerClick() {
395
- // console.log("videoTap:", devId);
396
- onPlayerTap && onPlayerTap(devId);
397
- },
398
- onInitdone: function initdone() {
399
- // console.log('视图层准备就绪:', devId);
400
- setPlayState(function (d) {
401
- d.updateLy = Math.random();
402
- d.initLy = true;
403
- });
404
- },
405
- onError: onError,
406
- onConnectchange: function connectchange(e) {
407
- var _e$detail2;
408
-
409
- // console.log('连接状态发生变化', e);
410
- var code = e === null || e === void 0 ? void 0 : (_e$detail2 = e.detail) === null || _e$detail2 === void 0 ? void 0 : _e$detail2.state;
411
- onChangeStreamStatus && onChangeStreamStatus(code === 0 ? 1001 : -1001);
412
-
413
- if (code === 0) {
414
- setPlayState(function (d) {
415
- d.connectState = true;
416
- });
417
- } else if ((code === -3 || code === -105) && retryCount.current < 1) {
418
- // 连接超时或鉴权失败重试一次
419
- retryCount.current++;
420
- retry();
421
- } else {
422
- setPlayState(function (d) {
423
- d.connectState = false;
424
- d.loadingState = false;
425
- d.errorState = true;
426
- d.errorMsg = locals.getLang(IpcFailCode[-1001]);
427
- });
428
- }
429
- },
430
- deviceId: devId,
431
- autoPauseIfNavigate: false,
432
- autoPauseIfOpenNative: false,
433
- muted: defaultMute,
434
- objectFit: "fillCrop",
435
- clarity: clarity,
436
- rotateZ: rotateZ,
437
- scalable: scalable,
438
- scaleMultiple: scaleMultiple,
439
- ptzControllable: ptzControllable,
440
- borderRadius: borderRadius,
441
- borderStyle: borderStyle,
442
- borderColor: borderColor,
443
- borderWidth: borderWidth,
444
- updateLayout: playState.updateLy,
445
- backgroundColor: bgColor
446
- }, extend)), /*#__PURE__*/React.createElement(View, {
447
- style: {
448
- borderRadius: "".concat(borderRadius)
449
- },
450
- className: cx(Styles.load_box, Styles.bg_center, !playState.loadingState && Styles.hide)
451
- }, /*#__PURE__*/React.createElement(View, {
452
- className: Styles.load_icon
453
- }), /*#__PURE__*/React.createElement(View, {
454
- className: Styles.load_des
455
- }, loadText || locals.getLang('get_video_stream'))), /*#__PURE__*/React.createElement(CoverView, {
456
- style: {
457
- borderRadius: "".concat(borderRadius)
458
- },
459
- className: cx(Styles.bg_center, Styles.state_label, !playState.errorState && Styles.hide)
460
- }, privateState ? /*#__PURE__*/React.createElement(View, null, /*#__PURE__*/React.createElement(View, {
461
- className: Styles.device_sleep
462
- }, locals.getLang('device_sleep')), /*#__PURE__*/React.createElement(View, {
463
- className: Styles.re_wake_camera // @ts-ignore
464
- ,
465
- onClick: handleReWakeCamera
466
- }, locals.getLang('re_wake_camera'))) : /*#__PURE__*/React.createElement(View, {
467
- style: {
468
- width: '95%',
469
- textAlign: 'center'
470
- }
471
- }, /*#__PURE__*/React.createElement(View, {
472
- className: cx(Styles.iconfont, Styles.icon_tishi, !playState.deviceOnlineState && Styles.hide)
473
- }), /*#__PURE__*/React.createElement(View, {
474
- className: Styles.err_msg
475
- }, /*#__PURE__*/React.createElement(View, {
476
- className: cx(Styles.msg, Styles.ellipsis)
477
- }, playState.errorMsg), /*#__PURE__*/React.createElement(View // @ts-ignore
478
- , {
479
- onClick: retry,
480
- className: cx(Styles.retry, !playState.deviceOnlineState && Styles.hide)
481
- }, locals.getLang('ipc_player_retry'))))));
482
- };
483
-
484
- export default Player;