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

added nack interceptors #1549

Closed
wants to merge 4 commits into from
Closed
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
14 changes: 14 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package webrtc

import (
"github.com/pion/logging"
"github.com/pion/webrtc/v3/pkg/interceptor"
)

// API bundles the global functions of the WebRTC and ORTC API.
Expand All @@ -13,6 +14,7 @@ import (
type API struct {
settingEngine *SettingEngine
mediaEngine *MediaEngine
interceptor interceptor.Interceptor
}

// NewAPI Creates a new API object for keeping semi-global settings to WebRTC objects
Expand All @@ -35,6 +37,10 @@ func NewAPI(options ...func(*API)) *API {
a.mediaEngine = &MediaEngine{}
}

if a.interceptor == nil {
a.interceptor = &interceptor.NoOp{}
}

return a
}

Expand All @@ -57,3 +63,11 @@ func WithSettingEngine(s SettingEngine) func(a *API) {
a.settingEngine = &s
}
}

// WithInterceptorRegistry allows providing Interceptors to the API.
// Settings should not be changed after passing the registry to an API.
func WithInterceptorRegistry(interceptorRegistry *InterceptorRegistry) func(a *API) {
return func(a *API) {
a.interceptor = interceptorRegistry.build()
}
}
9 changes: 8 additions & 1 deletion examples/save-to-disk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,15 @@ func main() {
panic(err)
}

s := webrtc.SettingEngine{}
s.SetSRTPReplayProtectionWindow(8192) // this is needed for nack for now
ir := &webrtc.InterceptorRegistry{}
if err := webrtc.RegisterDefaultInterceptors(&s, &m, ir); err != nil {
panic(err)
}

// Create the API object with the MediaEngine
api := webrtc.NewAPI(webrtc.WithMediaEngine(&m))
api := webrtc.NewAPI(webrtc.WithSettingEngine(s), webrtc.WithMediaEngine(&m), webrtc.WithInterceptorRegistry(ir))

