-
Notifications
You must be signed in to change notification settings - Fork 386
/
agora_rtc_engine_web.dart
248 lines (222 loc) · 7.8 KB
/
agora_rtc_engine_web.dart
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
@JS()
library agora_rtc_engine_web;
import 'dart:async';
import 'dart:convert';
// In order to *not* need this ignore, consider extracting the "web" version
// of your plugin as a separate package, instead of inlining it in the same
// package as the core of your plugin.
// ignore: avoid_web_libraries_in_flutter
import 'dart:html';
import 'dart:ui' as ui;
import 'package:agora_rtc_engine/src/enums.dart';
import 'package:flutter/services.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:js/js.dart';
import 'package:js/js_util.dart';
@JS('IrisRtcEngine')
class _IrisRtcEngine {
external _IrisRtcEngine();
external _IrisRtcChannel get channel;
external _IrisRtcDeviceManager get deviceManager;
external Future<dynamic> callApi(int apiType, String params, [Object? extra]);
external void setEventHandler(Function params);
}
@JS('IrisRtcChannel')
class _IrisRtcChannel {
external Future<dynamic> callApi(int apiType, String params, [Object? extra]);
external void setEventHandler(Function params);
}
@JS('IrisRtcDeviceManager')
class _IrisRtcDeviceManager {
external Future<dynamic> callApiAudio(int apiType, String params,
[Object? extra]);
external Future<dynamic> callApiVideo(int apiType, String params,
[Object? extra]);
}
/// A web implementation of the AgoraRtcEngine plugin.
class AgoraRtcEngineWeb {
// ignore: public_member_api_docs
static void registerWith(Registrar registrar) {
final methodChannel = MethodChannel(
'agora_rtc_engine',
const StandardMethodCodec(),
registrar,
);
final eventChannel = PluginEventChannel(
'agora_rtc_engine/events', const StandardMethodCodec(), registrar);
final pluginInstance = AgoraRtcEngineWeb();
methodChannel.setMethodCallHandler(pluginInstance.handleMethodCall);
eventChannel.setController(pluginInstance._controllerEngine);
MethodChannel(
'agora_rtc_channel',
const StandardMethodCodec(),
registrar,
).setMethodCallHandler(pluginInstance.handleChannelMethodCall);
PluginEventChannel(
'agora_rtc_channel/events', const StandardMethodCodec(), registrar)
.setController(pluginInstance._controllerChannel);
MethodChannel(
'agora_rtc_audio_device_manager',
const StandardMethodCodec(),
registrar,
).setMethodCallHandler(pluginInstance.handleADMMethodCall);
MethodChannel(
'agora_rtc_video_device_manager',
const StandardMethodCodec(),
registrar,
).setMethodCallHandler(pluginInstance.handleVDMMethodCall);
// ignore: undefined_prefixed_name
ui.platformViewRegistry.registerViewFactory('AgoraSurfaceView',
(int viewId) {
var element = DivElement();
MethodChannel('agora_rtc_engine/surface_view_$viewId',
const StandardMethodCodec(), registrar)
.setMethodCallHandler(
(call) => pluginInstance.handleViewMethodCall(call, element));
return element;
});
var element = ScriptElement()
..src =
'assets/packages/agora_rtc_engine/assets/AgoraRtcWrapper.bundle.js'
..type = 'application/javascript';
late StreamSubscription<Event> loadSubscription;
loadSubscription = element.onLoad.listen((event) {
loadSubscription.cancel();
pluginInstance._engineMain = _IrisRtcEngine();
pluginInstance._engineSub = _IrisRtcEngine();
});
document.body!.append(element);
}
final _controllerEngine = StreamController();
final _controllerChannel = StreamController();
late _IrisRtcEngine _engineMain;
late _IrisRtcEngine _engineSub;
_IrisRtcEngine _engine(Map<String, dynamic> args) {
bool subProcess = args['subProcess'];
if (subProcess) {
return _engineSub;
} else {
return _engineMain;
}
}
/// Handles method calls over the MethodChannel of this plugin.
/// Note: Check the "federated" architecture for a new way of doing this:
/// https://flutter.dev/go/federated-plugins
Future<dynamic> handleMethodCall(MethodCall call) async {
var args = <String, dynamic>{};
if (call.arguments != null) {
args = Map<String, dynamic>.from(call.arguments);
}
if (call.method == 'callApi') {
int apiType = args['apiType'];
if (apiType == 0) {
_engine(args).setEventHandler(allowInterop((String event, String data) {
_controllerEngine.add({
'methodName': event,
'data': data,
'subProcess': _engine(args) == _engineSub,
});
}));
_engine(args)
.channel
.setEventHandler(allowInterop((String event, String data) {
_controllerChannel.add({
'methodName': event,
'data': data,
});
}));
}
String param = args['params'];
return promiseToFuture(_engine(args).callApi(apiType, param));
} else {
throw PlatformException(code: ErrorCode.NotSupported.toString());
}
}
// ignore: public_member_api_docs
Future<dynamic> handleChannelMethodCall(MethodCall call) async {
var args = <String, dynamic>{};
if (call.arguments != null) {
args = Map<String, dynamic>.from(call.arguments);
}
if (call.method == 'callApi') {
int apiType = args['apiType'];
String param = args['params'];
return promiseToFuture(_engineMain.channel.callApi(apiType, param));
} else {
throw PlatformException(code: ErrorCode.NotSupported.toString());
}
}
// ignore: public_member_api_docs
Future<dynamic> handleADMMethodCall(MethodCall call) async {
var args = <String, dynamic>{};
if (call.arguments != null) {
args = Map<String, dynamic>.from(call.arguments);
}
if (call.method == 'callApi') {
int apiType = args['apiType'];
String param = args['params'];
return promiseToFuture(
_engineMain.deviceManager.callApiAudio(apiType, param));
} else {
throw PlatformException(code: ErrorCode.NotSupported.toString());
}
}
// ignore: public_member_api_docs
Future<dynamic> handleVDMMethodCall(MethodCall call) async {
var args = <String, dynamic>{};
if (call.arguments != null) {
args = Map<String, dynamic>.from(call.arguments);
}
if (call.method == 'callApi') {
int apiType = args['apiType'];
String param = args['params'];
return promiseToFuture(
_engineMain.deviceManager.callApiVideo(apiType, param));
} else {
throw PlatformException(code: ErrorCode.NotSupported.toString());
}
}
// ignore: public_member_api_docs
Future<dynamic> handleViewMethodCall(MethodCall call, Element element) async {
var data = <String, dynamic>{};
if (call.arguments != null) {
data = Map<String, dynamic>.from(call.arguments);
}
if (call.method == 'setData') {
final uid = data['userId'];
if (uid == 0) {
const kEngineSetupLocalVideo = 20;
return promiseToFuture(_engine(data).callApi(
kEngineSetupLocalVideo,
jsonEncode({
'canvas': {
'uid': 0,
'channelId': data['channelId'],
'renderMode': data['renderMode'],
'mirrorMode': data['mirrorMode'],
},
}),
element));
} else {
const kEngineSetupRemoteVideo = 21;
return promiseToFuture(_engine(data).callApi(
kEngineSetupRemoteVideo,
jsonEncode({
'canvas': {
'uid': uid,
'channelId': data['channelId'],
'renderMode': data['renderMode'],
'mirrorMode': data['mirrorMode'],
}
}),
element));
}
} else {
throw PlatformException(
code: 'Unimplemented',
details:
'agora_rtc_engine for web doesn\'t implement \'${call.method}\'',
);
}
}
}