-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
ion.ts
156 lines (123 loc) · 3.39 KB
/
ion.ts
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import Client, { Configuration } from './client';
import { LocalStream, RemoteStream, Constraints } from './stream';
import { BizClient } from './signal/biz';
import { IonSFUGRPCWebSignal } from './signal/grpc-web-impl';
import { Signal, Trickle } from './signal';
export { Client, LocalStream, RemoteStream, Constraints, Signal, Trickle };
export interface JoinResult {
success: boolean;
reason: string;
}
export enum PeerState {
NONE,
JOIN,
UPDATE,
LEAVE,
}
export interface Peer {
uid: string;
sid: string;
info: Record<string, any>;
}
export interface PeerEvent {
state: PeerState;
peer: Peer;
}
export enum StreamState {
NONE,
ADD,
REMOVE,
}
export interface StreamEvent {
uid: string;
state: StreamState;
streams: Stream[];
}
export interface Track {
id: string;
label: string;
kind: string;
simulcast: Map<string, string>;
}
export interface Stream {
id: string;
tracks: Track[];
}
export interface Message {
from: string;
to: string;
data: Record<string, any>;
}
export class IonConnector {
private _biz: BizClient;
private _sfu: Client | undefined;
private _sid: string;
private _uid: string;
onerror?: (err: Error) => void;
onjoin?: (success: boolean, reason: string) => void;
onleave?: (reason: string) => void;
onpeerevent?: (ev: PeerEvent) => void;
onstreamevent?: (ev: StreamEvent) => void;
onmessage?: (msg: Message) => void;
ontrack?: (track: MediaStreamTrack, stream: RemoteStream) => void;
ondatachannel?: (ev: RTCDataChannelEvent) => void;
onspeaker?: (ev: string[]) => void;
constructor(url: string, config?: Configuration) {
this._sid = '';
this._uid = '';
this._sfu = undefined;
this._biz = new BizClient(url);
this._biz.on('join-reply', async (success: boolean, reason: string) => {
if (this.onjoin) {
this.onjoin(success, reason);
}
if (success && !this._sfu) {
const signal = new IonSFUGRPCWebSignal(url);
const sfu = new Client(signal, config);
sfu.ontrack = (track: MediaStreamTrack, stream: RemoteStream) => this.ontrack?.call(this, track, stream);
sfu.ondatachannel = (ev: RTCDataChannelEvent) => this.ondatachannel?.call(this, ev);
sfu.onspeaker = (ev: string[]) => this.onspeaker?.call(this, ev);
this._sfu = sfu;
await sfu.join(this._sid, this._uid);
}
});
this._biz.on('leave-reply', (reason: string) => {
if (this.onleave) {
this.onleave(reason);
}
});
this._biz.on('peer-event', (ev: PeerEvent) => {
if (this.onpeerevent) {
this.onpeerevent(ev);
}
});
this._biz.on('stream-event', (ev: StreamEvent) => {
if (this.onstreamevent) {
this.onstreamevent(ev);
}
});
this._biz.on('message', (msg: Message) => {
if (this.onmessage) {
this.onmessage(msg);
}
});
}
get sfu() {
return this._sfu;
}
async join(sid: string, uid: string, info: Record<string, any>, token: string | undefined): Promise<JoinResult> {
this._sid = sid;
this._uid = uid;
return this._biz.join(sid, uid, info, token);
}
async leave(uid: string): Promise<string> {
return this._biz.leave(uid);
}
async message(from: string, to: string, data: Record<string, any>): Promise<void> {
return this._biz.sendMessage(from, to, data);
}
close() {
this._sfu?.close();
this._biz.close();
}
}