tirtc-react-native 2.3.1 → 2.4.0-alpha.4

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.
Files changed (77) hide show
  1. package/README.md +23 -7
  2. package/TiRtcReactNative.podspec +1 -1
  3. package/android/build.gradle +1 -1
  4. package/android/src/main/AndroidManifest.xml +2 -1
  5. package/android/src/main/java/com/tange/ai/tirtc/reactnative/TiRtcModule.kt +622 -28
  6. package/android/src/main/java/com/tange/ai/tirtc/reactnative/TiRtcReactNativeCleanup.kt +115 -0
  7. package/android/src/main/java/com/tange/ai/tirtc/reactnative/TiRtcReactNativeEventBinder.kt +32 -8
  8. package/android/src/main/java/com/tange/ai/tirtc/reactnative/TiRtcReactNativeEventSink.kt +16 -0
  9. package/android/src/main/java/com/tange/ai/tirtc/reactnative/TiRtcReactNativeMaps.kt +62 -0
  10. package/android/src/main/java/com/tange/ai/tirtc/reactnative/TiRtcReactNativeModuleSupport.kt +6 -6
  11. package/android/src/main/java/com/tange/ai/tirtc/reactnative/TiRtcReactNativeRegistry.kt +27 -0
  12. package/android/src/main/java/com/tange/ai/tirtc/reactnative/TiRtcReactNativeStorageSupport.kt +64 -0
  13. package/android/src/main/java/com/tange/ai/tirtc/reactnative/TiRtcVideoOutputViewManager.kt +17 -2
  14. package/ios/TiRtcReactNative+Conn.mm +173 -2
  15. package/ios/TiRtcReactNative+Input.mm +18 -4
  16. package/ios/TiRtcReactNative+Output.mm +41 -4
  17. package/ios/TiRtcReactNative+Private.h +4 -0
  18. package/ios/TiRtcReactNative+Storage.mm +513 -0
  19. package/ios/TiRtcReactNative.h +2 -1
  20. package/ios/TiRtcReactNative.mm +109 -1
  21. package/ios/TiRtcReactNativeEventBinder.h +8 -0
  22. package/ios/TiRtcReactNativeEventBinder.mm +102 -0
  23. package/ios/TiRtcReactNativeEventSink.h +3 -0
  24. package/ios/TiRtcReactNativeEventSink.mm +16 -0
  25. package/ios/TiRtcReactNativeRegistry.h +5 -0
  26. package/ios/TiRtcReactNativeRegistry.mm +43 -0
  27. package/ios/TiRtcVideoOutputView.mm +11 -0
  28. package/lib/module/audio_input.js +33 -17
  29. package/lib/module/audio_input.js.map +1 -1
  30. package/lib/module/audio_output.js +27 -10
  31. package/lib/module/audio_output.js.map +1 -1
  32. package/lib/module/conn.js +31 -7
  33. package/lib/module/conn.js.map +1 -1
  34. package/lib/module/errors.js +4 -3
  35. package/lib/module/errors.js.map +1 -1
  36. package/lib/module/index.js +2 -0
  37. package/lib/module/index.js.map +1 -1
  38. package/lib/module/logging.js +9 -1
  39. package/lib/module/logging.js.map +1 -1
  40. package/lib/module/logging_internal.js +27 -0
  41. package/lib/module/logging_internal.js.map +1 -0
  42. package/lib/module/recording.js +210 -0
  43. package/lib/module/recording.js.map +1 -0
  44. package/lib/module/runtime.js +13 -5
  45. package/lib/module/runtime.js.map +1 -1
  46. package/lib/module/specs/NativeTiRtc.js.map +1 -1
  47. package/lib/module/storage.js +706 -0
  48. package/lib/module/storage.js.map +1 -0
  49. package/lib/module/video_input.js +40 -22
  50. package/lib/module/video_input.js.map +1 -1
  51. package/lib/module/video_output.js +51 -10
  52. package/lib/module/video_output.js.map +1 -1
  53. package/lib/typescript/audio_input.d.ts +1 -0
  54. package/lib/typescript/conn.d.ts +2 -0
  55. package/lib/typescript/errors.d.ts +3 -2
  56. package/lib/typescript/index.d.ts +4 -0
  57. package/lib/typescript/logging_internal.d.ts +2 -0
  58. package/lib/typescript/recording.d.ts +53 -0
  59. package/lib/typescript/runtime.d.ts +2 -0
  60. package/lib/typescript/specs/NativeTiRtc.d.ts +88 -0
  61. package/lib/typescript/storage.d.ts +204 -0
  62. package/lib/typescript/video_input.d.ts +1 -0
  63. package/lib/typescript/video_output.d.ts +3 -0
  64. package/package.json +1 -1
  65. package/src/audio_input.ts +85 -59
  66. package/src/audio_output.ts +30 -17
  67. package/src/conn.ts +46 -28
  68. package/src/errors.ts +4 -3
  69. package/src/index.ts +54 -0
  70. package/src/logging.ts +7 -1
  71. package/src/logging_internal.ts +23 -0
  72. package/src/recording.ts +271 -0
  73. package/src/runtime.ts +14 -5
  74. package/src/specs/NativeTiRtc.ts +123 -0
  75. package/src/storage.ts +769 -0
  76. package/src/video_input.ts +97 -62
  77. package/src/video_output.ts +54 -16
