interceptor.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Package interceptor contains the Interceptor interface, with some useful interceptors that should be safe to use
  2. // in most cases.
  3. package interceptor
  4. import (
  5. "io"
  6. "github.com/pion/rtcp"
  7. "github.com/pion/rtp"
  8. )
  9. // Factory provides an interface for constructing interceptors
  10. type Factory interface {
  11. NewInterceptor(id string) (Interceptor, error)
  12. }
  13. // Interceptor can be used to add functionality to you PeerConnections by modifying any incoming/outgoing rtp/rtcp
  14. // packets, or sending your own packets as needed.
  15. type Interceptor interface {
  16. // BindRTCPReader lets you modify any incoming RTCP packets. It is called once per sender/receiver, however this might
  17. // change in the future. The returned method will be called once per packet batch.
  18. BindRTCPReader(reader RTCPReader) RTCPReader
  19. // BindRTCPWriter lets you modify any outgoing RTCP packets. It is called once per PeerConnection. The returned method
  20. // will be called once per packet batch.
  21. BindRTCPWriter(writer RTCPWriter) RTCPWriter
  22. // BindLocalStream lets you modify any outgoing RTP packets. It is called once for per LocalStream. The returned method
  23. // will be called once per rtp packet.
  24. BindLocalStream(info *StreamInfo, writer RTPWriter) RTPWriter
  25. // UnbindLocalStream is called when the Stream is removed. It can be used to clean up any data related to that track.
  26. UnbindLocalStream(info *StreamInfo)
  27. // BindRemoteStream lets you modify any incoming RTP packets. It is called once for per RemoteStream. The returned method
  28. // will be called once per rtp packet.
  29. BindRemoteStream(info *StreamInfo, reader RTPReader) RTPReader
  30. // UnbindRemoteStream is called when the Stream is removed. It can be used to clean up any data related to that track.
  31. UnbindRemoteStream(info *StreamInfo)
  32. io.Closer
  33. }
  34. // RTPWriter is used by Interceptor.BindLocalStream.
  35. type RTPWriter interface {
  36. // Write a rtp packet
  37. Write(header *rtp.Header, payload []byte, attributes Attributes) (int, error)
  38. }
  39. // RTPReader is used by Interceptor.BindRemoteStream.
  40. type RTPReader interface {
  41. // Read a rtp packet
  42. Read([]byte, Attributes) (int, Attributes, error)
  43. }
  44. // RTCPWriter is used by Interceptor.BindRTCPWriter.
  45. type RTCPWriter interface {
  46. // Write a batch of rtcp packets
  47. Write(pkts []rtcp.Packet, attributes Attributes) (int, error)
  48. }
  49. // RTCPReader is used by Interceptor.BindRTCPReader.
  50. type RTCPReader interface {
  51. // Read a batch of rtcp packets
  52. Read([]byte, Attributes) (int, Attributes, error)
  53. }
  54. // RTPWriterFunc is an adapter for RTPWrite interface
  55. type RTPWriterFunc func(header *rtp.Header, payload []byte, attributes Attributes) (int, error)
  56. // RTPReaderFunc is an adapter for RTPReader interface
  57. type RTPReaderFunc func([]byte, Attributes) (int, Attributes, error)
  58. // RTCPWriterFunc is an adapter for RTCPWriter interface
  59. type RTCPWriterFunc func(pkts []rtcp.Packet, attributes Attributes) (int, error)
  60. // RTCPReaderFunc is an adapter for RTCPReader interface
  61. type RTCPReaderFunc func([]byte, Attributes) (int, Attributes, error)
  62. // Write a rtp packet
  63. func (f RTPWriterFunc) Write(header *rtp.Header, payload []byte, attributes Attributes) (int, error) {
  64. return f(header, payload, attributes)
  65. }
  66. // Read a rtp packet
  67. func (f RTPReaderFunc) Read(b []byte, a Attributes) (int, Attributes, error) {
  68. return f(b, a)
  69. }
  70. // Write a batch of rtcp packets
  71. func (f RTCPWriterFunc) Write(pkts []rtcp.Packet, attributes Attributes) (int, error) {
  72. return f(pkts, attributes)
  73. }
  74. // Read a batch of rtcp packets
  75. func (f RTCPReaderFunc) Read(b []byte, a Attributes) (int, Attributes, error) {
  76. return f(b, a)
  77. }