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

Remove dynamic parameters and maps #498

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion example/lib/src/callscreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ class _MyCallScreenWidget extends State<CallScreenWidget>
}

void _handleHangup() {
call!.hangup({'status_code': 603});
final TerminateOptions options = TerminateOptions()..statusCode = 603;
call!.hangup(options);
_timer.cancel();
}

Expand Down
2 changes: 1 addition & 1 deletion example/test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'package:dart_sip_ua_example/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
await tester.pumpWidget(MyApp());

// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
Expand Down
3 changes: 1 addition & 2 deletions lib/sip_ua.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ export 'src/enum_helper.dart';
export 'src/sip_message.dart';
export 'src/sip_ua_helper.dart';
export 'src/transport_type.dart';
export 'src/types.dart';
export 'src/uri.dart';


4 changes: 1 addition & 3 deletions lib/src/config.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import 'package:sip_ua/sip_ua.dart';
import 'package:sip_ua/src/transports/socket_interface.dart';
import 'package:sip_ua/src/transports/tcp_socket.dart';

import 'constants.dart' as DartSIP_C;
import 'constants.dart';
import 'exceptions.dart' as Exceptions;
import 'grammar.dart';
import 'logger.dart';
import 'transports/web_socket.dart';
import 'uri.dart';
import 'utils.dart' as Utils;

// Default settings.
Expand Down
137 changes: 66 additions & 71 deletions lib/src/rtc_session.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import 'dart:async';
import 'dart:convert';

import 'package:crypto/crypto.dart';
import 'package:flutter_webrtc/flutter_webrtc.dart';
import 'package:sdp_transform/sdp_transform.dart' as sdp_transform;
import 'package:sdp_transform/sdp_transform.dart';

import 'package:sip_ua/sip_ua.dart';

import 'constants.dart' as DartSIP_C;
import 'constants.dart';
import 'dialog.dart';
Expand All @@ -22,11 +19,9 @@ import 'rtc_session/info.dart' as RTCSession_Info;
import 'rtc_session/info.dart';
import 'rtc_session/refer_notifier.dart';
import 'rtc_session/refer_subscriber.dart';
import 'sip_message.dart';
import 'timers.dart';
import 'transactions/transaction_base.dart';
import 'ua.dart';
import 'uri.dart';
import 'utils.dart' as utils;

