Compare commits

...

4 Commits

7 changed files with 93 additions and 10 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -295,8 +295,23 @@ func RunAgent(configDirectory string, configuration *models.Configuration, commu
communication.HandleAudio = make(chan models.AudioDataPartial, 10)
if rtspBackChannelClient.HasBackChannel {
communication.HasBackChannel = true
go WriteAudioToBackchannel(communication, rtspBackChannelClient)
for {
go WriteFileToBackChannel(communication, rtspBackChannelClient)
time.Sleep(10 * time.Second)
}
}
//go onvif.HandleAudioActions(configuration, communication, rtspBackChannelClient)
/*go func() {
for action := range communication.HandleAudio {
fmt.Println("onvif.HandleAudioActions(): action", action)
// Ensure cloud.WriteFileToBackChannel is implemented or replace with a valid function
if WriteFileToBackChannel != nil {
WriteFileToBackChannel(communication, rtspBackChannelClient)
} else {
fmt.Println("cloud.WriteFileToBackChannel is not defined")
}
}
}()*/
// If we reach this point, we have a working RTSP connection.
communication.CameraConnected = true

View File

@@ -2,6 +2,7 @@ package components
import (
"bufio"
"context"
"fmt"
"os"
"time"
@@ -10,6 +11,7 @@ import (
"github.com/kerberos-io/agent/machinery/src/log"
"github.com/kerberos-io/agent/machinery/src/models"
"github.com/kerberos-io/agent/machinery/src/packets"
"github.com/kerberos-io/agent/machinery/src/utils"
"github.com/kerberos-io/joy4/av"
"github.com/pion/rtp"
"github.com/zaf/g711"
@@ -41,6 +43,7 @@ func WriteAudioToBackchannel(communication *models.Communication, rtspClient cap
b := g711.EncodeUlawFrame(v)
bufferUlaw = append(bufferUlaw, b)
}
fmt.Println("WriteFileToBackChannel: bufferUlaw", bufferUlaw)
pkt := packets.Packet{
Packet: &rtp.Packet{
@@ -62,34 +65,77 @@ func WriteAudioToBackchannel(communication *models.Communication, rtspClient cap
length = (length + uint32(len(bufferUlaw))) % 65536
sequenceNumber = (sequenceNumber + 1) % 65535
time.Sleep(128 * time.Millisecond)
}
log.Log.Info("Audio.WriteAudioToBackchannel(): finished")
}
func WriteFileToBackChannel(infile av.DemuxCloser) {
func WriteFileToBackChannel(communication *models.Communication, rtspClient capture.RTSPClient) {
log.Log.Info("Audio.WriteFileToBackChannel(): writing to backchannel audio codec")
length := uint32(0)
sequenceNumber := uint16(0)
// Do the warmup!
file, err := os.Open("./audiofile.bye")
file, err := os.Open("./audio/police-siren.wav")
if err != nil {
fmt.Println("WriteFileToBackChannel: error opening audiofile.bye file")
}
defer file.Close()
time.Sleep(3 * time.Second)
// Create a random sequence number
ssrc := utils.RandomUint32()
// Read file into buffer
reader := bufio.NewReader(file)
buffer := make([]byte, 1024)
count := 0
for {
_, err := reader.Read(buffer)
if err != nil {
break
}
// Send to backchannel
infile.Write(buffer, 2, uint32(count))
// Encode PCM to MULAW
bufferUlaw := g711.EncodeUlaw(buffer)
fmt.Println("WriteFileToBackChannel: bufferUlaw", bufferUlaw)
count = count + 1024
time.Sleep(128 * time.Millisecond)
pkt := packets.Packet{
Packet: &rtp.Packet{
Header: rtp.Header{
Version: 2,
Marker: false, // should be true
PayloadType: 0, //packet.PayloadType, // will be owerwriten
SequenceNumber: sequenceNumber,
Timestamp: uint32(length),
SSRC: ssrc,
},
Payload: bufferUlaw,
},
}
err = rtspClient.WritePacket(pkt)
if err != nil {
log.Log.Error("Audio.WriteFileToBackChannel(): error writing packet to backchannel")
if err.Error() == "EOF" {
log.Log.Info("Audio.WriteFileToBackChannel(): EOF, restarting backchannel")
rtspClient.Close()
err = rtspClient.ConnectBackChannel(context.Background())
if err != nil {
log.Log.Error("Audio.WriteFileToBackChannel(): error connecting to backchannel")
fmt.Println(err)
}
err = rtspClient.StartBackChannel(context.Background())
if err != nil {
log.Log.Error("Audio.WriteFileToBackChannel(): error starting backchannel")
fmt.Println(err)
}
}
} else {
log.Log.Info("Audio.WriteFileToBackChannel(): wrote packet to backchannel")
}
length = (length + uint32(len(buffer))) % 65536
sequenceNumber = (sequenceNumber + 1) % 65535
}
log.Log.Info("Audio.WriteAudioToBaWriteFileToBackChannelckchannel(): finished")
}

View File

@@ -166,6 +166,12 @@ func ProcessMotion(motionCursor *packets.QueueCursor, configuration *models.Conf
NumberOfChanges: changesToReturn,
}
communication.HandleMotion <- dataToPass //Save data to the channel
audioToPass := models.AudioDataPartial{
Timestamp: time.Now().Unix(),
Data: []int16{},
}
communication.HandleAudio <- audioToPass //Save data to the channel
}
}
}

View File

@@ -12,6 +12,7 @@ import (
onvifc "github.com/cedricve/go-onvif"
"github.com/gin-gonic/gin"
"github.com/kerberos-io/agent/machinery/src/capture"
"github.com/kerberos-io/agent/machinery/src/log"
"github.com/kerberos-io/agent/machinery/src/models"
"github.com/kerberos-io/onvif"
@@ -189,6 +190,16 @@ func HandleONVIFActions(configuration *models.Configuration, communication *mode
log.Log.Debug("onvif.HandleONVIFActions(): finished")
}
func HandleAudioActions(configuration *models.Configuration, communication *models.Communication, backchannel capture.RTSPClient) {
log.Log.Debug("onvif.HandleAudioActions(): started")
/*for action := range communication.HandleAudio {
fmt.Println("onvif.HandleAudioActions(): action", action)
cloud.WriteFileToBackChannel(communication, backchannel)
}*/
log.Log.Debug("onvif.HandleAudioActions(): finished")
}
func ConnectToOnvifDevice(cameraConfiguration *models.IPCamera) (*onvif.Device, device.GetCapabilitiesResponse, error) {
log.Log.Debug("onvif.ConnectToOnvifDevice(): started")
dev, err := onvif.NewDevice(onvif.DeviceParams{

View File

@@ -407,3 +407,8 @@ func ImageToBytes(img image.Image) ([]byte, error) {
err := jpeg.Encode(w, img, &jpeg.Options{Quality: 15})
return buffer.Bytes(), err
}
func RandomUint32() uint32 {
// Generate a random uint32 value
return uint32(rand.Int31())
}