Skip to content

Commit

Permalink
Add serialization and deserialization of sensor msg and send them
Browse files Browse the repository at this point in the history
through data channel
  • Loading branch information
3DRX committed Dec 7, 2024
1 parent 64fbb8e commit 5174c64
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
22 changes: 22 additions & 0 deletions receiver/peer_connection_channel/peer_connection_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"log/slog"
"time"

sensor_msgs_msg "github.com/3DRX/webrtc-ros-bridge/rclgo_gen/sensor_msgs/msg"
"github.com/pion/interceptor"
"github.com/pion/rtcp"
"github.com/pion/webrtc/v3"
"github.com/tiiuae/rclgo/pkg/rclgo"
"github.com/tiiuae/rclgo/pkg/rclgo/types"
)

Expand Down Expand Up @@ -156,5 +158,25 @@ func (pc *PeerConnectionChannel) Spin() {
webmSaver.PushVP8(rtp)
}
})
pc.peerConnection.OnDataChannel(func(d *webrtc.DataChannel) {
d.OnMessage(func(msg webrtc.DataChannelMessage) {
serializedMsg := msg.Data
sensorMsg, err := rclgo.Deserialize(serializedMsg, sensor_msgs_msg.LaserScanTypeSupport)
if err != nil {
slog.Error("failed to deserialize sensor message", "error", err)
return
}
laserScanMsg, ok := sensorMsg.(*sensor_msgs_msg.LaserScan)
if !ok {
slog.Error("failed to cast sensor message to LaserScan")
return
}
slog.Info("datachannel message", "frame ID", laserScanMsg.Header.FrameId)
})
d.OnOpen(func() {
slog.Info("datachannel open", "label", d.Label(), "ID", d.ID())
d.SendText("Hello from receiver!")
})
})
go handleSignalingMessage(pc)
}
22 changes: 22 additions & 0 deletions sender/peer_connection_channel/peer_connection_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/pion/mediadevices/pkg/codec/vpx"
"github.com/pion/mediadevices/pkg/prop"
"github.com/pion/webrtc/v3"
"github.com/tiiuae/rclgo/pkg/rclgo"
"github.com/tiiuae/rclgo/pkg/rclgo/types"
)

Expand Down Expand Up @@ -163,6 +164,27 @@ func (pc *PeerConnectionChannel) handleRemoteICECandidate() {

func (pc *PeerConnectionChannel) Spin() {
go pc.chanDispatcher()

datachannel, err := pc.peerConnection.CreateDataChannel("data", nil)
if err != nil {
panic(err)
}
datachannel.OnOpen(func() {
slog.Info("datachannel open", "label", datachannel.Label(), "ID", datachannel.ID())
for {
sensorMsg := <-pc.sensorChan
serializedMsg, err := rclgo.Serialize(sensorMsg)
if err != nil {
slog.Error("failed to serialize sensor message", "error", err)
continue
}
datachannel.Send(serializedMsg)
}
})
datachannel.OnMessage(func(msg webrtc.DataChannelMessage) {
slog.Info("datachannel message", "data", string(msg.Data))
})

offer, err := pc.peerConnection.CreateOffer(nil)
if err != nil {
panic(err)
Expand Down

0 comments on commit 5174c64

Please sign in to comment.