-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathjanus_session.dart
More file actions
219 lines (212 loc) · 7.48 KB
/
Copy pathjanus_session.dart
File metadata and controls
219 lines (212 loc) · 7.48 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
part of janus_client;
class JanusSession {
late JanusTransport? _transport;
late JanusClient _context;
int? _sessionId;
Timer? _keepAliveTimer;
Map<int?, JanusPlugin> _pluginHandles = {};
int? get sessionId => _sessionId;
JanusSession(
{int? refreshInterval,
required JanusTransport transport,
required JanusClient context}) {
_context = context;
_transport = transport;
}
Future<void> create() async {
try {
String transaction = getUuid().v4();
Map<String, dynamic> request = {
"janus": "create",
"transaction": transaction,
..._context._tokenMap,
..._context._apiMap
}..removeWhere((key, value) => value == null);
Map<String, dynamic>? response;
if (_transport is RestJanusTransport) {
RestJanusTransport rest = (_transport as RestJanusTransport);
response = (await rest.post(request)) as Map<String, dynamic>?;
if (response != null) {
if (response.containsKey('janus') && response.containsKey('data')) {
_sessionId = response['data']['id'];
rest.sessionId = sessionId;
}
} else {
throw "Janus Server not live or incorrect url/path specified";
}
} else if (_transport is WebSocketJanusTransport) {
WebSocketJanusTransport ws = (_transport as WebSocketJanusTransport);
if (!ws.isConnected) {
ws.connect();
}
response = await ws.send(request, handleId: null);
if (response!.containsKey('janus') && response.containsKey('data')) {
_sessionId = response['data']['id'] as int?;
ws.sessionId = sessionId;
}
}
_keepAlive();
} on WebSocketChannelException catch (e) {
throw "Connection to given url can't be established\n reason:-" +
e.message!;
} catch (e) {
throw "Connection to given url can't be established\n reason:-" +
e.toString();
}
}
/// This can be used to attach plugin handle to the session.<br><br>
/// [opaqueId] : opaque id is an optional string identifier used for client side correlations in event handlers or admin API.<br>
Future<T> attach<T extends JanusPlugin>({String? opaqueId}) async {
JanusPlugin plugin;
int? handleId;
String transaction = getUuid().v4();
Map<String, dynamic> request = {
"janus": "attach",
"transaction": transaction,
..._context._apiMap,
..._context._tokenMap
};
if (opaqueId != null) {
request["opaque_id"] = opaqueId;
}
request["session_id"] = sessionId;
Map<String, dynamic>? response;
if (T == JanusVideoRoomPlugin) {
plugin = JanusVideoRoomPlugin(
transport: _transport,
context: _context,
handleId: handleId,
session: this);
} else if (T == JanusVideoCallPlugin) {
plugin = JanusVideoCallPlugin(
transport: _transport,
context: _context,
handleId: handleId,
session: this);
} else if (T == JanusStreamingPlugin) {
plugin = JanusStreamingPlugin(
transport: _transport,
context: _context,
handleId: handleId,
session: this);
} else if (T == JanusAudioBridgePlugin) {
plugin = JanusAudioBridgePlugin(
transport: _transport,
context: _context,
handleId: handleId,
session: this);
} else if (T == JanusTextRoomPlugin) {
plugin = JanusTextRoomPlugin(
transport: _transport,
context: _context,
handleId: handleId,
session: this);
} else if (T == JanusEchoTestPlugin) {
plugin = JanusEchoTestPlugin(
transport: _transport,
context: _context,
handleId: handleId,
session: this);
} else if (T == JanusSipPlugin) {
plugin = JanusSipPlugin(
transport: _transport,
context: _context,
handleId: handleId,
session: this);
} else {
throw UnimplementedError(
'''This Plugin is not defined kindly refer to Janus Server Docs
make sure you specify the type of plugin you want to attach like session.attach<JanusVideoRoomPlugin>();
''');
}
request.putIfAbsent("plugin", () => plugin.plugin);
_context._logger.fine(request);
if (_transport is RestJanusTransport) {
_context._logger.info('using rest transport for creating plugin handle');
RestJanusTransport rest = (_transport as RestJanusTransport);
response = (await rest.post(request)) as Map<String, dynamic>?;
_context._logger.fine(response);
if (response != null &&
response.containsKey('janus') &&
response.containsKey('data')) {
handleId = response['data']['id'];
rest.sessionId = sessionId;
} else {
throw "Network error or janus server not running";
}
} else if (_transport is WebSocketJanusTransport) {
_context._logger
.info('using web socket transport for creating plugin handle');
WebSocketJanusTransport ws = (_transport as WebSocketJanusTransport);
if (!ws.isConnected) {
ws.connect();
}
response = await ws.send(request, handleId: null);
if (response!.containsKey('janus') && response.containsKey('data')) {
handleId = response['data']['id'] as int?;
_context._logger.fine(response);
}
}
plugin.handleId = handleId;
_pluginHandles[handleId] = plugin;
try {
await plugin._init();
} on MissingPluginException {
_context._logger.info(
'Platform exception: i believe you are trying in unit tests, platform specific api not accessible');
}
plugin.onCreate();
return plugin as T;
}
void dispose() {
if (_keepAliveTimer != null) {
_keepAliveTimer!.cancel();
}
if (_transport != null) {
_transport?.dispose();
}
}
_keepAlive() {
if (sessionId != null) {
this._keepAliveTimer = Timer.periodic(
Duration(seconds: _context._refreshInterval), (timer) async {
try {
String transaction = getUuid().v4();
Map<String, dynamic>? response;
if (_transport is RestJanusTransport) {
RestJanusTransport rest = (_transport as RestJanusTransport);
_context._logger.finer("keep alive using RestTransport");
response = (await rest.post({
"janus": "keepalive",
"session_id": sessionId,
"transaction": transaction,
..._context._apiMap,
..._context._tokenMap
})) as Map<String, dynamic>;
_context._logger.finest(response);
} else if (_transport is WebSocketJanusTransport) {
_context._logger.finest("keep alive using WebSocketTransport");
WebSocketJanusTransport ws =
(_transport as WebSocketJanusTransport);
if (!ws.isConnected) {
_context._logger.finest(
"not connected trying to establish connection to webSocket");
ws.connect();
}
response = await ws.send({
"janus": "keepalive",
"session_id": sessionId,
"transaction": transaction,
..._context._apiMap,
..._context._tokenMap
}, handleId: null);
_context._logger.finest("keepalive request sent to webSocket");
_context._logger.finest(response);
}
} catch (e) {
timer.cancel();
}
});
}
}
}