-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ONVIF backchannel functionality with G711 encoding
- Loading branch information
Showing
9 changed files
with
138 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package components | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/kerberos-io/agent/machinery/src/log" | ||
"github.com/kerberos-io/agent/machinery/src/models" | ||
"github.com/kerberos-io/joy4/av" | ||
"github.com/zaf/g711" | ||
) | ||
|
||
func GetBackChannelAudioCodec(streams []av.CodecData, communication *models.Communication) av.AudioCodecData { | ||
for _, stream := range streams { | ||
if stream.Type().IsAudio() { | ||
if stream.Type().String() == "PCM_MULAW" { | ||
pcmuCodec := stream.(av.AudioCodecData) | ||
if pcmuCodec.IsBackChannel() { | ||
communication.HasBackChannel = true | ||
return pcmuCodec | ||
} | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func WriteAudioToBackchannel(infile av.DemuxCloser, streams []av.CodecData, communication *models.Communication) { | ||
log.Log.Info("WriteAudioToBackchannel: looking for backchannel audio codec") | ||
|
||
pcmuCodec := GetBackChannelAudioCodec(streams, communication) | ||
if pcmuCodec != nil { | ||
log.Log.Info("WriteAudioToBackchannel: found backchannel audio codec") | ||
|
||
length := 0 | ||
channel := pcmuCodec.GetIndex() * 2 // This is the same calculation as Interleaved property in the SDP file. | ||
for audio := range communication.HandleAudio { | ||
// Encode PCM to MULAW | ||
var bufferUlaw []byte | ||
for _, v := range audio.Data { | ||
b := g711.EncodeUlawFrame(v) | ||
bufferUlaw = append(bufferUlaw, b) | ||
} | ||
infile.Write(bufferUlaw, channel, uint32(length)) | ||
length = (length + len(bufferUlaw)) % 65536 | ||
time.Sleep(128 * time.Millisecond) | ||
} | ||
} | ||
log.Log.Info("WriteAudioToBackchannel: finished") | ||
|
||
} | ||
|
||
func WriteFileToBackChannel(infile av.DemuxCloser) { | ||
// Do the warmup! | ||
file, err := os.Open("./audiofile.bye") | ||
if err != nil { | ||
fmt.Println("WriteFileToBackChannel: error opening audiofile.bye file") | ||
} | ||
defer file.Close() | ||
|
||
// 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 | ||
fmt.Println(buffer) | ||
infile.Write(buffer, 2, uint32(count)) | ||
|
||
count = count + 1024 | ||
time.Sleep(128 * time.Millisecond) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package models | ||
|
||
type AudioDataPartial struct { | ||
Timestamp int64 `json:"timestamp" bson:"timestamp"` | ||
Data []int16 `json:"data" bson:"data"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters