Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: simplify interface to TWCC 4/4 #3003

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 21 additions & 24 deletions examples/bandwidth-estimation-from-disk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"github.com/pion/interceptor"
"github.com/pion/interceptor/pkg/cc"
"github.com/pion/interceptor/pkg/gcc"
"github.com/pion/webrtc/v4"

Check failure on line 24 in examples/bandwidth-estimation-from-disk/main.go

View workflow job for this annotation

GitHub Actions / lint / Go

could not import github.com/pion/webrtc/v4 (-: # github.com/pion/webrtc/v4
"github.com/pion/webrtc/v4/pkg/media"
"github.com/pion/webrtc/v4/pkg/media/ivfreader"
)
Expand Down Expand Up @@ -59,45 +59,45 @@
}

i := &interceptor.Registry{}
m := &webrtc.MediaEngine{}

Check failure on line 62 in examples/bandwidth-estimation-from-disk/main.go

View workflow job for this annotation

GitHub Actions / lint / Go

undefined: webrtc (typecheck)
if err := m.RegisterDefaultCodecs(); err != nil {
panic(err)
}

// Create a Congestion Controller. This analyzes inbound and outbound data and provides
// suggestions on how much we should be sending.
//
// Passing `nil` means we use the default Estimation Algorithm which is Google Congestion Control.
// You can use the other ones that Pion provides, or write your own!
congestionController, err := cc.NewInterceptor(func() (cc.BandwidthEstimator, error) {
return gcc.NewSendSideBWE(gcc.SendSideBWEInitialBitrate(lowBitrate))
})
if err != nil {
if err := webrtc.ConfigureTWCCHeaderExtensionSender(m, i); err != nil {
panic(err)
}

estimatorChan := make(chan cc.BandwidthEstimator, 1)
congestionController.OnNewPeerConnection(func(id string, estimator cc.BandwidthEstimator) { //nolint: revive
estimatorChan <- estimator
})

i.Add(congestionController)
if err = webrtc.ConfigureTWCCHeaderExtensionSender(m, i); err != nil {
if err := webrtc.RegisterDefaultInterceptors(m, i); err != nil {
panic(err)
}

if err = webrtc.RegisterDefaultInterceptors(m, i); err != nil {
api := webrtc.NewAPI(
webrtc.WithInterceptorRegistry(i), webrtc.WithMediaEngine(m),
)

estimator, err := gcc.NewSendSideBWE(
gcc.SendSideBWEInitialBitrate(lowBitrate),
)
if err != nil {
panic(err)
}

// Create a new RTCPeerConnection
peerConnection, err := webrtc.NewAPI(webrtc.WithInterceptorRegistry(i), webrtc.WithMediaEngine(m)).NewPeerConnection(webrtc.Configuration{
interceptor, err := cc.NewSingleInterceptor(estimator)

Check failure on line 85 in examples/bandwidth-estimation-from-disk/main.go

View workflow job for this annotation

GitHub Actions / lint / Go

undefined: cc.NewSingleInterceptor (typecheck)
if err != nil {
panic(err)
}
configuration := webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: []string{"stun:stun.l.google.com:19302"},
},
},
})
}

peerConnection, err := api.NewPeerConnection(
configuration,
webrtc.WithInterceptor(interceptor),
)
if err != nil {
panic(err)
}
Expand All @@ -107,9 +107,6 @@
}
}()

// Wait until our Bandwidth Estimator has been created
estimator := <-estimatorChan

// Create a video track
videoTrack, err := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8}, "video", "pion")
if err != nil {
Expand Down Expand Up @@ -280,7 +277,7 @@
}

// JSON encode + base64 a SessionDescription
func encode(obj *webrtc.SessionDescription) string {

Check failure on line 280 in examples/bandwidth-estimation-from-disk/main.go

View workflow job for this annotation

GitHub Actions / lint / Go

undefined: webrtc (typecheck)
b, err := json.Marshal(obj)
if err != nil {
panic(err)
Expand All @@ -290,7 +287,7 @@
}

// Decode a base64 and unmarshal JSON into a SessionDescription
func decode(in string, obj *webrtc.SessionDescription) {

Check failure on line 290 in examples/bandwidth-estimation-from-disk/main.go

View workflow job for this annotation

GitHub Actions / lint / Go

undefined: webrtc (typecheck)
b, err := base64.StdEncoding.DecodeString(in)
if err != nil {
panic(err)
Expand Down
25 changes: 21 additions & 4 deletions peerconnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,26 @@
log logging.LeveledLogger

interceptorRTCPWriter interceptor.RTCPWriter

extraInterceptors []interceptor.Interceptor
}

// NewPeerConnection creates a PeerConnection with the default codecs and interceptors.
//
// If you wish to customize the set of available codecs and/or the set of active interceptors,
// create an API with a custom MediaEngine and/or interceptor.Registry,
// then call [(*API).NewPeerConnection] instead of this function.
func NewPeerConnection(configuration Configuration) (*PeerConnection, error) {
func NewPeerConnection(configuration Configuration, options... func (*PeerConnection) error) (*PeerConnection, error) {
api := NewAPI()
return api.NewPeerConnection(configuration)
return api.NewPeerConnection(configuration, options...)
}

// NewPeerConnection creates a new PeerConnection with the provided configuration against the received API object.
// This method will attach a default set of codecs and interceptors to
// the resulting PeerConnection. If this behavior is not desired,
// set the set of codecs and interceptors explicitly by using
// [WithMediaEngine] and [WithInterceptorRegistry] when calling [NewAPI].
func (api *API) NewPeerConnection(configuration Configuration) (*PeerConnection, error) {
func (api *API) NewPeerConnection(configuration Configuration, options... func (*PeerConnection) error) (*PeerConnection, error) {
// https://w3c.github.io/webrtc-pc/#constructor (Step #2)
// Some variables defined explicitly despite their implicit zero values to
// allow better readability to understand what is happening.
Expand Down Expand Up @@ -136,12 +138,20 @@
api: api,
log: api.settingEngine.LoggerFactory.NewLogger("pc"),
}

for _, option := range options {
err := option(pc)
if err != nil {
return nil, err
}
}

pc.ops = newOperations(pc.updateNegotiationNeededFlagOnEmptyChain, pc.onNegotiationNeeded)

pc.iceConnectionState.Store(ICEConnectionStateNew)
pc.connectionState.Store(PeerConnectionStateNew)

i, err := api.interceptorRegistry.Build("")
i, err := api.interceptorRegistry.Build("", pc.extraInterceptors...)

Check failure on line 154 in peerconnection.go

View workflow job for this annotation

GitHub Actions / test (1.22) / Go 1.22

cannot use ... in call to non-variadic api.interceptorRegistry.Build

Check failure on line 154 in peerconnection.go

View workflow job for this annotation

GitHub Actions / test (1.22) / Go 1.22

cannot use ... in call to non-variadic api.interceptorRegistry.Build

Check failure on line 154 in peerconnection.go

View workflow job for this annotation

GitHub Actions / lint / Go

cannot use ... in call to non-variadic api.interceptorRegistry.Build) (typecheck)

Check failure on line 154 in peerconnection.go

View workflow job for this annotation

GitHub Actions / lint / Go

cannot use ... in call to non-variadic api.interceptorRegistry.Build) (typecheck)

Check failure on line 154 in peerconnection.go

View workflow job for this annotation

GitHub Actions / lint / Go

cannot use ... in call to non-variadic api.interceptorRegistry.Build) (typecheck)

Check failure on line 154 in peerconnection.go

View workflow job for this annotation

GitHub Actions / test (1.23) / Go 1.23

cannot use ... in call to non-variadic api.interceptorRegistry.Build

Check failure on line 154 in peerconnection.go

View workflow job for this annotation

GitHub Actions / test (1.23) / Go 1.23

cannot use ... in call to non-variadic api.interceptorRegistry.Build
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -195,6 +205,13 @@
return pc, nil
}

func WithInterceptor(i interceptor.Interceptor) func (*PeerConnection) error {
return func(pc *PeerConnection) error {
pc.extraInterceptors = append(pc.extraInterceptors, i)
return nil
}
}

// initConfiguration defines validation of the specified Configuration and
// its assignment to the internal configuration variable. This function differs
// from its SetConfiguration counterpart because most of the checks do not
Expand Down
Loading