// Prepare the configuration
config := webrtc.Configuration{
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/pion/webrtc/v3

go 1.12
go 1.13

require (
github.com/pion/datachannel v1.4.21
Expand Down
59 changes: 59 additions & 0 deletions interceptor_registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// +build !js

package webrtc

import (
"github.com/pion/logging"
"github.com/pion/webrtc/v3/pkg/interceptor"
)

// InterceptorRegistry is a collector for interceptors.
type InterceptorRegistry struct {
interceptors []interceptor.Interceptor
}

// Add adds a new Interceptor to the registry.
func (i *InterceptorRegistry) Add(icpr interceptor.Interceptor) {
i.interceptors = append(i.interceptors, icpr)
}

func (i *InterceptorRegistry) build() interceptor.Interceptor {
if len(i.interceptors) == 0 {
return &interceptor.NoOp{}
}

return interceptor.NewChain(i.interceptors)
}

// RegisterDefaultInterceptors will register some useful interceptors. If you want to customize which interceptors are loaded,
// you should copy the code from this method and remove unwanted interceptors.
func RegisterDefaultInterceptors(settingEngine *SettingEngine, mediaEngine *MediaEngine, interceptorRegistry *InterceptorRegistry) error {
loggerFactory := settingEngine.LoggerFactory
if loggerFactory == nil {
loggerFactory = logging.NewDefaultLoggerFactory()
}

err := ConfigureNack(loggerFactory, mediaEngine, interceptorRegistry)
if err != nil {
return err
}

return nil
}

// ConfigureNack will setup everything necessary for handling generating/responding to nack messages.
func ConfigureNack(loggerFactory logging.LoggerFactory, mediaEngine *MediaEngine, interceptorRegistry *InterceptorRegistry) error {
mediaEngine.RegisterFeedback(RTCPFeedback{Type: "nack"}, RTPCodecTypeVideo)
receiverNack, err := interceptor.NewReceiverNack(8192, loggerFactory.NewLogger("receiver_nack"))
if err != nil {
return err
}
interceptorRegistry.Add(receiverNack)
senderNack, err := interceptor.NewSenderNack(8192, loggerFactory.NewLogger("sender_nack"))
if err != nil {
return err
}
interceptorRegistry.Add(senderNack)

return nil
}
188 changes: 188 additions & 0 deletions interceptor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
// +build !js

package webrtc

import (
"sync"
"sync/atomic"
"testing"
"time"

"github.com/pion/rtcp"
"github.com/pion/rtp"
"github.com/pion/transport/test"
"github.com/pion/webrtc/v3/pkg/interceptor"
"github.com/pion/webrtc/v3/pkg/media"
"github.com/stretchr/testify/assert"
)

type testInterceptor struct {
t *testing.T
extensionID uint8
rtcpWriter atomic.Value
lastRTCP atomic.Value
interceptor.NoOp
}

func (t *testInterceptor) BindLocalStream(_ *interceptor.StreamInfo, writer interceptor.RTPWriter) interceptor.RTPWriter {
return interceptor.RTPWriterFunc(func(p *rtp.Packet, attributes interceptor.Attributes) (int, error) {
// set extension on outgoing packet
p.Header.Extension = true
p.Header.ExtensionProfile = 0xBEDE
assert.NoError(t.t, p.Header.SetExtension(t.extensionID, []byte("write")))

return writer.Write(p, attributes)
})
}

func (t *testInterceptor) BindRemoteStream(info *interceptor.StreamInfo, reader interceptor.RTPReader) interceptor.RTPReader {
return interceptor.RTPReaderFunc(func() (*rtp.Packet, interceptor.Attributes, error) {
p, attributes, err := reader.Read()
if err != nil {
return nil, nil, err
}
// set extension on incoming packet
p.Header.Extension = true
p.Header.ExtensionProfile = 0xBEDE
assert.NoError(t.t, p.Header.SetExtension(t.extensionID, []byte("read")))

// write back a pli
rtcpWriter := t.rtcpWriter.Load().(interceptor.RTCPWriter)
pli := &rtcp.PictureLossIndication{SenderSSRC: info.SSRC, MediaSSRC: info.SSRC}
_, err = rtcpWriter.Write([]rtcp.Packet{pli}, make(interceptor.Attributes))
assert.NoError(t.t, err)

return p, attributes, nil
})
}

func (t *testInterceptor) BindRTCPReader(reader interceptor.RTCPReader) interceptor.RTCPReader {
return interceptor.RTCPReaderFunc(func() ([]rtcp.Packet, interceptor.Attributes, error) {
pkts, attributes, err := reader.Read()
if err != nil {
return nil, nil, err
}

t.lastRTCP.Store(pkts[0])

return pkts, attributes, nil
})
}

func (t *testInterceptor) lastReadRTCP() rtcp.Packet {
p, _ := t.lastRTCP.Load().(rtcp.Packet)
return p
}

func (t *testInterceptor) BindRTCPWriter(writer interceptor.RTCPWriter) interceptor.RTCPWriter {
t.rtcpWriter.Store(writer)
return writer
}

func TestPeerConnection_Interceptor(t *testing.T) {
to := test.TimeOut(time.Second * 20)
defer to.Stop()

report := test.CheckRoutines(t)
defer report()

createPC := func(interceptor interceptor.Interceptor) *PeerConnection {
m := &MediaEngine{}
err := m.RegisterDefaultCodecs()
if err != nil {
t.Fatal(err)
}
ir := &InterceptorRegistry{}
ir.Add(interceptor)
pc, err := NewAPI(WithMediaEngine(m), WithInterceptorRegistry(ir)).NewPeerConnection(Configuration{})
if err != nil {
t.Fatal(err)
}

return pc
}

sendInterceptor := &testInterceptor{t: t, extensionID: 1}
senderPC := createPC(sendInterceptor)
receiverPC := createPC(&testInterceptor{t: t, extensionID: 2})

track, err := NewTrackLocalStaticSample(RTPCodecCapability{MimeType: "video/vp8"}, "video", "pion")
if err != nil {
t.Fatal(err)
}

sender, err := senderPC.AddTrack(track)
if err != nil {
t.Fatal(err)
}

pending := new(int32)
wg := &sync.WaitGroup{}

wg.Add(1)
*pending++
receiverPC.OnTrack(func(track *TrackRemote, receiver *RTPReceiver) {
p, readErr := track.ReadRTP()
if readErr != nil {
t.Fatal(readErr)
}
assert.Equal(t, p.Extension, true)
assert.Equal(t, "write", string(p.GetExtension(1)))
assert.Equal(t, "read", string(p.GetExtension(2)))
atomic.AddInt32(pending, -1)
wg.Done()

for {
_, readErr = track.ReadRTP()
if readErr != nil {
return
}
}
})

wg.Add(1)
*pending++
go func() {
_, readErr := sender.ReadRTCP()
assert.NoError(t, readErr)
atomic.AddInt32(pending, -1)
wg.Done()

for {
_, readErr = sender.ReadRTCP()
if readErr != nil {
return
}
}
}()

err = signalPair(senderPC, receiverPC)
if err != nil {
t.Fatal(err)
}

wg.Add(1)
go func() {
defer wg.Done()
for {
time.Sleep(time.Millisecond * 100)
if routineErr := track.WriteSample(media.Sample{Data: []byte{0x00}, Duration: time.Second}); routineErr != nil {
t.Error(routineErr)
return
}

if atomic.LoadInt32(pending) == 0 {
return
}
}
}()

wg.Wait()
assert.NoError(t, senderPC.Close())
assert.NoError(t, receiverPC.Close())

pli, _ := sendInterceptor.lastReadRTCP().(*rtcp.PictureLossIndication)
if pli == nil || pli.SenderSSRC == 0 {
t.Errorf("pli not found by send interceptor")
}
}
29 changes: 29 additions & 0 deletions interceptor_track_local.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// +build !js

package webrtc

import (
"sync/atomic"

"github.com/pion/rtp"
"github.com/pion/webrtc/v3/pkg/interceptor"
)

type interceptorTrackLocalWriter struct {
TrackLocalWriter
rtpWriter atomic.Value
}

func (i *interceptorTrackLocalWriter) setRTPWriter(writer interceptor.RTPWriter) {
i.rtpWriter.Store(writer)
}

func (i *interceptorTrackLocalWriter) WriteRTP(header *rtp.Header, payload []byte) (int, error) {
writer := i.rtpWriter.Load().(interceptor.RTPWriter)

if writer == nil {
return 0, nil
}

return writer.Write(&rtp.Packet{Header: *header, Payload: payload}, make(interceptor.Attributes))
}
Loading