-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathStringeeVideoRoom.js
More file actions
137 lines (122 loc) · 3.66 KB
/
StringeeVideoRoom.js
File metadata and controls
137 lines (122 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import {NativeEventEmitter} from 'react-native';
import {each, object} from 'underscore';
import {roomEvents} from '../helpers/StringeeHelper';
import {RNStringeeVideo} from '../StringeeClient';
import {
StringeeRoomUser,
StringeeVideoTrack,
StringeeVideoTrackInfo,
StringeeVideoTrackOption,
} from '../../index';
import type {RNStringeeEventCallback} from '../helpers/StringeeHelper';
export default class StringeeVideoRoom {
clientId: string;
id: string;
recorded: boolean;
constructor(props) {
this.id = props.id;
this.recorded = props.recorded;
this.clientId = props.clientId;
this.events = [];
this.subscriptions = [];
this.eventEmitter = new NativeEventEmitter(RNStringeeVideo);
}
registerRoomEvent(events: Object) {
if (typeof events !== 'object') {
return;
}
each(events, (handler, type) => {
const eventName = roomEvents[type];
if (eventName !== undefined) {
this.subscriptions.push(
this.eventEmitter.addListener(eventName, data => {
if (handler !== undefined) {
if (type === 'didJoinRoom' || type === 'didLeaveRoom') {
handler(new StringeeRoomUser(data));
} else if (
type === 'didAddVideoTrack' ||
type === 'didRemoveVideoTrack'
) {
handler(new StringeeVideoTrackInfo(data));
} else if (type === 'didReceiveRoomMessage') {
let msg = JSON.parse(data.msg);
let from = new StringeeRoomUser(data.from);
handler({msg, from});
} else if (type === 'trackReadyToPlay') {
handler(new StringeeVideoTrack(data));
}
}
}),
);
this.events.push(eventName);
RNStringeeVideo.setNativeEvent(eventName);
} else {
console.log(`${type} is not a supported event`);
}
});
}
close() {
this.subscriptions.forEach(e => e.remove());
this.subscriptions = [];
this.events.forEach(e => RNStringeeVideo.removeNativeEvent(e));
this.events = [];
}
publish(videoTrack: StringeeVideoTrack, callback: RNStringeeEventCallback) {
RNStringeeVideo.publish(
this.clientId,
this.id,
videoTrack.localId,
(status, code, message, data) => {
if (status) {
return callback(status, code, message, new StringeeVideoTrack(data));
} else {
return callback(status, code, message);
}
},
);
}
unpublish(videoTrack: StringeeVideoTrack, callback: RNStringeeEventCallback) {
RNStringeeVideo.unpublish(
this.clientId,
this.id,
videoTrack.localId,
callback,
);
}
subscribe(
videoTrackInfo: StringeeVideoTrackInfo,
options: StringeeVideoTrackOption,
callback: RNStringeeEventCallback,
) {
RNStringeeVideo.subscribe(
this.clientId,
this.id,
videoTrackInfo.id,
options,
(status, code, message, data) => {
if (status) {
return callback(status, code, message, new StringeeVideoTrack(data));
} else {
return callback(status, code, message);
}
},
);
}
unsubscribe(
videoTrackInfo: StringeeVideoTrackInfo,
callback: RNStringeeEventCallback,
) {
RNStringeeVideo.unsubscribe(
this.clientId,
this.id,
videoTrackInfo.id,
callback,
);
}
leave(allClient: boolean, callback: RNStringeeEventCallback) {
RNStringeeVideo.leave(this.clientId, this.id, allClient, callback);
}
sendMessage(msg, callback: RNStringeeEventCallback) {
RNStringeeVideo.sendMessage(this.clientId, this.id, msg, callback);
}
}