-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathjanus_video_call_plugin.dart
More file actions
101 lines (92 loc) · 4.52 KB
/
Copy pathjanus_video_call_plugin.dart
File metadata and controls
101 lines (92 loc) · 4.52 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
part of janus_client;
class JanusVideoCallPlugin extends JanusPlugin {
JanusVideoCallPlugin({handleId, context, transport, session})
: super(context: context, handleId: handleId, plugin: JanusPlugins.VIDEO_CALL, session: session, transport: transport);
/// Requests the list of registered peers. Results arrive via plugin events.
Future<void> getList() async {
const payload = {"request": "list"};
await this.send(data: payload);
}
/// Registers the local user so they can receive and place calls.
Future<void> register(String userName) async {
var payload = {"request": "register", "username": userName};
await this.send(data: payload);
}
/// Updates media or recording preferences for the current call.
///
/// Parameters map directly to the Janus `set` request fields.
Future<void> set({RTCSessionDescription? jsep, bool? audio, bool? video, int? bitrate, bool? record, String? filename, int? substream, int? temporal, int? fallback}) async {
var payload = {
"request": "set",
"audio": audio,
"video": video,
"bitrate": bitrate,
"record": record,
"filename": filename,
"substream": substream,
"temporal": temporal,
"fallback": fallback,
}..removeWhere((key, value) => value == null);
await this.send(data: payload, jsep: jsep);
}
/// Initiates a call toward [userName], creating an offer when one is not provided.
Future<void> call(String userName, {RTCSessionDescription? offer}) async {
var payload = {"request": "call", "username": userName};
if (offer == null) {
offer = await createOffer(audioRecv: true, videoRecv: true);
}
await this.send(data: payload, jsep: offer);
}
/// Accepts an incoming call, defaulting to an automatically generated answer.
Future<void> acceptCall({RTCSessionDescription? answer}) async {
var payload = {"request": "accept"};
if (answer == null) {
answer = await createAnswer();
}
await this.send(data: payload, jsep: answer);
}
/// Terminates the current call and notifies the remote peer.
Future<void> hangup({bool disposeStream = true}) async {
await super.hangup(disposeStream: disposeStream);
await this.send(data: {"request": "hangup"});
await dispose();
}
bool _onCreated = false;
/// Maps raw event payloads to strongly typed video-call events.
@override
void onCreate() {
if (_onCreated) {
return;
}
_onCreated = true;
messages?.listen((event) {
TypedEvent<JanusEvent> typedEvent = TypedEvent<JanusEvent>(event: JanusEvent.fromJson(event.event), jsep: event.jsep);
var data = typedEvent.event.plugindata?.data;
if (data == null) return;
if (data['videocall'] == 'event' && data['result'] != null && data['result']['event'] == 'registered') {
typedEvent.event.plugindata?.data = VideoCallRegisteredEvent.fromJson(data);
_typedMessagesSink?.add(typedEvent);
} else if (data['videocall'] == 'event' && data['result'] != null && data['result']['event'] == 'calling') {
typedEvent.event.plugindata?.data = VideoCallCallingEvent.fromJson(data);
_typedMessagesSink?.add(typedEvent);
} else if (data['videocall'] == 'event' && data['result'] != null && data['result']['event'] == 'update') {
typedEvent.event.plugindata?.data = VideoCallUpdateEvent.fromJson(data);
_typedMessagesSink?.add(typedEvent);
} else if (data['videocall'] == 'event' && data['result'] != null && data['result']['event'] == 'incomingcall') {
typedEvent.event.plugindata?.data = VideoCallIncomingCallEvent.fromJson(data);
_typedMessagesSink?.add(typedEvent);
} else if (data['videocall'] == 'event' && data['result'] != null && data['result']['event'] == 'accepted') {
typedEvent.event.plugindata?.data = VideoCallAcceptedEvent.fromJson(data);
_typedMessagesSink?.add(typedEvent);
} else if (data['videocall'] == 'event' && data['result'] != null && data['result']['event'] == 'hangup') {
typedEvent.event.plugindata?.data = VideoCallHangupEvent.fromJson(data);
_typedMessagesSink?.add(typedEvent);
} else if (data['videocall'] == 'event' && data['result'] != null && data['result'].containsKey('list')) {
typedEvent.event.plugindata?.data = VideoCallRegisteredListEvent.fromJson(data);
_typedMessagesSink?.add(typedEvent);
} else if (data['videocall'] == 'event' && (data['error_code'] != null || data['result']?['code'] != null)) {
_typedMessagesSink?.addError(JanusError.fromMap(data));
}
});
}
}