diff --git a/api.go b/api.go index c6010eb526f..f440f0bb358 100644 --- a/api.go +++ b/api.go @@ -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. @@ -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 @@ -35,6 +37,10 @@ func NewAPI(options ...func(*API)) *API { a.mediaEngine = &MediaEngine{} } + if a.interceptor == nil { + a.interceptor = &interceptor.NoOp{} + } + return a } @@ -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() + } +} diff --git a/deleteme.go b/deleteme.go new file mode 100644 index 00000000000..d38775551cd --- /dev/null +++ b/deleteme.go @@ -0,0 +1,74 @@ +package webrtc + +import ( + "github.com/pion/webrtc/v3/pkg/interceptor/movetopionrtp" +) + +func convertRTPParameters(in RTPParameters) movetopionrtp.RTPParameters { + return movetopionrtp.RTPParameters{ + HeaderExtensions: convertHeaderExtensions(in.HeaderExtensions), + Codecs: convertRTPCodecParameters(in.Codecs), + } +} + +func convertHeaderExtensions(in []RTPHeaderExtensionParameter) []movetopionrtp.RTPHeaderExtensionParameter { + result := make([]movetopionrtp.RTPHeaderExtensionParameter, 0, len(in)) + for _, v := range in { + result = append(result, convertHeaderExtension(v)) + } + + return result +} + +func convertHeaderExtension(in RTPHeaderExtensionParameter) movetopionrtp.RTPHeaderExtensionParameter { + return movetopionrtp.RTPHeaderExtensionParameter{ + URI: in.URI, + ID: in.ID, + } +} + +func convertRTPCodecParameters(in []RTPCodecParameters) []movetopionrtp.RTPCodecParameters { + result := make([]movetopionrtp.RTPCodecParameters, 0, len(in)) + for _, v := range in { + result = append(result, convertRTPCodecParameter(v)) + } + + return result +} + +func convertRTPCodecParameter(in RTPCodecParameters) movetopionrtp.RTPCodecParameters { + return movetopionrtp.RTPCodecParameters{ + RTPCodecCapability: movetopionrtp.RTPCodecCapability{ + MimeType: in.MimeType, + ClockRate: in.ClockRate, + Channels: in.Channels, + SDPFmtpLine: in.SDPFmtpLine, + RTCPFeedback: convertRTCPFeedbacks(in.RTCPFeedback), + }, + PayloadType: convertPayloadType(in.PayloadType), + } +} + +func convertRTCPFeedbacks(in []RTCPFeedback) []movetopionrtp.RTCPFeedback { + result := make([]movetopionrtp.RTCPFeedback, 0, len(in)) + for _, v := range in { + result = append(result, convertRTCPFeedback(v)) + } + + return result +} + +func convertRTCPFeedback(in RTCPFeedback) movetopionrtp.RTCPFeedback { + return movetopionrtp.RTCPFeedback{ + Type: in.Type, + Parameter: in.Parameter, + } +} + +func convertPayloadType(in PayloadType) movetopionrtp.PayloadType { + return movetopionrtp.PayloadType(in) +} + +func convertSSRC(in SSRC) movetopionrtp.SSRC { + return movetopionrtp.SSRC(in) +} diff --git a/examples/save-to-disk/main.go b/examples/save-to-disk/main.go index bf05bf24634..f2be3df9193 100644 --- a/examples/save-to-disk/main.go +++ b/examples/save-to-disk/main.go @@ -54,8 +54,13 @@ func main() { panic(err) } + ir := &webrtc.InterceptorRegistry{} + if err := webrtc.RegisterDefaultInterceptors(&m, ir); err != nil { + panic(err) + } + // Create the API object with the MediaEngine - api := webrtc.NewAPI(webrtc.WithMediaEngine(&m)) + api := webrtc.NewAPI(webrtc.WithMediaEngine(&m), webrtc.WithInterceptorRegistry(ir)) // Prepare the configuration config := webrtc.Configuration{ diff --git a/interceptor_registry.go b/interceptor_registry.go new file mode 100644 index 00000000000..9b04e9e7afa --- /dev/null +++ b/interceptor_registry.go @@ -0,0 +1,44 @@ +// +build !js + +package webrtc + +import ( + "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(mediaEngine *MediaEngine, interceptorRegistry *InterceptorRegistry) error { + err := ConfigureNack(mediaEngine, interceptorRegistry) + if err != nil { + return err + } + + return nil +} + +// ConfigureNack will setup everything necessary for handling generating/responding to nack messages. +func ConfigureNack(mediaEngine *MediaEngine, interceptorRegistry *InterceptorRegistry) error { + mediaEngine.RegisterFeedback(RTCPFeedback{Type: "nack"}, RTPCodecTypeVideo) + mediaEngine.RegisterFeedback(RTCPFeedback{Type: "nack", Parameter: "pli"}, RTPCodecTypeVideo) + interceptorRegistry.Add(&interceptor.NACK{}) + return nil +} diff --git a/interceptor_test.go b/interceptor_test.go new file mode 100644 index 00000000000..5667d890dda --- /dev/null +++ b/interceptor_test.go @@ -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) BindLocalTrack(_ *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) BindRemoteTrack(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: uint32(info.SSRC), MediaSSRC: uint32(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") + } +} diff --git a/interceptor_track_local.go b/interceptor_track_local.go new file mode 100644 index 00000000000..cbe0c78f8ec --- /dev/null +++ b/interceptor_track_local.go @@ -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)) +} diff --git a/mediaengine.go b/mediaengine.go index 45d678493f7..5dac7cb5d59 100644 --- a/mediaengine.go +++ b/mediaengine.go @@ -233,6 +233,22 @@ func (m *MediaEngine) RegisterHeaderExtension(extension RTPHeaderExtensionCapabi return nil } +// RegisterFeedback adds feedback mechanism to already registered codecs. +func (m *MediaEngine) RegisterFeedback(feedback RTCPFeedback, typ RTPCodecType) { + switch typ { + case RTPCodecTypeVideo: + for i, v := range m.videoCodecs { + v.RTCPFeedback = append(v.RTCPFeedback, feedback) + m.videoCodecs[i] = v + } + case RTPCodecTypeAudio: + for i, v := range m.audioCodecs { + v.RTCPFeedback = append(v.RTCPFeedback, feedback) + m.audioCodecs[i] = v + } + } +} + // GetHeaderExtensionID returns the negotiated ID for a header extension. // If the Header Extension isn't enabled ok will be false func (m *MediaEngine) GetHeaderExtensionID(extension RTPHeaderExtensionCapability) (val int, audioNegotiated, videoNegotiated bool) { @@ -249,19 +265,19 @@ func (m *MediaEngine) GetHeaderExtensionID(extension RTPHeaderExtensionCapabilit return } -func (m *MediaEngine) getCodecByPayload(payloadType PayloadType) (RTPCodecParameters, error) { +func (m *MediaEngine) getCodecByPayload(payloadType PayloadType) (RTPCodecParameters, RTPCodecType, error) { for _, codec := range m.negotiatedVideoCodecs { if codec.PayloadType == payloadType { - return codec, nil + return codec, RTPCodecTypeVideo, nil } } for _, codec := range m.negotiatedAudioCodecs { if codec.PayloadType == payloadType { - return codec, nil + return codec, RTPCodecTypeAudio, nil } } - return RTPCodecParameters{}, ErrCodecNotFound + return RTPCodecParameters{}, 0, ErrCodecNotFound } func (m *MediaEngine) collectStats(collector *statsReportCollector) { @@ -309,7 +325,7 @@ func (m *MediaEngine) updateCodecParameters(remoteCodec RTPCodecParameters, typ return err } - if _, err = m.getCodecByPayload(PayloadType(payloadType)); err != nil { + if _, _, err = m.getCodecByPayload(PayloadType(payloadType)); err != nil { return nil // not an error, we just ignore this codec we don't support } } @@ -378,8 +394,8 @@ func (m *MediaEngine) updateFromRemoteDescription(desc sdp.SessionDescription) e return err } - for id, extension := range extensions { - if err = m.updateHeaderExtension(extension, id, typ); err != nil { + for extension, id := range extensions { + if err = m.updateHeaderExtension(id, extension, typ); err != nil { return err } } @@ -405,6 +421,39 @@ func (m *MediaEngine) getCodecsByKind(typ RTPCodecType) []RTPCodecParameters { return nil } +func (m *MediaEngine) getRTPParametersByKind(typ RTPCodecType) RTPParameters { + headerExtensions := make([]RTPHeaderExtensionParameter, 0) + for id, e := range m.negotiatedHeaderExtensions { + if e.isAudio && typ == RTPCodecTypeAudio || e.isVideo && typ == RTPCodecTypeVideo { + headerExtensions = append(headerExtensions, RTPHeaderExtensionParameter{ID: id, URI: e.uri}) + } + } + + return RTPParameters{ + HeaderExtensions: headerExtensions, + Codecs: m.getCodecsByKind(typ), + } +} + +func (m *MediaEngine) getRTPParametersByPayloadType(payloadType PayloadType) (RTPParameters, error) { + codec, typ, err := m.getCodecByPayload(payloadType) + if err != nil { + return RTPParameters{}, err + } + + headerExtensions := make([]RTPHeaderExtensionParameter, 0) + for id, e := range m.negotiatedHeaderExtensions { + if e.isAudio && typ == RTPCodecTypeAudio || e.isVideo && typ == RTPCodecTypeVideo { + headerExtensions = append(headerExtensions, RTPHeaderExtensionParameter{ID: id, URI: e.uri}) + } + } + + return RTPParameters{ + HeaderExtensions: headerExtensions, + Codecs: []RTPCodecParameters{codec}, + }, nil +} + func (m *MediaEngine) negotiatedHeaderExtensionsForType(typ RTPCodecType) map[int]mediaEngineHeaderExtension { headerExtensions := map[int]mediaEngineHeaderExtension{} for id, e := range m.negotiatedHeaderExtensions { diff --git a/mediaengine_test.go b/mediaengine_test.go index f3312e876be..b857b2e7549 100644 --- a/mediaengine_test.go +++ b/mediaengine_test.go @@ -63,7 +63,7 @@ a=fmtp:111 minptime=10; useinbandfec=1 assert.False(t, m.negotiatedVideo) assert.True(t, m.negotiatedAudio) - opusCodec, err := m.getCodecByPayload(111) + opusCodec, _, err := m.getCodecByPayload(111) assert.NoError(t, err) assert.Equal(t, opusCodec.MimeType, mimeTypeOpus) }) @@ -85,10 +85,10 @@ a=fmtp:112 minptime=10; useinbandfec=1 assert.False(t, m.negotiatedVideo) assert.True(t, m.negotiatedAudio) - _, err := m.getCodecByPayload(111) + _, _, err := m.getCodecByPayload(111) assert.Error(t, err) - opusCodec, err := m.getCodecByPayload(112) + opusCodec, _, err := m.getCodecByPayload(112) assert.NoError(t, err) assert.Equal(t, opusCodec.MimeType, mimeTypeOpus) }) @@ -110,7 +110,7 @@ a=fmtp:111 minptime=10; useinbandfec=1 assert.False(t, m.negotiatedVideo) assert.True(t, m.negotiatedAudio) - opusCodec, err := m.getCodecByPayload(111) + opusCodec, _, err := m.getCodecByPayload(111) assert.NoError(t, err) assert.Equal(t, opusCodec.MimeType, "audio/OPUS") }) @@ -131,7 +131,7 @@ a=rtpmap:111 opus/48000/2 assert.False(t, m.negotiatedVideo) assert.True(t, m.negotiatedAudio) - opusCodec, err := m.getCodecByPayload(111) + opusCodec, _, err := m.getCodecByPayload(111) assert.NoError(t, err) assert.Equal(t, opusCodec.MimeType, mimeTypeOpus) }) diff --git a/peerconnection.go b/peerconnection.go index aee1360a4b7..f14fd9b7876 100644 --- a/peerconnection.go +++ b/peerconnection.go @@ -19,6 +19,7 @@ import ( "github.com/pion/rtcp" "github.com/pion/sdp/v3" "github.com/pion/webrtc/v3/internal/util" + "github.com/pion/webrtc/v3/pkg/interceptor" "github.com/pion/webrtc/v3/pkg/rtcerr" ) @@ -76,6 +77,8 @@ type PeerConnection struct { // A reference to the associated API state used by this connection api *API log logging.LeveledLogger + + interceptorRTCPWriter interceptor.RTCPWriter } // NewPeerConnection creates a peerconnection with the default @@ -119,6 +122,8 @@ func (api *API) NewPeerConnection(configuration Configuration) (*PeerConnection, log: api.settingEngine.LoggerFactory.NewLogger("pc"), } + pc.interceptorRTCPWriter = api.interceptor.BindRTCPWriter(interceptor.RTCPWriterFunc(pc.writeRTCP)) + var err error if err = pc.initConfiguration(configuration); err != nil { return nil, err @@ -1125,7 +1130,7 @@ func (pc *PeerConnection) startReceiver(incoming trackDetails, receiver *RTPRece return } - codec, err := pc.api.mediaEngine.getCodecByPayload(receiver.Track().PayloadType()) + params, err := pc.api.mediaEngine.getRTPParametersByPayloadType(receiver.Track().PayloadType()) if err != nil { pc.log.Warnf("no codec could be found for payloadType %d", receiver.Track().PayloadType()) return @@ -1133,7 +1138,8 @@ func (pc *PeerConnection) startReceiver(incoming trackDetails, receiver *RTPRece receiver.Track().mu.Lock() receiver.Track().kind = receiver.kind - receiver.Track().codec = codec + receiver.Track().codec = params.Codecs[0] + receiver.Track().params = params receiver.Track().mu.Unlock() pc.onTrack(receiver.Track(), receiver) @@ -1335,7 +1341,7 @@ func (pc *PeerConnection) handleUndeclaredSSRC(rtpStream io.Reader, ssrc SSRC) e continue } - codec, err := pc.api.mediaEngine.getCodecByPayload(payloadType) + params, err := pc.api.mediaEngine.getRTPParametersByPayloadType(payloadType) if err != nil { return err } @@ -1345,7 +1351,7 @@ func (pc *PeerConnection) handleUndeclaredSSRC(rtpStream io.Reader, ssrc SSRC) e continue } - track, err := t.Receiver().receiveForRid(rid, codec, ssrc) + track, err := t.Receiver().receiveForRid(rid, params, ssrc) if err != nil { return err } @@ -1730,28 +1736,33 @@ func (pc *PeerConnection) SetIdentityProvider(provider string) error { return errPeerConnSetIdentityProviderNotImplemented } -// WriteRTCP sends a user provided RTCP packet to the connected peer -// If no peer is connected the packet is discarded +// WriteRTCP sends a user provided RTCP packet to the connected peer. If no peer is connected the +// packet is discarded. It also runs any configured interceptors. func (pc *PeerConnection) WriteRTCP(pkts []rtcp.Packet) error { + _, err := pc.interceptorRTCPWriter.Write(pkts, make(interceptor.Attributes)) + return err +} + +func (pc *PeerConnection) writeRTCP(pkts []rtcp.Packet, _ interceptor.Attributes) (int, error) { raw, err := rtcp.Marshal(pkts) if err != nil { - return err + return 0, err } srtcpSession, err := pc.dtlsTransport.getSRTCPSession() if err != nil { - return nil + return 0, nil } writeStream, err := srtcpSession.OpenWriteStream() if err != nil { - return fmt.Errorf("%w: %v", errPeerConnWriteRTCPOpenWriteStream, err) + return 0, fmt.Errorf("%w: %v", errPeerConnWriteRTCPOpenWriteStream, err) } - if _, err := writeStream.Write(raw); err != nil { - return err + if n, err := writeStream.Write(raw); err != nil { + return n, err } - return nil + return 0, nil } // Close ends the PeerConnection @@ -1775,6 +1786,8 @@ func (pc *PeerConnection) Close() error { // continue the chain the Mux has to be closed. closeErrs := make([]error, 4) + closeErrs = append(closeErrs, pc.api.interceptor.Close()) + // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #4) for _, t := range pc.GetTransceivers() { if !t.stopped { diff --git a/pkg/interceptor/chain.go b/pkg/interceptor/chain.go new file mode 100644 index 00000000000..2a3766d328d --- /dev/null +++ b/pkg/interceptor/chain.go @@ -0,0 +1,83 @@ +// +build !js + +package interceptor + +import ( + "github.com/pion/webrtc/v3/internal/util" +) + +// Chain is an interceptor that runs all child interceptors in order. +type Chain struct { + interceptors []Interceptor +} + +// NewChain returns a new Chain interceptor. +func NewChain(interceptors []Interceptor) *Chain { + return &Chain{interceptors: interceptors} +} + +// BindRTCPReader lets you modify any incoming RTCP packets. It is called once per sender/receiver, however this might +// change in the future. The returned method will be called once per packet batch. +func (i *Chain) BindRTCPReader(reader RTCPReader) RTCPReader { + for _, interceptor := range i.interceptors { + reader = interceptor.BindRTCPReader(reader) + } + + return reader +} + +// BindRTCPWriter lets you modify any outgoing RTCP packets. It is called once per PeerConnection. The returned method +// will be called once per packet batch. +func (i *Chain) BindRTCPWriter(writer RTCPWriter) RTCPWriter { + for _, interceptor := range i.interceptors { + writer = interceptor.BindRTCPWriter(writer) + } + + return writer +} + +// BindLocalStream lets you modify any outgoing RTP packets. It is called once for per LocalStream. The returned method +// will be called once per rtp packet. +func (i *Chain) BindLocalStream(ctx *StreamInfo, writer RTPWriter) RTPWriter { + for _, interceptor := range i.interceptors { + writer = interceptor.BindLocalStream(ctx, writer) + } + + return writer +} + +// UnbindLocalStream is called when the Stream is removed. It can be used to clean up any data related to that track. +func (i *Chain) UnbindLocalStream(ctx *StreamInfo) { + for _, interceptor := range i.interceptors { + interceptor.UnbindLocalStream(ctx) + } +} + +// BindRemoteStream lets you modify any incoming RTP packets. It is called once for per RemoteStream. The returned method +// will be called once per rtp packet. +func (i *Chain) BindRemoteStream(ctx *StreamInfo, reader RTPReader) RTPReader { + for _, interceptor := range i.interceptors { + reader = interceptor.BindRemoteStream(ctx, reader) + } + + return reader +} + +// UnbindRemoteStream is called when the Stream is removed. It can be used to clean up any data related to that track. +func (i *Chain) UnbindRemoteStream(ctx *StreamInfo) { + for _, interceptor := range i.interceptors { + interceptor.UnbindRemoteStream(ctx) + } +} + +// Close closes the Interceptor, cleaning up any data if necessary. +func (i *Chain) Close() error { + var errs []error + for _, interceptor := range i.interceptors { + if err := interceptor.Close(); err != nil { + errs = append(errs, err) + } + } + + return util.FlattenErrs(errs) +} diff --git a/pkg/interceptor/interceptor.go b/pkg/interceptor/interceptor.go new file mode 100644 index 00000000000..047746850fd --- /dev/null +++ b/pkg/interceptor/interceptor.go @@ -0,0 +1,119 @@ +// +build !js + +// Package interceptor contains the Interceptor interface, with some useful interceptors that should be safe to use +// in most cases. +package interceptor + +import ( + "io" + + "github.com/pion/rtcp" + "github.com/pion/rtp" + "github.com/pion/webrtc/v3/pkg/interceptor/movetopionrtp" +) + +// Interceptor can be used to add functionality to you PeerConnections by modifying any incoming/outgoing rtp/rtcp +// packets, or sending your own packets as needed. +type Interceptor interface { + + // BindRTCPReader lets you modify any incoming RTCP packets. It is called once per sender/receiver, however this might + // change in the future. The returned method will be called once per packet batch. + BindRTCPReader(reader RTCPReader) RTCPReader + + // BindRTCPWriter lets you modify any outgoing RTCP packets. It is called once per PeerConnection. The returned method + // will be called once per packet batch. + BindRTCPWriter(writer RTCPWriter) RTCPWriter + + // BindLocalStream lets you modify any outgoing RTP packets. It is called once for per LocalStream. The returned method + // will be called once per rtp packet. + BindLocalStream(info *StreamInfo, writer RTPWriter) RTPWriter + + // UnbindLocalStream is called when the Stream is removed. It can be used to clean up any data related to that track. + UnbindLocalStream(info *StreamInfo) + + // BindRemoteStream lets you modify any incoming RTP packets. It is called once for per RemoteStream. The returned method + // will be called once per rtp packet. + BindRemoteStream(info *StreamInfo, reader RTPReader) RTPReader + + // UnbindRemoteStream is called when the Stream is removed. It can be used to clean up any data related to that track. + UnbindRemoteStream(info *StreamInfo) + + io.Closer +} + +// StreamInfo is the Context passed when a StreamLocal has been Binded/Unbinded from a PeerConnection, and used +// in Interceptors. +type StreamInfo struct { + ID string + Params movetopionrtp.RTPParameters + SSRC movetopionrtp.SSRC +} + +// RTPWriter is used by Interceptor.BindLocalStream. +type RTPWriter interface { + // Write a rtp packet + Write(p *rtp.Packet, attributes Attributes) (int, error) +} + +// RTPReader is used by Interceptor.BindRemoteStream. +type RTPReader interface { + // Read a rtp packet + Read() (*rtp.Packet, Attributes, error) +} + +// RTCPWriter is used by Interceptor.BindRTCPWriter. +type RTCPWriter interface { + // Write a batch of rtcp packets + Write(pkts []rtcp.Packet, attributes Attributes) (int, error) +} + +// RTCPReader is used by Interceptor.BindRTCPReader. +type RTCPReader interface { + // Read a batch of rtcp packets + Read() ([]rtcp.Packet, Attributes, error) +} + +// Attributes are a generic key/value store used by interceptors +type Attributes map[interface{}]interface{} + +// RTPWriterFunc is an adapter for RTPWrite interface +type RTPWriterFunc func(p *rtp.Packet, attributes Attributes) (int, error) + +// RTPReaderFunc is an adapter for RTPReader interface +type RTPReaderFunc func() (*rtp.Packet, Attributes, error) + +// RTCPWriterFunc is an adapter for RTCPWriter interface +type RTCPWriterFunc func(pkts []rtcp.Packet, attributes Attributes) (int, error) + +// RTCPReaderFunc is an adapter for RTCPReader interface +type RTCPReaderFunc func() ([]rtcp.Packet, Attributes, error) + +// Write a rtp packet +func (f RTPWriterFunc) Write(p *rtp.Packet, attributes Attributes) (int, error) { + return f(p, attributes) +} + +// Read a rtp packet +func (f RTPReaderFunc) Read() (*rtp.Packet, Attributes, error) { + return f() +} + +// Write a batch of rtcp packets +func (f RTCPWriterFunc) Write(pkts []rtcp.Packet, attributes Attributes) (int, error) { + return f(pkts, attributes) +} + +// Read a batch of rtcp packets +func (f RTCPReaderFunc) Read() ([]rtcp.Packet, Attributes, error) { + return f() +} + +// Get returns the attribute associated with key. +func (a Attributes) Get(key interface{}) interface{} { + return a[key] +} + +// Set sets the attribute associated with key to the given value. +func (a Attributes) Set(key interface{}, val interface{}) { + a[key] = val +} diff --git a/pkg/interceptor/movetopionrtp/other.go b/pkg/interceptor/movetopionrtp/other.go new file mode 100644 index 00000000000..9536fff3fb0 --- /dev/null +++ b/pkg/interceptor/movetopionrtp/other.go @@ -0,0 +1,16 @@ +package movetopionrtp + +// SSRC represents a synchronization source +// A synchronization source is a randomly chosen +// value meant to be globally unique within a particular +// RTP session. Used to identify a single stream of media. +// +// https://tools.ietf.org/html/rfc3550#section-3 +type SSRC uint32 + +// PayloadType identifies the format of the RTP payload and determines +// its interpretation by the application. Each codec in a RTP Session +// will have a different PayloadType +// +// https://tools.ietf.org/html/rfc3550#section-3 +type PayloadType uint8 diff --git a/pkg/interceptor/movetopionrtp/rtcpfeedback.go b/pkg/interceptor/movetopionrtp/rtcpfeedback.go new file mode 100644 index 00000000000..c2714e11591 --- /dev/null +++ b/pkg/interceptor/movetopionrtp/rtcpfeedback.go @@ -0,0 +1,14 @@ +package movetopionrtp + +// RTCPFeedback signals the connection to use additional RTCP packet types. +// https://draft.ortc.org/#dom-rtcrtcpfeedback +type RTCPFeedback struct { + // Type is the type of feedback. + // see: https://draft.ortc.org/#dom-rtcrtcpfeedback + // valid: ack, ccm, nack, goog-remb, transport-cc + Type string + + // The parameter value depends on the type. + // For example, type="nack" parameter="pli" will send Picture Loss Indicator packets. + Parameter string +} diff --git a/pkg/interceptor/movetopionrtp/rtpcodec.go b/pkg/interceptor/movetopionrtp/rtpcodec.go new file mode 100644 index 00000000000..41ee7bb91a5 --- /dev/null +++ b/pkg/interceptor/movetopionrtp/rtpcodec.go @@ -0,0 +1,39 @@ +// Package movetopionrtp contains stuff +package movetopionrtp + +// RTPCodecCapability provides information about codec capabilities. +// +// https://w3c.github.io/webrtc-pc/#dictionary-rtcrtpcodeccapability-members +type RTPCodecCapability struct { + MimeType string + ClockRate uint32 + Channels uint16 + SDPFmtpLine string + RTCPFeedback []RTCPFeedback +} + +// RTPHeaderExtensionParameter represents a negotiated RFC5285 RTP header extension. +// +// https://w3c.github.io/webrtc-pc/#dictionary-rtcrtpheaderextensionparameters-members +type RTPHeaderExtensionParameter struct { + URI string + ID int +} + +// RTPCodecParameters is a sequence containing the media codecs that an RtpSender +// will choose from, as well as entries for RTX, RED and FEC mechanisms. This also +// includes the PayloadType that has been negotiated +// +// https://w3c.github.io/webrtc-pc/#rtcrtpcodecparameters +type RTPCodecParameters struct { + RTPCodecCapability + PayloadType PayloadType +} + +// RTPParameters is a list of negotiated codecs and header extensions +// +// https://w3c.github.io/webrtc-pc/#dictionary-rtcrtpparameters-members +type RTPParameters struct { + HeaderExtensions []RTPHeaderExtensionParameter + Codecs []RTPCodecParameters +} diff --git a/pkg/interceptor/nack.go b/pkg/interceptor/nack.go new file mode 100644 index 00000000000..4e6131ad5c8 --- /dev/null +++ b/pkg/interceptor/nack.go @@ -0,0 +1,14 @@ +// +build !js + +package interceptor + +// NACK interceptor generates/responds to nack messages. +type NACK struct { + NoOp +} + +// BindRemoteStream lets you modify any incoming RTP packets. It is called once for per RemoteStream. The returned method +// will be called once per rtp packet. +func (n *NACK) BindRemoteStream(_ *StreamInfo, reader RTPReader) RTPReader { + return reader +} diff --git a/pkg/interceptor/noop.go b/pkg/interceptor/noop.go new file mode 100644 index 00000000000..c8c23c26762 --- /dev/null +++ b/pkg/interceptor/noop.go @@ -0,0 +1,42 @@ +// +build !js + +package interceptor + +// NoOp is an Interceptor that does not modify any packets. It can embedded in other interceptors, so it's +// possible to implement only a subset of the methods. +type NoOp struct{} + +// BindRTCPReader lets you modify any incoming RTCP packets. It is called once per sender/receiver, however this might +// change in the future. The returned method will be called once per packet batch. +func (i *NoOp) BindRTCPReader(reader RTCPReader) RTCPReader { + return reader +} + +// BindRTCPWriter lets you modify any outgoing RTCP packets. It is called once per PeerConnection. The returned method +// will be called once per packet batch. +func (i *NoOp) BindRTCPWriter(writer RTCPWriter) RTCPWriter { + return writer +} + +// BindLocalStream lets you modify any outgoing RTP packets. It is called once for per LocalStream. The returned method +// will be called once per rtp packet. +func (i *NoOp) BindLocalStream(_ *StreamInfo, writer RTPWriter) RTPWriter { + return writer +} + +// UnbindLocalStream is called when the Stream is removed. It can be used to clean up any data related to that track. +func (i *NoOp) UnbindLocalStream(_ *StreamInfo) {} + +// BindRemoteStream lets you modify any incoming RTP packets. It is called once for per RemoteStream. The returned method +// will be called once per rtp packet. +func (i *NoOp) BindRemoteStream(_ *StreamInfo, reader RTPReader) RTPReader { + return reader +} + +// UnbindRemoteStream is called when the Stream is removed. It can be used to clean up any data related to that track. +func (i *NoOp) UnbindRemoteStream(_ *StreamInfo) {} + +// Close closes the Interceptor, cleaning up any data if necessary. +func (i *NoOp) Close() error { + return nil +} diff --git a/rtpcodec.go b/rtpcodec.go index c03abd13d86..2d9b6d7600a 100644 --- a/rtpcodec.go +++ b/rtpcodec.go @@ -57,6 +57,14 @@ type RTPHeaderExtensionCapability struct { URI string } +// RTPHeaderExtensionParameter represents a negotiated RFC5285 RTP header extension. +// +// https://w3c.github.io/webrtc-pc/#dictionary-rtcrtpheaderextensionparameters-members +type RTPHeaderExtensionParameter struct { + URI string + ID int +} + // RTPCodecParameters is a sequence containing the media codecs that an RtpSender // will choose from, as well as entries for RTX, RED and FEC mechanisms. This also // includes the PayloadType that has been negotiated @@ -77,6 +85,14 @@ type RTCRtpCapabilities struct { Codecs []RTPCodecCapability } +// RTPParameters is a list of negotiated codecs and header extensions +// +// https://w3c.github.io/webrtc-pc/#dictionary-rtcrtpparameters-members +type RTPParameters struct { + HeaderExtensions []RTPHeaderExtensionParameter + Codecs []RTPCodecParameters +} + // Do a fuzzy find for a codec in the list of codecs // Used for lookup up a codec in an existing list to find a match func codecParametersFuzzySearch(needle RTPCodecParameters, haystack []RTPCodecParameters) (RTPCodecParameters, error) { diff --git a/rtpreceiver.go b/rtpreceiver.go index 1b37f624aa8..6aa0541eacd 100644 --- a/rtpreceiver.go +++ b/rtpreceiver.go @@ -9,6 +9,7 @@ import ( "github.com/pion/rtcp" "github.com/pion/srtp" + "github.com/pion/webrtc/v3/pkg/interceptor" ) // trackStreams maintains a mapping of RTP/RTCP streams to a specific track @@ -31,6 +32,8 @@ type RTPReceiver struct { // A reference to the associated api object api *API + + interceptorRTCPReader interceptor.RTCPReader } // NewRTPReceiver constructs a new RTPReceiver @@ -39,14 +42,17 @@ func (api *API) NewRTPReceiver(kind RTPCodecType, transport *DTLSTransport) (*RT return nil, errRTPReceiverDTLSTransportNil } - return &RTPReceiver{ + r := &RTPReceiver{ kind: kind, transport: transport, api: api, closed: make(chan interface{}), received: make(chan interface{}), tracks: []trackStreams{}, - }, nil + } + r.interceptorRTCPReader = api.interceptor.BindRTCPReader(interceptor.RTCPReaderFunc(r.readRTCP)) + + return r, nil } // Transport returns the currently-configured *DTLSTransport or nil @@ -94,11 +100,12 @@ func (r *RTPReceiver) Receive(parameters RTPReceiveParameters) error { if len(parameters.Encodings) == 1 && parameters.Encodings[0].SSRC != 0 { t := trackStreams{ - track: &TrackRemote{ - kind: r.kind, - ssrc: parameters.Encodings[0].SSRC, - receiver: r, - }, + track: NewTrackRemote( + r.kind, + parameters.Encodings[0].SSRC, + "", + r, + ), } var err error @@ -111,11 +118,12 @@ func (r *RTPReceiver) Receive(parameters RTPReceiveParameters) error { } else { for _, encoding := range parameters.Encodings { r.tracks = append(r.tracks, trackStreams{ - track: &TrackRemote{ - kind: r.kind, - rid: encoding.RID, - receiver: r, - }, + track: NewTrackRemote( + r.kind, + 0, + encoding.RID, + r, + ), }) } } @@ -148,15 +156,27 @@ func (r *RTPReceiver) ReadSimulcast(b []byte, rid string) (n int, err error) { } } -// ReadRTCP is a convenience method that wraps Read and unmarshal for you +// ReadRTCP is a convenience method that wraps Read and unmarshal for you. +// It also runs any configured interceptors. func (r *RTPReceiver) ReadRTCP() ([]rtcp.Packet, error) { + pkts, _, err := r.interceptorRTCPReader.Read() + return pkts, err +} + +// ReadRTCP is a convenience method that wraps Read and unmarshal for you +func (r *RTPReceiver) readRTCP() ([]rtcp.Packet, interceptor.Attributes, error) { b := make([]byte, receiveMTU) i, err := r.Read(b) if err != nil { - return nil, err + return nil, nil, err } - return rtcp.Unmarshal(b[:i]) + pkts, err := rtcp.Unmarshal(b[:i]) + if err != nil { + return nil, nil, err + } + + return pkts, make(interceptor.Attributes), nil } // ReadSimulcastRTCP is a convenience method that wraps ReadSimulcast and unmarshal for you @@ -232,7 +252,7 @@ func (r *RTPReceiver) readRTP(b []byte, reader *TrackRemote) (n int, err error) // receiveForRid is the sibling of Receive expect for RIDs instead of SSRCs // It populates all the internal state for the given RID -func (r *RTPReceiver) receiveForRid(rid string, codec RTPCodecParameters, ssrc SSRC) (*TrackRemote, error) { +func (r *RTPReceiver) receiveForRid(rid string, params RTPParameters, ssrc SSRC) (*TrackRemote, error) { r.mu.Lock() defer r.mu.Unlock() @@ -240,7 +260,8 @@ func (r *RTPReceiver) receiveForRid(rid string, codec RTPCodecParameters, ssrc S if r.tracks[i].track.RID() == rid { r.tracks[i].track.mu.Lock() r.tracks[i].track.kind = r.kind - r.tracks[i].track.codec = codec + r.tracks[i].track.codec = params.Codecs[0] + r.tracks[i].track.params = params r.tracks[i].track.ssrc = ssrc r.tracks[i].track.mu.Unlock() diff --git a/rtpsender.go b/rtpsender.go index 8cfe97d30a1..6661cdd1de3 100644 --- a/rtpsender.go +++ b/rtpsender.go @@ -8,7 +8,9 @@ import ( "github.com/pion/randutil" "github.com/pion/rtcp" + "github.com/pion/rtp" "github.com/pion/srtp" + "github.com/pion/webrtc/v3/pkg/interceptor" ) // RTPSender allows an application to control how a given Track is encoded and transmitted to a remote peer @@ -16,13 +18,12 @@ type RTPSender struct { track TrackLocal rtcpReadStream *srtp.ReadStreamSRTCP - rtpWriteStream *srtp.WriteStreamSRTP + context TrackLocalContext transport *DTLSTransport payloadType PayloadType ssrc SSRC - codec RTPCodecParameters // nolint:godox // TODO(sgotti) remove this when in future we'll avoid replacing @@ -36,6 +37,8 @@ type RTPSender struct { mu sync.RWMutex sendCalled, stopCalled chan interface{} + + interceptorRTCPReader interceptor.RTCPReader } // NewRTPSender constructs a new RTPSender @@ -51,7 +54,7 @@ func (api *API) NewRTPSender(track TrackLocal, transport *DTLSTransport) (*RTPSe return nil, err } - return &RTPSender{ + r := &RTPSender{ track: track, transport: transport, api: api, @@ -59,7 +62,10 @@ func (api *API) NewRTPSender(track TrackLocal, transport *DTLSTransport) (*RTPSe stopCalled: make(chan interface{}), ssrc: SSRC(randutil.NewMathRandomGenerator().Uint32()), id: id, - }, nil + } + r.interceptorRTCPReader = api.interceptor.BindRTCPReader(interceptor.RTCPReaderFunc(r.readRTCP)) + + return r, nil } func (r *RTPSender) isNegotiated() bool { @@ -97,11 +103,7 @@ func (r *RTPSender) ReplaceTrack(track TrackLocal) error { defer r.mu.Unlock() if r.hasSent() { - if err := r.track.Unbind(TrackLocalContext{ - id: r.id, - ssrc: r.ssrc, - writeStream: r.rtpWriteStream, - }); err != nil { + if err := r.track.Unbind(r.context); err != nil { return err } } @@ -111,12 +113,7 @@ func (r *RTPSender) ReplaceTrack(track TrackLocal) error { return nil } - if _, err := track.Bind(TrackLocalContext{ - id: r.id, - codecs: []RTPCodecParameters{r.codec}, - ssrc: r.ssrc, - writeStream: r.rtpWriteStream, - }); err != nil { + if _, err := track.Bind(r.context); err != nil { return err } @@ -148,18 +145,38 @@ func (r *RTPSender) Send(parameters RTPSendParameters) error { return err } - if r.rtpWriteStream, err = srtpSession.OpenWriteStream(); err != nil { + rtpWriteStream, err := srtpSession.OpenWriteStream() + if err != nil { return err } - if r.codec, err = r.track.Bind(TrackLocalContext{ + writeStream := &interceptorTrackLocalWriter{TrackLocalWriter: rtpWriteStream} + + r.context = TrackLocalContext{ id: r.id, - codecs: r.api.mediaEngine.getCodecsByKind(r.track.Kind()), + params: r.api.mediaEngine.getRTPParametersByKind(r.track.Kind()), ssrc: parameters.Encodings.SSRC, - writeStream: r.rtpWriteStream, - }); err != nil { + writeStream: writeStream, + } + + codec, err := r.track.Bind(r.context) + if err != nil { return err } + r.context.params.Codecs = []RTPCodecParameters{codec} + + info := &interceptor.StreamInfo{ + ID: r.context.id, + Params: convertRTPParameters(r.context.params), + SSRC: convertSSRC(r.context.ssrc), + } + writeStream.setRTPWriter( + r.api.interceptor.BindLocalStream( + info, + interceptor.RTPWriterFunc(func(p *rtp.Packet, attributes interceptor.Attributes) (int, error) { + return rtpWriteStream.WriteRTP(&p.Header, p.Payload) + }), + )) close(r.sendCalled) return nil @@ -194,15 +211,26 @@ func (r *RTPSender) Read(b []byte) (n int, err error) { } } -// ReadRTCP is a convenience method that wraps Read and unmarshals for you +// ReadRTCP is a convenience method that wraps Read and unmarshals for you. +// It also runs any configured interceptors. func (r *RTPSender) ReadRTCP() ([]rtcp.Packet, error) { + pkts, _, err := r.interceptorRTCPReader.Read() + return pkts, err +} + +func (r *RTPSender) readRTCP() ([]rtcp.Packet, interceptor.Attributes, error) { b := make([]byte, receiveMTU) i, err := r.Read(b) if err != nil { - return nil, err + return nil, nil, err + } + + pkts, err := rtcp.Unmarshal(b[:i]) + if err != nil { + return nil, nil, err } - return rtcp.Unmarshal(b[:i]) + return pkts, make(interceptor.Attributes), nil } // hasSent tells if data has been ever sent for this instance diff --git a/track_local.go b/track_local.go index 1b232b9517e..e6e1da1f490 100644 --- a/track_local.go +++ b/track_local.go @@ -11,10 +11,11 @@ type TrackLocalWriter interface { Write(b []byte) (int, error) } -// TrackLocalContext is the Context passed when a TrackLocal has been Binded/Unbinded from a PeerConnection +// TrackLocalContext is the Context passed when a TrackLocal has been Binded/Unbinded from a PeerConnection, and used +// in Interceptors. type TrackLocalContext struct { id string - codecs []RTPCodecParameters + params RTPParameters ssrc SSRC writeStream TrackLocalWriter } @@ -22,7 +23,13 @@ type TrackLocalContext struct { // CodecParameters returns the negotiated RTPCodecParameters. These are the codecs supported by both // PeerConnections and the SSRC/PayloadTypes func (t *TrackLocalContext) CodecParameters() []RTPCodecParameters { - return t.codecs + return t.params.Codecs +} + +// HeaderExtensions returns the negotiated RTPHeaderExtensionParameters. These are the header extensions supported by +// both PeerConnections and the SSRC/PayloadTypes +func (t *TrackLocalContext) HeaderExtensions() []RTPHeaderExtensionParameter { + return t.params.HeaderExtensions } // SSRC requires the negotiated SSRC of this track diff --git a/track_remote.go b/track_remote.go index dd48e3a8451..c59f5e650a5 100644 --- a/track_remote.go +++ b/track_remote.go @@ -6,6 +6,7 @@ import ( "sync" "github.com/pion/rtp" + "github.com/pion/webrtc/v3/pkg/interceptor" ) // TrackRemote represents a single inbound source of media @@ -19,10 +20,30 @@ type TrackRemote struct { kind RTPCodecType ssrc SSRC codec RTPCodecParameters + params RTPParameters rid string receiver *RTPReceiver peeked []byte + + interceptorRTPReader interceptor.RTPReader +} + +// NewTrackRemote creates a new TrackRemote. +func NewTrackRemote(kind RTPCodecType, ssrc SSRC, rid string, receiver *RTPReceiver) *TrackRemote { + t := &TrackRemote{ + kind: kind, + ssrc: ssrc, + rid: rid, + receiver: receiver, + } + t.interceptorRTPReader = t.receiver.api.interceptor.BindRemoteStream(&interceptor.StreamInfo{ + ID: t.id, + Params: convertRTPParameters(t.params), + SSRC: convertSSRC(t.ssrc), + }, interceptor.RTPReaderFunc(t.readRTP)) + + return t } // ID is the unique identifier for this Track. This should be unique for the @@ -125,19 +146,25 @@ func (t *TrackRemote) peek(b []byte) (n int, err error) { return } -// ReadRTP is a convenience method that wraps Read and unmarshals for you +// ReadRTP is a convenience method that wraps Read and unmarshals for you. +// It also runs any configured interceptors. func (t *TrackRemote) ReadRTP() (*rtp.Packet, error) { + p, _, err := t.interceptorRTPReader.Read() + return p, err +} + +func (t *TrackRemote) readRTP() (*rtp.Packet, interceptor.Attributes, error) { b := make([]byte, receiveMTU) i, err := t.Read(b) if err != nil { - return nil, err + return nil, nil, err } r := &rtp.Packet{} if err := r.Unmarshal(b[:i]); err != nil { - return nil, err + return nil, nil, err } - return r, nil + return r, make(interceptor.Attributes), nil } // determinePayloadType blocks and reads a single packet to determine the PayloadType for this Track