|
| 1 | +using System; |
| 2 | +using System.Buffers; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace SharpRTSPServer |
| 7 | +{ |
| 8 | + public class PCMUTrack : ITrack |
| 9 | + { |
| 10 | + public int ID { get; set; } = 1; |
| 11 | + public int SamplingRate { get; } = 8000; |
| 12 | + public int Channels { get; } = 1; |
| 13 | + |
| 14 | + public bool IsReady { get { return true; } } |
| 15 | + |
| 16 | + public int PayloadType => 0; |
| 17 | + |
| 18 | + public StringBuilder BuildSDP(StringBuilder sdp) |
| 19 | + { |
| 20 | + sdp.Append($"m=audio 0 RTP/AVP {PayloadType}\n"); // <---- Payload Type 0 means G711 ULAW |
| 21 | + sdp.Append($"a=control:trackID={ID}\n"); |
| 22 | + sdp.Append($"a=rtpmap:{PayloadType} PCMU/{SamplingRate}\n"); |
| 23 | + return sdp; |
| 24 | + } |
| 25 | + |
| 26 | + public (List<Memory<byte>>, List<IMemoryOwner<byte>>) PrepareRtpPackets(List<byte[]> samples, uint rtpTimestamp) |
| 27 | + { |
| 28 | + List<Memory<byte>> rtpPackets = new List<Memory<byte>>(); |
| 29 | + List<IMemoryOwner<byte>> memoryOwners = new List<IMemoryOwner<byte>>(); |
| 30 | + |
| 31 | + for (int i = 0; i < samples.Count; i++) |
| 32 | + { |
| 33 | + var audioPacket = samples[i]; |
| 34 | + var size = 12 + audioPacket.Length; |
| 35 | + var owner = MemoryPool<byte>.Shared.Rent(size); |
| 36 | + memoryOwners.Add(owner); |
| 37 | + |
| 38 | + var rtpPacket = owner.Memory.Slice(0, size); |
| 39 | + |
| 40 | + const bool rtpPadding = false; |
| 41 | + const bool rtpHasExtension = false; |
| 42 | + int rtpCsrcCount = 0; |
| 43 | + const bool rtpMarker = true; |
| 44 | + |
| 45 | + RTPPacketUtil.WriteHeader(rtpPacket.Span, |
| 46 | + RTPPacketUtil.RTP_VERSION, rtpPadding, rtpHasExtension, rtpCsrcCount, rtpMarker, PayloadType); |
| 47 | + |
| 48 | + RTPPacketUtil.WriteTS(rtpPacket.Span, rtpTimestamp); |
| 49 | + audioPacket.CopyTo(rtpPacket.Slice(12)); |
| 50 | + rtpPackets.Add(rtpPacket); |
| 51 | + } |
| 52 | + |
| 53 | + return (rtpPackets, memoryOwners); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public class PCMATrack : ITrack |
| 58 | + { |
| 59 | + public int ID { get; set; } = 1; |
| 60 | + public int SamplingRate { get; } = 8000; |
| 61 | + public int Channels { get; } = 1; |
| 62 | + |
| 63 | + public bool IsReady { get { return true; } } |
| 64 | + |
| 65 | + public int PayloadType => 8; |
| 66 | + |
| 67 | + public StringBuilder BuildSDP(StringBuilder sdp) |
| 68 | + { |
| 69 | + sdp.Append($"m=audio 0 RTP/AVP {PayloadType}\n"); // <---- Payload Type 8 means G711 ALAW |
| 70 | + sdp.Append($"a=control:trackID={ID}\n"); |
| 71 | + sdp.Append($"a=rtpmap:{PayloadType} PCMA/{SamplingRate}\n"); |
| 72 | + return sdp; |
| 73 | + } |
| 74 | + |
| 75 | + public (List<Memory<byte>>, List<IMemoryOwner<byte>>) PrepareRtpPackets(List<byte[]> samples, uint rtpTimestamp) |
| 76 | + { |
| 77 | + List<Memory<byte>> rtpPackets = new List<Memory<byte>>(); |
| 78 | + List<IMemoryOwner<byte>> memoryOwners = new List<IMemoryOwner<byte>>(); |
| 79 | + |
| 80 | + for (int i = 0; i < samples.Count; i++) |
| 81 | + { |
| 82 | + var audioPacket = samples[i]; |
| 83 | + var size = 12 + audioPacket.Length; |
| 84 | + var owner = MemoryPool<byte>.Shared.Rent(size); |
| 85 | + memoryOwners.Add(owner); |
| 86 | + |
| 87 | + var rtpPacket = owner.Memory.Slice(0, size); |
| 88 | + |
| 89 | + const bool rtpPadding = false; |
| 90 | + const bool rtpHasExtension = false; |
| 91 | + int rtpCsrcCount = 0; |
| 92 | + const bool rtpMarker = true; |
| 93 | + |
| 94 | + RTPPacketUtil.WriteHeader(rtpPacket.Span, |
| 95 | + RTPPacketUtil.RTP_VERSION, rtpPadding, rtpHasExtension, rtpCsrcCount, rtpMarker, PayloadType); |
| 96 | + |
| 97 | + RTPPacketUtil.WriteTS(rtpPacket.Span, rtpTimestamp); |
| 98 | + audioPacket.CopyTo(rtpPacket.Slice(12)); |
| 99 | + rtpPackets.Add(rtpPacket); |
| 100 | + } |
| 101 | + |
| 102 | + return (rtpPackets, memoryOwners); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments