Make TrackLocalContext an interface

This allows external users to provide their own TrackLocalContext to be
bound to a track.
This commit is contained in:
Kevin Wang
2022-04-05 00:15:44 -04:00
committed by Sean DuBois
parent 6a3a97fbdb
commit 667d0ff6dc
2 changed files with 42 additions and 18 deletions

View File

@@ -27,7 +27,7 @@ type trackEncoding struct {
rtcpInterceptor interceptor.RTCPReader rtcpInterceptor interceptor.RTCPReader
streamInfo interceptor.StreamInfo streamInfo interceptor.StreamInfo
context TrackLocalContext context *baseTrackLocalContext
ssrc SSRC ssrc SSRC
} }
@@ -242,13 +242,13 @@ func (r *RTPSender) ReplaceTrack(track TrackLocal) error {
} }
var replacedTrack TrackLocal var replacedTrack TrackLocal
var context *TrackLocalContext var context *baseTrackLocalContext
if len(r.trackEncodings) != 0 { if len(r.trackEncodings) != 0 {
replacedTrack = r.trackEncodings[0].track replacedTrack = r.trackEncodings[0].track
context = &r.trackEncodings[0].context context = r.trackEncodings[0].context
} }
if r.hasSent() && replacedTrack != nil { if r.hasSent() && replacedTrack != nil {
if err := replacedTrack.Unbind(*context); err != nil { if err := replacedTrack.Unbind(context); err != nil {
return err return err
} }
} }
@@ -258,16 +258,16 @@ func (r *RTPSender) ReplaceTrack(track TrackLocal) error {
return nil return nil
} }
codec, err := track.Bind(TrackLocalContext{ codec, err := track.Bind(&baseTrackLocalContext{
id: context.id, id: context.ID(),
params: r.api.mediaEngine.getRTPParametersByKind(track.Kind(), []RTPTransceiverDirection{RTPTransceiverDirectionSendonly}), params: r.api.mediaEngine.getRTPParametersByKind(track.Kind(), []RTPTransceiverDirection{RTPTransceiverDirectionSendonly}),
ssrc: context.ssrc, ssrc: context.SSRC(),
writeStream: context.writeStream, writeStream: context.WriteStream(),
rtcpInterceptor: context.rtcpInterceptor, rtcpInterceptor: context.RTCPReader(),
}) })
if err != nil { if err != nil {
// Re-bind the original track // Re-bind the original track
if _, reBindErr := replacedTrack.Bind(*context); reBindErr != nil { if _, reBindErr := replacedTrack.Bind(context); reBindErr != nil {
return reBindErr return reBindErr
} }
@@ -297,7 +297,7 @@ func (r *RTPSender) Send(parameters RTPSendParameters) error {
for idx, trackEncoding := range r.trackEncodings { for idx, trackEncoding := range r.trackEncodings {
writeStream := &interceptorToTrackLocalWriter{} writeStream := &interceptorToTrackLocalWriter{}
trackEncoding.context = TrackLocalContext{ trackEncoding.context = &baseTrackLocalContext{
id: r.id, id: r.id,
params: r.api.mediaEngine.getRTPParametersByKind(trackEncoding.track.Kind(), []RTPTransceiverDirection{RTPTransceiverDirectionSendonly}), params: r.api.mediaEngine.getRTPParametersByKind(trackEncoding.track.Kind(), []RTPTransceiverDirection{RTPTransceiverDirectionSendonly}),
ssrc: parameters.Encodings[idx].SSRC, ssrc: parameters.Encodings[idx].SSRC,

View File

@@ -19,7 +19,31 @@ type TrackLocalWriter interface {
// TrackLocalContext is the Context passed when a TrackLocal has been Binded/Unbinded from a PeerConnection, and used // TrackLocalContext is the Context passed when a TrackLocal has been Binded/Unbinded from a PeerConnection, and used
// in Interceptors. // in Interceptors.
type TrackLocalContext struct { type TrackLocalContext interface {
// CodecParameters returns the negotiated RTPCodecParameters. These are the codecs supported by both
// PeerConnections and the SSRC/PayloadTypes
CodecParameters() []RTPCodecParameters
// HeaderExtensions returns the negotiated RTPHeaderExtensionParameters. These are the header extensions supported by
// both PeerConnections and the SSRC/PayloadTypes
HeaderExtensions() []RTPHeaderExtensionParameter
// SSRC requires the negotiated SSRC of this track
// This track may have multiple if RTX is enabled
SSRC() SSRC
// WriteStream returns the WriteStream for this TrackLocal. The implementer writes the outbound
// media packets to it
WriteStream() TrackLocalWriter
// ID is a unique identifier that is used for both Bind/Unbind
ID() string
// RTCPReader returns the RTCP interceptor for this TrackLocal. Used to read RTCP of this TrackLocal.
RTCPReader() interceptor.RTCPReader
}
type baseTrackLocalContext struct {
id string id string
params RTPParameters params RTPParameters
ssrc SSRC ssrc SSRC
@@ -29,35 +53,35 @@ type TrackLocalContext struct {
// CodecParameters returns the negotiated RTPCodecParameters. These are the codecs supported by both // CodecParameters returns the negotiated RTPCodecParameters. These are the codecs supported by both
// PeerConnections and the SSRC/PayloadTypes // PeerConnections and the SSRC/PayloadTypes
func (t *TrackLocalContext) CodecParameters() []RTPCodecParameters { func (t *baseTrackLocalContext) CodecParameters() []RTPCodecParameters {
return t.params.Codecs return t.params.Codecs
} }
// HeaderExtensions returns the negotiated RTPHeaderExtensionParameters. These are the header extensions supported by // HeaderExtensions returns the negotiated RTPHeaderExtensionParameters. These are the header extensions supported by
// both PeerConnections and the SSRC/PayloadTypes // both PeerConnections and the SSRC/PayloadTypes
func (t *TrackLocalContext) HeaderExtensions() []RTPHeaderExtensionParameter { func (t *baseTrackLocalContext) HeaderExtensions() []RTPHeaderExtensionParameter {
return t.params.HeaderExtensions return t.params.HeaderExtensions
} }
// SSRC requires the negotiated SSRC of this track // SSRC requires the negotiated SSRC of this track
// This track may have multiple if RTX is enabled // This track may have multiple if RTX is enabled
func (t *TrackLocalContext) SSRC() SSRC { func (t *baseTrackLocalContext) SSRC() SSRC {
return t.ssrc return t.ssrc
} }
// WriteStream returns the WriteStream for this TrackLocal. The implementer writes the outbound // WriteStream returns the WriteStream for this TrackLocal. The implementer writes the outbound
// media packets to it // media packets to it
func (t *TrackLocalContext) WriteStream() TrackLocalWriter { func (t *baseTrackLocalContext) WriteStream() TrackLocalWriter {
return t.writeStream return t.writeStream
} }
// ID is a unique identifier that is used for both Bind/Unbind // ID is a unique identifier that is used for both Bind/Unbind
func (t *TrackLocalContext) ID() string { func (t *baseTrackLocalContext) ID() string {
return t.id return t.id
} }
// RTCPReader returns the RTCP interceptor for this TrackLocal. Used to read RTCP of this TrackLocal. // RTCPReader returns the RTCP interceptor for this TrackLocal. Used to read RTCP of this TrackLocal.
func (t *TrackLocalContext) RTCPReader() interceptor.RTCPReader { func (t *baseTrackLocalContext) RTCPReader() interceptor.RTCPReader {
return t.rtcpInterceptor return t.rtcpInterceptor
} }