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

Sip session timeout fix etc. #10

Merged
merged 10 commits into from
Sep 26, 2019
42 changes: 28 additions & 14 deletions example/lib/src/callscreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_webrtc/webrtc.dart';
import 'sip_ua_helper.dart';
import 'package:sip_ua/src/RTCSession.dart';

class CallScreenWidget extends StatefulWidget {
SIPUAHelper _helper;
Expand All @@ -28,11 +29,11 @@ class _MyCallScreenWidget extends State<CallScreenWidget> {

bool _muted = false;
bool _hold = false;
var _state = 'new';
String _state = 'new';

get session => helper.session;
RTCSession get session => helper.session;

get helper => widget._helper;
SIPUAHelper get helper => widget._helper;

get voiceonly =>
(_localStream == null || _localStream.getVideoTracks().length == 0) &&
Expand Down Expand Up @@ -60,17 +61,25 @@ class _MyCallScreenWidget extends State<CallScreenWidget> {
_timer = Timer.periodic(Duration(seconds: 1), (Timer timer) {
//print('tick => ${timer.tick}');
Duration duration = Duration(seconds: timer.tick);
this.setState(() {
_timeLabel = [duration.inMinutes, duration.inSeconds]
.map((seg) => seg.remainder(60).toString().padLeft(2, '0'))
.join(':');
});
if (mounted) {
this.setState(() {
_timeLabel = [duration.inMinutes, duration.inSeconds]
.map((seg) => seg.remainder(60).toString().padLeft(2, '0'))
.join(':');
});
} else {
_timer.cancel();
}
});
}

_initRenderers() async {
await _localRenderer.initialize();
await _remoteRenderer.initialize();
if (_localRenderer != null) {
await _localRenderer.initialize();
}
if (_remoteRenderer != null) {
await _remoteRenderer.initialize();
}
}

_disposeRenderers() {
Expand All @@ -89,7 +98,7 @@ class _MyCallScreenWidget extends State<CallScreenWidget> {
}

_handleCalllState(state, data) {
if (state != 'stream') {
if (state != 'stream') {
_state = state;
}

Expand All @@ -106,7 +115,7 @@ class _MyCallScreenWidget extends State<CallScreenWidget> {
_backToDialPad();
break;
}
this.setState(() {});
this.setState(() {});
}

_removeEventListeners() {
Expand All @@ -123,11 +132,15 @@ class _MyCallScreenWidget extends State<CallScreenWidget> {
_handelStreams(event) async {
var stream = event['stream'];
if (event['originator'] == 'local') {
_localRenderer.srcObject = stream;
if (_localRenderer != null) {
_localRenderer.srcObject = stream;
}
_localStream = stream;
}
if (event['originator'] == 'remote') {
_remoteRenderer.srcObject = stream;
if (_remoteRenderer != null) {
_remoteRenderer.srcObject = stream;
}
_remoteStream = stream;
}

Expand All @@ -150,6 +163,7 @@ class _MyCallScreenWidget extends State<CallScreenWidget> {

_handleHangup() {
helper.hangup();
_timer.cancel();
}

_handleAccept() {
Expand Down
2 changes: 1 addition & 1 deletion example/lib/src/dialpad.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class DialPadWidget extends StatefulWidget {

class _MyDialPadWidget extends State<DialPadWidget> {
var _dest = 'sip:111_6ackea@tryit.jssip.net';
get helper => widget._helper;
SIPUAHelper get helper => widget._helper;
TextEditingController _textController;

@override
Expand Down
13 changes: 7 additions & 6 deletions example/lib/src/sip_ua_helper.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import 'dart:async';
import 'package:sip_ua/sip_ua.dart';
import 'package:events2/events2.dart';
import 'package:sip_ua/src/RTCSession.dart';

class SIPUAHelper extends EventEmitter {
UA _ua;
Settings _settings;
final logger = new Logger('SIPUA::Helper');
var _session;
var _registered = false;
var _connected = false;
RTCSession _session;
bool _registered = false;
bool _connected = false;
var _registerState = 'new';
var _localStream;
var _remoteStream;
Expand All @@ -19,11 +20,11 @@ class SIPUAHelper extends EventEmitter {

debugerror(error) => logger.error(error);

get session => _session;
RTCSession get session => _session;

get registered => _registered;
bool get registered => _registered;

get connected => _connected;
bool get connected => _connected;

get registerState => _registerState;

Expand Down
37 changes: 19 additions & 18 deletions lib/src/RTCSession.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,20 @@ class C {
const holdMediaTypes = ['audio', 'video'];

class SIPTimers {
var ackTimer;
var expiresTimer;
var invite2xxTimer;
var userNoAnswerTimer;
Timer ackTimer;
Timer expiresTimer;
Timer invite2xxTimer;
Timer userNoAnswerTimer;
}

class RFC4028Timers {
var enabled;
var refreshMethod;
var defaultExpires;
var currentExpires;
bool enabled;
SipMethod refreshMethod;
int defaultExpires;
int currentExpires;
bool running;
bool refresher;
var timer;
Timer timer;
RFC4028Timers(this.enabled, this.refreshMethod, this.defaultExpires,
this.currentExpires, this.running, this.refresher, this.timer);
}
Expand All @@ -70,13 +70,13 @@ class RTCSession extends EventEmitter {
MediaStream _localMediaStream;
var _data;
Map<String,Dialog> _earlyDialogs;
var _from_tag;
String _from_tag;
var _to_tag;
var _rtcAnswerConstraints;
var _timers;
SIPTimers _timers;
bool _is_confirmed;
bool _is_canceled;
var _sessionTimers;
RFC4028Timers _sessionTimers;
var _cancel_reason;
var _status;
Dialog _dialog;
Expand All @@ -98,7 +98,7 @@ class RTCSession extends EventEmitter {
var _local_identity;
var _remote_identity;

var _contact;
String _contact;
var _tones;
var _sendDTMF;
final logger = new Logger('RTCSession');
Expand Down Expand Up @@ -319,8 +319,8 @@ class RTCSession extends EventEmitter {
this._from_tag = Utils.newTag();

// Set anonymous property.
var anonymous = options['anonymous'] ?? false;
var requestParams = {'from_tag': this._from_tag};
bool anonymous = options['anonymous'] ?? false;
Map<String,dynamic> requestParams = {'from_tag': this._from_tag};
this._ua.contact.anonymous = anonymous;
this._ua.contact.outbound = true;
this._contact = this._ua.contact.toString();
Expand Down Expand Up @@ -2439,18 +2439,19 @@ class RTCSession extends EventEmitter {
}
}

onSucceeded(response) async {
onSucceeded(IncomingResponse response) async {
if (this._status == C.STATUS_TERMINATED) {
return;
}

// Handle Session Timers.
this._handleSessionTimersInIncomingResponse(response);

// If it is a 2XX retransmission exit now.
if (succeeded != null) {
return;
}

// Handle Session Timers.
this._handleSessionTimersInIncomingResponse(response);

// Must have SDP answer.
if (sdpOffer != null) {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/Timers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Timers {
60000; // See RFC 3261 Section 13.3.1.1
}

setTimeout(fn, duration) {
Timer setTimeout(fn, duration) {
return new Timer(new Duration(milliseconds: duration), fn);
}

Expand All @@ -26,7 +26,7 @@ clearTimeout(Timer timer) {
timer.cancel();
}

setInterval(fn, interval) {
Timer setInterval(fn, interval) {
return new Timer.periodic(new Duration(milliseconds: interval), (Timer timer) {
fn();
});
Expand Down
4 changes: 2 additions & 2 deletions lib/src/UA.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class UA extends EventEmitter {
var _applicants;
Map<String,RTCSession> _sessions = {};
Transport _transport;
var _contact;
Contact _contact;
var _status;
var _error;
var _transactions;
Expand Down Expand Up @@ -137,7 +137,7 @@ class UA extends EventEmitter {

get status => this._status;

get contact => this._contact;
Contact get contact => this._contact;

Settings get configuration => this._configuration;

Expand Down
4 changes: 2 additions & 2 deletions lib/src/Utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ isEmpty(value) {
}

// Used by 'newTag'.
createRandomToken(size, {base = 32}) {
String createRandomToken(size, {base = 32}) {
return randomAlphaNumeric(size).toLowerCase();
}

newTag() => createRandomToken(10);
String newTag() => createRandomToken(10);

newUUID() => new Uuid().v4();

Expand Down
17 changes: 11 additions & 6 deletions lib/src/WebSocketInterface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,13 @@ class WebSocketInterface implements Socket {
}
debug('connecting to WebSocket ${this._url}');
try {
this._ws = await WebSocket.connect(this._url, headers: {
'Sec-WebSocket-Protocol': 'sip',
...this._wsExtraHeaders
});
this._ws = await WebSocket.connect(this._url,
headers: {'Sec-WebSocket-Protocol': 'sip', ...this._wsExtraHeaders});
this._ws.listen((data) {
this._onMessage(data);
}, onDone: () {
logger.debug('Closed by server [${this._ws.closeCode}, ${this._ws.closeReason}]!');
logger.debug(
'Closed by server [${this._ws.closeCode}, ${this._ws.closeReason}]!');
_connected = false;
this._onClose(true, this._ws.closeCode, this._ws.closeReason);
});
Expand Down Expand Up @@ -152,7 +151,13 @@ class WebSocketInterface implements Socket {

_onMessage(data) {
debug('Received WebSocket message');
this.ondata(data);
if (data != null) {
if (data.toString().trim().length > 0) {
this.ondata(data);
} else {
debug("Received and ignored empty packet");
}
}
}

_onError(e) {
Expand Down