Skip to content

Commit

Permalink
review modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
masterada committed Nov 23, 2020
1 parent c5da1c6 commit e11ebaa
Show file tree
Hide file tree
Showing 18 changed files with 497 additions and 231 deletions.
5 changes: 3 additions & 2 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,7 +14,7 @@ import (
type API struct {
settingEngine *SettingEngine
mediaEngine *MediaEngine
interceptor Interceptor
interceptor interceptor.Interceptor
}

// NewAPI Creates a new API object for keeping semi-global settings to WebRTC objects
Expand All @@ -37,7 +38,7 @@ func NewAPI(options ...func(*API)) *API {
}

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

return a
Expand Down
74 changes: 74 additions & 0 deletions deleteme.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 1 addition & 2 deletions examples/save-to-disk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/pion/rtcp"
"github.com/pion/webrtc/v3"
"github.com/pion/webrtc/v3/examples/internal/signal"
"github.com/pion/webrtc/v3/pkg/interceptor"
"github.com/pion/webrtc/v3/pkg/media"
"github.com/pion/webrtc/v3/pkg/media/ivfwriter"
"github.com/pion/webrtc/v3/pkg/media/oggwriter"
Expand Down Expand Up @@ -56,7 +55,7 @@ func main() {
}

ir := &webrtc.InterceptorRegistry{}
if err := interceptor.RegisterDefaults(&m, ir); err != nil {
if err := webrtc.RegisterDefaultInterceptors(&m, ir); err != nil {
panic(err)
}

Expand Down
90 changes: 0 additions & 90 deletions interceptor.go

This file was deleted.

35 changes: 29 additions & 6 deletions interceptor_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,43 @@

package webrtc

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

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

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

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

return &chainInterceptor{interceptors: i.interceptors}
return interceptor.NewChain(i.interceptors)
}

// RegisterDefaults 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
}
43 changes: 22 additions & 21 deletions interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,33 @@ import (
"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
writeRTCP atomic.Value
rtcpWriter atomic.Value
lastRTCP atomic.Value
NoOpInterceptor
interceptor.NoOp
}

func (t *testInterceptor) BindLocalTrack(_ *TrackLocalContext, write WriteRTP) WriteRTP {
return func(p *rtp.Packet, attributes map[interface{}]interface{}) (int, error) {
func (t *testInterceptor) BindLocalTrack(_ *interceptor.TrackInfo, 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 write(p, attributes)
}
return writer.Write(p, attributes)
})
}

func (t *testInterceptor) BindRemoteTrack(ctx *TrackRemoteContext, read ReadRTP) ReadRTP {
return func() (*rtp.Packet, map[interface{}]interface{}, error) {
p, attributes, err := read()
func (t *testInterceptor) BindRemoteTrack(info *interceptor.TrackInfo, 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
}
Expand All @@ -46,36 +47,36 @@ func (t *testInterceptor) BindRemoteTrack(ctx *TrackRemoteContext, read ReadRTP)
assert.NoError(t.t, p.Header.SetExtension(t.extensionID, []byte("read")))

// write back a pli
writeRTCP := t.writeRTCP.Load().(WriteRTCP)
pli := &rtcp.PictureLossIndication{SenderSSRC: uint32(ctx.SSRC()), MediaSSRC: uint32(ctx.SSRC())}
_, err = writeRTCP([]rtcp.Packet{pli}, make(map[interface{}]interface{}))
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) BindReadRTCP(read ReadRTCP) ReadRTCP {
return func() ([]rtcp.Packet, map[interface{}]interface{}, error) {
pkts, attributes, err := read()
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) BindWriteRTCP(write WriteRTCP) WriteRTCP {
t.writeRTCP.Store(write)
return write
func (t *testInterceptor) BindRTCPWriter(writer interceptor.RTCPWriter) interceptor.RTCPWriter {
t.rtcpWriter.Store(writer)
return writer
}

func TestPeerConnection_Interceptor(t *testing.T) {
Expand All @@ -85,7 +86,7 @@ func TestPeerConnection_Interceptor(t *testing.T) {
report := test.CheckRoutines(t)
defer report()

createPC := func(interceptor Interceptor) *PeerConnection {
createPC := func(interceptor interceptor.Interceptor) *PeerConnection {
m := &MediaEngine{}
err := m.RegisterDefaultCodecs()
if err != nil {
Expand Down
13 changes: 7 additions & 6 deletions interceptor_track_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@ import (
"sync/atomic"

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

type interceptorTrackLocalWriter struct {
TrackLocalWriter
writeRTP atomic.Value
rtpWriter atomic.Value
}

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

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

if writeRTP == nil {
if writer == nil {
return 0, nil
}

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

0 comments on commit e11ebaa

Please sign in to comment.