class C {
Expand Down Expand Up @@ -314,7 +309,7 @@ class RTCSession extends EventManager implements Owner {
requestParams['from_display_name'] = options['from_display_name'] ?? '';
requestParams['from_uri'] = URI.parse(options['from_uri']);
extraHeaders
.add('P-Preferred-Identity: ${_ua.configuration.uri.toString()}');
.add('P-Preferred-Identity: ${_ua.configuration.uri.toString()}');
}

if (anonymous) {
Expand Down Expand Up @@ -713,21 +708,21 @@ class RTCSession extends EventManager implements Owner {
/**
* Terminate the call.
*/
void terminate([Map<String, dynamic>? options]) {
void terminate([TerminateOptions? options]) {
logger.d('terminate()');

options = options ?? <String, dynamic>{};
options = options ?? TerminateOptions();

Object cause = options['cause'] ?? DartSIP_C.CausesType.BYE;
String cause = options.cause ?? DartSIP_C.CausesType.BYE;

List<dynamic> extraHeaders = options['extraHeaders'] != null
? utils.cloneArray(options['extraHeaders'])
List<dynamic> extraHeaders = options.extraHeaders != null
? utils.cloneArray(options.extraHeaders)
: <dynamic>[];
Object? body = options['body'];
String? body = options.body;

String? cancel_reason;
int? status_code = options['status_code'] as int?;
String? reason_phrase = options['reason_phrase'] as String?;
int? status_code = options.statusCode;
String? reason_phrase = options.reasonPhrase;

// Check Session Status.
if (_status == C.STATUS_TERMINATED) {
Expand Down Expand Up @@ -784,8 +779,8 @@ class RTCSession extends EventManager implements Owner {
case C.STATUS_CONFIRMED:
logger.d('terminating session');

reason_phrase = options['reason_phrase'] as String? ??
DartSIP_C.REASON_PHRASE[status_code ?? 0];
reason_phrase =
options.reasonPhrase ?? DartSIP_C.REASON_PHRASE[status_code ?? 0];

if (status_code != null && (status_code < 200 || status_code >= 700)) {
throw Exceptions.InvalidStateError(
Expand Down Expand Up @@ -1060,11 +1055,11 @@ class RTCSession extends EventManager implements Owner {
}
});
handlers.on(EventCallFailed(), (EventCallFailed event) {
terminate(<String, dynamic>{
'cause': DartSIP_C.CausesType.WEBRTC_ERROR,
'status_code': 500,
'reason_phrase': 'Hold Failed'
});
final TerminateOptions options = TerminateOptions()
..cause = DartSIP_C.CausesType.WEBRTC_ERROR
..statusCode = 500
..reasonPhrase = 'Hold Failed';
terminate(options);
});

if (options['useUpdate'] != null) {
Expand Down Expand Up @@ -1110,11 +1105,11 @@ class RTCSession extends EventManager implements Owner {
}
});
handlers.on(EventCallFailed(), (EventCallFailed event) {
terminate(<String, dynamic>{
'cause': DartSIP_C.CausesType.WEBRTC_ERROR,
'status_code': 500,
'reason_phrase': 'Unhold Failed'
});
final TerminateOptions options = TerminateOptions()
..cause = DartSIP_C.CausesType.WEBRTC_ERROR
..statusCode = 500
..reasonPhrase = 'Unhold Failed';
terminate(options);
});

if (options['useUpdate'] != null) {
Expand Down Expand Up @@ -1166,11 +1161,11 @@ class RTCSession extends EventManager implements Owner {
});

handlers.on(EventCallFailed(), (EventCallFailed event) {
terminate(<String, dynamic>{
'cause': DartSIP_C.CausesType.WEBRTC_ERROR,
'status_code': 500,
'reason_phrase': 'Media Renegotiation Failed'
});
final TerminateOptions options = TerminateOptions()
..cause = DartSIP_C.CausesType.WEBRTC_ERROR
..statusCode = 500
..reasonPhrase = 'Media Renegotiation Failed';
terminate(options);
});

_setLocalMediaStatus();
Expand Down Expand Up @@ -1288,10 +1283,10 @@ class RTCSession extends EventManager implements Owner {

if (_late_sdp) {
if (request.body == null) {
terminate(<String, dynamic>{
'cause': DartSIP_C.CausesType.MISSING_SDP,
'status_code': 400
});
final TerminateOptions options = TerminateOptions()
..cause = DartSIP_C.CausesType.MISSING_SDP
..statusCode = 400;
terminate(options);
break;
}

Expand All @@ -1304,10 +1299,10 @@ class RTCSession extends EventManager implements Owner {
try {
await _connection!.setRemoteDescription(answer);
} catch (error) {
terminate(<String, dynamic>{
'cause': DartSIP_C.CausesType.BAD_MEDIA_DESCRIPTION,
'status_code': 488
});
final TerminateOptions options = TerminateOptions()
..cause = DartSIP_C.CausesType.BAD_MEDIA_DESCRIPTION
..statusCode = 488;
terminate(options);
logger.e(
'emit "peerconnection:setremotedescriptionfailed" [error:${error.toString()}]');
emit(EventSetRemoteDescriptionFailed(exception: error));
Expand Down Expand Up @@ -1405,35 +1400,35 @@ class RTCSession extends EventManager implements Owner {
void onTransportError() {
logger.e('onTransportError()');
if (_status != C.STATUS_TERMINATED) {
terminate(<String, dynamic>{
'status_code': 500,
'reason_phrase': DartSIP_C.CausesType.CONNECTION_ERROR,
'cause': DartSIP_C.CausesType.CONNECTION_ERROR
});
final TerminateOptions options = TerminateOptions()
..statusCode = 500
..reasonPhrase = DartSIP_C.CausesType.CONNECTION_ERROR
..cause = DartSIP_C.CausesType.CONNECTION_ERROR;
terminate(options);
}
}

void onRequestTimeout() {
logger.e('onRequestTimeout()');

if (_status != C.STATUS_TERMINATED) {
terminate(<String, dynamic>{
'status_code': 408,
'reason_phrase': DartSIP_C.CausesType.REQUEST_TIMEOUT,
'cause': DartSIP_C.CausesType.REQUEST_TIMEOUT
});
final TerminateOptions options = TerminateOptions()
..statusCode = 408
..reasonPhrase = DartSIP_C.CausesType.REQUEST_TIMEOUT
..cause = DartSIP_C.CausesType.REQUEST_TIMEOUT;
terminate(options);
}
}

void onDialogError() {
logger.e('onDialogError()');

if (_status != C.STATUS_TERMINATED) {
terminate(<String, dynamic>{
'status_code': 500,
'reason_phrase': DartSIP_C.CausesType.DIALOG_ERROR,
'cause': DartSIP_C.CausesType.DIALOG_ERROR
});
final TerminateOptions options = TerminateOptions()
..statusCode = 500
..reasonPhrase = DartSIP_C.CausesType.DIALOG_ERROR
..cause = DartSIP_C.CausesType.DIALOG_ERROR;
terminate(options);
}
}

Expand Down Expand Up @@ -1598,11 +1593,11 @@ class RTCSession extends EventManager implements Owner {
logger.d('ICE Restart was successful');
});
handlers.on(EventCallFailed(), (EventCallFailed event) {
terminate(<String, dynamic>{
'cause': DartSIP_C.CausesType.WEBRTC_ERROR,
'status_code': 500,
'reason_phrase': 'Media Renegotiation Failed'
});
final TerminateOptions options = TerminateOptions()
..cause = DartSIP_C.CausesType.WEBRTC_ERROR
..statusCode = 500
..reasonPhrase = 'Media Renegotiation Failed';
terminate(options);
});
offerConstraints['eventHandlers'] = handlers;
renegotiate(options: offerConstraints);
Expand All @@ -1614,11 +1609,11 @@ class RTCSession extends EventManager implements Owner {
_connection!.onIceConnectionState = (RTCIceConnectionState state) {
// TODO(cloudwebrtc): Do more with different states.
if (state == RTCIceConnectionState.RTCIceConnectionStateFailed) {
terminate(<String, dynamic>{
'cause': DartSIP_C.CausesType.RTP_TIMEOUT,
'status_code': 408,
'reason_phrase': DartSIP_C.CausesType.RTP_TIMEOUT
});
final TerminateOptions options = TerminateOptions()
..cause = DartSIP_C.CausesType.RTP_TIMEOUT
..statusCode = 408
..reasonPhrase = DartSIP_C.CausesType.RTP_TIMEOUT;
terminate(options);
} else if (state ==
RTCIceConnectionState.RTCIceConnectionStateDisconnected) {
_iceRestart();
Expand Down Expand Up @@ -2937,7 +2932,7 @@ class RTCSession extends EventManager implements Owner {
}

/// SDP offers may contain text media channels. e.g. Older clients using linphone.
///
///
/// WebRTC does not support text media channels, so remove them.
String? _sdpOfferToWebRTC(String? sdpInput) {
if (sdpInput == null) {
Expand Down Expand Up @@ -3065,11 +3060,11 @@ class RTCSession extends EventManager implements Owner {

logger.e('runSessionTimer() | timer expired, terminating the session');

terminate(<String, dynamic>{
'cause': DartSIP_C.CausesType.REQUEST_TIMEOUT,
'status_code': 408,
'reason_phrase': 'Session Timer Expired'
});
final TerminateOptions options = TerminateOptions()
..cause = DartSIP_C.CausesType.REQUEST_TIMEOUT
..statusCode = 408
..reasonPhrase = 'Session Timer Expired';
terminate(options);
}, expires! * 1100);
}
}
Expand Down
3 changes: 1 addition & 2 deletions lib/src/rtc_session/dtmf.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import 'package:flutter_webrtc/flutter_webrtc.dart';

import 'package:sip_ua/sip_ua.dart';

import '../constants.dart';
import '../event_manager/event_manager.dart';
import '../event_manager/internal_events.dart';
import '../exceptions.dart' as Exceptions;
import '../logger.dart';
import '../rtc_session.dart' as rtc;
import '../sip_message.dart';
import '../utils.dart' as Utils;

class C {
Expand Down
10 changes: 5 additions & 5 deletions lib/src/sip_ua_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:sip_ua/src/event_manager/internal_events.dart';
import 'package:sip_ua/src/map_helper.dart';
import 'package:sip_ua/src/transports/socket_interface.dart';
import 'package:sip_ua/src/transports/tcp_socket.dart';

import 'config.dart';
import 'constants.dart' as DartSIP_C;
import 'event_manager/event_manager.dart';
Expand All @@ -16,7 +17,6 @@ import 'logger.dart';
import 'message.dart';
import 'rtc_session.dart';
import 'rtc_session/refer_subscriber.dart';
import 'sip_message.dart';
import 'stack_trace_nj.dart';
import 'subscriber.dart';
import 'transports/web_socket.dart';
Expand Down Expand Up @@ -179,9 +179,9 @@ class SIPUAHelper extends EventManager {
_settings.instance_id = uaSettings.instanceId;
_settings.registrar_server = uaSettings.registrarServer;
_settings.contact_uri = uaSettings.contact_uri;
_settings.connection_recovery_max_interval =
_settings.connection_recovery_max_interval =
uaSettings.connectionRecoveryMaxInterval;
_settings.connection_recovery_min_interval =
_settings.connection_recovery_min_interval =
uaSettings.connectionRecoveryMinInterval;
_settings.terminateOnAudioMediaPortZero =
uaSettings.terminateOnMediaPortZero;
Expand Down Expand Up @@ -423,7 +423,7 @@ class SIPUAHelper extends EventManager {
s.subscribe();
}

void terminateSessions(Map<String, dynamic> options) {
void terminateSessions(TerminateOptions? options) {
_ua!.terminateSessions(options);
}

Expand Down Expand Up @@ -550,7 +550,7 @@ class Call {
refer.on(EventReferFailed(), (EventReferFailed data) {});
}

void hangup([Map<String, dynamic>? options]) {
void hangup([TerminateOptions? options]) {
assert(_session != null, 'ERROR(hangup): rtc session is invalid!');
if (peerConnection != null) {
for (MediaStream? stream in peerConnection!.getLocalStreams()) {
Expand Down
9 changes: 9 additions & 0 deletions lib/src/types.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class TerminateOptions {
TerminateOptions();

String? cause;
List<dynamic>? extraHeaders;
String? body;
int? statusCode;
String? reasonPhrase;
}
3 changes: 2 additions & 1 deletion lib/src/ua.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';

import 'package:sip_ua/src/transport_type.dart';
import 'package:sip_ua/src/transports/socket_interface.dart';
import 'package:sip_ua/src/types.dart';
import 'config.dart' as config;
import 'config.dart';
import 'constants.dart' as DartSIP_C;
Expand Down Expand Up @@ -294,7 +295,7 @@ class UA extends EventManager {
/**
* Terminate ongoing sessions.
*/
void terminateSessions(Map<String, dynamic> options) {
void terminateSessions(TerminateOptions? options) {
logger.d('terminateSessions()');
_sessions.forEach((String? key, _) {
if (!_sessions[key]!.isEnded()) {
Expand Down