Skip to content

Commit

Permalink
update to flutter_ion 0.5.3 (#37)
Browse files Browse the repository at this point in the history
* update to flutter_ion 0.5.3

* IonSDKSFU
  • Loading branch information
Johannes0Horn committed Aug 18, 2021
1 parent 9ebe69b commit 22a3cbe
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 68 deletions.
43 changes: 28 additions & 15 deletions lib/controllers/ion_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ import 'package:shared_preferences/shared_preferences.dart';

class IonController extends GetxController {
SharedPreferences? _prefs;
IonConnector? _ion;
late String _sid;
late String _name;
final String _uid = Uuid().v4();
IonConnector? get ion => _ion;
IonBaseConnector? _baseConnector;
IonAppBiz? _biz;
IonSDKSFU? _sfu;

String get sid => _sid;

String get uid => _uid;

String get name => _name;
Client? get sfu => _ion?.sfu;

IonAppBiz? get biz => _biz;

IonSDKSFU? get sfu => _sfu;

@override
void onInit() async {
Expand All @@ -28,22 +35,28 @@ class IonController extends GetxController {
return _prefs!;
}

connect(host) async {
if (_ion == null) {
var url = 'http://$host:5551';
_ion = new IonConnector(url: url);
}
setup(host) {
_baseConnector = new IonBaseConnector(host);
_biz = new IonAppBiz(_baseConnector!);
_sfu = new IonSDKSFU(_baseConnector!);
}

connect() async {
await _biz!.connect();
await _sfu!.connect();
}

joinBIZ(String roomID, String displayName) async {
_biz!.join(sid: roomID, uid: _uid, info: {'name': '$displayName'});
}

join(String sid, String displayName) async {
_sid = sid;
_name = displayName;
_ion?.join(sid: _sid, uid: _uid, info: {'name': '$displayName'});
joinSFU(String roomID, String displayName) async {
_sfu!.join(roomID, displayName);
}

close() async {
_ion?.leave(_uid);
_ion?.close();
_ion = null;
_biz?.leave(_uid);
_biz?.close();
_biz = null;
}
}
4 changes: 2 additions & 2 deletions lib/pages/chat/chat_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ChatController extends GetxController {
);
_messages.value.insert(0, message);
}
_helper.ion?.onMessage = _messageProcess;
_helper.biz?.onMessage = _messageProcess;
}

void _messageProcess(Message msg) async {
Expand Down Expand Up @@ -83,7 +83,7 @@ class ChatController extends GetxController {
'text': text,
};

_helper.ion?.message(_helper.uid, _helper.sid, info);
_helper.biz?.message(_helper.uid, _helper.sid, info);

var msg = ChatMessage(
_helper.uid,
Expand Down
8 changes: 4 additions & 4 deletions lib/pages/login/login_page.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import 'package:flutter/material.dart';
import 'package:ion/pages/meeting/meeting_page.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:ion/controllers/ion_controller.dart';
import 'package:ion/utils/utils.dart';
import 'package:get/get.dart';

class LoginBinding implements Bindings {
@override
void dependencies() {
Get.lazyPut<LoginController>(() => LoginController());
Get.lazyPut<IonController>(() => IonController());
Get.lazyPut<MeetingController>(() => MeetingController());
}
}

class LoginController extends GetxController {
final _helper = Get.find<IonController>();
final _meetingController = Get.find<MeetingController>();
late SharedPreferences prefs;
late var _server = ''.obs;
late var _sid = ''.obs;
Expand All @@ -33,7 +33,7 @@ class LoginController extends GetxController {
}
prefs.setString('server', _server.value);
prefs.setString('room', _sid.value);
_helper.connect(_server);
_meetingController.connect();
Get.toNamed('/meeting');
return true;
}
Expand Down
115 changes: 71 additions & 44 deletions lib/pages/meeting/meeting_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_webrtc/flutter_webrtc.dart';
import 'package:flutter_ion/flutter_ion.dart';
import 'package:community_material_icon/community_material_icon.dart';
Expand All @@ -10,7 +11,6 @@ class MeetingBinding implements Bindings {
@override
void dependencies() {
Get.lazyPut<MeetingController>(() => MeetingController());
Get.lazyPut<IonController>(() => IonController());
}
}

Expand All @@ -21,6 +21,7 @@ class VideoRendererAdapter {
MediaStream stream;
RTCVideoViewObjectFit _objectFit =
RTCVideoViewObjectFit.RTCVideoViewObjectFitCover;

VideoRendererAdapter._internal(this.mid, this.stream, this.local);

static Future<VideoRendererAdapter> create(
Expand Down Expand Up @@ -65,56 +66,87 @@ class VideoRendererAdapter {
}

class MeetingController extends GetxController {
final _helper = Get.find<IonController>();
final _ionController = Get.find<IonController>();
late SharedPreferences prefs;
final videoRenderers = Rx<List<VideoRendererAdapter>>([]);
LocalStream? _localStream;
IonConnector? get ion => _helper.ion;

IonAppBiz? get biz => _ionController.biz;

IonSDKSFU? get sfu => _ionController.sfu;

var _cameraOff = false.obs;
var _microphoneOff = false.obs;
var _speakerOn = true.obs;
var _scaffoldkey = GlobalKey<ScaffoldState>();
GlobalKey<ScaffoldState>? _scaffoldkey;
var name = ''.obs;
var room = ''.obs;

@override
@mustCallSuper
void onInit() async {
super.onInit();
prefs = await _helper.prefs();

if (ion == null) {
print(":::IonHelper is not initialized!:::");
if (biz == null || sfu == null) {
print(":::BIZ or SFU is not initialized!:::");
print("Goback to /login");
Get.offNamed('/login');
SchedulerBinding.instance!.addPostFrameCallback((_) {
Get.offNamed('/login');
_cleanUp();
});
return;
}
}

ion?.onJoin = (bool success, String reason) async {
this._showSnackBar(":::Join success:::");
connect() async {
_scaffoldkey = GlobalKey();

prefs = await _ionController.prefs();

//if this client is hosted as a website, using https, the ion-backend has to be
//reached via wss. So the address should be for example:
//https://your-backend-address.com
var host = prefs.getString('server') ?? '127.0.0.1';
host = 'http://' + host + ':5551';

//init sfu and biz clients
_ionController.setup(host);

sfu!.ontrack = (MediaStreamTrack track, RemoteStream stream) async {
if (track.kind == 'video') {
_addAdapter(
await VideoRendererAdapter.create(stream.id, stream.stream, false));
}
};

biz?.onJoin = (bool success, String reason) async {
if (success) {
try {
var resolution = prefs.getString('resolution') ?? 'hd';
var codec = prefs.getString('codec') ?? 'vp8';
//join SFU
await sfu!.join(room.value, name.value);

var resolution = prefs.getString('resolution') ?? 'hd';
var codec = prefs.getString('codec') ?? 'vp8';
_localStream = await LocalStream.getUserMedia(
constraints: Constraints.defaults
..simulcast = false
..resolution = resolution
..codec = codec);
ion?.sfu!.publish(_localStream!);
..simulcast = false
..resolution = resolution
..codec = codec);
sfu!.publish(_localStream!);
_addAdapter(await VideoRendererAdapter.create(
_localStream!.stream.id, _localStream!.stream, true));
} catch (error) {
print('publish err ${error.toString()}');
}
}
this._showSnackBar(":::Join success:::");
};

ion?.onLeave = (String reason) {
biz?.onLeave = (String reason) {
this._showSnackBar(":::Leave success:::");
};

ion?.onPeerEvent = (PeerEvent event) {
biz?.onPeerEvent = (PeerEvent event) {
var name = event.peer.info['name'];
var state = '';
switch (event.state) {
Expand All @@ -133,7 +165,7 @@ class MeetingController extends GetxController {
this._showSnackBar(":::Peer [${event.peer.uid}:$name] $state:::");
};

ion?.onStreamEvent = (StreamEvent event) async {
biz?.onStreamEvent = (StreamEvent event) async {
switch (event.state) {
case StreamState.NONE:
break;
Expand All @@ -153,16 +185,13 @@ class MeetingController extends GetxController {
}
};

ion?.onTrack = (MediaStreamTrack track, RemoteStream stream) async {
if (track.kind == 'video') {
_addAdapter(
await VideoRendererAdapter.create(stream.id, stream.stream, false));
}
};
//connect to BIZ and SFU
await _ionController.connect();

//join BIZ
name.value = prefs.getString('display_name') ?? 'Guest';
room.value = prefs.getString('room') ?? 'room1';
_helper.join(room.value, name.value);
_ionController.joinBIZ(room.value, name.value);
}

_removeAdapter(String mid) {
Expand Down Expand Up @@ -252,23 +281,18 @@ class MeetingController extends GetxController {
}

_cleanUp() async {
var ion = _helper.ion;

if (_localVideo != null) {
await _localStream!.unpublish();
}

videoRenderers.value.forEach((item) async {
var stream = item.stream;
try {
ion?.sfu!.close();
sfu!.close();
await stream.dispose();
} catch (error) {}
});
videoRenderers.value.clear();
await _helper.close();

Get.back();
await _ionController.close();
}

_showSnackBar(String message) {
Expand Down Expand Up @@ -314,7 +338,7 @@ class MeetingController extends GetxController {
style: TextStyle(color: Colors.red),
),
onPressed: () {
Get.back();
Get.toNamed("/login");
_cleanUp();
},
)
Expand All @@ -324,13 +348,14 @@ class MeetingController extends GetxController {

class BoxSize {
BoxSize({required this.width, required this.height});

double width;
double height;
}

class MeetingView extends GetView<MeetingController> {
IonConnector? get ion => controller.ion;
List<VideoRendererAdapter> get remoteVideos => controller._remoteVideos;

VideoRendererAdapter? get localVideo => controller._localVideo;

final double localWidth = 114.0;
Expand All @@ -356,7 +381,8 @@ class MeetingView extends GetView<MeetingController> {
onDoubleTap: () {
adapter.switchObjFit();
},
child: RTCVideoView(adapter.renderer!, objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitContain));
child: RTCVideoView(adapter.renderer!,
objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitContain));
});
}

Expand Down Expand Up @@ -400,7 +426,8 @@ class MeetingView extends GetView<MeetingController> {
onDoubleTap: () {
localVideo?.switchObjFit();
},
child: RTCVideoView(localVideo!.renderer!, objectFit: localVideo!.objFit)),
child: RTCVideoView(localVideo!.renderer!,
objectFit: localVideo!.objFit)),
));
});
}
Expand Down Expand Up @@ -635,12 +662,12 @@ class MeetingView extends GetView<MeetingController> {
margin: EdgeInsets.all(0.0),
child: Center(
child: Obx(() => Text(
'ION Conference [${controller.room.value}]',
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
),
)),
'ION Conference [${controller.room.value}]',
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
),
)),
),
),
Row(
Expand All @@ -663,7 +690,7 @@ class MeetingView extends GetView<MeetingController> {
color: Colors.white,
),
onPressed: () {
Get.toNamed('/chat');
Get.back();
},
),
],
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/app_routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ part of 'app_pages.dart';

abstract class Routes {
static const Home = 'Login';
static const NotFound = 'not-found';
static const NotFound = '/not-found';
}

extension RoutesExtension on String {
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
flutter_ion: ^0.4.4
flutter_ion: ^0.5.3
shared_preferences: ^2.0.5
date_format: ^2.0.2
quiver: ^3.0.0
community_material_icon: ^5.9.55
get: 4.1.1
get: ^4.3.8
get_storage: ^2.0.2
responsive_builder: ^0.4.1

Expand Down

0 comments on commit 22a3cbe

Please sign in to comment.