我正在使用 flutter 开发适用于 Android 和 IOS 的 voip 应用程序。我正在使用 dart sip_ua。我能够注册到信令服务器并接收呼叫。客户端还能够连接到ice服务器。但是,呼叫在 30 秒后终止,并出现错误 rtp 超时。以下是错误日志:
I/flutter ( 6747): Record-Route: <sip:192.168.0.7:5080;transport=ws;r2=on;lr=on;nat=yes;rtp=bridge;rtp=ws>
I/flutter ( 6747): Record-Route: <sip:192.168.0.7;r2=on;lr=on;nat=yes;rtp=bridge;rtp=ws>
D/FlutterWebRTCPlugin( 6747): onAddTrack
D/FlutterWebRTCPlugin( 6747): onIceGatheringChangeGATHERING
D/FlutterWebRTCPlugin( 6747): onConnectionChangeCONNECTING
D/FlutterWebRTCPlugin( 6747): onIceCandidate
D/FlutterWebRTCPlugin( 6747): onIceGatheringChangeCOMPLETE
I/flutter ( 6747): Record-Route: <sip:192.168.0.7:5080;transport=ws;r2=on;lr=on;nat=yes;rtp=bridge;rtp=ws>
I/flutter ( 6747): Record-Route: <sip:192.168.0.7;r2=on;lr=on;nat=yes;rtp=bridge;rtp=ws>
I/flutter ( 6747): m=audio 56441 RTP/SAVPF 0 8 101
D/FlutterWebRTCPlugin( 6747): onIceCandidate
I/flutter ( 6747): m=audio 56441 RTP/SAVPF 0 8 101
D/FlutterWebRTCPlugin( 6747): onConnectionChangeFAILED
I/flutter ( 6747): [2024-01-02 00:28:28.596] Level.debug sip_ua_helper.dart:249 ::: call ended with cause: Code: [408], Cause: RTP Timeout, Reason: RTP Timeout
D/FlutterWebRTCPlugin( 6747): onConnectionChangeCLOSED
I/flutter ( 6747): Route: <sip:192.168.0.7:5080;transport=ws;r2=on;lr=on;nat=yes;rtp=bridge;rtp=ws>
I/flutter ( 6747): Route: <sip:192.168.0.7;r2=on;lr=on;nat=yes;rtp=bridge;rtp=ws>
I/flutter ( 6747): Reason: SIP ;case=408; text="RTP Timeout"
以下是sip ua代码
`import 'package:flutter/material.dart';
import 'package:flutter_callkit_incoming/entities/entities.dart';
import 'package:uuid/uuid.dart';
import 'package:flutter_callkit_incoming/flutter_callkit_incoming.dart';
import 'package:sip_ua/sip_ua.dart';
import 'package:flutter_webrtc/flutter_webrtc.dart';
void main() {
runApp(const MyApp());
var uuid = const Uuid();
var sipHelper = SIPUAHelper();
SipEvents sipevent = SipEvents();
sipevent._initRenderers();
var ua = UaSettings();
ua.authorizationUser = '2167';
ua.password = 'XXXX';
ua.webSocketUrl = 'wss://turn.konza.go.ke/ws';
ua.uri = 'sip:[email protected]';
ua.iceGatheringTimeout = 3000;
ua.iceServers = <Map<String, String>>[
<String, String>{
'url': 'turn:turn.konza.go.ke:3478',
'username': 'goipvoice',
'credential': 'XXXXX',
},
];
ua.register = true;
sipHelper.start(ua);
print("listeners here");
print(sipHelper.listeners);
sipHelper.addSipUaHelperListener(sipevent);
}
class SipEvents extends SipUaHelperListener {
final RTCVideoRenderer? _localRenderer = RTCVideoRenderer();
final RTCVideoRenderer? _remoteRenderer = RTCVideoRenderer();
double? _localVideoHeight;
double? _localVideoWidth;
final mediaConstraints = <String, dynamic>{
'audio': true,
'video': false,
};
MediaStream? _localStream;
MediaStream? _remoteStream;
late MediaStream mediaStream;
void _handleAccept() async {
mediaStream = await navigator.mediaDevices.getUserMedia(mediaConstraints);
}
void _handelStreams(CallState event) async {
MediaStream? stream = event.stream;
if (event.originator == 'local') {
if (_localRenderer != null) {
_localRenderer!.srcObject = stream;
}
if (!WebRTC.platformIsDesktop) {
event.stream?.getAudioTracks().first.enableSpeakerphone(false);
}
_localStream = stream;
}
if (event.originator == 'remote') {
if (_remoteRenderer != null) {
_remoteRenderer!.srcObject = stream;
}
_remoteStream = stream;
}
}
void _initRenderers() async {
if (_localRenderer != null) {
await _localRenderer!.initialize();
}
if (_remoteRenderer != null) {
await _remoteRenderer!.initialize();
}
}
@override
Future<void> callStateChanged(Call call, CallState state) async {
// TODO: implement callStateChanged
String stateName = state.state.name;
print("new Call $stateName");
print(state.state.name);
if (stateName == "PROGRESS") {
print("call has started");
try {
mediaStream =
await navigator.mediaDevices.getUserMedia(mediaConstraints);
call.answer(SIPUAHelper().buildCallOptions(true),
mediaStream: mediaStream);
print("gotten media");
var peerConnection = call.peerConnection;
print("peer connection $peerConnection");
} catch (e) {}
}
if (stateName == "STREAM") {
_handelStreams(state);
}
}
@override
void onNewMessage(SIPMessageRequest msg) {
// TODO: implement onNewMessage
print("new sip mesasge");
}
@override
void onNewNotify(Notify ntf) {
// TODO: implement onNewNotify
print("new notify");
}
@override
void registrationStateChanged(RegistrationState state) {
// TODO: implement registrationStateChanged
print("new registration");
}
@override
void transportStateChanged(TransportState state) {
// TODO: implement transportStateChanged
print("new transport");
}
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// TRY THIS: Try running your application with "flutter run". You'll see
// the application has a purple toolbar. Then, without quitting the app,
// try changing the seedColor in the colorScheme below to Colors.green
// and then invoke "hot reload" (save your changes or press the "hot
// reload" button in a Flutter-supported IDE, or press "r" if you used
// the command line to start the app).
//
// Notice that the counter didn't reset back to zero; the application
// state is not lost during the reload. To reset the state, use hot
// restart instead.
//
// This works for code too, not just values: Most code changes can be
// tested with just a hot reload.
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(
title: 'Flutter Demo Home Page hot reload count here'),
);
}
}
class MyHomePage extends StatefulWidget implements SipUaHelperListener {
const MyHomePage({super.key, required this.title});
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
void initState() {
SIPUAHelper().addSipUaHelperListener(this);
}
@override
State<MyHomePage> createState() => _MyHomePageState();
@override
void callStateChanged(Call call, CallState state) {
// TODO: implement callStateChanged
print("new message");
}
@override
void onNewMessage(SIPMessageRequest msg) {
// TODO: implement onNewMessage
print("new message");
}
@override
void onNewNotify(Notify ntf) {
// TODO: implement onNewNotify
print("new message");
}
@override
void registrationStateChanged(RegistrationState state) {
// TODO: implement registrationStateChanged
print("new message");
}
@override
void transportStateChanged(TransportState state) {
// TODO: implement transportStateChanged
print("new message");
}
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
var uuid = const Uuid();
var callid = uuid.v4();
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// TRY THIS: Try changing the color here to a specific color (to
// Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
// change color while the other colors stay the same.
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
//
// TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
// action in the IDE, or press "p" in the console), to see the
// wireframe for each widget.
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times here:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
ElevatedButton(
onPressed: () async {
print("action button pressed");
CallKitParams params = CallKitParams(
id: callid,
nameCaller: 'Emmanuel',
appName: 'go mobile',
type: 0,
duration: 30000,
textAccept: 'Answer',
textDecline: 'Decline',
handle: '0716597086',
extra: {'userId': '1a2b3c4d'},
missedCallNotification: const NotificationParams(
id: 234,
showNotification: true,
callbackText: "Call back",
),
android: const AndroidParams(
ringtonePath: 'system_ringtone_default',
isCustomNotification: true,
isShowLogo: false,
));
await FlutterCallkitIncoming.showCallkitIncoming(params);
FlutterCallkitIncoming.onEvent.listen((event) {
print(event?.event);
switch (event?.event) {
case Event.actionCallAccept:
FlutterCallkitIncoming.setCallConnected(callid);
print("call accepter");
break;
default:
}
});
},
child: const Text('Incoming Call'),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}`
请帮忙解决这个问题。我是 flutter 新手,但我有 javascript 上的 webbrct 经验。
我尝试更换ice服务器并测试ice服务器连接以确保ice服务器和信令服务器正常工作。
此问题一般发生在没有媒体通过的情况下。为此需要在 sip 服务器上启用 RTP 端口。
步骤1: 配置 RTP 端口范围
<param name="rtp-start-port" value="16384"/>
<param name="rtp-end-port" value="32768"/>
步骤2: 允许指定的RTP端口范围通过防火墙
sudo ufw allow 16384:32768/udp
第3步: 重新启动 SIP 配置文件以应用更改。