@@ -0,0 +1,706 @@
1
+ import * as React from 'react';
2
+ import { View } from 'react-native';
3
+ import { isFiniteSafeInteger, isPlainObject, isUInt8, normalizeCreateResult, } from './errors';
4
+ import { registerNativeEventHandler, unregisterNativeEventHandler } from './events';
5
+ import { TiRtcLogging } from './logging';
6
+ import { NativeTiRtc, NativeTiRtcVideoOutputView } from './native';
7
+ import { clearNativeIdentity, readNativeIdentity, setNativeIdentity, subscribeNativeIdentity, } from './native_object_id';
8
+ import { NativeMediaFileBase, recordingFailure } from './recording';
9
+ export const TI_CLOUD_STORAGE_ERROR_OK = 0;
10
+ export const TI_CLOUD_STORAGE_ERROR_INVALID_ARGUMENT = 6000;
11
+ export const TI_CLOUD_STORAGE_ERROR_NOT_INITIALIZED = 6001;
12
+ export const TI_CLOUD_STORAGE_ERROR_TOKEN_EXPIRED = 6014;
13
+ export const TI_CLOUD_STORAGE_ERROR_ALREADY_INITIALIZED = 6022;
14
+ export const TI_CLOUD_STORAGE_ERROR_PERMISSION_DENIED = 6024;
15
+ export const TI_CLOUD_STORAGE_ERROR_IN_USE = 6026;
16
+ export const TI_CLOUD_STORAGE_ERROR_NOT_STARTED = 6027;
17
+ export const TI_CLOUD_STORAGE_ERROR_NOT_BOUND = 6029;
18
+ export const TI_CLOUD_STORAGE_ERROR_NOT_CONFIGURED = 6030;
19
+ export const TI_CLOUD_STORAGE_ERROR_RESOURCE_EXHAUSTED = 6043;
20
+ export const TI_CLOUD_STORAGE_ERROR_FILE_WRITE_FAILED = 6046;
21
+ export const TI_CLOUD_STORAGE_ERROR_UNSUPPORTED_FORMAT = 6113;
22
+ export const TI_CLOUD_STORAGE_ERROR_IO_FAILED = 6114;
23
+ export const TI_CLOUD_STORAGE_ERROR_CANCELLED = 6115;
24
+ export const TI_CLOUD_STORAGE_ERROR_RANGE_TOO_LARGE = 6117;
25
+ export const TI_CLOUD_STORAGE_ERROR_NO_FRAME = 6118;
26
+ export const TI_CLOUD_STORAGE_ERROR_NO_RECORDABLE_MEDIA = 6119;
27
+ export const TI_CLOUD_STORAGE_ERROR_RECORDING_OVERRUN = 6120;
28
+ export const TI_CLOUD_STORAGE_ERROR_RECORDING_UNREADABLE = 6122;
29
+ export const TI_CLOUD_STORAGE_ERROR_UNAVAILABLE = 6123;
30
+ export const TI_CLOUD_STORAGE_ERROR_STOPPED = 6124;
31
+ export const TiCloudStorageReplaySpeed = {
32
+ x0_125: 'x0_125',
33
+ x0_25: 'x0_25',
34
+ x0_5: 'x0_5',
35
+ x1: 'x1',
36
+ x2: 'x2',
37
+ x4: 'x4',
38
+ x8: 'x8',
39
+ };
40
+ const tiCloudStorageLogTag = 'TIRTC_REACT_NATIVE_CLOUD_STORAGE';
41
+ function tiCloudStorageLogOutcome(operation, code, details = '') {
42
+ const message = `[${operation}] outcome=${code === TI_CLOUD_STORAGE_ERROR_OK ? 'ok' : 'failed'} error_code=${code}${details}`;
43
+ if (code === TI_CLOUD_STORAGE_ERROR_OK)
44
+ TiRtcLogging.i(tiCloudStorageLogTag, message);
45
+ else
46
+ TiRtcLogging.e(tiCloudStorageLogTag, message);
47
+ }
48
+ export const TiCloudStorageAudioOutputState = {
49
+ idle: 'idle',
50
+ buffering: 'buffering',
51
+ playing: 'playing',
52
+ failed: 'failed',
53
+ paused: 'paused',
54
+ completed: 'completed',
55
+ };
56
+ export const TiCloudStorageVideoOutputState = {
57
+ idle: 'idle',
58
+ buffering: 'buffering',
59
+ rendering: 'rendering',
60
+ failed: 'failed',
61
+ paused: 'paused',
62
+ completed: 'completed',
63
+ };
64
+ class RecordingFile extends NativeMediaFileBase {
65
+ constructor(path, durationMs, id) {
66
+ super(path, id, 'video');
67
+ this.durationMs = durationMs;
68
+ }
69
+ }
70
+ class SnapshotFile extends NativeMediaFileBase {
71
+ constructor(path, id) {
72
+ super(path, id, 'image');
73
+ }
74
+ }
75
+ function success(data) {
76
+ return { success: true, code: null, message: null, data };
77
+ }
78
+ function validTime(value) {
79
+ return isFiniteSafeInteger(value) && value >= 0;
80
+ }
81
+ function speedToNative(speed) {
82
+ switch (speed) {
83
+ case TiCloudStorageReplaySpeed.x0_125:
84
+ return 6;
85
+ case TiCloudStorageReplaySpeed.x0_25:
86
+ return 5;
87
+ case TiCloudStorageReplaySpeed.x0_5:
88
+ return 4;
89
+ case TiCloudStorageReplaySpeed.x1:
90
+ return 0;
91
+ case TiCloudStorageReplaySpeed.x2:
92
+ return 1;
93
+ case TiCloudStorageReplaySpeed.x4:
94
+ return 2;
95
+ case TiCloudStorageReplaySpeed.x8:
96
+ return 3;
97
+ default:
98
+ return null;
99
+ }
100
+ }
101
+ function speedFromNative(speed) {
102
+ switch (speed) {
103
+ case 6:
104
+ return TiCloudStorageReplaySpeed.x0_125;
105
+ case 5:
106
+ return TiCloudStorageReplaySpeed.x0_25;
107
+ case 4:
108
+ return TiCloudStorageReplaySpeed.x0_5;
109
+ case 0:
110
+ return TiCloudStorageReplaySpeed.x1;
111
+ case 1:
112
+ return TiCloudStorageReplaySpeed.x2;
113
+ case 2:
114
+ return TiCloudStorageReplaySpeed.x4;
115
+ case 3:
116
+ return TiCloudStorageReplaySpeed.x8;
117
+ default:
118
+ return null;
119
+ }
120
+ }
121
+ function audioStateFromNative(state) {
122
+ switch (state) {
123
+ case TiCloudStorageAudioOutputState.idle:
124
+ case TiCloudStorageAudioOutputState.buffering:
125
+ case TiCloudStorageAudioOutputState.playing:
126
+ case TiCloudStorageAudioOutputState.failed:
127
+ case TiCloudStorageAudioOutputState.paused:
128
+ case TiCloudStorageAudioOutputState.completed:
129
+ return state;
130
+ default:
131
+ return TiCloudStorageAudioOutputState.failed;
132
+ }
133
+ }
134
+ function videoStateFromNative(state) {
135
+ switch (state) {
136
+ case TiCloudStorageVideoOutputState.idle:
137
+ case TiCloudStorageVideoOutputState.buffering:
138
+ case TiCloudStorageVideoOutputState.rendering:
139
+ case TiCloudStorageVideoOutputState.failed:
140
+ case TiCloudStorageVideoOutputState.paused:
141
+ case TiCloudStorageVideoOutputState.completed:
142
+ return state;
143
+ default:
144
+ return TiCloudStorageVideoOutputState.failed;
145
+ }
146
+ }
147
+ export class TiCloudStorage {
148
+ constructor(token) {
149
+ this.token = token;
150
+ this.identity = null;
151
+ this.disposed = false;
152
+ }
153
+ static init(options) {
154
+ if (!isPlainObject(options) || typeof options.appId !== 'string' || options.appId.length === 0) {
155
+ tiCloudStorageLogOutcome('cloud_storage_initialize', TI_CLOUD_STORAGE_ERROR_INVALID_ARGUMENT);
156
+ return Promise.resolve(TI_CLOUD_STORAGE_ERROR_INVALID_ARGUMENT);
157
+ }
158
+ return NativeTiRtc.tiCloudStorageInitialize({
159
+ appId: options.appId,
160
+ endpoint: options.endpoint ?? '',
161
+ consoleLogEnabled: options.consoleLogEnabled ?? false,
162
+ }).then((code) => {
163
+ tiCloudStorageLogOutcome('cloud_storage_initialize', code);
164
+ return code;
165
+ });
166
+ }
167
+ static shutdown() {
168
+ const nativeCode = NativeTiRtc.tiCloudStorageShutdown();
169
+ const code = nativeCode === TI_CLOUD_STORAGE_ERROR_NOT_INITIALIZED ? TI_CLOUD_STORAGE_ERROR_OK : nativeCode;
170
+ tiCloudStorageLogOutcome('cloud_storage_shutdown', code);
171
+ return code;
172
+ }
173
+ static errorToString(code) {
174
+ return NativeTiRtc.errorToString(code);
175
+ }
176
+ updateToken(token) {
177
+ if (token.length === 0)
178
+ return TI_CLOUD_STORAGE_ERROR_INVALID_ARGUMENT;
179
+ const identity = this.ensureIdentity();
180
+ if (typeof identity === 'number')
181
+ return identity;
182
+ const code = NativeTiRtc.tiCloudStorageUpdateToken(identity.objectId, token);
183
+ if (code === 0)
184
+ this.token = token;
185
+ tiCloudStorageLogOutcome('cloud_storage_update_token', code);
186
+ return code;
187
+ }
188
+ listRecordings(startTimeMs, endTimeMs) {
189
+ if (!validTime(startTimeMs) || !validTime(endTimeMs) || endTimeMs <= startTimeMs) {
190
+ return Promise.resolve({
191
+ code: TI_CLOUD_STORAGE_ERROR_INVALID_ARGUMENT,
192
+ recordings: [],
193
+ });
194
+ }
195
+ const identity = this.ensureIdentity();
196
+ if (typeof identity === 'number') {
197
+ tiCloudStorageLogOutcome('recording_list', identity);
198
+ return Promise.resolve({ code: identity, recordings: [] });
199
+ }
200
+ TiRtcLogging.i(tiCloudStorageLogTag, `[recording_list] outcome=started range_duration_ms=${endTimeMs - startTimeMs}`);
201
+ return NativeTiRtc.tiCloudStorageListRecordings(identity.objectId, startTimeMs, endTimeMs).then((result) => {
202
+ tiCloudStorageLogOutcome('recording_list', result.code, ` recording_count=${result.recordings.length}`);
203
+ return result;
204
+ });
205
+ }
206
+ listRecordingDays(startDate, endDate, timeZoneId = 'Asia/Shanghai') {
207
+ if (typeof startDate !== 'string' ||
208
+ typeof endDate !== 'string' ||
209
+ typeof timeZoneId !== 'string' ||
210
+ timeZoneId.length === 0) {
211
+ return Promise.resolve({ code: TI_CLOUD_STORAGE_ERROR_INVALID_ARGUMENT, days: [] });
212
+ }
213
+ const identity = this.ensureIdentity();
214
+ if (typeof identity === 'number') {
215
+ tiCloudStorageLogOutcome('recording_days_list', identity);
216
+ return Promise.resolve({ code: identity, days: [] });
217
+ }
218
+ TiRtcLogging.i(tiCloudStorageLogTag, '[recording_days_list] outcome=started');
219
+ return NativeTiRtc.tiCloudStorageListRecordingDays(identity.objectId, startDate, endDate, timeZoneId).then((result) => {
220
+ tiCloudStorageLogOutcome('recording_days_list', result.code, ` day_count=${result.days.length}`);
221
+ return result;
222
+ });
223
+ }
224
+ createReplay() {
225
+ return new TiCloudStorageReplay(this);
226
+ }
227
+ exportRecording(options, onProgress) {
228
+ if (!validTime(options.startTimeMs) ||
229
+ !validTime(options.endTimeMs) ||
230
+ options.endTimeMs <= options.startTimeMs ||
231
+ !isUInt8(options.videoChannelId) ||
232
+ (options.audioChannelId !== undefined && !isUInt8(options.audioChannelId))) {
233
+ return recordingFailure(TI_CLOUD_STORAGE_ERROR_INVALID_ARGUMENT);
234
+ }
235
+ const identity = this.ensureIdentity();
236
+ if (typeof identity === 'number')
237
+ return recordingFailure(identity);
238
+ const result = NativeTiRtc.tiCloudStorageExportRecording(identity.objectId, options.startTimeMs, options.endTimeMs, options.videoChannelId, options.audioChannelId ?? null);
239
+ tiCloudStorageLogOutcome('export_start', result.code, ` range_duration_ms=${options.endTimeMs - options.startTimeMs}`);
240
+ return result.code === 0 && result.taskId !== null
241
+ ? success(new TiCloudStorageExportTask(result.taskId, this, onProgress))
242
+ : recordingFailure(result.code);
243
+ }
244
+ dispose() {
245
+ if (this.disposed)
246
+ return 0;
247
+ if (this.identity === null) {
248
+ this.disposed = true;
249
+ this.token = '';
250
+ return 0;
251
+ }
252
+ const identity = this.identity;
253
+ const code = NativeTiRtc.tiCloudStorageDispose(identity.objectId);
254
+ if (code === 0) {
255
+ unregisterNativeEventHandler(identity);
256
+ clearNativeIdentity(this);
257
+ this.identity = null;
258
+ this.disposed = true;
259
+ this.token = '';
260
+ }
261
+ return code;
262
+ }
263
+ ensureIdentity() {
264
+ if (this.disposed)
265
+ return TI_CLOUD_STORAGE_ERROR_NOT_INITIALIZED;
266
+ if (this.identity !== null)
267
+ return this.identity;
268
+ if (this.token.length === 0)
269
+ return TI_CLOUD_STORAGE_ERROR_INVALID_ARGUMENT;
270
+ const result = normalizeCreateResult(NativeTiRtc.tiCloudStorageCreate(this.token));
271
+ if (result.code !== 0)
272
+ return result.code;
273
+ this.identity = {
274
+ objectId: result.objectId,
275
+ generation: result.generation,
276
+ };
277
+ setNativeIdentity(this, this.identity);
278
+ return this.identity;
279
+ }
280
+ }
281
+ function tiCloudStorageNativeIdentity(cloudStorage) {
282
+ return cloudStorage.ensureIdentity();
283
+ }
284
+ export class TiCloudStorageReplay {
285
+ constructor(cloudStorage) {
286
+ this.cloudStorage = cloudStorage;
287
+ this.identity = null;
288
+ this.disposed = false;
289
+ this.currentSpeed = 'x1';
290
+ this.currentTime = null;
291
+ this.onTimeChanged = null;
292
+ this.onError = null;
293
+ this.onCompleted = null;
294
+ }
295
+ get speed() {
296
+ if (this.identity !== null) {
297
+ const info = NativeTiRtc.tiCloudStorageReplayGetInfo(this.identity.objectId);
298
+ if (info.code === 0)
299
+ this.currentSpeed = speedFromNative(info.speed) ?? this.currentSpeed;
300
+ }
301
+ return this.currentSpeed;
302
+ }
303
+ get currentTimeMs() {
304
+ if (this.identity !== null) {
305
+ const info = NativeTiRtc.tiCloudStorageReplayGetInfo(this.identity.objectId);
306
+ if (info.code === 0)
307
+ this.currentTime = info.currentTimeMs;
308
+ }
309
+ return this.currentTime;
310
+ }
311
+ play(startTimeMs, endTimeMs, initialTimeMs) {
312
+ if (!validTime(startTimeMs) ||
313
+ !validTime(endTimeMs) ||
314
+ endTimeMs <= startTimeMs ||
315
+ (initialTimeMs !== undefined &&
316
+ (!validTime(initialTimeMs) || initialTimeMs < startTimeMs || initialTimeMs >= endTimeMs)))
317
+ return TI_CLOUD_STORAGE_ERROR_INVALID_ARGUMENT;
318
+ const identity = this.ensureIdentity();
319
+ const code = typeof identity === 'number'
320
+ ? identity
321
+ : NativeTiRtc.tiCloudStorageReplayPlay(identity.objectId, startTimeMs, endTimeMs, initialTimeMs ?? null);
322
+ tiCloudStorageLogOutcome('replay_play', code, ` range_duration_ms=${endTimeMs - startTimeMs}`);
323
+ return code;
324
+ }
325
+ pause() {
326
+ const code = this.command(NativeTiRtc.tiCloudStorageReplayPause);
327
+ tiCloudStorageLogOutcome('replay_pause', code);
328
+ return code;
329
+ }
330
+ resume() {
331
+ const code = this.command(NativeTiRtc.tiCloudStorageReplayResume);
332
+ tiCloudStorageLogOutcome('replay_resume', code);
333
+ return code;
334
+ }
335
+ seek(timeMs) {
336
+ if (!validTime(timeMs))
337
+ return TI_CLOUD_STORAGE_ERROR_INVALID_ARGUMENT;
338
+ const identity = this.ensureIdentity();
339
+ const code = typeof identity === 'number' ? identity : NativeTiRtc.tiCloudStorageReplaySeek(identity.objectId, timeMs);
340
+ tiCloudStorageLogOutcome('replay_seek', code);
341
+ return code;
342
+ }
343
+ setSpeed(speed) {
344
+ const index = speedToNative(speed);
345
+ if (index === null)
346
+ return TI_CLOUD_STORAGE_ERROR_INVALID_ARGUMENT;
347
+ const identity = this.ensureIdentity();
348
+ if (typeof identity === 'number')
349
+ return identity;
350
+ const code = NativeTiRtc.tiCloudStorageReplaySetSpeed(identity.objectId, index);
351
+ if (code === 0)
352
+ this.currentSpeed = speed;
353
+ tiCloudStorageLogOutcome('replay_speed', code, ` speed=${speed}`);
354
+ return code;
355
+ }
356
+ startRecording(options) {
357
+ if (!isUInt8(options.videoChannelId) ||
358
+ (options.audioChannelId !== undefined && !isUInt8(options.audioChannelId))) {
359
+ return recordingFailure(TI_CLOUD_STORAGE_ERROR_INVALID_ARGUMENT);
360
+ }
361
+ const identity = this.ensureIdentity();
362
+ if (typeof identity === 'number')
363
+ return recordingFailure(identity);
364
+ const result = NativeTiRtc.tiCloudStorageReplayStartRecording(identity.objectId, options.videoChannelId, options.audioChannelId ?? null);
365
+ tiCloudStorageLogOutcome('replay_recording_start', result.code);
366
+ return result.code === 0 && result.taskId !== null
367
+ ? success(new TiCloudStorageRecordingTask(result.taskId, this))
368
+ : recordingFailure(result.code);
369
+ }
370
+ stop() {
371
+ const code = this.command(NativeTiRtc.tiCloudStorageReplayStop);
372
+ if (code === 0)
373
+ this.currentTime = null;
374
+ tiCloudStorageLogOutcome('replay_stop', code);
375
+ return code;
376
+ }
377
+ dispose() {
378
+ if (this.disposed)
379
+ return 0;
380
+ if (this.identity === null) {
381
+ this.disposed = true;
382
+ this.onTimeChanged = null;
383
+ this.onError = null;
384
+ this.onCompleted = null;
385
+ return 0;
386
+ }
387
+ const identity = this.identity;
388
+ const code = NativeTiRtc.tiCloudStorageReplayDispose(identity.objectId);
389
+ if (code === 0) {
390
+ unregisterNativeEventHandler(identity);
391
+ clearNativeIdentity(this);
392
+ this.identity = null;
393
+ this.disposed = true;
394
+ this.onTimeChanged = null;
395
+ this.onError = null;
396
+ this.onCompleted = null;
397
+ }
398
+ tiCloudStorageLogOutcome('replay_dispose', code);
399
+ return code;
400
+ }
401
+ command(operation) {
402
+ const identity = this.ensureIdentity();
403
+ return typeof identity === 'number' ? identity : operation(identity.objectId);
404
+ }
405
+ ensureIdentity() {
406
+ if (this.disposed)
407
+ return TI_CLOUD_STORAGE_ERROR_NOT_INITIALIZED;
408
+ if (this.identity !== null)
409
+ return this.identity;
410
+ const cloudStorage = tiCloudStorageNativeIdentity(this.cloudStorage);
411
+ if (typeof cloudStorage === 'number')
412
+ return cloudStorage;
413
+ const result = normalizeCreateResult(NativeTiRtc.tiCloudStorageCreateReplay(cloudStorage.objectId));
414
+ if (result.code !== 0)
415
+ return result.code;
416
+ this.identity = {
417
+ objectId: result.objectId,
418
+ generation: result.generation,
419
+ };
420
+ setNativeIdentity(this, this.identity);
421
+ registerNativeEventHandler(this.identity, (event) => this.handleEvent(event));
422
+ return this.identity;
423
+ }
424
+ handleEvent(event) {
425
+ if (event.type === 'tiCloudStorageReplayTimeChanged' && event.timestampMs !== undefined) {
426
+ this.currentTime = event.timestampMs;
427
+ this.onTimeChanged?.(event.timestampMs);
428
+ }
429
+ else if (event.type === 'tiCloudStorageReplayCompleted')
430
+ this.onCompleted?.();
431
+ else if (event.type === 'tiCloudStorageReplayError')
432
+ this.onError?.(event.code ?? TI_CLOUD_STORAGE_ERROR_UNAVAILABLE);
433
+ }
434
+ }
435
+ function nativeReplayIdentity(replay) {
436
+ return replay.ensureIdentity();
437
+ }
438
+ class TiCloudStorageOutputBase {
439
+ constructor() {
440
+ this.identity = null;
441
+ this.disposed = false;
442
+ this.bound = false;
443
+ }
444
+ ensureIdentity() {
445
+ if (this.disposed)
446
+ return TI_CLOUD_STORAGE_ERROR_NOT_INITIALIZED;
447
+ if (this.identity !== null)
448
+ return this.identity;
449
+ const result = normalizeCreateResult(this.create());
450
+ if (result.code !== 0)
451
+ return result.code;
452
+ this.identity = {
453
+ objectId: result.objectId,
454
+ generation: result.generation,
455
+ };
456
+ setNativeIdentity(this, this.identity);
457
+ registerNativeEventHandler(this.identity, (event) => this.handleEvent(event));
458
+ return this.identity;
459
+ }
460
+ releaseIdentity() {
461
+ if (this.identity !== null) {
462
+ unregisterNativeEventHandler(this.identity);
463
+ clearNativeIdentity(this);
464
+ }
465
+ this.identity = null;
466
+ this.disposed = true;
467
+ }
468
+ }
469
+ export class TiCloudStorageAudioOutput extends TiCloudStorageOutputBase {
470
+ constructor() {
471
+ super(...arguments);
472
+ this.currentState = 'idle';
473
+ this.onStateChanged = null;
474
+ this.onError = null;
475
+ }
476
+ create() {
477
+ return NativeTiRtc.tiCloudStorageCreateAudioOutput();
478
+ }
479
+ get state() {
480
+ if (this.identity !== null)
481
+ this.currentState = audioStateFromNative(NativeTiRtc.tiCloudStorageAudioOutputGetState(this.identity.objectId).state ?? this.currentState);
482
+ return this.currentState;
483
+ }
484
+ attach(replay, channelId) {
485
+ if (!isUInt8(channelId))
486
+ return TI_CLOUD_STORAGE_ERROR_INVALID_ARGUMENT;
487
+ const output = this.ensureIdentity();
488
+ const replayId = nativeReplayIdentity(replay);
489
+ if (typeof output === 'number')
490
+ return output;
491
+ if (typeof replayId === 'number')
492
+ return replayId;
493
+ const code = NativeTiRtc.tiCloudStorageAudioOutputAttach(output.objectId, replayId.objectId, channelId);
494
+ if (code === 0)
495
+ this.bound = true;
496
+ tiCloudStorageLogOutcome('audio_output_attach', code);
497
+ return code;
498
+ }
499
+ setVolume(volumePercent) {
500
+ if (!isFiniteSafeInteger(volumePercent) || volumePercent < 0 || volumePercent > 100)
501
+ return TI_CLOUD_STORAGE_ERROR_INVALID_ARGUMENT;
502
+ const output = this.ensureIdentity();
503
+ return typeof output === 'number' ? output : NativeTiRtc.tiCloudStorageAudioOutputSetVolume(output.objectId, volumePercent);
504
+ }
505
+ detach() {
506
+ if (this.identity === null)
507
+ return 0;
508
+ const code = NativeTiRtc.tiCloudStorageAudioOutputDetach(this.identity.objectId);
509
+ if (code === 0) {
510
+ this.bound = false;
511
+ this.currentState = TiCloudStorageAudioOutputState.idle;
512
+ }
513
+ tiCloudStorageLogOutcome('audio_output_detach', code);
514
+ return code;
515
+ }
516
+ dispose() {
517
+ if (this.disposed)
518
+ return 0;
519
+ if (this.bound)
520
+ return TI_CLOUD_STORAGE_ERROR_IN_USE;
521
+ if (this.identity === null) {
522
+ this.disposed = true;
523
+ this.currentState = TiCloudStorageAudioOutputState.idle;
524
+ this.onStateChanged = null;
525
+ this.onError = null;
526
+ return 0;
527
+ }
528
+ const code = NativeTiRtc.tiCloudStorageAudioOutputDispose(this.identity.objectId);
529
+ if (code === 0) {
530
+ this.releaseIdentity();
531
+ this.currentState = TiCloudStorageAudioOutputState.idle;
532
+ this.onStateChanged = null;
533
+ this.onError = null;
534
+ }
535
+ tiCloudStorageLogOutcome('audio_output_dispose', code);
536
+ return code;
537
+ }
538
+ handleEvent(event) {
539
+ if (event.type === 'audioOutputStateChanged' && event.state !== undefined) {
540
+ this.currentState = audioStateFromNative(event.state);
541
+ this.onStateChanged?.(this.currentState);
542
+ }
543
+ else if (event.type === 'audioOutputError')
544
+ this.onError?.(event.code ?? TI_CLOUD_STORAGE_ERROR_IO_FAILED);
545
+ }
546
+ }
547
+ export class TiCloudStorageVideoOutput extends TiCloudStorageOutputBase {
548
+ constructor() {
549
+ super(...arguments);
550
+ this.currentState = 'idle';
551
+ this.onStateChanged = null;
552
+ this.onError = null;
553
+ }
554
+ create() {
555
+ return NativeTiRtc.tiCloudStorageCreateVideoOutput();
556
+ }
557
+ get state() {
558
+ if (this.identity !== null)
559
+ this.currentState = videoStateFromNative(NativeTiRtc.tiCloudStorageVideoOutputGetState(this.identity.objectId).state ?? this.currentState);
560
+ return this.currentState;
561
+ }
562
+ view(props = {}) {
563
+ this.ensureIdentity();
564
+ return React.createElement(TiCloudStorageVideoOutputView, {
565
+ ...props,
566
+ output: this,
567
+ });
568
+ }
569
+ attach(replay, channelId) {
570
+ if (!isUInt8(channelId))
571
+ return TI_CLOUD_STORAGE_ERROR_INVALID_ARGUMENT;
572
+ const output = this.ensureIdentity();
573
+ const replayId = nativeReplayIdentity(replay);
574
+ if (typeof output === 'number')
575
+ return output;
576
+ if (typeof replayId === 'number')
577
+ return replayId;
578
+ const code = NativeTiRtc.tiCloudStorageVideoOutputAttach(output.objectId, replayId.objectId, channelId);
579
+ if (code === 0)
580
+ this.bound = true;
581
+ tiCloudStorageLogOutcome('video_output_attach', code);
582
+ return code;
583
+ }
584
+ async takeSnapshot() {
585
+ const output = this.ensureIdentity();
586
+ if (typeof output === 'number')
587
+ return recordingFailure(output);
588
+ const result = await NativeTiRtc.tiCloudStorageVideoOutputTakeSnapshot(output.objectId);
589
+ tiCloudStorageLogOutcome('snapshot', result.code);
590
+ return result.code === 0 && result.fileId !== null && result.filePath !== null
591
+ ? success(new SnapshotFile(result.filePath, result.fileId))
592
+ : recordingFailure(result.code);
593
+ }
594
+ detach() {
595
+ if (this.identity === null)
596
+ return 0;
597
+ const code = NativeTiRtc.tiCloudStorageVideoOutputDetach(this.identity.objectId);
598
+ if (code === 0) {
599
+ this.bound = false;
600
+ this.currentState = TiCloudStorageVideoOutputState.idle;
601
+ }
602
+ tiCloudStorageLogOutcome('video_output_detach', code);
603
+ return code;
604
+ }
605
+ dispose() {
606
+ if (this.disposed)
607
+ return 0;
608
+ if (this.bound)
609
+ return TI_CLOUD_STORAGE_ERROR_IN_USE;
610
+ if (this.identity === null) {
611
+ this.disposed = true;
612
+ this.currentState = TiCloudStorageVideoOutputState.idle;
613
+ this.onStateChanged = null;
614
+ this.onError = null;
615
+ return 0;
616
+ }
617
+ const code = NativeTiRtc.tiCloudStorageVideoOutputDispose(this.identity.objectId);
618
+ if (code === 0) {
619
+ this.releaseIdentity();
620
+ this.currentState = TiCloudStorageVideoOutputState.idle;
621
+ this.onStateChanged = null;
622
+ this.onError = null;
623
+ }
624
+ tiCloudStorageLogOutcome('video_output_dispose', code);
625
+ return code;
626
+ }
627
+ handleEvent(event) {
628
+ if (event.type === 'videoOutputStateChanged' && event.state !== undefined) {
629
+ this.currentState = videoStateFromNative(event.state);
630
+ this.onStateChanged?.(this.currentState);
631
+ }
632
+ else if (event.type === 'videoOutputError')
633
+ this.onError?.(event.code ?? TI_CLOUD_STORAGE_ERROR_IO_FAILED);
634
+ }
635
+ }
636
+ export function TiCloudStorageVideoOutputView(props) {
637
+ const { output, resizeMode = 'contain', ...viewProps } = props;
638
+ const identity = useOwnerIdentity(output);
639
+ return identity === null
640
+ ? React.createElement(View, viewProps)
641
+ : React.createElement(NativeTiRtcVideoOutputView, {
642
+ ...viewProps,
643
+ nativeObjectId: identity.objectId,
644
+ generation: identity.generation,
645
+ resizeMode,
646
+ });
647
+ }
648
+ function useOwnerIdentity(owner) {
649
+ const [, update] = React.useReducer((value) => value + 1, 0);
650
+ React.useEffect(() => subscribeNativeIdentity(owner, update), [owner]);
651
+ return readNativeIdentity(owner);
652
+ }
653
+ export class TiCloudStorageRecordingTask {
654
+ constructor(taskId, owner) {
655
+ this.taskId = taskId;
656
+ this.owner = owner;
657
+ this.stopPromise = null;
658
+ }
659
+ stop() {
660
+ void this.owner;
661
+ return (this.stopPromise ?? (this.stopPromise = NativeTiRtc.tiCloudStorageRecordingTaskStop(this.taskId).then((result) => {
662
+ tiCloudStorageLogOutcome('replay_recording_stop', result.code, ` duration_ms=${result.durationMs ?? 0}`);
663
+ return result.code === 0 && result.fileId !== null && result.filePath !== null && result.durationMs !== null
664
+ ? success(new RecordingFile(result.filePath, result.durationMs, result.fileId))
665
+ : recordingFailure(result.code);
666
+ })));
667
+ }
668
+ }
669
+ export class TiCloudStorageExportTask {
670
+ constructor(taskId, owner, onProgress) {
671
+ this.taskId = taskId;
672
+ this.owner = owner;
673
+ this.onProgress = onProgress;
674
+ this.resultPromise = NativeTiRtc.tiCloudStorageExportResult(taskId).then((result) => {
675
+ tiCloudStorageLogOutcome('export_terminal', result.code, ` duration_ms=${result.durationMs ?? 0}`);
676
+ return result.code === 0 && result.fileId !== null && result.filePath !== null && result.durationMs !== null
677
+ ? success(new RecordingFile(result.filePath, result.durationMs, result.fileId))
678
+ : recordingFailure(result.code);
679
+ });
680
+ if (onProgress !== undefined) {
681
+ let lastProgress = -1;
682
+ const timer = setInterval(() => {
683
+ const progress = NativeTiRtc.tiCloudStorageExportProgress(taskId);
684
+ if (progress !== lastProgress) {
685
+ lastProgress = progress;
686
+ onProgress(progress);
687
+ }
688
+ }, 100);
689
+ const clear = () => clearInterval(timer);
690
+ void this.resultPromise.then(clear, clear);
691
+ }
692
+ }
693
+ get progress() {
694
+ void this.owner;
695
+ return NativeTiRtc.tiCloudStorageExportProgress(this.taskId);
696
+ }
697
+ get result() {
698
+ return this.resultPromise;
699
+ }
700
+ stop() {
701
+ const code = NativeTiRtc.tiCloudStorageExportStop(this.taskId);
702
+ tiCloudStorageLogOutcome('export_stop', code);
703
+ return this.resultPromise;
704
+ }
705
+ }
706
+ //# sourceMappingURL=storage.js.map