Skip to content

Commit bc2090c

Browse files
committed
R Refactoring, comments
1 parent 8f59586 commit bc2090c

5 files changed

Lines changed: 110 additions & 12 deletions

File tree

src/SharpRTSPServer/AACTrack.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ namespace SharpRTSPServer
1212
public class AACTrack : ITrack
1313
{
1414
/// <summary>
15-
/// Track ID.
15+
/// AAC Audio Codec name.
16+
/// </summary>
17+
public string Codec => "mpeg4-generic";
18+
19+
/// <summary>
20+
/// Track ID. Used to identify the track in the SDP.
1621
/// </summary>
1722
public int ID { get; set; } = 1;
1823

@@ -22,7 +27,7 @@ public class AACTrack : ITrack
2227
public int SamplingRate { get; set; } = 44100;
2328

2429
/// <summary>
25-
/// Number of channels.
30+
/// Number of channels. 1 for Mono, 2 for Stereo, ...
2631
/// </summary>
2732
public int Channels { get; set; } = 1;
2833

@@ -91,16 +96,27 @@ public void SetConfigDescriptor(byte[] configDescriptor)
9196
this.ConfigDescriptor = configDescriptor;
9297
}
9398

99+
/// <summary>
100+
/// Build the SDP for this track.
101+
/// </summary>
102+
/// <param name="sdp">SDP <see cref="StringBuilder"/>.</param>
103+
/// <returns><see cref="StringBuilder"/>.</returns>
94104
public StringBuilder BuildSDP(StringBuilder sdp)
95105
{
96106
sdp.Append($"m=audio 0 RTP/AVP {PayloadType}\n"); // <---- Payload Type 0 means G711 ULAW, 96+ means dynamic payload type
97107
sdp.Append($"a=control:trackID={ID}\n");
98-
sdp.Append($"a=rtpmap:{PayloadType} mpeg4-generic/{SamplingRate}/{Channels}\n");
108+
sdp.Append($"a=rtpmap:{PayloadType} {Codec}/{SamplingRate}/{Channels}\n");
99109
sdp.Append($"a=fmtp:{PayloadType} profile-level-id={GetAACProfileLevel(SamplingRate, Channels)}; " +
100110
$"config={Utilities.ToHexString(ConfigDescriptor)}; streamType=5; mode=AAC-hbr; objectType=64; sizeLength=13; indexLength=3; indexDeltaLength=3\n");
101111
return sdp;
102112
}
103113

