Skip to content

Commit

Permalink
Merge pull request #43 from pion/re-design-core-protocol
Browse files Browse the repository at this point in the history
Re design core protocol
  • Loading branch information
adwpc authored Nov 8, 2021
2 parents cb73697 + fbd6408 commit 8a96225
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 72 deletions.
49 changes: 32 additions & 17 deletions lib/controllers/ion_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ class IonController extends GetxController {
late String _sid;
late String _name;
final String _uid = Uuid().v4();
IonBaseConnector? _baseConnector;
IonAppBiz? _biz;
IonSDKSFU? _sfu;
Connector? _connector;
Room? _room;
RTC? _rtc;

String get sid => _sid;

String get uid => _uid;

String get name => _name;

IonAppBiz? get biz => _biz;
Room? get room => _room;

IonSDKSFU? get sfu => _sfu;
RTC? get rtc => _rtc;

@override
void onInit() async {
Expand All @@ -39,29 +39,44 @@ class IonController extends GetxController {
{required String host,
required String room,
required String name}) async {
_baseConnector = new IonBaseConnector(host);
_biz = new IonAppBiz(_baseConnector!);
_sfu = new IonSDKSFU(_baseConnector!);
print('IonController setup');
_connector = new Connector(host);
_room = new Room(_connector!);
_rtc = new RTC(_connector!);
_sid = room;
_name = name;
print('IonController setup ok');
}

connect() async {
await _biz!.connect();
await _sfu!.connect();
await _room!.connect();
await _rtc!.connect();
print('IonController connect()');
}

joinBIZ() async {
_biz!.join(sid: _sid, uid: _uid, info: {'name': '$_name'});
joinROOM() async {
_room!.join(
peer: Peer()
..sid = _sid
..uid = _uid
..displayname = _uid
..extrainfo = []
..destination = ''
..role = Role.HOST
..direction = Direction.BILATERAL
..protocol = Protocol.WEBRTC
..avatar = ''
..vendor = '');
print('joinROOM ' + 'sid=' + sid + ' uid=' + uid);
}

joinSFU() async {
_sfu!.join(_sid, _uid);
joinRTC() async {
_rtc!.join(_sid, _uid, JoinConfig());
}

close() async {
_biz?.leave(_uid);
_biz?.close();
_biz = null;
_room?.leave(_uid);
_room?.close();
_room = null;
}
}
4 changes: 2 additions & 2 deletions lib/pages/chat/chat_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ChatMessage extends StatelessWidget {
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(_name, style: Theme.of(context).textTheme.subhead),
Text(_name, style: Theme.of(context).textTheme.subtitle1),
Container(
margin: const EdgeInsets.only(top: 5.0),
child: Text(_text),
Expand Down Expand Up @@ -55,7 +55,7 @@ class ChatMessage extends StatelessWidget {
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(_createAt, style: Theme.of(context).textTheme.subhead),
Text(_createAt, style: Theme.of(context).textTheme.subtitle1),
Container(
margin: const EdgeInsets.only(top: 5.0),
child: Text(_text),
Expand Down
18 changes: 11 additions & 7 deletions lib/pages/chat/chat_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@ class ChatController extends GetxController {
);
_messages.value.insert(0, message);
}
_helper.biz?.onMessage = _messageProcess;
_helper.room?.onMessage = _messageProcess;
}

void _messageProcess(Message msg) async {
if (msg.from == _helper.uid) {
print('Skip self message');
return;
}
var info = msg.data;
var sender = info['name'];
var text = info['text'];
var uid = info['uid'] as String;
// var info = msg.payload;
var sender = msg.from;
// var text = info['text'];
var uid = msg.from;
//print('message: sender = ' + sender + ', text = ' + text);
ChatMessage message = ChatMessage(
uid,
text,
"TODO", //TODO come from text var
sender,
formatDate(DateTime.now(), [HH, ':', nn, ':', ss]),
uid == _helper.uid,
Expand Down Expand Up @@ -83,7 +83,11 @@ class ChatController extends GetxController {
'text': text,
};

_helper.biz?.message(_helper.uid, _helper.sid, info);
_helper.room?.message(Message()
..from = _helper.uid
..to = _helper.uid
..type = 'Map'
..payload = []); //TODO

var msg = ChatMessage(
_helper.uid,
Expand Down
92 changes: 47 additions & 45 deletions lib/pages/meeting/meeting_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,24 @@ class MeetingController extends GetxController {
final videoRenderers = Rx<List<VideoRendererAdapter>>([]);
LocalStream? _localStream;

IonAppBiz? get biz => _ionController.biz;
Room? get room => _ionController.room;

IonSDKSFU? get sfu => _ionController.sfu;
RTC? get rtc => _ionController.rtc;

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

@override
@mustCallSuper
void onInit() async {
super.onInit();

if (biz == null || sfu == null) {
print(":::BIZ or SFU is not initialized!:::");
if (room == null || rtc == null) {
print(":::ROOM or SFU is not initialized!:::");
print("Goback to /login");
SchedulerBinding.instance!.addPostFrameCallback((_) {
Get.offNamed('/login');
Expand All @@ -108,49 +108,47 @@ class MeetingController extends GetxController {
//https://your-backend-address.com
var host = prefs.getString('server') ?? '127.0.0.1';
host = 'http://' + host + ':5551';
//join BIZ
//join room
name.value = prefs.getString('display_name') ?? 'Guest';
room.value = prefs.getString('room') ?? 'room1';
rid.value = prefs.getString('room') ?? 'room1';

//init sfu and biz clients
_ionController.setup(host: host, name: name.value, room: room.value);
_ionController.setup(host: host, name: name.value, room: rid.value);

sfu!.ontrack = (MediaStreamTrack track, RemoteStream stream) async {
rtc!.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 {
//join SFU
await _ionController.joinSFU();

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);
sfu!.publish(_localStream!);
_addAdapter(await VideoRendererAdapter.create(
_localStream!.stream.id, _localStream!.stream, true));
} catch (error) {
print('publish err ${error.toString()}');
}
room?.onJoin = (JoinResult) async {
try {
//join SFU
await _ionController.joinRTC();

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);
rtc!.publish(_localStream!);
_addAdapter(await VideoRendererAdapter.create(
_localStream!.stream.id, _localStream!.stream, true));
} catch (error) {
print('publish err ${error.toString()}');
}
this._showSnackBar(":::Join success:::");
};

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

biz?.onPeerEvent = (PeerEvent event) {
var name = event.peer.info['name'];
room?.onPeerEvent = (PeerEvent event) {
var name = event.peer.displayname;
var state = '';
switch (event.state) {
case PeerState.NONE:
Expand All @@ -168,21 +166,25 @@ class MeetingController extends GetxController {
this._showSnackBar(":::Peer [${event.peer.uid}:$name] $state:::");
};

biz?.onStreamEvent = (StreamEvent event) async {
rtc?.ontrackevent = (TrackEvent event) async {
switch (event.state) {
case StreamState.NONE:
case TrackState.ADD:
if (event.tracks.isNotEmpty) {
var id = event.tracks[0].id;
this._showSnackBar(":::track-add [$id]:::");
}
break;
case StreamState.ADD:
if (event.streams.isNotEmpty) {
var mid = event.streams[0].id;
this._showSnackBar(":::stream-add [$mid]:::");
case TrackState.REMOVE:
if (event.tracks.isNotEmpty) {
var id = event.tracks[0].id;
this._showSnackBar(":::track-remove [$id]:::");
_removeAdapter(id);
}
break;
case StreamState.REMOVE:
if (event.streams.isNotEmpty) {
var mid = event.streams[0].id;
this._showSnackBar(":::stream-remove [$mid]:::");
_removeAdapter(mid);
case TrackState.UPDATE:
if (event.tracks.isNotEmpty) {
var id = event.tracks[0].id;
this._showSnackBar(":::track-update [$id]:::");
}
break;
}
Expand All @@ -191,7 +193,7 @@ class MeetingController extends GetxController {
//connect to BIZ and SFU
await _ionController.connect();

_ionController.joinBIZ();
_ionController.joinROOM();
}

_removeAdapter(String mid) {
Expand Down Expand Up @@ -287,7 +289,7 @@ class MeetingController extends GetxController {
videoRenderers.value.forEach((item) async {
var stream = item.stream;
try {
sfu!.close();
rtc!.close();
await stream.dispose();
} catch (error) {}
});
Expand Down Expand Up @@ -662,7 +664,7 @@ class MeetingView extends GetView<MeetingController> {
margin: EdgeInsets.all(0.0),
child: Center(
child: Obx(() => Text(
'ION Conference [${controller.room.value}]',
'ION Conference [${controller.rid.value}]',
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
Expand Down
5 changes: 4 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.0
flutter_ion: ^0.5.4
flutter_ion: ^1.0.0
# flutter_ion:
# path: ../ion-sdk-flutter

shared_preferences: ^2.0.5
date_format: ^2.0.2
quiver: ^3.0.0
Expand Down

0 comments on commit 8a96225

Please sign in to comment.