@@ -61,7 +61,7 @@ public bool HasAudio
6161 /// <param name="durationRtpUnits">The duration in RTP timestamp units of the audio sample. This
6262 /// value is added to the previous RTP timestamp when building the RTP header.</param>
6363 /// <param name="sample">The audio sample to set as the RTP packet payload.</param>
64- public void SendAudio ( uint durationRtpUnits , byte [ ] sample )
64+ public void SendAudio ( uint durationRtpUnits , ArraySegment < byte > sample )
6565 {
6666 if ( ! sendingFormatFound )
6767 {
@@ -71,14 +71,25 @@ public void SendAudio(uint durationRtpUnits, byte[] sample)
7171 SendAudioFrame ( durationRtpUnits , NegotiatedFormat . ID , sample ) ;
7272 }
7373
74+ /// <summary>
75+ /// Sends an audio sample to the remote peer.
76+ /// </summary>
77+ /// <param name="durationRtpUnits">The duration in RTP timestamp units of the audio sample. This
78+ /// value is added to the previous RTP timestamp when building the RTP header.</param>
79+ /// <param name="sample">The audio sample to set as the RTP packet payload.</param>
80+ public void SendAudio ( uint durationRtpUnits , byte [ ] sample )
81+ {
82+ SendAudio ( durationRtpUnits , new ArraySegment < byte > ( sample ) ) ;
83+ }
84+
7485 /// <summary>
7586 /// Sends an audio packet to the remote party.
7687 /// </summary>
7788 /// <param name="duration">The duration of the audio payload in timestamp units. This value
7889 /// gets added onto the timestamp being set in the RTP header.</param>
7990 /// <param name="payloadTypeID">The payload ID to set in the RTP header.</param>
80- /// <param name="buffer ">The audio payload to send.</param>
81- public void SendAudioFrame ( uint duration , int payloadTypeID , byte [ ] buffer )
91+ /// <param name="bufferSegment ">The audio payload to send.</param>
92+ public void SendAudioFrame ( uint duration , int payloadTypeID , ArraySegment < byte > bufferSegment )
8293 {
8394 if ( CheckIfCanSendRtpRaw ( ) )
8495 {
@@ -99,29 +110,30 @@ public void SendAudioFrame(uint duration, int payloadTypeID, byte[] buffer)
99110 // See https://github.com/sipsorcery/sipsorcery/issues/394.
100111
101112 int maxPayload = RTPSession . RTP_MAX_PAYLOAD ;
102- int totalPackets = ( buffer . Length + maxPayload - 1 ) / maxPayload ;
113+ int totalPackets = ( bufferSegment . Count + maxPayload - 1 ) / maxPayload ;
103114
104115 uint totalIncrement = 0 ;
105116 uint startTimestamp = LocalTrack . Timestamp ; // Keep track of where we started.
106117
107118 for ( int index = 0 ; index < totalPackets ; index ++ )
108119 {
109120 int offset = index * maxPayload ;
110- int payloadLength = Math . Min ( maxPayload , buffer . Length - offset ) ;
121+ int payloadLength = Math . Min ( maxPayload , bufferSegment . Count - offset ) ;
111122
112- double fraction = ( double ) payloadLength / buffer . Length ;
123+ double fraction = ( double ) payloadLength / bufferSegment . Count ;
113124 uint packetDuration = ( uint ) Math . Round ( fraction * duration ) ;
114125
115- byte [ ] payload = new byte [ payloadLength ] ;
116- Buffer . BlockCopy ( buffer , offset , payload , 0 , payloadLength ) ;
117-
118126 // RFC3551 specifies that for audio the marker bit should always be 0 except for when returning
119127 // from silence suppression. For video the marker bit DOES get set to 1 for the last packet
120128 // in a frame.
121129 int markerBit = 0 ;
122-
130+ #if NETCOREAPP2_1_OR_GREATER && ! NETFRAMEWORK
131+ var memorySegment = bufferSegment . Slice ( offset , payloadLength ) ;
132+ #else
133+ var memorySegment = new ArraySegment < byte > ( bufferSegment . Array ! , offset , payloadLength ) ;
134+ #endif
123135 // Send this packet at the current LocalTrack.Timestamp
124- SendRtpRaw ( payload , LocalTrack . Timestamp , markerBit , payloadTypeID , true ) ;
136+ SendRtpRaw ( memorySegment , LocalTrack . Timestamp , markerBit , payloadTypeID , true ) ;
125137
126138 // After sending, increment the timestamp by this packet's portion.
127139 // This ensures the timestamp increments for the next packet, including the first one.
@@ -143,6 +155,18 @@ public void SendAudioFrame(uint duration, int payloadTypeID, byte[] buffer)
143155 }
144156 }
145157
158+ /// <summary>
159+ /// Sends an audio packet to the remote party.
160+ /// </summary>
161+ /// <param name="duration">The duration of the audio payload in timestamp units. This value
162+ /// gets added onto the timestamp being set in the RTP header.</param>
163+ /// <param name="payloadTypeID">The payload ID to set in the RTP header.</param>
164+ /// <param name="buffer">The audio payload to send.</param>
165+ public void SendAudioFrame ( uint duration , int payloadTypeID , byte [ ] buffer )
166+ {
167+ SendAudioFrame ( duration , payloadTypeID , new ArraySegment < byte > ( buffer ) ) ;
168+ }
169+
146170 /// <summary>
147171 /// Sends an RTP event for a DTMF tone as per RFC2833. Sending the event requires multiple packets to be sent.
148172 /// This method will hold onto the socket until all the packets required for the event have been sent. The send
0 commit comments