Skip to content

Commit

Permalink
Expose PublishDataPacket, ability to set topic (#298)
Browse files Browse the repository at this point in the history
* Expose PublishDataPacket, ability to set topic

Closes #290

* fmt
  • Loading branch information
davidzhao authored Oct 5, 2023
1 parent 47ee75f commit 4b0bff9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
3 changes: 2 additions & 1 deletion callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
33 changes: 21 additions & 12 deletions localparticipant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4b0bff9

Please sign in to comment.