github.com/pion/webrtc/v4@v4.0.1/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 PayloadTypes
    25  	CodecParameters() []RTPCodecParameters
    26  
    27  	// HeaderExtensions returns the negotiated RTPHeaderExtensionParameters. These are the header extensions supported by
    28  	// both PeerConnections and the URI/IDs
    29  	HeaderExtensions() []RTPHeaderExtensionParameter
    30  
    31  	// SSRC returns the negotiated SSRC of this track
    32  	SSRC() SSRC
    33  
    34  	// SSRCRetransmission returns the negotiated SSRC used to send retransmissions for this track
    35  	SSRCRetransmission() SSRC
    36  
    37  	// SSRCForwardErrorCorrection returns the negotiated SSRC to send forward error correction for this track
    38  	SSRCForwardErrorCorrection() SSRC
    39  
    40  	// WriteStream returns the WriteStream for this TrackLocal. The implementer writes the outbound
    41  	// media packets to it
    42  	WriteStream() TrackLocalWriter
    43  
    44  	// ID is a unique identifier that is used for both Bind/Unbind
    45  	ID() string
    46  
    47  	// RTCPReader returns the RTCP interceptor for this TrackLocal. Used to read RTCP of this TrackLocal.
    48  	RTCPReader() interceptor.RTCPReader
    49  }
    50  
    51  type baseTrackLocalContext struct {
    52  	id                     string
    53  	params                 RTPParameters
    54  	ssrc, ssrcRTX, ssrcFEC SSRC
    55  	writeStream            TrackLocalWriter
    56  	rtcpInterceptor        interceptor.RTCPReader
    57  }
    58  
    59  // CodecParameters returns the negotiated RTPCodecParameters. These are the codecs supported by both
    60  // PeerConnections and the SSRC/PayloadTypes
    61  func (t *baseTrackLocalContext) CodecParameters() []RTPCodecParameters {
    62  	return t.params.Codecs
    63  }
    64  
    65  // HeaderExtensions returns the negotiated RTPHeaderExtensionParameters. These are the header extensions supported by
    66  // both PeerConnections and the SSRC/PayloadTypes
    67  func (t *baseTrackLocalContext) HeaderExtensions() []RTPHeaderExtensionParameter {
    68  	return t.params.HeaderExtensions
    69  }
    70  
    71  // SSRC requires the negotiated SSRC of this track
    72  func (t *baseTrackLocalContext) SSRC() SSRC {
    73  	return t.ssrc
    74  }
    75  
    76  // SSRCRetransmission returns the negotiated SSRC used to send retransmissions for this track
    77  func (t *baseTrackLocalContext) SSRCRetransmission() SSRC {
    78  	return t.ssrcRTX
    79  }
    80  
    81  // SSRCForwardErrorCorrection returns the negotiated SSRC to send forward error correction for this track
    82  func (t *baseTrackLocalContext) SSRCForwardErrorCorrection() SSRC {
    83  	return t.ssrcFEC
    84  }
    85  
    86  // WriteStream returns the WriteStream for this TrackLocal. The implementer writes the outbound
    87  // media packets to it
    88  func (t *baseTrackLocalContext) WriteStream() TrackLocalWriter {
    89  	return t.writeStream
    90  }
    91  
    92  // ID is a unique identifier that is used for both Bind/Unbind
    93  func (t *baseTrackLocalContext) ID() string {
    94  	return t.id
    95  }
    96  
    97  // RTCPReader returns the RTCP interceptor for this TrackLocal. Used to read RTCP of this TrackLocal.
    98  func (t *baseTrackLocalContext) RTCPReader() interceptor.RTCPReader {
    99  	return t.rtcpInterceptor
   100  }
   101  
   102  // TrackLocal is an interface that controls how the user can send media
   103  // The user can provide their own TrackLocal implementations, or use
   104  // the implementations in pkg/media
   105  type TrackLocal interface {
   106  	// Bind should implement the way how the media data flows from the Track to the PeerConnection
   107  	// This will be called internally after signaling is complete and the list of available
   108  	// codecs has been determined
   109  	Bind(TrackLocalContext) (RTPCodecParameters, error)
   110  
   111  	// Unbind should implement the teardown logic when the track is no longer needed. This happens
   112  	// because a track has been stopped.
   113  	Unbind(TrackLocalContext) error
   114  
   115  	// ID is the unique identifier for this Track. This should be unique for the
   116  	// stream, but doesn't have to globally unique. A common example would be 'audio' or 'video'
   117  	// and StreamID would be 'desktop' or 'webcam'
   118  	ID() string
   119  
   120  	// RID is the RTP Stream ID for this track.
   121  	RID() string
   122  
   123  	// StreamID is the group this track belongs too. This must be unique
   124  	StreamID() string
   125  
   126  	// Kind controls if this TrackLocal is audio or video
   127  	Kind() RTPCodecType
   128  }