mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 13:25:40 +00:00
Compare commits
1 Commits
v0.16.0
...
feat/sip-c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
68a943e141 |
@@ -7,6 +7,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -21,16 +22,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type SIPCallServer struct {
|
type SIPCallServer struct {
|
||||||
config config.ModuleConfig
|
config config.ModuleConfig
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
router route.RouteIO
|
router route.RouteIO
|
||||||
IP string
|
IP string
|
||||||
Port int
|
Port int
|
||||||
Transport string
|
Transport string
|
||||||
UserAgent string
|
UserAgent string
|
||||||
dg *diago.Diago
|
RecordingPath string
|
||||||
logger *slog.Logger
|
dg *diago.Diago
|
||||||
cancel context.CancelFunc
|
logger *slog.Logger
|
||||||
|
cancel context.CancelFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
type SIPCallMessage struct {
|
type SIPCallMessage struct {
|
||||||
@@ -86,7 +88,19 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &SIPCallServer{config: moduleConfig, IP: ipString, Port: int(portNum), Transport: transportString, UserAgent: userAgentString, logger: CreateLogger(moduleConfig)}, nil
|
recordingPathString := ""
|
||||||
|
|
||||||
|
recordingPath, ok := params["recordingPath"]
|
||||||
|
if ok {
|
||||||
|
specificRecordingPath, ok := recordingPath.(string)
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("sip.call.server recordingPath must be a string")
|
||||||
|
}
|
||||||
|
recordingPathString = specificRecordingPath
|
||||||
|
}
|
||||||
|
|
||||||
|
return &SIPCallServer{config: moduleConfig, IP: ipString, Port: int(portNum), Transport: transportString, UserAgent: userAgentString, RecordingPath: recordingPathString, logger: CreateLogger(moduleConfig)}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -151,6 +165,41 @@ func (scs *SIPCallServer) HandleCall(inDialog *diago.DialogServerSession) {
|
|||||||
dialogContext := context.WithValue(scs.ctx, sipCallContextKey("call"), &SIPCall{
|
dialogContext := context.WithValue(scs.ctx, sipCallContextKey("call"), &SIPCall{
|
||||||
inDialog: inDialog,
|
inDialog: inDialog,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if scs.RecordingPath != "" {
|
||||||
|
filename := path.Join(scs.RecordingPath, fmt.Sprintf("%s-%d.wav", inDialog.ToUser(), time.Now().Unix()))
|
||||||
|
wavFile, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0755)
|
||||||
|
if err != nil {
|
||||||
|
scs.logger.Error("error creating recording file", "error", err, "filename", filename)
|
||||||
|
} else {
|
||||||
|
recorder, err := inDialog.AudioStereoRecordingCreate(wavFile)
|
||||||
|
if err != nil {
|
||||||
|
scs.logger.Error("error creating recording", "error", err)
|
||||||
|
wavFile.Close()
|
||||||
|
} else {
|
||||||
|
go func() {
|
||||||
|
<-inDialog.Context().Done()
|
||||||
|
err := recorder.Close()
|
||||||
|
if err != nil {
|
||||||
|
scs.logger.Error("error closing recording", "error", err)
|
||||||
|
}
|
||||||
|
wavFile.Close()
|
||||||
|
scs.logger.Debug("finished recording", "filename", filename)
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
bytes, err := media.Copy(recorder.AudioReader(), recorder.AudioWriter())
|
||||||
|
fmt.Println("recorded bytes", bytes)
|
||||||
|
if err != nil {
|
||||||
|
if !errors.Is(err, io.EOF) {
|
||||||
|
scs.logger.Error("error while recording", "error", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
scs.router.HandleInput(dialogContext, scs.Id(), SIPCallMessage{
|
scs.router.HandleInput(dialogContext, scs.Id(), SIPCallMessage{
|
||||||
To: inDialog.ToUser(),
|
To: inDialog.ToUser(),
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user