-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide API so that handling around RTP can be easily defined by the user. See the design doc here[0] [0] pion/webrtc-v3-design#34
- Loading branch information
Showing
14 changed files
with
382 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// +build !js | ||
|
||
package webrtc | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/pion/rtcp" | ||
"github.com/pion/rtp" | ||
) | ||
|
||
type WriteRTP func(p *rtp.Packet, attributes map[interface{}]interface{}) (int, error) | ||
type ReadRTP func() (*rtp.Packet, map[interface{}]interface{}, error) | ||
type WriteRTCP func(pkts []rtcp.Packet, attributes map[interface{}]interface{}) (int, error) | ||
type ReadRTCP func() ([]rtcp.Packet, map[interface{}]interface{}, error) | ||
|
||
type Interceptor interface { | ||
BindReadRTCP(read ReadRTCP) ReadRTCP // TODO: call this | ||
BindWriteRTCP(write WriteRTCP) WriteRTCP // TODO: call this | ||
|
||
BindLocalTrack(ctx *TrackLocalContext, write WriteRTP) WriteRTP | ||
UnbindLocalTrack(ctx *TrackLocalContext) | ||
|
||
BindRemoteTrack(ctx *TrackRemoteContext, read ReadRTP) ReadRTP | ||
UnbindRemoteTrack(ctx *TrackRemoteContext) | ||
|
||
io.Closer | ||
} | ||
|
||
type DummyInterceptor struct{} | ||
|
||
func (d *DummyInterceptor) BindReadRTCP(read ReadRTCP) ReadRTCP { | ||
return read | ||
} | ||
|
||
func (d *DummyInterceptor) BindWriteRTCP(write WriteRTCP) WriteRTCP { | ||
return write | ||
} | ||
|
||
func (d *DummyInterceptor) BindLocalTrack(_ *TrackLocalContext, write WriteRTP) WriteRTP { | ||
return write | ||
} | ||
|
||
func (d *DummyInterceptor) UnbindLocalTrack(_ *TrackLocalContext) {} | ||
|
||
func (d *DummyInterceptor) BindRemoteTrack(_ *TrackRemoteContext, read ReadRTP) ReadRTP { | ||
return read | ||
} | ||
|
||
func (d *DummyInterceptor) UnbindRemoteTrack(_ *TrackRemoteContext) {} | ||
|
||
func (d *DummyInterceptor) Close() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// +build !js | ||
|
||
package webrtc | ||
|
||
import ( | ||
"github.com/pion/logging" | ||
) | ||
|
||
type interceptorChain struct { | ||
interceptors []Interceptor | ||
log logging.LeveledLogger | ||
} | ||
|
||
func newInterceptorChain(interceptors []Interceptor, log logging.LeveledLogger) Interceptor { | ||
return &interceptorChain{interceptors: interceptors, log: log} | ||
} | ||
|
||
func (i *interceptorChain) BindReadRTCP(read ReadRTCP) ReadRTCP { | ||
for _, interceptor := range i.interceptors { | ||
read = interceptor.BindReadRTCP(read) | ||
} | ||
|
||
return read | ||
} | ||
|
||
func (i *interceptorChain) BindWriteRTCP(write WriteRTCP) WriteRTCP { | ||
for _, interceptor := range i.interceptors { | ||
write = interceptor.BindWriteRTCP(write) | ||
} | ||
|
||
return write | ||
} | ||
|
||
func (i *interceptorChain) BindLocalTrack(ctx *TrackLocalContext, write WriteRTP) WriteRTP { | ||
for _, interceptor := range i.interceptors { | ||
write = interceptor.BindLocalTrack(ctx, write) | ||
} | ||
|
||
return write | ||
} | ||
|
||
func (i *interceptorChain) UnbindLocalTrack(ctx *TrackLocalContext) { | ||
for _, interceptor := range i.interceptors { | ||
interceptor.UnbindLocalTrack(ctx) | ||
} | ||
} | ||
|
||
func (i *interceptorChain) BindRemoteTrack(ctx *TrackRemoteContext, read ReadRTP) ReadRTP { | ||
for _, interceptor := range i.interceptors { | ||
read = interceptor.BindRemoteTrack(ctx, read) | ||
} | ||
|
||
return read | ||
} | ||
|
||
func (i *interceptorChain) UnbindRemoteTrack(ctx *TrackRemoteContext) { | ||
for _, interceptor := range i.interceptors { | ||
interceptor.UnbindRemoteTrack(ctx) | ||
} | ||
} | ||
|
||
func (i *interceptorChain) Close() error { | ||
var err error | ||
for _, interceptor := range i.interceptors { | ||
if err2 := interceptor.Close(); err2 != nil { | ||
if err == nil { | ||
err = err2 | ||
} else { | ||
i.log.Warnf("failed closing interceptor: %+v", err2) | ||
} | ||
} | ||
} | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// +build !js | ||
|
||
package webrtc | ||
|
||
import ( | ||
"github.com/pion/rtcp" | ||
) | ||
|
||
func WrapPeerConnection(pc *PeerConnection, interceptor Interceptor) *PeerConnection { | ||
if interceptor == nil { | ||
return pc | ||
} | ||
|
||
writeRTCP := interceptor.BindWriteRTCP(func(pkts []rtcp.Packet, attributes map[interface{}]interface{}) (int, error) { | ||
return pc.writeRTCP(pkts) | ||
}) | ||
|
||
pc.interceptorWriteRTCP = func(pkts []rtcp.Packet) error { | ||
_, err := writeRTCP(pkts, make(map[interface{}]interface{})) | ||
return err | ||
} | ||
|
||
return pc | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// +build !js | ||
|
||
package webrtc | ||
|
||
import ( | ||
"github.com/pion/rtp" | ||
) | ||
|
||
type interceptorChainTrackLocalWrapper struct { | ||
TrackLocal | ||
interceptor Interceptor | ||
} | ||
|
||
type interceptorTrackLocalWriter struct { | ||
TrackLocalWriter | ||
writeRTP WriteRTP | ||
} | ||
|
||
func WrapTrackLocal(track TrackLocal, interceptor Interceptor) TrackLocal { | ||
if interceptor == nil { | ||
return track | ||
} | ||
|
||
return &interceptorChainTrackLocalWrapper{TrackLocal: track, interceptor: interceptor} | ||
} | ||
|
||
func (i *interceptorTrackLocalWriter) WriteRTP(header *rtp.Header, payload []byte) (int, error) { | ||
return i.writeRTP(&rtp.Packet{Header: *header, Payload: payload}, make(map[interface{}]interface{})) | ||
} | ||
|
||
func (i *interceptorChainTrackLocalWrapper) Bind(context TrackLocalContext) error { | ||
trackWriteStream := context.WriteStream() | ||
writeRTP := i.interceptor.BindLocalTrack(&context, func(p *rtp.Packet, attributes map[interface{}]interface{}) (int, error) { | ||
return trackWriteStream.WriteRTP(&p.Header, p.Payload) | ||
}) | ||
|
||
context.writeStream = &interceptorTrackLocalWriter{TrackLocalWriter: trackWriteStream, writeRTP: writeRTP} | ||
|
||
return i.TrackLocal.Bind(context) | ||
} | ||
|
||
func (i *interceptorChainTrackLocalWrapper) Unbind(context TrackLocalContext) error { | ||
i.interceptor.UnbindLocalTrack(&context) | ||
|
||
return i.TrackLocal.Unbind(context) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// +build !js | ||
|
||
package webrtc | ||
|
||
import ( | ||
"github.com/pion/rtp" | ||
) | ||
|
||
func WrapTrackRemote(track *TrackRemote, interceptor Interceptor) *TrackRemote { | ||
if interceptor == nil { | ||
return track | ||
} | ||
|
||
readRTP := interceptor.BindRemoteTrack(track.context(), func() (*rtp.Packet, map[interface{}]interface{}, error) { | ||
p, err := track.readRTP() | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
return p, make(map[interface{}]interface{}), nil | ||
}) | ||
|
||
track.interceptorReadRTP = func() (*rtp.Packet, error) { | ||
p, _, err := readRTP() | ||
return p, err | ||
} | ||
|
||
return track | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.