Skip to content

Commit d0ab50e

Browse files
committed
B Fixed exceptions when disconnected
1 parent 9ef11d1 commit d0ab50e

1 file changed

Lines changed: 41 additions & 13 deletions

File tree

src/SharpRTSPServer/RTSPServer.cs

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ public void FeedInRawVideoSamples(uint rtpTimestamp, List<byte[]> samples)
574574
}
575575
}
576576

577+
bool writeError = false;
577578
// There could be more than 1 RTP packet (if the data is fragmented)
578579
foreach (var rtpPacket in rtpPackets)
579580
{
@@ -590,18 +591,34 @@ public void FeedInRawVideoSamples(uint rtpTimestamp, List<byte[]> samples)
590591
// send the whole NAL. ** We could fragment the RTP packet into smaller chuncks that fit within the MTU
591592
// Send to the IP address of the Client
592593
// Send to the UDP Port the Client gave us in the SETUP command
593-
connection.Video.RtpChannel.WriteToDataPort(rtpPacket.Span);
594+
var channel = connection.Video.RtpChannel;
595+
if (channel != null)
596+
{
597+
channel.WriteToDataPort(rtpPacket.Span);
598+
}
599+
else
600+
{
601+
writeError = true;
602+
}
594603
}
595604
catch (Exception e)
596605
{
597-
Console.WriteLine("UDP Write Exception " + e);
598-
Console.WriteLine("Error writing to listener " + connection.Listener.RemoteAdress);
599-
Console.WriteLine("Removing session " + connection.SessionId + " due to write error");
600-
RemoveSession(connection);
606+
_logger.LogWarning("UDP Write Exception " + e);
607+
writeError = true;
601608
break; // exit out of foreach loop
602609
}
603610
}
604-
connection.Video.OctetCount += (uint)samples.Sum(nal => nal.Length); // QUESTION - Do I need to include the RTP header bytes/fragmenting bytes
611+
612+
if (writeError)
613+
{
614+
_logger.LogWarning("Error writing to listener " + connection.Listener.RemoteAdress);
615+
_logger.LogWarning("Removing session " + connection.SessionId + " due to write error");
616+
RemoveSession(connection);
617+
}
618+
else
619+
{
620+
connection.Video.OctetCount += (uint)samples.Sum(nal => nal.Length); // QUESTION - Do I need to include the RTP header bytes/fragmenting bytes
621+
}
605622
}
606623
}
607624

@@ -642,7 +659,6 @@ public void FeedInRawAudioSamples(uint rtpTimestamp, List<byte[]> samples)
642659

643660
_logger.LogDebug("Sending audio session {sessionId} {TransportLogName} RTP timestamp={rtpTimestamp}. Sequence={sequenceNumber}",
644661
connection.SessionId, TransportLogName(connection.Audio.RtpChannel), rtpTimestamp, connection.Audio.SequenceNumber);
645-
bool writeError = false;
646662

647663
if (connection.Audio.MustSendRtcpPacket)
648664
{
@@ -652,6 +668,7 @@ public void FeedInRawAudioSamples(uint rtpTimestamp, List<byte[]> samples)
652668
}
653669
}
654670

671+
bool writeError = false;
655672
// There could be more than 1 RTP packet (if the data is fragmented)
656673
foreach (var rtpPacket in rtpPackets)
657674
{
@@ -665,24 +682,35 @@ public void FeedInRawAudioSamples(uint rtpTimestamp, List<byte[]> samples)
665682
try
666683
{
667684
// send the whole RTP packet
668-
connection.Audio.RtpChannel.WriteToDataPort(rtpPacket.Span);
685+
var channel = connection.Audio.RtpChannel;
686+
if (channel != null)
687+
{
688+
channel.WriteToDataPort(rtpPacket.Span);
689+
}
690+
else
691+
{
692+
writeError = true;
693+
}
669694
}
670695
catch (Exception e)
671696
{
672697
_logger.LogWarning(e, "UDP Write Exception");
673-
_logger.LogWarning("Error writing to listener {address}", connection.Listener.RemoteAdress);
674698
writeError = true;
699+
break;
675700
}
676701
}
677702

678703
if (writeError)
679704
{
680-
Console.WriteLine("Removing session " + connection.SessionId + " due to write error");
705+
_logger.LogWarning("Error writing to listener {address}", connection.Listener.RemoteAdress);
706+
_logger.LogWarning("Removing session " + connection.SessionId + " due to write error");
681707
RemoveSession(connection);
682708
}
683-
684-
connection.Audio.RtpPacketCount++;
685-
connection.Audio.OctetCount += (uint)samples.Sum(frame => frame.Length); // QUESTION - Do I need to include the RTP header bytes/fragmenting bytes
709+
else
710+
{
711+
connection.Audio.RtpPacketCount++;
712+
connection.Audio.OctetCount += (uint)samples.Sum(frame => frame.Length); // QUESTION - Do I need to include the RTP header bytes/fragmenting bytes
713+
}
686714
}
687715
}
688716

0 commit comments

Comments
 (0)