8000 dtmf sending added · next-coder/flutter-webrtc@71d9589 · GitHub
[go: up one dir, main page]

Skip to content

Commit 71d9589

Browse files
committed
dtmf sending added
Added dtmf functions to IOS and Android, peerConnectionId getter added to RTCPeerconnection.dart
1 parent 7b6e110 commit 71d9589

File tree

5 files changed

+101
-1
lines changed

5 files changed

+101
-1
lines changed

android/src/main/java/com/cloudwebrtc/webrtc/MethodCallHandlerImpl.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,33 @@ public void onMethodCall(MethodCall call, @NonNull Result notSafeResult) {
249249
result);
250250
break;
251251
}
252+
case "sendDtmf": {
253+
String peerConnectionId = call.argument("peerConnectionId");
254+
String tone = call.argument("tone");
255+
int duration = call.argument("duration");
256+
int gap = call.argument("gap");
257+
PeerConnection peerConnection = getPeerConnection(peerConnectionId);
258+
if (peerConnection != null) {
259+
RtpSender m_audioSender = null;
260+
for (RtpSender sender : peerConnection.getSenders()) {
261+
262+
if (sender.track().kind().equals("audio")) {
263+
m_audioSender = sender;
264+
}
265+
}
266+
if (m_audioSender != null) {
267+
DtmfSender dtmfSender = m_audioSender.dtmf();
268+
dtmfSender.insertDtmf(tone, duration, gap);
269+
}
270+
result.success("success");
271+
} else {
272+
Log.d(TAG, "dtmf() peerConnection is null");
273+
result
274+
.error("dtmf", "getRemoteDescription() peerConnection is null",
275+
null);
276+
}
277+
break;
278+
}
252279
case "addCandidate": {
253280
String peerConnectionId = call.argument("peerConnectionId");
254281
Map<String, Object> candidate = call.argument("candidate");

ios/Classes/FlutterWebRTCPlugin.m

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,40 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult) result
239239
message:[NSString stringWithFormat:@"Error: peerConnection not found!"]
240240
details:nil]);
241241
}
242+
} else if ([@"sendDtmf" isEqualToString:call.method]) {
243+
NSDictionary* argsMap = call.arguments;
244+
NSString* peerConnectionId = argsMap[@"peerConnectionId"];
245+
NSString* tone = argsMap[@"tone"];
246+
NSString* durationString = argsMap[@"duration"];
247+
NSString* gapString = argsMap[@"gap"];
248+
249+
double duration = [durationString doubleValue];
250+
double gap = [gapString doubleValue];
251+
RTCPeerConnection *peerConnection = self.peerConnections[peerConnectionId];
252+
if(peerConnection) {
253+
254+
RTCRtpSender* m_audioSender = nil ;
255+
for( RTCRtpSender *rtpSender in peerConnection.senders){
256+
if([[[rtpSender track] kind] isEqualToString:@"audio"]) {
257+
258+
m_audioSender = rtpSender;
259+
}
260+
}
261+
if(m_audioSender){
262+
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
263+
[queue addOperationWithBlock:^{
264+
[m_audioSender.dtmfSender insertDtmf :(NSString *)tone
265+
duration:(NSTimeInterval) duration interToneGap:(NSTimeInterval)gap];
266+
NSLog(@"DTMF Tone played ");
267+
}];
268+
}
269+
270+
result(@{@"result": @"success"});
271+
} else {
272+
result([FlutterError errorWithCode:[NSString stringWithFormat:@"%@Failed",call.method]
273+
message:[NSString stringWithFormat:@"Error: peerConnection not found!"]
274+
details:nil]);
275+
}
242276
} else if ([@"addCandidate" isEqualToString:call.method]) {
243277
NSDictionary* argsMap = call.arguments;
244278
NSString* peerConnectionId = argsMap[@"peerConnectionId"];

lib/flutter_webrtc.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ export 'src/rtc_stats_report.dart';
2323
export 'src/rtc_video_view.dart'
2424
if (dart.library.html) 'src/web/rtc_video_view.dart';
2525
export 'src/utils.dart' if (dart.library.html) 'src/web/utils.dart';
26+
export 'rtc_dtmf_sender.dart';

lib/src/rtc_dtmf_sender.dart

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,37 @@
1-
class RTCDTMFSender {}
1+
import 'dart:io';
2+
import 'webrtc.dart';
3+
import 'package:flutter/services.dart';
4+
5+
class RTCDTMFSender {
6+
// peer connection Id must be defined as a variable where this function will be called.
7+
8+
static Future<void> sendDtmf(String peerConnectionId, String tone,
9+
{double duration, double gap}) async {
10+
final MethodChannel _channel = WebRTC.methodChannel();
11+
12+
double _duration = 0.0;
13+
double _gap = 0.0;
14+
// IOS accepts gap and duration in seconds so conversion to be needed
15+
if (duration != null) {
16+
if (Platform.isIOS) {
17+
_duration = duration / 1000;
18+
} else {
19+
_duration = duration;
20+
}
21+
}
22+
if (gap != null) {
23+
if (Platform.isIOS) {
24+
_gap = gap / 1000;
25+
} else {
26+
_gap = gap;
27+
}
28+
}
29+
30+
await _channel.invokeMethod('sendDtmf', <String, dynamic>{
31+
'peerConnectionId': peerConnectionId,
32+
'tone': tone,
33+
'duration': _duration,
34+
'gap': _gap,
35+
});
36+
}
37+
}

lib/src/rtc_peerconnection.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ class RTCPeerConnection {
7474

7575
RTCIceConnectionState get iceConnectionState => _iceConnectionState;
7676

77+
String get peerConnectionId => _peerConnectionId;
78+
7779
/*
7880
* PeerConnection event listener.
7981
*/

0 commit comments

Comments
 (0)
0