-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdtmf.go
124 lines (106 loc) · 2.37 KB
/
dtmf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package sipnexus
import (
"fmt"
"sync"
"github.com/itzmanish/sipnexus/pkg/logger"
"github.com/pion/rtp"
"github.com/pion/webrtc/v3"
)
type DTMFHandler struct {
logger logger.Logger
dtmfPayload uint8
eventHandlers map[string]func(string)
mu sync.RWMutex
}
func NewDTMFHandler(logger logger.Logger, dtmfPayload uint8) *DTMFHandler {
return &DTMFHandler{
logger: logger,
dtmfPayload: dtmfPayload,
eventHandlers: map[string]func(string){},
}
}
func (d *DTMFHandler) processEvent(event string) {
d.logger.Info(fmt.Sprintf("DTMF event received: %s", event))
d.notifyHandlers(event)
}
func (d *DTMFHandler) HandleDTMF(packet *rtp.Packet) error {
if packet.PayloadType != d.dtmfPayload {
return fmt.Errorf("not a DTMF event packet")
}
if len(packet.Payload) < 4 {
return fmt.Errorf("invalid DTMF packet length")
}
event := packet.Payload[0]
endOfEvent := (packet.Payload[1] & 0x80) != 0
char := d.mapDTMFEventToChar(event)
if endOfEvent {
go d.processEvent(char)
}
return nil
}
func (d *DTMFHandler) mapDTMFEventToChar(event byte) string {
switch event {
case 0:
return "0"
case 1:
return "1"
case 2:
return "2"
case 3:
return "3"
case 4:
return "4"
case 5:
return "5"
case 6:
return "6"
case 7:
return "7"
case 8:
return "8"
case 9:
return "9"
case 10:
return "*"
case 11:
return "#"
case 12:
return "A"
case 13:
return "B"
case 14:
return "C"
case 15:
return "D"
default:
return "?"
}
}
func (d *DTMFHandler) RegisterHandler(event string, handler func(string)) {
d.mu.Lock()
defer d.mu.Unlock()
d.eventHandlers[event] = handler
}
func (d *DTMFHandler) notifyHandlers(event string) {
d.mu.RLock()
defer d.mu.RUnlock()
if handler, ok := d.eventHandlers[event]; ok {
go handler(event)
}
}
func (d *DTMFHandler) IntegrateWithPion(pc *webrtc.PeerConnection) {
// TODO: need to find a way to get DTMF tone in a call
}
func (d *DTMFHandler) ProcessDTMFEvent(event string, sessionID string) {
d.logger.Info(fmt.Sprintf("Processing DTMF event %s for session %s", event, sessionID))
// TODO: Implement DTMF-triggered actions (e.g., IVR menu navigation)
switch event {
case "1":
d.logger.Info("DTMF 1 pressed: Navigating to main menu")
case "2":
d.logger.Info("DTMF 2 pressed: Transferring to agent")
default:
d.logger.Info("Unhandled DTMF event: " + event)
}
d.notifyHandlers(event)
}