Skip to content

Commit 667d0ff

Browse files
kevmo314Sean-Der
authored andcommitted
Make TrackLocalContext an interface
This allows external users to provide their own TrackLocalContext to be bound to a track.
1 parent 6a3a97f commit 667d0ff

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

rtpsender.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type trackEncoding struct {
2727
rtcpInterceptor interceptor.RTCPReader
2828
streamInfo interceptor.StreamInfo
2929

30-
context TrackLocalContext
30+
context *baseTrackLocalContext
3131

3232
ssrc SSRC
3333
}
@@ -242,13 +242,13 @@ func (r *RTPSender) ReplaceTrack(track TrackLocal) error {
242242
}
243243

244244
var replacedTrack TrackLocal
245-
var context *TrackLocalContext
245+
var context *baseTrackLocalContext
246246
if len(r.trackEncodings) != 0 {
247247
replacedTrack = r.trackEncodings[0].track
248-
context = &r.trackEncodings[0].context
248+
context = r.trackEncodings[0].context
249249
}
250250
if r.hasSent() && replacedTrack != nil {
251-
if err := replacedTrack.Unbind(*context); err != nil {
251+
if err := replacedTrack.Unbind(context); err != nil {
252252
return err
253253
}
254254
}
@@ -258,16 +258,16 @@ func (r *RTPSender) ReplaceTrack(track TrackLocal) error {
258258
return nil
259259
}
260260

261-
codec, err := track.Bind(TrackLocalContext{
262-
id: context.id,
261+
codec, err := track.Bind(&baseTrackLocalContext{
262+
id: context.ID(),
263263
params: r.api.mediaEngine.getRTPParametersByKind(track.Kind(), []RTPTransceiverDirection{RTPTransceiverDirectionSendonly}),
264-
ssrc: context.ssrc,
265-
writeStream: context.writeStream,
266-
rtcpInterceptor: context.rtcpInterceptor,
264+
ssrc: context.SSRC(),
265+
writeStream: context.WriteStream(),
266+
rtcpInterceptor: context.RTCPReader(),
267267
})
268268
if err != nil {
269269
// Re-bind the original track
270-
if _, reBindErr := replacedTrack.Bind(*context); reBindErr != nil {
270+
if _, reBindErr := replacedTrack.Bind(context); reBindErr != nil {
271271
return reBindErr
272272
}
273273

@@ -297,7 +297,7 @@ func (r *RTPSender) Send(parameters RTPSendParameters) error {
297297

298298
for idx, trackEncoding := range r.trackEncodings {
299299
writeStream := &interceptorToTrackLocalWriter{}
300-
trackEncoding.context = TrackLocalContext{
300+
trackEncoding.context = &baseTrackLocalContext{
301301
id: r.id,
302302
params: r.api.mediaEngine.getRTPParametersByKind(trackEncoding.track.Kind(), []RTPTransceiverDirection{RTPTransceiverDirectionSendonly}),
303303
ssrc: parameters.Encodings[idx].SSRC,

track_local.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,31 @@ type TrackLocalWriter interface {
1919

2020
// TrackLocalContext is the Context passed when a TrackLocal has been Binded/Unbinded from a PeerConnection, and used
2121
// in Interceptors.
22-
type TrackLocalContext struct {
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 {
2347
id string
2448
params RTPParameters
2549
ssrc SSRC
@@ -29,35 +53,35 @@ type TrackLocalContext struct {
2953

3054
// CodecParameters returns the negotiated RTPCodecParameters. These are the codecs supported by both
3155
// PeerConnections and the SSRC/PayloadTypes
32-
func (t *TrackLocalContext) CodecParameters() []RTPCodecParameters {
56+
func (t *baseTrackLocalContext) CodecParameters() []RTPCodecParameters {
3357
return t.params.Codecs
3458
}
3559

3660
// HeaderExtensions returns the negotiated RTPHeaderExtensionParameters. These are the header extensions supported by
3761
// both PeerConnections and the SSRC/PayloadTypes
38-
func (t *TrackLocalContext) HeaderExtensions() []RTPHeaderExtensionParameter {
62+
func (t *baseTrackLocalContext) HeaderExtensions() []RTPHeaderExtensionParameter {
3963
return t.params.HeaderExtensions
4064
}
4165

4266
// SSRC requires the negotiated SSRC of this track
4367
// This track may have multiple if RTX is enabled
44-
func (t *TrackLocalContext) SSRC() SSRC {
68+
func (t *baseTrackLocalContext) SSRC() SSRC {
4569
return t.ssrc
4670
}
4771

4872
// WriteStream returns the WriteStream for this TrackLocal. The implementer writes the outbound
4973
// media packets to it
50-
func (t *TrackLocalContext) WriteStream() TrackLocalWriter {
74+
func (t *baseTrackLocalContext) WriteStream() TrackLocalWriter {
5175
return t.writeStream
5276
}
5377

5478
// ID is a unique identifier that is used for both Bind/Unbind
55-
func (t *TrackLocalContext) ID() string {
79+
func (t *baseTrackLocalContext) ID() string {
5680
return t.id
5781
}
5882

5983
// RTCPReader returns the RTCP interceptor for this TrackLocal. Used to read RTCP of this TrackLocal.
60-
func (t *TrackLocalContext) RTCPReader() interceptor.RTCPReader {
84+
func (t *baseTrackLocalContext) RTCPReader() interceptor.RTCPReader {
6185
return t.rtcpInterceptor
6286
}
6387

0 commit comments

Comments
 (0)