@realsee/live 0.4.0-alpha.3 → 0.4.0-beta.1

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/README.md CHANGED
@@ -30,26 +30,24 @@ SDKs(iOS/Android/微信小程序)和 Live API 可以快速实现实时同屏
30
30
 
31
31
  ```bash
32
32
  # npm
33
- npm i @realsee/live tslib
33
+ npm i @realsee/live
34
34
  # yarn
35
- yarn add @realsee/live tslib
35
+ yarn add @realsee/live
36
36
  ```
37
37
 
38
- > **Live** 通过 [TypeScript](http://www.typescriptlang.org/) 编写,依赖 [tslib]() 运行时,如果你的项目不是由 [TypeScript](http://www.typescriptlang.org/) 构成,请安装此运行时。
39
-
40
38
  ### 如何使用
41
39
 
42
40
  简单使用样例(先不考虑实时语音能力):
43
41
 
44
42
  ```js
45
- import { createLive } from '@realsee/live'
43
+ import { createLive } from '@realsee/live';
46
44
 
47
45
  const live = createLive({
48
46
  url: 'wss://ws-access.realsee.com/***/?=xxx' /* 连接 WebSocket 服务的 URL */,
49
47
  getTicket: async () => {
50
48
  /* 获取带看"门票"的回调函数 */
51
49
  },
52
- })
50
+ });
53
51
  ```
54
52
 
55
53
  在创建 `live` 实例时配置了两个参数:
@@ -82,12 +80,12 @@ const live = createLive({
82
80
 
83
81
  ```js
84
82
  // 发送本地 帧数据 (Five State)
85
- live.sendKeyframe('five', { panoIndex: 13 })
83
+ live.sendKeyframe('five', { panoIndex: 13 });
86
84
 
87
85
  // 接收远程 帧数据 (Five State)
88
86
  live.keyframes.on('five', (newState, prevState) => {
89
87
  // 可以拿最新的 `newState` 更新本地 UI 状态
90
- })
88
+ });
91
89
  ```
92
90
 
93
91
  您也可以通过 `live.snapshot` 属性来获取当前所有序列帧数据的"快照"。
@@ -98,19 +96,19 @@ live.keyframes.on('five', (newState, prevState) => {
98
96
 
99
97
  ```js
100
98
  // 属性:获取本地用户信息
101
- live.selfInfo
99
+ live.selfInfo;
102
100
 
103
101
  // 属性:获取当前房间内所有用户列表
104
- live.userList
102
+ live.userList;
105
103
 
106
104
  // 方法:更新本地用户信息
107
- live.setSelfInfo({})
105
+ live.setSelfInfo({});
108
106
 
109
107
  // 事件:本地用户信息发生变更
110
- live.on('selfInfoUpdate', (userInfo) => {})
108
+ live.on('selfInfoUpdate', (userInfo) => {});
111
109
 
112
110
  // 事件:房间用户列表信息发生变更
113
- live.on('userListUpdate', (userList) => {})
111
+ live.on('userListUpdate', (userList) => {});
114
112
  ```
115
113
 
116
114
  ### 广播消息
@@ -118,13 +116,16 @@ live.on('userListUpdate', (userList) => {})
118
116
  除了帧数据和房间信息之外,还可以用事件的形式向房间内的其他用户广播消息:
119
117
 
120
118
  ```js
121
- live.broadcast(data /* 广播的数据 */, ['user_id_1', 'user_id_2'] /* 收到此用户的userId */)
119
+ live.broadcast(
120
+ data /* 广播的数据 */,
121
+ ['user_id_1', 'user_id_2'] /* 收到此用户的userId */,
122
+ );
122
123
  ```
123
124
 
124
125
  此时,房间内用户 Id 为 `user_id_1`、`user_id_2` 的用户会收到此广播消息(其他用户无感知):
125
126
 
126
127
  ```js
127
- live.on('broadcast', (data /* 广播的数据 */) => {})
128
+ live.on('broadcast', (data /* 广播的数据 */) => {});
128
129
  ```
129
130
 
130
131
  更多的例子请查看 [Live API](https://unpkg.com/@realsee/live/docs/index.html) 文档 。
@@ -139,8 +140,8 @@ live.on('broadcast', (data /* 广播的数据 */) => {})
139
140
 
140
141
  ```ts
141
142
  live.on('ws.readyStateUpdate', (readyState: WebSocketState) => {
142
- console.log(readyState) // readyState 即是最新的 WebScoket 状态
143
- })
143
+ console.log(readyState); // readyState 即是最新的 WebScoket 状态
144
+ });
144
145
  ```
145
146
 
146
147
  ### WebSocket 事件
@@ -202,8 +203,8 @@ live.on('ws.readyStateUpdate', (readyState: WebSocketState) => {
202
203
 
203
204
  ```ts
204
205
  live.$RTC.on('userVolumes', (userVolumes: UserVolume[]) => {
205
- console.log(userVolumes) // userVolumes 正在说话的用户及其音量数据。
206
- })
206
+ console.log(userVolumes); // userVolumes 正在说话的用户及其音量数据。
207
+ });
207
208
  ```
208
209
 
209
210
  ### 异常处理
@@ -256,32 +257,31 @@ live.$RTC.on('userVolumes', (userVolumes: UserVolume[]) => {
256
257
  如果是 iOS/Android App(已接入如视 VRTC 容器 SDK)
257
258
 
258
259
  ```js
259
- import { JSBridgeApp } from '@realsee/jsbridge-x/lib/app'
260
- import { createLive } from '@realsee/live'
261
- import { VRWebViewRTC } from '@realsee/live/RTC/VRWebViewRTC'
260
+ import JSBridge from '@realsee/jsbridge-x';
261
+ import { createLive, VRWebViewRTC } from '@realsee/live';
262
262
 
263
- const jsBridgeInstance = new JSBridgeApp()
263
+ const jsBridgeInstance = new JSBridge();
264
264
 
265
265
  const rtcInstance = new VRWebViewRTC({
266
266
  jsBridge,
267
267
  getVoiceSign: () => {},
268
- })
268
+ });
269
269
 
270
270
  const live = createLive({
271
271
  rtc: rtcInstance,
272
272
  getTicket: async () => '',
273
- })
273
+ });
274
274
  ```
275
275
 
276
276
  提供 RTC 实例配置参数之后,语音的连接、断开、重连等逻辑就托管给了 **Live** 。您可以通过 live 实例上的 `$RTC` 命名空间访问语音相关的状态和事件。
277
277
 
278
278
  ```js
279
279
  // 状态:用户是否成功加入语音房间
280
- live.$RTC.joined
280
+ live.$RTC.joined;
281
281
  // 状态:用户麦克风状态
282
- live.$RTC.micro
282
+ live.$RTC.micro;
283
283
  // 监听用户音量变化
284
- live.$RTC.on('userVolumes', (userVolumes) => {})
284
+ live.$RTC.on('userVolumes', (userVolumes) => {});
285
285
  ```
286
286
 
287
287
  ## 浏览器端支持
@@ -302,17 +302,16 @@ yarn add trtc-js-sdk
302
302
  使用方式:
303
303
 
304
304
  ```tsx
305
- import { createLive } from '@realsee/live'
306
- import { BrowserRTC } from '@realsee/live/RTC/BrowserRTC'
305
+ import { createLive, BrowserRTC } from '@realsee/live';
307
306
 
308
307
  const rtcInstance = new VRWebViewRTC({
309
308
  getVoiceSign: () => {},
310
- })
309
+ });
311
310
 
312
311
  const live = createLive({
313
312
  rtc: rtcInstance,
314
313
  getTicket: async () => '',
315
- })
314
+ });
316
315
  ```
317
316
 
318
317
  ### Chime
@@ -327,17 +326,38 @@ yarn add amazon-chime-sdk-js
327
326
  使用方式:
328
327
 
329
328
  ```tsx
330
- import { createLive } from '@realsee/live'
331
- import { BrowserRTC4Chime } from '@realsee/live/RTC/BrowserRTC4Chime'
329
+ import { createLive, BrowserRTC4Chime } from '@realsee/live';
332
330
 
333
331
  const rtcInstance = new BrowserRTC4Chime({
334
332
  getVoiceSign: () => {},
335
- })
333
+ });
336
334
 
337
335
  const live = createLive({
338
336
  rtc: rtcInstance,
339
337
  getTicket: async () => '',
340
- })
338
+ });
339
+ ```
340
+
341
+ ### 使用内置的 JsBridge 和 RTC 实现
342
+
343
+ 使用方式:
344
+
345
+ ```tsx
346
+ import { createLive } from '@realsee/live';
347
+
348
+ const live = createLive({
349
+ useBuiltInRTC: true, // 使用内置rtc
350
+ builtInRTCType: BuiltInRTCType.RealseeXChimeRTC, // 内置 rtc 类型,不填则默认trtc,chime请指明
351
+ useBuiltInJsBridge: true, // 使用内置的jsBridge
352
+ getVoiceSign, // 内置的rtc还是要把 getVoiceSign 方法传递进去
353
+ getTicket: async () => '' /* 必须提供 */,
354
+ });
355
+
356
+ // 可以直接使用里面的 jsBridge, 该实例由 @realsee/jsbridge-x 提供支持
357
+ const jsBridge = live.jsBridge;
358
+ jsBridge
359
+ .ready()
360
+ .then(() => console.info(`${jsBridge.webViewType} jsBridge is ready`));
341
361
  ```
342
362
 
343
363
  ## TypeScript & React 支持
@@ -350,57 +370,57 @@ const live = createLive({
350
370
  步骤一:新建文件 `LiveReact.ts` 并创建您自己的 `LiveReact` 实例。
351
371
 
352
372
  ```tsx
353
- import { createLiveReact } from '@realsee/live'
373
+ import { createLiveReact } from '@realsee/live';
354
374
 
355
375
  /** 定义帧数据快照结构 */
356
376
  interface Snapshot {
357
377
  /** 以 Five 状态快照为例 */
358
378
  five: {
359
- panoIndex: number
360
- fov: number
361
- mode: 'Panorama' | 'Model'
362
- latitude: number
363
- longitude: number
364
- }
379
+ panoIndex: number;
380
+ fov: number;
381
+ mode: 'Panorama' | 'Model';
382
+ latitude: number;
383
+ longitude: number;
384
+ };
365
385
  }
366
386
 
367
387
  /** 通过 `createLiveReact()` 函数创建实例*/
368
388
 
369
389
  const LiveReact = createLiveReact<Snapshot>({
370
390
  getTicket: async () => '' /* 必须提供 */,
371
- })
391
+ });
372
392
 
373
- export default LiveReact
393
+ export default LiveReact;
374
394
 
375
395
  /** 如果您不喜欢 `LiveReact.LiveProvider` 这类编程习惯,可以将需要的函数"抛"出来 */
376
- export const LiveProvider = LiveReact.LiveProvider
377
- export const useConnect = LiveReact.useConnect
378
- export const useKeyframe = LiveReact.useKeyframe
396
+ export const LiveProvider = LiveReact.LiveProvider;
397
+ export const useConnect = LiveReact.useConnect;
398
+ export const useKeyframe = LiveReact.useKeyframe;
379
399
  // export ..
380
400
  ```
381
401
 
382
402
  步骤二:通过 `LiveProvider` 将 `live` 实例集成到 `React` 的 [Context](https://reactjs.org/docs/context.html) 上下文环境中。
383
403
 
384
404
  ```jsx
385
- import { LiveProvider } from './LiveReact'
405
+ import { LiveProvider } from './LiveReact';
386
406
 
387
407
  ReactDOM.render(
388
408
  <LiveProvider>
389
409
  <RootApp />
390
410
  </LiveProvider>,
391
- containter
392
- )
411
+ containter,
412
+ );
393
413
  ```
394
414
 
395
415
  步骤三:在您的 `React` 函数组件中使用相关 `Hooks` 函数。
396
416
 
397
417
  ```jsx
398
- import { useConnect } from './LiveReact'
418
+ import { useConnect } from './LiveReact';
399
419
 
400
420
  function ReconnectBtn() {
401
- const connect = useConnect()
421
+ const connect = useConnect();
402
422
 
403
- return <button onClick={() => connect()}>重新连接</button>
423
+ return <button onClick={() => connect()}>重新连接</button>;
404
424
  }
405
425
  ```
406
426
 
@@ -2,12 +2,14 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ var JSBridge = require('@realsee/jsbridge-x');
5
6
  var amazonChimeSdkJs = require('amazon-chime-sdk-js');
6
7
  var TRTC = require('trtc-js-sdk');
7
8
  var React = require('react');
8
9
 
9
10
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
10
11
 
12
+ var JSBridge__default = /*#__PURE__*/_interopDefaultLegacy(JSBridge);
11
13
  var TRTC__default = /*#__PURE__*/_interopDefaultLegacy(TRTC);
12
14
  var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
13
15
 
@@ -137,7 +139,12 @@ exports.RTCType = void 0;
137
139
  RTCType["RealseeBrowserRTC"] = "RealseeBrowserRTC";
138
140
  RTCType["RealseeVRWebViewRTC"] = "RealseeVRWebViewRTC";
139
141
  RTCType["RealseeBrowserChimeRTC"] = "RealseeBrowserChimeRTC";
140
- })(exports.RTCType || (exports.RTCType = {}));
142
+ })(exports.RTCType || (exports.RTCType = {}));
143
+ exports.BuiltInRTCType = void 0;
144
+ (function (BuiltInRTCType) {
145
+ BuiltInRTCType["RealseeXTRTC"] = "trtc";
146
+ BuiltInRTCType["RealseeXChimeRTC"] = "chime";
147
+ })(exports.BuiltInRTCType || (exports.BuiltInRTCType = {}));
141
148
 
142
149
  // uuid
143
150
  function S4() {
@@ -888,6 +895,12 @@ function appendUri(uri, query) {
888
895
  return uri + newQuery;
889
896
  }
890
897
 
898
+ const userAgent = globalThis.navigator.userAgent;
899
+ const isAndroid = /android/gi.test(userAgent);
900
+ const isIOS = /iPhone|iPad\s+OS/gi.test(userAgent);
901
+ const isWxMiniProgram = /MicroMessenger/gi.test(userAgent) && /miniProgram/gi.test(userAgent);
902
+ const isRealseeVRWebview = /realsee/gi.test(userAgent);
903
+
891
904
  /**
892
905
  * 判断是否是真实的对象类型
893
906
  * @param value
@@ -1252,9 +1265,13 @@ function createLive(options) {
1252
1265
  };
1253
1266
  class LiveTpl extends Subscribe {
1254
1267
  constructor() {
1255
- var _a;
1268
+ var _a, _b, _c;
1256
1269
  super();
1257
1270
  this.keyframes = new Subscribe();
1271
+ const jsBridge = ((_a = options.jsBridge) !== null && _a !== void 0 ? _a : options.useBuiltInJsBridge)
1272
+ ? new JSBridge__default["default"]()
1273
+ : undefined;
1274
+ liveController.options.jsBridge = jsBridge;
1258
1275
  // 没有传rtc实例时
1259
1276
  if (!options.rtc) {
1260
1277
  // 取指定的内部rtc类型
@@ -1262,15 +1279,15 @@ function createLive(options) {
1262
1279
  if (!options.getVoiceSign) {
1263
1280
  throw new Error('GetVoiceSign method must be passed when using built-in RTC instances');
1264
1281
  }
1265
- const getVoiceSign = (_a = options.getVoiceSign) !== null && _a !== void 0 ? _a : unimplementedGetVoiceSign;
1282
+ const getVoiceSign = (_b = options.getVoiceSign) !== null && _b !== void 0 ? _b : unimplementedGetVoiceSign;
1266
1283
  switch (options.rtcType) {
1267
1284
  case exports.RTCType.RealseeVRWebViewRTC: {
1268
1285
  // 内置的 VRWebViewRTC
1269
- if (!options.jsBridge) {
1286
+ if (!jsBridge) {
1270
1287
  throw new Error('JsBridge instance must be passed when using built-in VRWebViewRTC instances');
1271
1288
  }
1272
1289
  liveController.options.rtc = new VRWebViewRTC({
1273
- jsBridge: options.jsBridge,
1290
+ jsBridge,
1274
1291
  getVoiceSign,
1275
1292
  });
1276
1293
  break;
@@ -1295,6 +1312,30 @@ function createLive(options) {
1295
1312
  }
1296
1313
  }
1297
1314
  }
1315
+ // 都不满足时取内置
1316
+ if (options.useBuiltInRTC) {
1317
+ if (!options.getVoiceSign) {
1318
+ throw new Error('GetVoiceSign method must be passed when using built-in RTC instances');
1319
+ }
1320
+ const getVoiceSign = (_c = options.getVoiceSign) !== null && _c !== void 0 ? _c : unimplementedGetVoiceSign;
1321
+ if (!jsBridge) {
1322
+ throw new Error('jsBridge instance must be passed when using built-in RTC instances');
1323
+ }
1324
+ // vrwebview 默认
1325
+ if ((isRealseeVRWebview && (isAndroid || isIOS)) || isWxMiniProgram) {
1326
+ liveController.options.rtc = new VRWebViewRTC({
1327
+ jsBridge,
1328
+ getVoiceSign,
1329
+ });
1330
+ }
1331
+ else {
1332
+ // 其它终端使用
1333
+ liveController.options.rtc =
1334
+ options.builtInRTCType === exports.BuiltInRTCType.RealseeXChimeRTC
1335
+ ? new BrowserRTC4Chime({ getVoiceSign })
1336
+ : new BrowserRTC({ getVoiceSign });
1337
+ }
1338
+ }
1298
1339
  }
1299
1340
  if (!liveController.options.rtc) {
1300
1341
  console.warn('unused rtc');
@@ -1342,6 +1383,9 @@ function createLive(options) {
1342
1383
  get $RTC() {
1343
1384
  return liveController.options.rtc;
1344
1385
  }
1386
+ get jsBridge() {
1387
+ return liveController.options.jsBridge;
1388
+ }
1345
1389
  async setSelfInfo(userInfo) {
1346
1390
  if (this.readyState !== exports.WebSocketState.OPEN) {
1347
1391
  return new Error('The connection is not open and ready to communicate.');
@@ -1588,6 +1632,7 @@ function createLive(options) {
1588
1632
  function createLiveReact(options) {
1589
1633
  const LiveContext = React__default["default"].createContext(null);
1590
1634
  const live = createLive(options);
1635
+ const jsBridge = live.jsBridge;
1591
1636
  function LiveProvider(props) {
1592
1637
  return (React__default["default"].createElement(LiveContext.Provider, { value: props.live || live }, props.children));
1593
1638
  }
@@ -1783,6 +1828,7 @@ function createLiveReact(options) {
1783
1828
  }
1784
1829
  return {
1785
1830
  live,
1831
+ jsBridge,
1786
1832
  LiveProvider,
1787
1833
  unsafe__useLiveInstance,
1788
1834
  unsafe__useRTCInstance,