114+
/// <summary>
115+
/// Creates RTP packets.
116+
/// </summary>
117+
/// <param name="samples">An array of AAC fragments. By default single fragment is expected.</param>
118+
/// <param name="rtpTimestamp">RTP timestamp in the timescale of the track.</param>
119+
/// <returns>RTP packets.</returns>
104120
public (List<Memory<byte>>, List<IMemoryOwner<byte>>) CreateRtpPackets(List<byte[]> samples, uint rtpTimestamp)
105121
{
106122
List<Memory<byte>> rtpPackets = new List<Memory<byte>>();

src/SharpRTSPServer/G711Track.cs

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,60 @@
55

66
namespace SharpRTSPServer
77
{
8+
/// <summary>
9+
/// G711 PCMU (U-LAW) track.
10+
/// </summary>
811
public class PCMUTrack : ITrack
912
{
13+
/// <summary>
14+
/// PCMU Audio Codec name.
15+
/// </summary>
16+
public string Codec => "PCMU";
17+
18+
/// <summary>
19+
/// Track ID. Used to identify the track in the SDP.
20+
/// </summary>
1021
public int ID { get; set; } = 1;
22+
23+
/// <summary>
24+
/// Sampling rate.
25+
/// </summary>
1126
public int SamplingRate { get; } = 8000;
27+
28+
/// <summary>
29+
/// Number of channels. 1 for Mono.
30+
/// </summary>
1231
public int Channels { get; } = 1;
1332

33+
/// <summary>
34+
/// Is the track ready?
35+
/// </summary>
1436
public bool IsReady { get { return true; } }
1537

38+
/// <summary>
39+
/// Payload type. PCMU uses static payload type 0.
40+
/// </summary>
1641
public int PayloadType { get; set; } = 0;
1742

43+
/// <summary>
44+
/// Build the SDP for this track.
45+
/// </summary>
46+
/// <param name="sdp">SDP <see cref="StringBuilder"/>.</param>
47+
/// <returns><see cref="StringBuilder"/>.</returns>
1848
public StringBuilder BuildSDP(StringBuilder sdp)
1949
{
20-
sdp.Append($"m=audio 0 RTP/AVP {PayloadType}\n"); // <---- Payload Type 0 means G711 ULAW
50+
sdp.Append($"m=audio 0 RTP/AVP {PayloadType}\n");
2151
sdp.Append($"a=control:trackID={ID}\n");
22-
sdp.Append($"a=rtpmap:{PayloadType} PCMU/{SamplingRate}\n");
52+
sdp.Append($"a=rtpmap:{PayloadType} {Codec}/{SamplingRate}\n");
2353
return sdp;
2454
}
2555

56+
/// <summary>
57+
/// Creates RTP packets.
58+
/// </summary>
59+
/// <param name="samples">An array of PCMU fragments. By default single fragment is expected.</param>
60+
/// <param name="rtpTimestamp">RTP timestamp in the timescale of the track.</param>
61+
/// <returns>RTP packets.</returns>
2662
public (List<Memory<byte>>, List<IMemoryOwner<byte>>) CreateRtpPackets(List<byte[]> samples, uint rtpTimestamp)
2763
{
2864
List<Memory<byte>> rtpPackets = new List<Memory<byte>>();
@@ -54,24 +90,60 @@ public StringBuilder BuildSDP(StringBuilder sdp)
5490
}
5591
}
5692

93+
/// <summary>
94+
/// G711 PCMA (A-LAW) track.
95+
/// </summary>
5796
public class PCMATrack : ITrack
5897
{
98+
/// <summary>
99+
/// PCMA Audio Codec name.
100+
/// </summary>
101+
public string Codec => "PCMA";
102+
103+
/// <summary>
104+
/// Track ID. Used to identify the track in the SDP.
105+
/// </summary>
59106
public int ID { get; set; } = 1;
107+
108+
/// <summary>
109+
/// Sampling rate.
110+
/// </summary>
60111
public int SamplingRate { get; } = 8000;
112+
113+
/// <summary>
114+
/// Number of channels. 1 for Mono.
115+
/// </summary>
61116
public int Channels { get; } = 1;
62117

118+
/// <summary>
119+
/// Is the track ready?
120+
/// </summary>
63121
public bool IsReady { get { return true; } }
64122

123+
/// <summary>
124+
/// Payload type. PCMA uses static payload type 8.
125+
/// </summary>
65126
public int PayloadType { get; set; } = 8;
66127

128+
/// <summary>
129+
/// Build the SDP for this track.
130+
/// </summary>
131+
/// <param name="sdp">SDP <see cref="StringBuilder"/>.</param>
132+
/// <returns><see cref="StringBuilder"/>.</returns>
67133
public StringBuilder BuildSDP(StringBuilder sdp)
68134
{
69-
sdp.Append($"m=audio 0 RTP/AVP {PayloadType}\n"); // <---- Payload Type 8 means G711 ALAW
135+
sdp.Append($"m=audio 0 RTP/AVP {PayloadType}\n");
70136
sdp.Append($"a=control:trackID={ID}\n");
71-
sdp.Append($"a=rtpmap:{PayloadType} PCMA/{SamplingRate}\n");
137+
sdp.Append($"a=rtpmap:{PayloadType} {Codec}/{SamplingRate}\n");
72138
return sdp;
73139
}
74140

141+
/// <summary>
142+
/// Creates RTP packets.
143+
/// </summary>
144+
/// <param name="samples">An array of PCMA fragments. By default single fragment is expected.</param>
145+
/// <param name="rtpTimestamp">RTP timestamp in the timescale of the track.</param>
146+
/// <returns>RTP packets.</returns>
75147
public (List<Memory<byte>>, List<IMemoryOwner<byte>>) CreateRtpPackets(List<byte[]> samples, uint rtpTimestamp)
76148
{
77149
List<Memory<byte>> rtpPackets = new List<Memory<byte>>();

src/SharpRTSPServer/H264Track.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class H264Track : ITrack
1313
/// <summary>
1414
/// H264 Video Codec name.
1515
/// </summary>
16-
public const string VideoCodec = "H264";
16+
public string Codec => "H264";
1717

1818
/// <summary>
1919
/// Default video track clock rate.
@@ -152,7 +152,7 @@ public StringBuilder BuildSDP(StringBuilder sdp)
152152

153153
sdp.Append($"m=video 0 RTP/AVP {PayloadType}\n");
154154
sdp.Append($"a=control:trackID={ID}\n");
155-
sdp.Append($"a=rtpmap:{PayloadType} {VideoCodec}/{VideoClock}\n");
155+
sdp.Append($"a=rtpmap:{PayloadType} {Codec}/{VideoClock}\n");
156156
sdp.Append($"a=fmtp:{PayloadType} profile-level-id=").Append(profileLevelIdStr)
157157
.Append("; sprop-parameter-sets=").Append(spsStr).Append(',').Append(ppsStr).Append("\n");
158158
return sdp;

src/SharpRTSPServer/H265Track.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class H265Track : ITrack
1313
/// <summary>
1414
/// H265 Video Codec name.
1515
/// </summary>
16-
public const string VideoCodec = "H265";
16+
public string Codec => "H265";
1717

1818
/// <summary>
1919
/// Default video track clock rate.
@@ -121,7 +121,7 @@ public StringBuilder BuildSDP(StringBuilder sdp)
121121

122122
sdp.Append($"m=video 0 RTP/AVP {PayloadType}\n");
123123
sdp.Append($"a=control:trackID={ID}\n");
124-
sdp.Append($"a=rtpmap:{PayloadType} {VideoCodec}/{VideoClock}\n");
124+
sdp.Append($"a=rtpmap:{PayloadType} {Codec}/{VideoClock}\n");
125125
sdp.Append($"a=fmtp:{PayloadType} sprop-vps=").Append(vps_str).Append("; sprop-sps=").Append(sps_str).Append("; sprop-pps=").Append(pps_str).Append("\n");
126126
return sdp;
127127
}

src/SharpRTSPServer/RTSPServer.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ public void CheckTimeouts(out int currentRtspCount, out int currentRtspPlayCount
525525
/// <summary>
526526
/// Feed in Raw video samples (for H264/H265 this expects NALUs).
527527
/// </summary>
528-
/// <param name="rtpTimestamp">Timestamp.</param>
528+
/// <param name="rtpTimestamp">Timestamp in the RTP timescale units.</param>
529529
/// <param name="samples">Array of NALUs.</param>
530530
public void FeedInRawVideoSamples(uint rtpTimestamp, List<byte[]> samples)
531531
{
@@ -598,6 +598,11 @@ public void FeedInRawVideoSamples(uint rtpTimestamp, List<byte[]> samples)
598598
}
599599
}
600600

601+
/// <summary>
602+
/// Feed in Raw audio samples (for AAC this expects a single frame).
603+
/// </summary>
604+
/// <param name="rtpTimestamp">Timestamp in the RTP timescale units.</param>
605+
/// <param name="samples">Array of frames.</param>
601606
public void FeedInRawAudioSamples(uint rtpTimestamp, List<byte[]> samples)
602607
{
603608
CheckTimeouts(out _, out int currentRtspPlayCount);
@@ -838,6 +843,11 @@ public void UpdateKeepAlive()
838843

839844
public interface ITrack
840845
{
846+
/// <summary>
847+
/// Codec name.
848+
/// </summary>
849+
string Codec { get; }
850+
841851
/// <summary>
842852
/// Track ID. Used to identify the track in the SDP.
843853
/// </summary>

0 commit comments

Comments
 (0)