sample-xmonitor-js 1.0.1 → 1.0.3

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.d.ts CHANGED
@@ -22,6 +22,11 @@ export default class MonitorFactory {
22
22
  * @param {any} setup 监视器相关的参数
23
23
  */
24
24
  init(type: MonitorType, element: HTMLDivElement, setup?: Setup): Promise<Monitor>;
25
+ switch(option: {
26
+ deviceId?: string;
27
+ useFrontCamera?: boolean;
28
+ [key: string]: any;
29
+ }): Promise<void>;
25
30
  /**
26
31
  * 启动所有监视器
27
32
  */
@@ -41,6 +46,16 @@ export declare function getInstance(options?: {
41
46
  monitors: Array<{
42
47
  provider: DeviceProvider;
43
48
  element: any;
44
- setup?: DefaultSetup | Omit<TxrtcSetup, 'roomId' | 'userId'>;
49
+ setup?: DefaultSetup | TxrtcSetup;
45
50
  }>;
46
51
  }): MonitorFactory;
52
+ export declare function switchCamera(option: {
53
+ deviceId?: string;
54
+ useFrontCamera?: boolean;
55
+ [key: string]: any;
56
+ }): void;
57
+ export declare function getSpeakerList(requestPermission?: boolean): Promise<MediaDeviceInfo[]>;
58
+ export declare function setCurrentSpeaker(speakerId: string): Promise<void>;
59
+ export declare function getMicrophoneList(requestPermission?: boolean): Promise<MediaDeviceInfo[]>;
60
+ export declare function getCameraList(requestPermission?: boolean): Promise<MediaDeviceInfo[]>;
61
+ export declare function isSupported(): Promise<any>;
package/lib/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { MObject, MonitorEvents, MonitorType, NotSupportError } from "./Monitor";
2
2
  import DefaultMonitor from "./default";
3
3
  import TxrtcMonitor from "./txrtc";
4
+ import TRTC from "trtc-sdk-v5";
4
5
  export default class MonitorFactory {
5
6
  constructor(pointer) {
6
7
  this.monitorMap = new Map();
@@ -47,6 +48,14 @@ export default class MonitorFactory {
47
48
  this.monitorMap.set(type.toString(), monitor);
48
49
  return monitor;
49
50
  }
51
+ async switch(option) {
52
+ for (let [, monitor] of this.monitorMap) {
53
+ if (monitor.type.device !== 'screen') {
54
+ await monitor.switch(option);
55
+ return;
56
+ }
57
+ }
58
+ }
50
59
  /**
51
60
  * 启动所有监视器
52
61
  */
@@ -113,3 +122,24 @@ export function getInstance(options) {
113
122
  }
114
123
  return factory;
115
124
  }
125
+ export function switchCamera(option) {
126
+ if (!factory) {
127
+ throw new Error('实例尚未创建,请先执行初始化');
128
+ }
129
+ factory.switch(option);
130
+ }
131
+ export function getSpeakerList(requestPermission) {
132
+ return TRTC.getSpeakerList(requestPermission);
133
+ }
134
+ export function setCurrentSpeaker(speakerId) {
135
+ return TRTC.setCurrentSpeaker(speakerId);
136
+ }
137
+ export function getMicrophoneList(requestPermission) {
138
+ return TRTC.getMicrophoneList(requestPermission);
139
+ }
140
+ export function getCameraList(requestPermission) {
141
+ return TRTC.getCameraList(requestPermission);
142
+ }
143
+ export function isSupported() {
144
+ return TRTC.isSupported();
145
+ }
@@ -13,7 +13,6 @@ export default class TxrtcMonitor extends Monitor {
13
13
  protected readonly setup: TxrtcSetup;
14
14
  constructor(options: MonitorOptions);
15
15
  start(): Promise<any>;
16
- initEvent(): Promise<Boolean>;
17
16
  /**
18
17
  * 发送自定义消息
19
18
  * @param message
@@ -12,7 +12,7 @@ export default class TxrtcMonitor extends Monitor {
12
12
  }
13
13
  }
14
14
  async start() {
15
- let streamId = `${this.setup.roomId}_${this.setup.userId}_${this.type.device}`;
15
+ let streamId = `${this.setup.roomId}_${this.setup.userId}`;
16
16
  console.debug(`Start ${this.type.device} TxrtcMonitor with streamId ${streamId}`);
17
17
  if (this.active)
18
18
  return;
@@ -32,39 +32,33 @@ export default class TxrtcMonitor extends Monitor {
32
32
  if (this.setup.userDefineRecordId) {
33
33
  params.userDefineRecordId = this.setup.userDefineRecordId;
34
34
  }
35
- await this.trtc.enterRoom(params);
36
- }
37
- async initEvent() {
38
- return new Promise((resolve) => {
39
- this.trtc.on(TRTC.EVENT.ERROR, (error) => {
40
- let err = error;
41
- if (this.options.type.device === 'screen') {
42
- err = new ScreenTypeError(error.message, 'ScreenTypeError');
43
- }
44
- this.dispatchEvent('error', null, err);
45
- this.stop();
46
- resolve(false);
47
- }).on(TRTC.EVENT.SCREEN_SHARE_STOPPED, () => {
48
- this.dispatchEvent('cancel');
49
- this.stop();
50
- }).on(TRTC.EVENT.TRACK, () => {
51
- +this.dispatchEvent('start');
52
- this.active = true;
53
- this.element = this.options.element.querySelector('video');
54
- resolve(true);
55
- }).on(TRTC.EVENT.VIDEO_PLAY_STATE_CHANGED, ({ state }) => {
56
- if (state === 'STOPPED') {
57
- this.dispatchEvent('stop');
58
- }
59
- else if (state === 'PLAYING') {
60
- this.dispatchEvent('playing');
61
- }
62
- }).on(TRTC.EVENT.CUSTOM_MESSAGE, (message) => {
63
- let decoder = new TextDecoder('utf-8');
64
- let payload = decoder.decode(message.data);
65
- this.dispatchEvent('message', payload);
66
- });
35
+ this.trtc.on(TRTC.EVENT.ERROR, (error) => {
36
+ let err = error;
37
+ if (this.options.type.device === 'screen') {
38
+ err = new ScreenTypeError(error.message, 'ScreenTypeError');
39
+ }
40
+ this.dispatchEvent('error', null, err);
41
+ this.stop();
42
+ }).on(TRTC.EVENT.SCREEN_SHARE_STOPPED, () => {
43
+ this.dispatchEvent('cancel');
44
+ this.stop();
45
+ }).on(TRTC.EVENT.TRACK, () => {
46
+ this.active = true;
47
+ this.dispatchEvent('start');
48
+ }).on(TRTC.EVENT.VIDEO_PLAY_STATE_CHANGED, ({ state }) => {
49
+ if (state === 'STOPPED') {
50
+ this.dispatchEvent('stop');
51
+ }
52
+ else if (state === 'PLAYING') {
53
+ this.dispatchEvent('playing');
54
+ }
55
+ this.element = this.options.element.querySelector('video');
56
+ }).on(TRTC.EVENT.CUSTOM_MESSAGE, (message) => {
57
+ let decoder = new TextDecoder('utf-8');
58
+ let payload = decoder.decode(message.data);
59
+ this.dispatchEvent('message', payload);
67
60
  });
61
+ await this.trtc.enterRoom(params);
68
62
  }
69
63
  /**
70
64
  * 发送自定义消息
@@ -97,15 +91,14 @@ export class TxrtcScreenMonitor extends TxrtcMonitor {
97
91
  super({
98
92
  type: new MonitorType('txrtc', 'screen'), object, element, setup: {
99
93
  ...setup,
100
- userId: object.id,
101
- roomId: object.room
94
+ userId: setup?.userId || object.id,
95
+ roomId: setup?.roomId || object.room
102
96
  }
103
97
  });
104
98
  }
105
99
  async start() {
106
100
  await super.start();
107
101
  await this.trtc.startScreenShare({ publish: true, view: this.options.element });
108
- await this.initEvent();
109
102
  }
110
103
  async stop() {
111
104
  await this.trtc.stopScreenShare();
@@ -117,8 +110,8 @@ export class TxrtcCameraMonitor extends TxrtcMonitor {
117
110
  super({
118
111
  type: new MonitorType('txrtc', 'camera'), element, object, setup: {
119
112
  ...setup,
120
- userId: object.id,
121
- roomId: object.room
113
+ userId: setup?.userId || object.id,
114
+ roomId: setup?.roomId || object.room
122
115
  }
123
116
  });
124
117
  }
@@ -126,14 +119,13 @@ export class TxrtcCameraMonitor extends TxrtcMonitor {
126
119
  await super.start();
127
120
  await this.trtc.startLocalAudio({ publish: true });
128
121
  await this.trtc.startLocalVideo({ publish: true, view: this.options.element });
129
- await this.initEvent();
130
122
  }
131
123
  /**
132
124
  * 切换摄像头
133
125
  */
134
126
  async switch(option) {
135
127
  if (option?.cameraId || option?.useFrontCamera) {
136
- await this.trtc.updateLocalVideo({ option });
128
+ await this.trtc.updateLocalVideo({ option, publish: true, view: this.options.element });
137
129
  }
138
130
  }
139
131
  async stop() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sample-xmonitor-js",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "",
5
5
  "main": "./lib/index",
6
6
  "files": [