track_local.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package webrtc
  4. import (
  5. "github.com/pion/interceptor"
  6. "github.com/pion/rtp"
  7. )
  8. // TrackLocalWriter is the Writer for outbound RTP Packets
  9. type TrackLocalWriter interface {
  10. // WriteRTP encrypts a RTP packet and writes to the connection
  11. WriteRTP(header *rtp.Header, payload []byte) (int, error)
  12. // Write encrypts and writes a full RTP packet
  13. Write(b []byte) (int, error)
  14. }
  15. // TrackLocalContext is the Context passed when a TrackLocal has been Binded/Unbinded from a PeerConnection, and used
  16. // in Interceptors.
  17. type TrackLocalContext interface {
  18. // CodecParameters returns the negotiated RTPCodecParameters. These are the codecs supported by both
  19. // PeerConnections and the SSRC/PayloadTypes
  20. CodecParameters() []RTPCodecParameters
  21. // HeaderExtensions returns the negotiated RTPHeaderExtensionParameters. These are the header extensions supported by
  22. // both PeerConnections and the SSRC/PayloadTypes
  23. HeaderExtensions() []RTPHeaderExtensionParameter
  24. // SSRC requires the negotiated SSRC of this track
  25. // This track may have multiple if RTX is enabled
  26. SSRC() SSRC
  27. // WriteStream returns the WriteStream for this TrackLocal. The implementer writes the outbound
  28. // media packets to it
  29. WriteStream() TrackLocalWriter
  30. // ID is a unique identifier that is used for both Bind/Unbind
  31. ID() string
  32. // RTCPReader returns the RTCP interceptor for this TrackLocal. Used to read RTCP of this TrackLocal.
  33. RTCPReader() interceptor.RTCPReader
  34. }
  35. type baseTrackLocalContext struct {
  36. id string
  37. params RTPParameters
  38. ssrc SSRC
  39. writeStream TrackLocalWriter
  40. rtcpInterceptor interceptor.RTCPReader
  41. }
  42. // CodecParameters returns the negotiated RTPCodecParameters. These are the codecs supported by both
  43. // PeerConnections and the SSRC/PayloadTypes
  44. func (t *baseTrackLocalContext) CodecParameters() []RTPCodecParameters {
  45. return t.params.Codecs
  46. }
  47. // HeaderExtensions returns the negotiated RTPHeaderExtensionParameters. These are the header extensions supported by
  48. // both PeerConnections and the SSRC/PayloadTypes
  49. func (t *baseTrackLocalContext) HeaderExtensions() []RTPHeaderExtensionParameter {
  50. return t.params.HeaderExtensions
  51. }
  52. // SSRC requires the negotiated SSRC of this track
  53. // This track may have multiple if RTX is enabled
  54. func (t *baseTrackLocalContext) SSRC() SSRC {
  55. return t.ssrc
  56. }
  57. // WriteStream returns the WriteStream for this TrackLocal. The implementer writes the outbound
  58. // media packets to it
  59. func (t *baseTrackLocalContext) WriteStream() TrackLocalWriter {
  60. return t.writeStream
  61. }
  62. // ID is a unique identifier that is used for both Bind/Unbind
  63. func (t *baseTrackLocalContext) ID() string {
  64. return t.id
  65. }
  66. // RTCPReader returns the RTCP interceptor for this TrackLocal. Used to read RTCP of this TrackLocal.
  67. func (t *baseTrackLocalContext) RTCPReader() interceptor.RTCPReader {
  68. return t.rtcpInterceptor
  69. }
  70. // TrackLocal is an interface that controls how the user can send media
  71. // The user can provide their own TrackLocal implementations, or use
  72. // the implementations in pkg/media
  73. type TrackLocal interface {
  74. // Bind should implement the way how the media data flows from the Track to the PeerConnection
  75. // This will be called internally after signaling is complete and the list of available
  76. // codecs has been determined
  77. Bind(TrackLocalContext) (RTPCodecParameters, error)
  78. // Unbind should implement the teardown logic when the track is no longer needed. This happens
  79. // because a track has been stopped.
  80. Unbind(TrackLocalContext) error
  81. // ID is the unique identifier for this Track. This should be unique for the
  82. // stream, but doesn't have to globally unique. A common example would be 'audio' or 'video'
  83. // and StreamID would be 'desktop' or 'webcam'
  84. ID() string
  85. // RID is the RTP Stream ID for this track.
  86. RID() string
  87. // StreamID is the group this track belongs too. This must be unique
  88. StreamID() string
  89. // Kind controls if this TrackLocal is audio or video
  90. Kind() RTPCodecType
  91. }