Browse Source

feat(ts) migrate JibriSession to TS

dev0
Yash 6 months ago
parent
commit
afa89aea6a
No account linked to committer's email address
1 changed files with 72 additions and 33 deletions
  1. 72
    33
      modules/recording/JibriSession.ts

modules/recording/JibriSession.js → modules/recording/JibriSession.ts View File

1
 import { $iq } from 'strophe.js';
1
 import { $iq } from 'strophe.js';
2
 
2
 
3
 import recordingXMLUtils from './recordingXMLUtils';
3
 import recordingXMLUtils from './recordingXMLUtils';
4
+import JitsiParticipant from '../../JitsiParticipant';
5
+
6
+export interface IJibriSessionOptions {
7
+    connection?: any;
8
+    focusMucJid?: string;
9
+    mode?: string;
10
+    sessionID?: string;
11
+    status?: string;
12
+}
13
+
14
+export interface IStartOptions {
15
+    appData?: string;
16
+    broadcastId?: string;
17
+    focusMucJid: string;
18
+    streamId?: string;
19
+}
20
+
21
+export interface IStopOptions {
22
+    focusMucJid: string;
23
+}
24
+
25
+export interface IQOptions {
26
+    action?: 'start' | 'stop';
27
+    appData?: string;
28
+    broadcastId?: string;
29
+    focusMucJid: string;
30
+    streamId?: string;
31
+}
4
 
32
 
5
 /**
33
 /**
6
  * Represents a recording session.
34
  * Represents a recording session.
7
  */
35
  */
