chain.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package interceptor
  4. // Chain is an interceptor that runs all child interceptors in order.
  5. type Chain struct {
  6. interceptors []Interceptor
  7. }
  8. // NewChain returns a new Chain interceptor.
  9. func NewChain(interceptors []Interceptor) *Chain {
  10. return &Chain{interceptors: interceptors}
  11. }
  12. // BindRTCPReader lets you modify any incoming RTCP packets. It is called once per sender/receiver, however this might
  13. // change in the future. The returned method will be called once per packet batch.
  14. func (i *Chain) BindRTCPReader(reader RTCPReader) RTCPReader {
  15. for _, interceptor := range i.interceptors {
  16. reader = interceptor.BindRTCPReader(reader)
  17. }
  18. return reader
  19. }
  20. // BindRTCPWriter lets you modify any outgoing RTCP packets. It is called once per PeerConnection. The returned method
  21. // will be called once per packet batch.
  22. func (i *Chain) BindRTCPWriter(writer RTCPWriter) RTCPWriter {
  23. for _, interceptor := range i.interceptors {
  24. writer = interceptor.BindRTCPWriter(writer)
  25. }
  26. return writer
  27. }
  28. // BindLocalStream lets you modify any outgoing RTP packets. It is called once for per LocalStream. The returned method
  29. // will be called once per rtp packet.
  30. func (i *Chain) BindLocalStream(ctx *StreamInfo, writer RTPWriter) RTPWriter {
  31. for _, interceptor := range i.interceptors {
  32. writer = interceptor.BindLocalStream(ctx, writer)
  33. }
  34. return writer
  35. }
  36. // UnbindLocalStream is called when the Stream is removed. It can be used to clean up any data related to that track.
  37. func (i *Chain) UnbindLocalStream(ctx *StreamInfo) {
  38. for _, interceptor := range i.interceptors {
  39. interceptor.UnbindLocalStream(ctx)
  40. }
  41. }
  42. // BindRemoteStream lets you modify any incoming RTP packets. It is called once for per RemoteStream. The returned method
  43. // will be called once per rtp packet.
  44. func (i *Chain) BindRemoteStream(ctx *StreamInfo, reader RTPReader) RTPReader {
  45. for _, interceptor := range i.interceptors {
  46. reader = interceptor.BindRemoteStream(ctx, reader)
  47. }
  48. return reader
  49. }
  50. // UnbindRemoteStream is called when the Stream is removed. It can be used to clean up any data related to that track.
  51. func (i *Chain) UnbindRemoteStream(ctx *StreamInfo) {
  52. for _, interceptor := range i.interceptors {
  53. interceptor.UnbindRemoteStream(ctx)
  54. }
  55. }
  56. // Close closes the Interceptor, cleaning up any data if necessary.
  57. func (i *Chain) Close() error {
  58. var errs []error
  59. for _, interceptor := range i.interceptors {
  60. errs = append(errs, interceptor.Close())
  61. }
  62. return flattenErrs(errs)
  63. }