Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix data channel not work issue #210

Merged
merged 1 commit into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/src/call_sample/call_sample.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class _CallSampleState extends State<CallSample> {

_accept() {
if (_session != null) {
_signaling?.accept(_session!.sid);
_signaling?.accept(_session!.sid, 'video');
}
}

Expand Down
123 changes: 103 additions & 20 deletions lib/src/call_sample/data_channel_sample.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class _DataChannelSampleState extends State<DataChannelSample> {
var _text = '';
// ignore: unused_element
_DataChannelSampleState();
bool _waitAccept = false;

@override
initState() {
Expand All @@ -39,6 +40,55 @@ class _DataChannelSampleState extends State<DataChannelSample> {
_timer?.cancel();
}

Future<bool?> _showAcceptDialog() {
return showDialog<bool?>(
context: context,
builder: (context) {
return AlertDialog(
title: Text("title"),
content: Text("accept?"),
actions: <Widget>[
MaterialButton(
child: Text(
'Reject',
style: TextStyle(color: Colors.red),
),
onPressed: () => Navigator.of(context).pop(false),
),
MaterialButton(
child: Text(
'Accept',
style: TextStyle(color: Colors.green),
),
onPressed: () => Navigator.of(context).pop(true),
),
],
);
},
);
}

Future<bool?> _showInvateDialog() {
return showDialog<bool?>(
context: context,
builder: (context) {
return AlertDialog(
title: Text("title"),
content: Text("waiting"),
actions: <Widget>[
TextButton(
child: Text("cancel"),
onPressed: () {
Navigator.of(context).pop(false);
_hangUp();
},
),
],
);
},
);
}

void _connect(BuildContext context) async {
_signaling ??= Signaling(widget.host, context)..connect();

Expand All @@ -65,33 +115,54 @@ class _DataChannelSampleState extends State<DataChannelSample> {
}
};

_signaling?.onCallStateChange = (Session session, CallState state) {
_signaling?.onCallStateChange = (Session session, CallState state) async {
switch (state) {
case CallState.CallStateNew:
{
setState(() {
_session = session;
_inCalling = true;
});
_timer =
Timer.periodic(Duration(seconds: 1), _handleDataChannelTest);
break;
}
setState(() {
_session = session;
});
_timer = Timer.periodic(Duration(seconds: 1), _handleDataChannelTest);
break;
case CallState.CallStateBye:
{
setState(() {
_inCalling = false;
});
_timer?.cancel();
_dataChannel = null;
_inCalling = false;
_session = null;
_text = '';
break;
if (_waitAccept) {
print('peer reject');
_waitAccept = false;
Navigator.of(context).pop(false);
}
setState(() {
_inCalling = false;
});
_timer?.cancel();
_dataChannel = null;
_inCalling = false;
_session = null;
_text = '';
break;
case CallState.CallStateInvite:
_waitAccept = true;
_showInvateDialog();
break;
case CallState.CallStateConnected:
if (_waitAccept) {
_waitAccept = false;
Navigator.of(context).pop(false);
}
setState(() {
_inCalling = true;
});
break;
case CallState.CallStateRinging:
bool? accept = await _showAcceptDialog();
if (accept!) {
_accept();
setState(() {
_inCalling = true;
});
} else {
_reject();
}

break;
}
};

Expand All @@ -117,6 +188,18 @@ class _DataChannelSampleState extends State<DataChannelSample> {
}
}

_accept() {
if (_session != null) {
_signaling?.accept(_session!.sid, 'data');
}
}

_reject() {
if (_session != null) {
_signaling?.reject(_session!.sid);
}
}

_hangUp() {
_signaling?.bye(_session!.sid);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/call_sample/signaling.dart
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,12 @@ class Signaling {
}
}

void accept(String sessionId) {
void accept(String sessionId, String media) {
var session = _sessions[sessionId];
if (session == null) {
return;
}
_createAnswer(session, 'video');
_createAnswer(session, media);
}

void reject(String sessionId) {
Expand Down