From 61372cfa680048fd477e5fc687453a6f09ab1876 Mon Sep 17 00:00:00 2001 From: Alessandro Ros Date: Mon, 24 Mar 2025 18:42:48 +0100 Subject: [PATCH] rename OnPacketLost2 into OnPacketsLost (#736) --- client.go | 16 ++++++++-------- client_format.go | 8 ++++---- client_play_test.go | 4 ++-- internal/rtcpreceiver/rtcpreceiver_test.go | 4 ++-- pkg/rtcpreceiver/rtcpreceiver_test.go | 4 ++-- server_handler.go | 22 +++++++++++++++++----- server_record_test.go | 4 ++-- server_session_format.go | 6 ++---- server_test.go | 8 ++++---- 9 files changed, 43 insertions(+), 33 deletions(-) diff --git a/client.go b/client.go index bdfad137..c6893394 100644 --- a/client.go +++ b/client.go @@ -218,11 +218,11 @@ type ClientOnTransportSwitchFunc func(err error) // ClientOnPacketLostFunc is the prototype of Client.OnPacketLost. // -// Deprecated: replaced by ClientOnPacketLost2Func +// Deprecated: replaced by ClientOnPacketsLostFunc type ClientOnPacketLostFunc func(err error) -// ClientOnPacketLost2Func is the prototype of Client.OnPacketLost2. -type ClientOnPacketLost2Func func(lost uint64) +// ClientOnPacketsLostFunc is the prototype of Client.OnPacketsLost. +type ClientOnPacketsLostFunc func(lost uint64) // ClientOnDecodeErrorFunc is the prototype of Client.OnDecodeError. type ClientOnDecodeErrorFunc func(err error) @@ -314,10 +314,10 @@ type Client struct { OnTransportSwitch ClientOnTransportSwitchFunc // called when the client detects lost packets. // - // Deprecated: replaced by OnPacketLost2. + // Deprecated: replaced by OnPacketsLost OnPacketLost ClientOnPacketLostFunc // called when the client detects lost packets. - OnPacketLost2 ClientOnPacketLost2Func + OnPacketsLost ClientOnPacketsLostFunc // called when a non-fatal decode error occurs. OnDecodeError ClientOnDecodeErrorFunc @@ -435,12 +435,12 @@ func (c *Client) Start(scheme string, host string) error { } } if c.OnPacketLost != nil { - c.OnPacketLost2 = func(lost uint64) { + c.OnPacketsLost = func(lost uint64) { c.OnPacketLost(liberrors.ErrClientRTPPacketsLost{Lost: uint(lost)}) //nolint:staticcheck } } - if c.OnPacketLost2 == nil { - c.OnPacketLost2 = func(lost uint64) { + if c.OnPacketsLost == nil { + c.OnPacketsLost = func(lost uint64) { log.Printf("%d RTP %s lost", lost, func() string { diff --git a/client_format.go b/client_format.go index 828e1cbd..450ac38b 100644 --- a/client_format.go +++ b/client_format.go @@ -93,7 +93,7 @@ func (cf *clientFormat) stop() { func (cf *clientFormat) readPacketRTPUDP(pkt *rtp.Packet) { packets, lost := cf.udpReorderer.Process(pkt) if lost != 0 { - cf.onPacketRTPLost(lost) + cf.handlePacketsLost(lost) // do not return } @@ -107,7 +107,7 @@ func (cf *clientFormat) readPacketRTPUDP(pkt *rtp.Packet) { func (cf *clientFormat) readPacketRTPTCP(pkt *rtp.Packet) { lost := cf.tcpLossDetector.Process(pkt) if lost != 0 { - cf.onPacketRTPLost(lost) + cf.handlePacketsLost(lost) // do not return } @@ -128,9 +128,9 @@ func (cf *clientFormat) handlePacketRTP(pkt *rtp.Packet, now time.Time) { cf.onPacketRTP(pkt) } -func (cf *clientFormat) onPacketRTPLost(lost uint64) { +func (cf *clientFormat) handlePacketsLost(lost uint64) { atomic.AddUint64(cf.rtpPacketsLost, lost) - cf.cm.c.OnPacketLost2(lost) + cf.cm.c.OnPacketsLost(lost) } func (cf *clientFormat) writePacketRTPInQueueUDP(payload []byte) error { diff --git a/client_play_test.go b/client_play_test.go index 73ceb068..895785c2 100644 --- a/client_play_test.go +++ b/client_play_test.go @@ -3144,8 +3144,8 @@ func TestClientPlayDecodeErrors(t *testing.T) { v := TransportTCP return &v }(), - OnPacketLost: func(err error) { - require.EqualError(t, err, "69 RTP packets lost") + OnPacketsLost: func(lost uint64) { + require.Equal(t, uint64(69), lost) close(errorRecv) }, OnDecodeError: func(err error) { diff --git a/internal/rtcpreceiver/rtcpreceiver_test.go b/internal/rtcpreceiver/rtcpreceiver_test.go index e228328d..0a351a39 100644 --- a/internal/rtcpreceiver/rtcpreceiver_test.go +++ b/internal/rtcpreceiver/rtcpreceiver_test.go @@ -159,7 +159,7 @@ func TestRTCPReceiverOverflow(t *testing.T) { <-done } -func TestRTCPReceiverPacketLost(t *testing.T) { +func TestRTCPReceiverPacketsLost(t *testing.T) { done := make(chan struct{}) rr := &RTCPReceiver{ @@ -236,7 +236,7 @@ func TestRTCPReceiverPacketLost(t *testing.T) { <-done } -func TestRTCPReceiverOverflowPacketLost(t *testing.T) { +func TestRTCPReceiverOverflowPacketsLost(t *testing.T) { done := make(chan struct{}) rr := &RTCPReceiver{ diff --git a/pkg/rtcpreceiver/rtcpreceiver_test.go b/pkg/rtcpreceiver/rtcpreceiver_test.go index 36570516..37ac1375 100644 --- a/pkg/rtcpreceiver/rtcpreceiver_test.go +++ b/pkg/rtcpreceiver/rtcpreceiver_test.go @@ -155,7 +155,7 @@ func TestRTCPReceiverOverflow(t *testing.T) { <-done } -func TestRTCPReceiverPacketLost(t *testing.T) { +func TestRTCPReceiverPacketsLost(t *testing.T) { done := make(chan struct{}) rr, err := New( @@ -230,7 +230,7 @@ func TestRTCPReceiverPacketLost(t *testing.T) { <-done } -func TestRTCPReceiverOverflowPacketLost(t *testing.T) { +func TestRTCPReceiverOverflowPacketsLost(t *testing.T) { done := make(chan struct{}) rr, err := New( diff --git a/server_handler.go b/server_handler.go index 7706aec4..389dcc16 100644 --- a/server_handler.go +++ b/server_handler.go @@ -193,21 +193,33 @@ type ServerHandlerOnSetParameter interface { } // ServerHandlerOnPacketLostCtx is the context of OnPacketLost. +// +// Deprecated: replaced by ServerHandlerOnPacketsLostCtx type ServerHandlerOnPacketLostCtx struct { Session *ServerSession - Lost uint64 - - // - // Deprecated: replaced by Lost - Error error + Error error } // ServerHandlerOnPacketLost can be implemented by a ServerHandler. +// +// Deprecated: replaced by ServerHandlerOnPacketsLost type ServerHandlerOnPacketLost interface { // called when the server detects lost packets. OnPacketLost(*ServerHandlerOnPacketLostCtx) } +// ServerHandlerOnPacketsLostCtx is the context of OnPacketsLost. +type ServerHandlerOnPacketsLostCtx struct { + Session *ServerSession + Lost uint64 +} + +// ServerHandlerOnPacketsLost can be implemented by a ServerHandler. +type ServerHandlerOnPacketsLost interface { + // called when the server detects lost packets. + OnPacketsLost(*ServerHandlerOnPacketsLostCtx) +} + // ServerHandlerOnDecodeErrorCtx is the context of OnDecodeError. type ServerHandlerOnDecodeErrorCtx struct { Session *ServerSession diff --git a/server_record_test.go b/server_record_test.go index e1dc813d..98885171 100644 --- a/server_record_test.go +++ b/server_record_test.go @@ -1259,8 +1259,8 @@ func TestServerRecordDecodeErrors(t *testing.T) { StatusCode: base.StatusOK, }, nil }, - onPacketLost: func(ctx *ServerHandlerOnPacketLostCtx) { - require.EqualError(t, ctx.Error, "69 RTP packets lost") + onPacketsLost: func(ctx *ServerHandlerOnPacketsLostCtx) { + require.Equal(t, uint64(69), ctx.Lost) close(errorRecv) }, onDecodeError: func(ctx *ServerHandlerOnDecodeErrorCtx) { diff --git a/server_session_format.go b/server_session_format.go index 106a8195..682714d9 100644 --- a/server_session_format.go +++ b/server_session_format.go @@ -12,7 +12,6 @@ import ( "github.com/bluenviron/gortsplib/v4/internal/rtplossdetector" "github.com/bluenviron/gortsplib/v4/internal/rtpreorderer" "github.com/bluenviron/gortsplib/v4/pkg/format" - "github.com/bluenviron/gortsplib/v4/pkg/liberrors" ) type serverSessionFormat struct { @@ -115,11 +114,10 @@ func (sf *serverSessionFormat) handlePacketRTP(pkt *rtp.Packet, now time.Time) { func (sf *serverSessionFormat) onPacketRTPLost(lost uint64) { atomic.AddUint64(sf.rtpPacketsLost, lost) - if h, ok := sf.sm.ss.s.Handler.(ServerHandlerOnPacketLost); ok { - h.OnPacketLost(&ServerHandlerOnPacketLostCtx{ + if h, ok := sf.sm.ss.s.Handler.(ServerHandlerOnPacketsLost); ok { + h.OnPacketsLost(&ServerHandlerOnPacketsLostCtx{ Session: sf.sm.ss, Lost: lost, - Error: liberrors.ErrServerRTPPacketsLost{Lost: uint(lost)}, //nolint:staticcheck }) } else { log.Printf("%d RTP %s lost", diff --git a/server_test.go b/server_test.go index 4e2a8916..4a5af334 100644 --- a/server_test.go +++ b/server_test.go @@ -96,7 +96,7 @@ type testServerHandler struct { onPause func(*ServerHandlerOnPauseCtx) (*base.Response, error) onSetParameter func(*ServerHandlerOnSetParameterCtx) (*base.Response, error) onGetParameter func(*ServerHandlerOnGetParameterCtx) (*base.Response, error) - onPacketLost func(*ServerHandlerOnPacketLostCtx) + onPacketsLost func(*ServerHandlerOnPacketsLostCtx) onDecodeError func(*ServerHandlerOnDecodeErrorCtx) } @@ -180,9 +180,9 @@ func (sh *testServerHandler) OnGetParameter(ctx *ServerHandlerOnGetParameterCtx) return nil, fmt.Errorf("unimplemented") } -func (sh *testServerHandler) OnPacketLost(ctx *ServerHandlerOnPacketLostCtx) { - if sh.onPacketLost != nil { - sh.onPacketLost(ctx) +func (sh *testServerHandler) OnPacketsLost(ctx *ServerHandlerOnPacketsLostCtx) { + if sh.onPacketsLost != nil { + sh.onPacketsLost(ctx) } }