refactor: move media channel handling to common/ and setup duplex channel

a duplex media channel should be more realistic, you generally both send and receive media when doing video call and
stuff
This commit is contained in:
KokaKiwi 2025-08-16 18:48:43 +02:00
parent ff738ff045
commit 01bde142d4
No known key found for this signature in database
GPG key ID: FD333F84686EFE78
5 changed files with 187 additions and 102 deletions

View file

@ -48,6 +48,7 @@ import (
"gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/constants"
"gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/event"
"gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/media"
"gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/messages"
"gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/namematcher"
"gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/task"
@ -451,18 +452,13 @@ func (sf *SnowflakeProxy) makePeerConnectionFromOffer(
return nil, fmt.Errorf("accept: NewPeerConnection: %s", err)
}
pc.OnTrack(func(remote *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
log.Printf("Track has started streamId(%s) id(%s) rid(%s) \n", remote.StreamID(), remote.ID(), remote.RID())
for {
rtcpBuf := make([]byte, 1500)
for {
if _, _, err := receiver.Read(rtcpBuf); err != nil {
return
}
}
}
})
// Start duplex media handling (both incoming and outgoing tracks)
mediaChannel := media.NewMediaChannel()
err = mediaChannel.Start(pc)
if err != nil {
log.Printf("Failed to setup proxy media channel: %v", err)
}
pc.OnDataChannel(func(dc *webrtc.DataChannel) {
log.Printf("New Data Channel %s-%d\n", dc.Label(), dc.ID())
@ -512,6 +508,9 @@ func (sf *SnowflakeProxy) makePeerConnectionFromOffer(
}
sf.EventDispatcher.OnNewSnowflakeEvent(event.EventOnProxyConnectionOver{Country: country})
// Clean up media channel
mediaChannel.Stop()
conn.dc = nil
dc.Close()
pw.Close()
@ -838,7 +837,7 @@ func (sf *SnowflakeProxy) Start() error {
err = sf.checkNATType(config, sf.NATProbeURL)
if err != nil {
// non-fatal error. Log it and continue
log.Printf(err.Error())
log.Printf("%s", err.Error())
setCurrentNATType(NATUnknown)
}
sf.EventDispatcher.OnNewSnowflakeEvent(event.EventOnCurrentNATTypeDetermined{CurNATType: getCurrentNATType()})