8
 export default class JibriSession {
36
 export default class JibriSession {
37
+    private _connection?: any;
38
+    private _mode?: string;
39
+    private _jibriJid: string | null = null;
40
+    private _statusFromJicofo: string = '';
41
+    private _sessionID?: string;
42
+    private _status?: string;
43
+    private _error?: string;
44
+    private _liveStreamViewURL?: string;
45
+    private _initiator?: JitsiParticipant | string;
46
+    private _terminator?: JitsiParticipant | string;
47
+    private _focusMucJid?: string;
48
+
9
     /**
49
     /**
10
      * Initializes a new JibriSession instance.
50
      * Initializes a new JibriSession instance.
11
      *
51
      *
12
      * @constructor
52
      * @constructor
13
      */
53
      */
14
-    constructor(options = {}) {
54
+    constructor(options: IJibriSessionOptions = {}) {
15
         this._connection = options.connection;
55
         this._connection = options.connection;
16
         this._mode = options.mode;
56
         this._mode = options.mode;
17
         this._jibriJid = null;
57
         this._jibriJid = null;
18
         this._statusFromJicofo = '';
58
         this._statusFromJicofo = '';
19
-
20
         this._setSessionID(options.sessionID);
59
         this._setSessionID(options.sessionID);
21
         this.setStatus(options.status);
60
         this.setStatus(options.status);
61
+        this._focusMucJid = options.focusMucJid;
22
     }
62
     }
23
 
63
 
24
     /**
64
     /**
26
      *
66
      *
27
      * @returns {string|undefined}
67
      * @returns {string|undefined}
28
      */
68
      */
29
-    getError() {
69
+    getError(): string | undefined {
30
         return this._error;
70
         return this._error;
31
     }
71
     }
32
 
72
 
35
      *
75
      *
36
      * @returns {string|undefined}
76
      * @returns {string|undefined}
37
      */
77
      */
38
-    getID() {
78
+    getID(): string | undefined {
39
         return this._sessionID;
79
         return this._sessionID;
40
     }
80
     }
41
 
81
 
44
      *
84
      *
45
      * @returns {JitsiParticipant|string} The participant that started the session.
85
      * @returns {JitsiParticipant|string} The participant that started the session.
46
      */
86
      */
47
-    getInitiator() {
87
+    getInitiator(): JitsiParticipant | string {
48
         return this._initiator;
88
         return this._initiator;
49
     }
89
     }
50
 
90
 
53
      *
93
      *
54
      * @returns {string|undefined}
94
      * @returns {string|undefined}
55
      */
95
      */
56
-    getLiveStreamViewURL() {
96
+    getLiveStreamViewURL(): string | undefined {
57
         return this._liveStreamViewURL;
97
         return this._liveStreamViewURL;
58
     }
98
     }
59
 
99
 
62
      *
102
      *
63
      * @returns {string|undefined}
103
      * @returns {string|undefined}
64
      */
104
      */
65
-    getStatus() {
105
+    getStatus(): string | undefined {
66
         // If _status is not set fallback to the status reported by jicofo.
106
         // If _status is not set fallback to the status reported by jicofo.
67
         if (this._status) {
107
         if (this._status) {
68
             return this._status;
108
             return this._status;
74
     /**
114
     /**
75
      * @returns {string|undefined} the JID of jibri associated with this session.
115
      * @returns {string|undefined} the JID of jibri associated with this session.
76
      */
116
      */
77
-    getJibriJid() {
117
+    getJibriJid(): string | undefined {
78
         return this._jibriJid;
118
         return this._jibriJid;
79
     }
119
     }
80
 
120
 
83
      *
123
      *
84
      * @returns {JitsiParticipant|string} The participant that stopped the session.
124
      * @returns {JitsiParticipant|string} The participant that stopped the session.
85
      */
125
      */
86
-    getTerminator() {
126
+    getTerminator(): JitsiParticipant | string {
87
         return this._terminator;
127
         return this._terminator;
88
     }
128
     }
89
 
129
 
92
      *
132
      *
93
      * @returns {string}
133
      * @returns {string}
94
      */
134
      */
95
-    getMode() {
135
+    getMode(): string {
96
         return this._mode;
136
         return this._mode;
97
     }
137
     }
98
 
138
 
103
      * entered an error state.
143
      * entered an error state.
104
      * @returns {void}
144
      * @returns {void}
105
      */
145
      */
106
-    setError(error) {
146
+    setError(error: string): void {
107
         this._error = error;
147
         this._error = error;
108
     }
148
     }
109
 
149
 
114
      * @param {string} url - The live stream URL associated with the session.
154
      * @param {string} url - The live stream URL associated with the session.
115
      * @returns {void}
155
      * @returns {void}
116
      */
156
      */
117
-    setLiveStreamViewURL(url) {
157
+    setLiveStreamViewURL(url: string): void {
118
         this._liveStreamViewURL = url;
158
         this._liveStreamViewURL = url;
119
     }
159
     }
120
 
160
 
124
      * @param {string} status - The new status to set.
164
      * @param {string} status - The new status to set.
125
      * @returns {void}
165
      * @returns {void}
126
      */
166
      */
127
-    setStatus(status) {
167
+    setStatus(status?: string): void {
128
         this._status = status;
168
         this._status = status;
129
     }
169
     }
130
 
170
 
134
      *
174
      *
135
      * @param {string} status
175
      * @param {string} status
136
      */
176
      */
137
-    setStatusFromJicofo(status) {
177
+    setStatusFromJicofo(status: string): void {
138
         this._statusFromJicofo = status;
178
         this._statusFromJicofo = status;
139
     }
179
     }
140
 
180
 
143
      *
183
      *
144
      * @param {*} jibriJid
184
      * @param {*} jibriJid
145
      */
185
      */
146
-    setJibriJid(jibriJid) {
186
+    setJibriJid(jibriJid: string | null): void {
147
         this._jibriJid = jibriJid;
187
         this._jibriJid = jibriJid;
148
     }
188
     }
149
 
189
 
152
      * @param {JitsiParticipant | string} participant - The participant or resource id
192
      * @param {JitsiParticipant | string} participant - The participant or resource id
153
      * if local participant.
193
      * if local participant.
154
      */
194
      */
155
-    setInitiator(participant) {
195
+    setInitiator(participant: JitsiParticipant | string): void {
156
         this._initiator = participant;
196
         this._initiator = participant;
157
     }
197
     }
158
 
198
 
161
      * @param {JitsiParticipant | string} participant - The participant or the resource id
201
      * @param {JitsiParticipant | string} participant - The participant or the resource id
162
      * if local participant.
202
      * if local participant.
163
      */
203
      */
164
-    setTerminator(participant) {
204
+    setTerminator(participant: JitsiParticipant | string): void {
165
         this._terminator = participant;
205
         this._terminator = participant;
166
     }
206
     }
167
 
207
 
182
      * streaming service provider.
222
      * streaming service provider.
183
      * @returns Promise
223
      * @returns Promise
184
      */
224
      */
185
-    start({ appData, broadcastId, focusMucJid, streamId }) {
225
+    start({ appData, broadcastId, focusMucJid, streamId }: IStartOptions): Promise<void> {
186
         return new Promise((resolve, reject) => {
226
         return new Promise((resolve, reject) => {
187
-            this._connection.sendIQ(
227
+            this._connection?.sendIQ(
188
                 this._createIQ({
228
                 this._createIQ({
189
                     action: 'start',
229
                     action: 'start',
190
                     appData,
230
                     appData,
192
                     broadcastId,
232
                     broadcastId,
193
                     streamId
233
                     streamId
194
                 }),
234
                 }),
195
-                result => {
196
-                    // All users will eventually receive the 'pending' status
197
-                    // from the backend, but for the user initiating the session
198
-                    // it's better to give some instant feedback that recording
199
-                    // is starting so fire 'pending' here manually.
235
+                (result: any) => {
200
                     this.setStatus('pending');
236
                     this.setStatus('pending');
201
                     this._setSessionID(
237
                     this._setSessionID(
202
-                        recordingXMLUtils.getSessionIdFromIq(result));
238
+                        recordingXMLUtils.getSessionIdFromIq(result)
239
+                    );
203
 
240
 
204
                     resolve();
241
                     resolve();
205
                 },
242
                 },
206
-                error => {
243
+                (error: any) => {
207
                     this._setErrorFromIq(error);
244
                     this._setErrorFromIq(error);
208
 
245
 
209
                     reject(error);
246
                     reject(error);
210
-                });
247
+                }
248
+            );
211
         });
249
         });
212
     }
250
     }
213
 
251
 
220
      * that controls recording.
258
      * that controls recording.
221
      * @returns Promise
259
      * @returns Promise
222
      */
260
      */
223
-    stop({ focusMucJid }) {
261
+    stop({ focusMucJid }: IStopOptions): Promise<any> {
224
         return new Promise((resolve, reject) => {
262
         return new Promise((resolve, reject) => {
225
-            this._connection.sendIQ(
263
+            this._connection?.sendIQ(
226
                 this._createIQ({
264
                 this._createIQ({
227
                     action: 'stop',
265
                     action: 'stop',
228
                     focusMucJid
266
                     focusMucJid
229
                 }),
267
                 }),
230
                 resolve,
268
                 resolve,
231
-                reject);
269
+                reject
270
+            );
232
         });
271
         });
233
     }
272
     }
234
 
273
 
248
      * streaming service provider.
287
      * streaming service provider.
249
      * @returns Object - The XMPP IQ message.
288
      * @returns Object - The XMPP IQ message.
250
      */
289
      */
251
-    _createIQ({ action, appData, broadcastId, focusMucJid, streamId }) {
290
+    _createIQ({ action, appData, broadcastId, focusMucJid, streamId }: IQOptions) {
252
         return $iq({
291
         return $iq({
253
             to: focusMucJid,
292
             to: focusMucJid,
254
             type: 'set'
293
             type: 'set'
271
      * @private
310
      * @private
272
      * @returns {void}
311
      * @returns {void}
273
      */
312
      */
274
-    _setErrorFromIq(errorIq) {
313
+    _setErrorFromIq(errorIq: any): void {
275
         const error = errorIq.getElementsByTagName('error')[0];
314
         const error = errorIq.getElementsByTagName('error')[0];
276
 
315
 
277
         this.setError(error.children[0].tagName);
316
         this.setError(error.children[0].tagName);
284
      * @private
323
      * @private
285
      * @returns {void}
324
      * @returns {void}
286
      */
325
      */
287
-    _setSessionID(sessionID) {
326
+    _setSessionID(sessionID?: string): void {
288
         this._sessionID = sessionID;
327
         this._sessionID = sessionID;
289
     }
328
     }
290
 }
329
 }

Loading…
Cancel
Save