1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-24 22:09:21 +00:00
comunicmobile/lib/helpers/calls_helper.dart

80 lines
2.5 KiB
Dart
Raw Permalink Normal View History

2020-04-20 15:29:36 +00:00
import 'dart:convert';
2020-04-20 11:24:40 +00:00
import 'package:comunic/helpers/websocket_helper.dart';
2020-04-20 12:02:32 +00:00
import 'package:comunic/lists/call_members_list.dart';
2020-04-20 11:43:17 +00:00
import 'package:comunic/models/call_config.dart';
2020-04-20 12:02:32 +00:00
import 'package:comunic/models/call_member.dart';
2021-02-07 16:09:08 +00:00
import 'package:flutter_webrtc/flutter_webrtc.dart';
2020-04-20 11:24:40 +00:00
/// Calls helper
///
/// @author Pierre Hubert
class CallsHelper {
/// Join a call
static Future<void> join(int convID) async =>
2020-04-20 11:43:17 +00:00
await ws("calls/join", {"convID": convID});
2020-04-20 11:24:40 +00:00
/// Leave a call
static Future<void> leave(int convID) async =>
2020-04-20 11:43:17 +00:00
await ws("calls/leave", {"convID": convID});
/// Get calls configuration
static Future<CallConfig> getConfig() async {
final response = await ws("calls/config", {});
return CallConfig(
iceServers: response["iceServers"].cast<String>(),
);
}
2020-04-20 12:02:32 +00:00
/// Get current call members
static Future<CallMembersList> getMembers(int callID) async =>
CallMembersList()
..addAll((await ws("calls/members", {"callID": callID}))
.map((f) => CallMember(
2020-04-20 13:02:49 +00:00
userID: f["userID"],
2020-04-20 12:02:32 +00:00
status: f["ready"] ? MemberStatus.READY : MemberStatus.JOINED,
))
.toList()
.cast<CallMember>());
2020-04-20 13:50:01 +00:00
/// Request an offer to access another peer's stream
static Future<void> requestOffer(int callID, int? peerID) async =>
2020-04-20 13:50:01 +00:00
await ws("calls/request_offer", {"callID": callID, "peerID": peerID});
2020-04-20 15:29:36 +00:00
/// Send a Session Description message to the server
static Future<void> sendSessionDescription(
int callID, int? peerID, RTCSessionDescription sdp) async =>
2020-04-20 15:29:36 +00:00
await ws("calls/signal", {
"callID": callID,
"peerID": peerID,
"type": "SDP",
"data": jsonEncode(sdp.toMap())
});
2020-04-20 15:53:31 +00:00
/// Send an IceCandidate
static Future<void> sendIceCandidate(
int callID, int? peerID, RTCIceCandidate candidate) async =>
2020-04-20 15:53:31 +00:00
await ws("calls/signal", {
"callID": callID,
"peerID": peerID,
"type": "CANDIDATE",
2020-04-20 16:13:28 +00:00
"data": jsonEncode(candidate.toMap())
2020-04-20 15:53:31 +00:00
});
2020-04-22 16:29:00 +00:00
/// Mark ourselves as ready to stream to other peers
static Future<void> markPeerReady(int callID) async =>
await ws("calls/mark_ready", {"callID": callID});
2020-04-22 16:35:19 +00:00
/// Notify other peers that we stopped streaming
///
/// This method never throw
static Future<void> notifyStoppedStreaming(int callID) async {
try {
await ws("calls/stop_streaming", {"callID": callID});
} catch (e, stack) {
print("$e\n$stack");
}
}
2020-04-20 11:24:40 +00:00
}