From 4b0bff91ab8a409b335762a5a8eaaa7eaacd3b90 Mon Sep 17 00:00:00 2001 From: David Zhao Date: Wed, 4 Oct 2023 21:51:58 -0700 Subject: [PATCH] Expose PublishDataPacket, ability to set topic (#298) * Expose PublishDataPacket, ability to set topic Closes #290 * fmt --- callback.go | 3 ++- localparticipant.go | 33 +++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/callback.go b/callback.go index 02c724d2..9f694c91 100644 --- a/callback.go +++ b/callback.go @@ -15,8 +15,9 @@ package lksdk import ( - "github.com/livekit/protocol/livekit" "github.com/pion/webrtc/v3" + + "github.com/livekit/protocol/livekit" ) type ParticipantCallback struct { diff --git a/localparticipant.go b/localparticipant.go index d8ec36c6..6e0991e4 100644 --- a/localparticipant.go +++ b/localparticipant.go @@ -257,30 +257,39 @@ func (p *LocalParticipant) closeTracks() { } } -func (p *LocalParticipant) PublishData(data []byte, kind livekit.DataPacket_Kind, destinationSids []string) error { - packet := &livekit.DataPacket{ +func (p *LocalParticipant) PublishDataPacket(userPacket *livekit.UserPacket, kind livekit.DataPacket_Kind) error { + if userPacket == nil { + return ErrInvalidParameter + } + dataPacket := &livekit.DataPacket{ Kind: kind, Value: &livekit.DataPacket_User{ - User: &livekit.UserPacket{ - // this is enforced on the server side, setting for completeness - ParticipantSid: p.SID(), - Payload: data, - DestinationSids: destinationSids, - }, + User: userPacket, }, } - if err := p.engine.ensurePublisherConnected(true); err != nil { return err } - // encode packet - encoded, err := proto.Marshal(packet) + encoded, err := proto.Marshal(dataPacket) if err != nil { return err } - return p.engine.GetDataChannel(kind).Send(encoded) + return p.engine.GetDataChannel(dataPacket.Kind).Send(encoded) +} + +func (p *LocalParticipant) PublishData( + data []byte, + kind livekit.DataPacket_Kind, + destinationSids []string, +) error { + packet := &livekit.UserPacket{ + Payload: data, + DestinationSids: destinationSids, + } + + return p.PublishDataPacket(packet, kind) } func (p *LocalParticipant) UnpublishTrack(sid string) error {