interceptor.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // The MIT License (MIT)
  2. //
  3. // # Copyright (c) 2021 Winlin
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. // this software and associated documentation files (the "Software"), to deal in
  7. // the Software without restriction, including without limitation the rights to
  8. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. // the Software, and to permit persons to whom the Software is furnished to do so,
  10. // subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. package janus
  22. import (
  23. "github.com/pion/interceptor"
  24. "github.com/pion/rtcp"
  25. "github.com/pion/rtp"
  26. )
  27. type rtpInterceptorOptionFunc func(i *rtpInterceptor)
  28. type rtpInteceptorFactory struct {
  29. p *rtpInterceptor
  30. }
  31. func (v *rtpInteceptorFactory) NewInterceptor(id string) (interceptor.Interceptor, error) {
  32. return v.p, nil
  33. }
  34. // Common RTP packet interceptor for benchmark.
  35. // @remark Should never merge with rtcpInterceptor, because they has the same Write interface.
  36. type rtpInterceptor struct {
  37. // If rtpReader is nil, use the default next one to read.
  38. rtpReader interceptor.RTPReaderFunc
  39. nextRTPReader interceptor.RTPReader
  40. // If rtpWriter is nil, use the default next one to write.
  41. rtpWriter interceptor.RTPWriterFunc
  42. nextRTPWriter interceptor.RTPWriter
  43. // Other common fields.
  44. bypassInterceptor
  45. }
  46. func newRTPInterceptor(options ...rtpInterceptorOptionFunc) *rtpInteceptorFactory {
  47. v := &rtpInterceptor{}
  48. for _, opt := range options {
  49. opt(v)
  50. }
  51. return &rtpInteceptorFactory{v}
  52. }
  53. func (v *rtpInterceptor) BindLocalStream(info *interceptor.StreamInfo, writer interceptor.RTPWriter) interceptor.RTPWriter {
  54. v.nextRTPWriter = writer
  55. return v // Handle all RTP
  56. }
  57. func (v *rtpInterceptor) Write(header *rtp.Header, payload []byte, attributes interceptor.Attributes) (int, error) {
  58. if v.rtpWriter != nil {
  59. return v.rtpWriter(header, payload, attributes)
  60. }
  61. return v.nextRTPWriter.Write(header, payload, attributes)
  62. }
  63. func (v *rtpInterceptor) UnbindLocalStream(info *interceptor.StreamInfo) {
  64. }
  65. func (v *rtpInterceptor) BindRemoteStream(info *interceptor.StreamInfo, reader interceptor.RTPReader) interceptor.RTPReader {
  66. v.nextRTPReader = reader
  67. return v // Handle all RTP
  68. }
  69. func (v *rtpInterceptor) Read(b []byte, a interceptor.Attributes) (int, interceptor.Attributes, error) {
  70. if v.rtpReader != nil {
  71. return v.rtpReader(b, a)
  72. }
  73. return v.nextRTPReader.Read(b, a)
  74. }
  75. func (v *rtpInterceptor) UnbindRemoteStream(info *interceptor.StreamInfo) {
  76. }
  77. type rtcpInterceptorOptionFunc func(i *rtcpInterceptor)
  78. type rtcpInteceptorFactory struct {
  79. p *rtcpInterceptor
  80. }
  81. func (v *rtcpInteceptorFactory) NewInterceptor(id string) (interceptor.Interceptor, error) {
  82. return v.p, nil
  83. }
  84. // Common RTCP packet interceptor for benchmark.
  85. // @remark Should never merge with rtpInterceptor, because they has the same Write interface.
  86. type rtcpInterceptor struct {
  87. // If rtcpReader is nil, use the default next one to read.
  88. rtcpReader interceptor.RTCPReaderFunc
  89. nextRTCPReader interceptor.RTCPReader
  90. // If rtcpWriter is nil, use the default next one to write.
  91. rtcpWriter interceptor.RTCPWriterFunc
  92. nextRTCPWriter interceptor.RTCPWriter
  93. // Other common fields.
  94. bypassInterceptor
  95. }
  96. func newRTCPInterceptor(options ...rtcpInterceptorOptionFunc) *rtcpInteceptorFactory {
  97. v := &rtcpInterceptor{}
  98. for _, opt := range options {
  99. opt(v)
  100. }
  101. return &rtcpInteceptorFactory{v}
  102. }
  103. func (v *rtcpInterceptor) BindRTCPReader(reader interceptor.RTCPReader) interceptor.RTCPReader {
  104. v.nextRTCPReader = reader
  105. return v // Handle all RTCP
  106. }
  107. func (v *rtcpInterceptor) Read(b []byte, a interceptor.Attributes) (int, interceptor.Attributes, error) {
  108. if v.rtcpReader != nil {
  109. return v.rtcpReader(b, a)
  110. }
  111. return v.nextRTCPReader.Read(b, a)
  112. }
  113. func (v *rtcpInterceptor) BindRTCPWriter(writer interceptor.RTCPWriter) interceptor.RTCPWriter {
  114. v.nextRTCPWriter = writer
  115. return v // Handle all RTCP
  116. }
  117. func (v *rtcpInterceptor) Write(pkts []rtcp.Packet, attributes interceptor.Attributes) (int, error) {
  118. if v.rtcpWriter != nil {
  119. return v.rtcpWriter(pkts, attributes)
  120. }
  121. return v.nextRTCPWriter.Write(pkts, attributes)
  122. }
  123. // Do nothing.
  124. type bypassInterceptor struct {
  125. interceptor.Interceptor
  126. }
  127. func (v *bypassInterceptor) BindRTCPReader(reader interceptor.RTCPReader) interceptor.RTCPReader {
  128. return reader
  129. }
  130. func (v *bypassInterceptor) BindRTCPWriter(writer interceptor.RTCPWriter) interceptor.RTCPWriter {
  131. return writer
  132. }
  133. func (v *bypassInterceptor) BindLocalStream(info *interceptor.StreamInfo, writer interceptor.RTPWriter) interceptor.RTPWriter {
  134. return writer
  135. }
  136. func (v *bypassInterceptor) UnbindLocalStream(info *interceptor.StreamInfo) {
  137. }
  138. func (v *bypassInterceptor) BindRemoteStream(info *interceptor.StreamInfo, reader interceptor.RTPReader) interceptor.RTPReader {
  139. return reader
  140. }
  141. func (v *bypassInterceptor) UnbindRemoteStream(info *interceptor.StreamInfo) {
  142. }
  143. func (v *bypassInterceptor) Close() error {
  144. return nil
  145. }