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