-
Notifications
You must be signed in to change notification settings - Fork 408
/
SDPMediaAnnouncement.cs
567 lines (489 loc) · 22.4 KB
/
SDPMediaAnnouncement.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
//-----------------------------------------------------------------------------
// Filename: SDPMediaAnnouncement.cs
//
// Description:
//
// Remarks:
//
// An example of an "application" type media announcement use is negotiating
// SCTP-over-DTLS which acts as the transport for WebRTC data channels.
// https://tools.ietf.org/html/rfc8841
// "Session Description Protocol (SDP) Offer/Answer Procedures for Stream
// Control Transmission Protocol (SCTP) over Datagram Transport Layer
// Security (DTLS) Transport"
//
// Author(s):
// Aaron Clauson (aaron@sipsorcery.com)
// Jacek Dzija
// Mateusz Greczek
//
// History:
// ?? Aaron Clauson Created, Hobart, Australia.
// rj2: add SDPSecurityDescription parser
// 30 Mar 2021 Jacek Dzija,Mateusz Greczek Added MSRP
//
// License:
// BSD 3-Clause "New" or "Revised" License, see included LICENSE.md file.
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Logging;
using SIPSorcery.net.RTP;
using SIPSorceryMedia.Abstractions;
namespace SIPSorcery.Net
{
/// <summary>
/// An attribute used to defined additional properties about
/// a media source and the relationship between them.
/// As specified in RFC5576, https://tools.ietf.org/html/rfc5576.
/// </summary>
public class SDPSsrcAttribute
{
public const string MEDIA_CNAME_ATTRIBUE_PREFIX = "cname";
public uint SSRC { get; set; }
public string Cname { get; set; }
public string GroupID { get; set; }
/// <summary>
/// Default constructor.
/// </summary>
/// <param name="ssrc">The SSRC that should match an RTP stream.</param>
/// <param name="cname">Optional. The CNAME value to use in RTCP SDES sections.</param>
/// <param name="groupID">Optional. If this "ssrc" attribute is part of a
/// group this is the group ID.</param>
public SDPSsrcAttribute(uint ssrc, string cname, string groupID)
{
SSRC = ssrc;
Cname = cname;
GroupID = groupID;
}
}
public class SDPMediaAnnouncement
{
public const string MEDIA_EXTENSION_MAP_ATTRIBUE_PREFIX = "a=extmap:";
public const string MEDIA_FORMAT_ATTRIBUTE_PREFIX = "a=rtpmap:";
public const string MEDIA_FORMAT_FEEDBACK_PREFIX = "a=rtcp-fb:";
public const string MEDIA_FORMAT_PARAMETERS_ATTRIBUE_PREFIX = "a=fmtp:";
public const string MEDIA_FORMAT_SSRC_ATTRIBUE_PREFIX = "a=ssrc:";
public const string MEDIA_FORMAT_SSRC_GROUP_ATTRIBUE_PREFIX = "a=ssrc-group:";
public const string MEDIA_FORMAT_SCTP_MAP_ATTRIBUE_PREFIX = "a=sctpmap:";
public const string MEDIA_FORMAT_SCTP_PORT_ATTRIBUE_PREFIX = "a=sctp-port:";
public const string MEDIA_FORMAT_MAX_MESSAGE_SIZE_ATTRIBUE_PREFIX = "a=max-message-size:";
public const string MEDIA_FORMAT_PATH_MSRP_PREFIX = "a=path:msrp:";
public const string MEDIA_FORMAT_PATH_ACCEPT_TYPES_PREFIX = "a=accept-types:";
public const string TIAS_BANDWIDTH_ATTRIBUE_PREFIX = "b=TIAS:";
public const MediaStreamStatusEnum DEFAULT_STREAM_STATUS = MediaStreamStatusEnum.SendRecv;
public const string m_CRLF = "\r\n";
private static ILogger logger = SIPSorcery.Sys.Log.Logger;
public SDPConnectionInformation Connection;
// Media Announcement fields.
public SDPMediaTypesEnum Media = SDPMediaTypesEnum.audio; // Media type for the stream.
public int Port; // For UDP transports should be in the range 1024 to 65535 and for RTP compliance should be even (only even ports used for data).
public string Transport = "RTP/AVP"; // Defined types RTP/AVP (RTP Audio Visual Profile) and udp.
public string IceUfrag; // If ICE is being used the username for the STUN requests.
public string IcePwd; // If ICE is being used the password for the STUN requests.
public string IceOptions; // Optional attribute to specify support ICE options, e.g. "trickle".
public bool IceEndOfCandidates; // If ICE candidate trickling is being used this needs to be set if all candidates have been gathered.
public IceRolesEnum? IceRole = null;
public string DtlsFingerprint; // If DTLS handshake is being used this is the fingerprint or our DTLS certificate.
public int MLineIndex = 0;
/// <summary>
/// If being used in a bundle this the ID for the announcement.
/// Example: a=mid:audio or a=mid:video.
/// </summary>
public string MediaID;
/// <summary>
/// The "ssrc" attributes group ID as specified in RFC5576.
/// </summary>
public string SsrcGroupID;
/// <summary>
/// The "sctpmap" attribute defined in https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-26 for
/// use in WebRTC data channels.
/// </summary>
public string SctpMap;
/// <summary>
/// The "sctp-port" attribute defined in https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-26 for
/// use in WebRTC data channels.
/// </summary>
public ushort? SctpPort = null;
/// <summary>
/// The "max-message-size" attribute defined in https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-26 for
/// use in WebRTC data channels.
/// </summary>
public long MaxMessageSize = 0;
/// <summary>
/// If the RFC5576 is being used this is the list of "ssrc" attributes
/// supplied.
/// </summary>
public List<SDPSsrcAttribute> SsrcAttributes = new List<SDPSsrcAttribute>();
/// <summary>
/// Optional Transport Independent Application Specific Maximum (TIAS) bandwidth.
/// </summary>
public uint TIASBandwidth = 0;
public List<string> BandwidthAttributes = new List<string>();
/// <summary>
/// In media definitions, "i=" fields are primarily intended for labelling media streams https://tools.ietf.org/html/rfc4566#page-12
/// </summary>
public string MediaDescription;
/// <summary>
/// For AVP these will normally be a media payload type as defined in the RTP Audio/Video Profile.
/// </summary>
public Dictionary<int, SDPAudioVideoMediaFormat> MediaFormats = new Dictionary<int, SDPAudioVideoMediaFormat>();
/// <summary>
/// a=extmap - Mapping for RTP header extensions
/// </summary>
public Dictionary<int, RTPHeaderExtension> HeaderExtensions = new Dictionary<int, RTPHeaderExtension>();
/// <summary>
/// For AVP these will normally be a media payload type as defined in the RTP Audio/Video Profile.
/// </summary>
public SDPMessageMediaFormat MessageMediaFormat = new SDPMessageMediaFormat();
/// <summary>
/// List of media formats for "application media announcements. Application media announcements have different
/// semantics to audio/video announcements. They can also use aribtrary strings as the format ID.
/// </summary>
public Dictionary<string, SDPApplicationMediaFormat> ApplicationMediaFormats = new Dictionary<string, SDPApplicationMediaFormat>();
public List<string> ExtraMediaAttributes = new List<string>(); // Attributes that were not recognised.
public List<SDPSecurityDescription> SecurityDescriptions = new List<SDPSecurityDescription>(); //2018-12-21 rj2: add a=crypto parsing etc.
public List<string> IceCandidates;
/// <summary>
/// The stream status of this media announcement.
/// </summary>
public MediaStreamStatusEnum? MediaStreamStatus { get; set; }
public SDPMediaAnnouncement()
{ }
public SDPMediaAnnouncement(int port)
{
Port = port;
}
public SDPMediaAnnouncement(SDPConnectionInformation connection)
{
Connection = connection;
}
public SDPMediaAnnouncement(SDPMediaTypesEnum mediaType, int port, List<SDPAudioVideoMediaFormat> mediaFormats)
{
Media = mediaType;
Port = port;
MediaStreamStatus = DEFAULT_STREAM_STATUS;
if (mediaFormats != null)
{
foreach (var fmt in mediaFormats)
{
if (!MediaFormats.ContainsKey(fmt.ID))
{
MediaFormats.Add(fmt.ID, fmt);
}
}
}
}
public SDPMediaAnnouncement(SDPMediaTypesEnum mediaType, int port, List<SDPApplicationMediaFormat> appMediaFormats)
{
Media = mediaType;
Port = port;
if (appMediaFormats != null)
{
foreach (var fmt in appMediaFormats)
{
if (!ApplicationMediaFormats.ContainsKey(fmt.ID))
{
ApplicationMediaFormats.Add(fmt.ID, fmt);
}
}
}
}
public SDPMediaAnnouncement(SDPMediaTypesEnum mediaType, SDPConnectionInformation connection, int port, SDPMessageMediaFormat messageMediaFormat)
{
Media = mediaType;
Port = port;
Connection = connection;
MessageMediaFormat = messageMediaFormat;
}
public void ParseMediaFormats(string formatList)
{
if (!String.IsNullOrWhiteSpace(formatList))
{
string[] formatIDs = Regex.Split(formatList, @"\s");
if (formatIDs != null)
{
foreach (string formatID in formatIDs)
{
if (Media == SDPMediaTypesEnum.application)
{
ApplicationMediaFormats.Add(formatID, new SDPApplicationMediaFormat(formatID));
}
else if (Media == SDPMediaTypesEnum.message)
{
//TODO
}
else
{
if (Int32.TryParse(formatID, out int id)
&& !MediaFormats.ContainsKey(id)
&& id < SDPAudioVideoMediaFormat.DYNAMIC_ID_MIN)
{
if (Enum.IsDefined(typeof(SDPWellKnownMediaFormatsEnum), id) &&
Enum.TryParse<SDPWellKnownMediaFormatsEnum>(formatID, out var wellKnown))
{
MediaFormats.Add(id, new SDPAudioVideoMediaFormat(wellKnown));
}
else
{
logger.LogWarning($"Excluding unrecognised well known media format ID {id}.");
}
}
}
}
}
}
}
public override string ToString()
{
string announcement = "m=" + Media + " " + Port + " " + Transport + " " + GetFormatListToString() + m_CRLF;
announcement += !string.IsNullOrWhiteSpace(MediaDescription) ? "i=" + MediaDescription + m_CRLF : null;
announcement += (Connection == null) ? null : Connection.ToString();
if (TIASBandwidth > 0)
{
announcement += TIAS_BANDWIDTH_ATTRIBUE_PREFIX + TIASBandwidth + m_CRLF;
}
foreach (string bandwidthAttribute in BandwidthAttributes)
{
announcement += "b=" + bandwidthAttribute + m_CRLF;
}
announcement += !string.IsNullOrWhiteSpace(IceUfrag) ? "a=" + SDP.ICE_UFRAG_ATTRIBUTE_PREFIX + ":" + IceUfrag + m_CRLF : null;
announcement += !string.IsNullOrWhiteSpace(IcePwd) ? "a=" + SDP.ICE_PWD_ATTRIBUTE_PREFIX + ":" + IcePwd + m_CRLF : null;
announcement += !string.IsNullOrWhiteSpace(DtlsFingerprint) ? "a=" + SDP.DTLS_FINGERPRINT_ATTRIBUTE_PREFIX + ":" + DtlsFingerprint + m_CRLF : null;
announcement += IceRole != null ? $"a={SDP.ICE_SETUP_ATTRIBUTE_PREFIX}:{IceRole}{m_CRLF}" : null;
if (IceCandidates?.Count() > 0)
{
foreach (var candidate in IceCandidates)
{
announcement += $"a={SDP.ICE_CANDIDATE_ATTRIBUTE_PREFIX}:{candidate}{m_CRLF}";
}
}
if (IceOptions != null)
{
announcement += $"a={SDP.ICE_OPTIONS}:" + IceOptions + m_CRLF;
}
if (IceEndOfCandidates)
{
announcement += $"a={SDP.END_ICE_CANDIDATES_ATTRIBUTE}" + m_CRLF;
}
announcement += !string.IsNullOrWhiteSpace(MediaID) ? "a=" + SDP.MEDIA_ID_ATTRIBUTE_PREFIX + ":" + MediaID + m_CRLF : null;
announcement += GetFormatListAttributesToString();
announcement += string.Join("", HeaderExtensions.Select(x => $"{MEDIA_EXTENSION_MAP_ATTRIBUE_PREFIX}{x.Value.Id} {x.Value.Uri}{m_CRLF}"));
foreach (string extra in ExtraMediaAttributes)
{
announcement += string.IsNullOrWhiteSpace(extra) ? null : extra + m_CRLF;
}
foreach (SDPSecurityDescription desc in this.SecurityDescriptions)
{
announcement += desc.ToString() + m_CRLF;
}
if (MediaStreamStatus != null)
{
announcement += MediaStreamStatusType.GetAttributeForMediaStreamStatus(MediaStreamStatus.Value) + m_CRLF;
}
if (SsrcGroupID != null && SsrcAttributes.Count > 0)
{
announcement += MEDIA_FORMAT_SSRC_GROUP_ATTRIBUE_PREFIX + SsrcGroupID;
foreach (var ssrcAttr in SsrcAttributes)
{
announcement += $" {ssrcAttr.SSRC}";
}
announcement += m_CRLF;
}
if (SsrcAttributes.Count > 0)
{
foreach (var ssrcAttr in SsrcAttributes)
{
if (!string.IsNullOrWhiteSpace(ssrcAttr.Cname))
{
announcement += $"{MEDIA_FORMAT_SSRC_ATTRIBUE_PREFIX}{ssrcAttr.SSRC} {SDPSsrcAttribute.MEDIA_CNAME_ATTRIBUE_PREFIX}:{ssrcAttr.Cname}" + m_CRLF;
}
else
{
announcement += $"{MEDIA_FORMAT_SSRC_ATTRIBUE_PREFIX}{ssrcAttr.SSRC}" + m_CRLF;
}
}
}
// If the "sctpmap" attribute is set, use it instead of the separate "sctpport" and "max-message-size"
// attributes. They both contain the same information. The "sctpmap" is the legacy attribute and if
// an application sets it then it's likely to be for a specific reason.
if (SctpMap != null)
{
announcement += $"{MEDIA_FORMAT_SCTP_MAP_ATTRIBUE_PREFIX}{SctpMap}" + m_CRLF;
}
else
{
if (SctpPort != null)
{
announcement += $"{MEDIA_FORMAT_SCTP_PORT_ATTRIBUE_PREFIX}{SctpPort}" + m_CRLF;
}
if (MaxMessageSize != 0)
{
announcement += $"{MEDIA_FORMAT_MAX_MESSAGE_SIZE_ATTRIBUE_PREFIX}{MaxMessageSize}" + m_CRLF;
}
}
return announcement;
}
public string GetFormatListToString()
{
if (Media == SDPMediaTypesEnum.application)
{
StringBuilder sb = new StringBuilder();
foreach (var appFormat in ApplicationMediaFormats)
{
sb.Append(appFormat.Key);
sb.Append(" ");
}
return sb.ToString().Trim();
}
else if (Media == SDPMediaTypesEnum.message)
{
return "*";
}
else
{
string mediaFormatList = null;
foreach (var mediaFormat in MediaFormats)
{
mediaFormatList += mediaFormat.Key + " ";
}
return (mediaFormatList != null) ? mediaFormatList.Trim() : null;
}
}
public string GetFormatListAttributesToString()
{
if (Media == SDPMediaTypesEnum.application)
{
if (ApplicationMediaFormats.Count > 0)
{
StringBuilder sb = new StringBuilder();
foreach (var appFormat in ApplicationMediaFormats)
{
if (appFormat.Value.Rtpmap != null)
{
sb.Append($"{MEDIA_FORMAT_ATTRIBUTE_PREFIX}{appFormat.Key} {appFormat.Value.Rtpmap}{m_CRLF}");
}
if (appFormat.Value.Fmtp != null)
{
sb.Append($"{MEDIA_FORMAT_PARAMETERS_ATTRIBUE_PREFIX}{appFormat.Key} {appFormat.Value.Fmtp}{m_CRLF}");
}
}
return sb.ToString();
}
else
{
return null;
}
}
else if (Media == SDPMediaTypesEnum.message)
{
StringBuilder sb = new StringBuilder();
var mediaFormat = MessageMediaFormat;
var acceptTypes = mediaFormat.AcceptTypes;
if (acceptTypes != null && acceptTypes.Count >0)
{
sb.Append(MEDIA_FORMAT_PATH_ACCEPT_TYPES_PREFIX);
foreach (var type in acceptTypes)
{
sb.Append($"{type} ");
}
sb.Append($"{m_CRLF}");
}
if (mediaFormat.Endpoint != null )
{
sb.Append($"{MEDIA_FORMAT_PATH_MSRP_PREFIX}//{Connection.ConnectionAddress}:{Port}/{mediaFormat.Endpoint}{m_CRLF}");
}
return sb.ToString();
}
else
{
string formatAttributes = null;
if (MediaFormats != null)
{
foreach (var mediaFormat in MediaFormats.Select(y => y.Value))
{
if (mediaFormat.Rtpmap == null)
{
// Well known media formats are not required to add an rtpmap but we do so any way as some SIP
// stacks don't work without it.
formatAttributes += MEDIA_FORMAT_ATTRIBUTE_PREFIX + mediaFormat.ID + " " + mediaFormat.Name() + "/" + mediaFormat.ClockRate() + m_CRLF;
}
else
{
formatAttributes += MEDIA_FORMAT_ATTRIBUTE_PREFIX + mediaFormat.ID + " " + mediaFormat.Rtpmap + m_CRLF;
}
foreach (var rtcpFeedbackMessage in mediaFormat.SupportedRtcpFeedbackMessages)
{
formatAttributes += MEDIA_FORMAT_FEEDBACK_PREFIX + mediaFormat.ID + " " + rtcpFeedbackMessage + m_CRLF;
}
if (mediaFormat.Fmtp != null)
{
formatAttributes += MEDIA_FORMAT_PARAMETERS_ATTRIBUE_PREFIX + mediaFormat.ID + " " + mediaFormat.Fmtp + m_CRLF;
}
}
}
return formatAttributes;
}
}
public void AddExtra(string attribute)
{
if (!string.IsNullOrWhiteSpace(attribute))
{
ExtraMediaAttributes.Add(attribute);
}
}
public bool HasCryptoLine(SDPSecurityDescription.CryptoSuites cryptoSuite)
{
if (this.SecurityDescriptions == null)
{
return false;
}
foreach (SDPSecurityDescription secdesc in this.SecurityDescriptions)
{
if (secdesc.CryptoSuite == cryptoSuite)
{
return true;
}
}
return false;
}
public SDPSecurityDescription GetCryptoLine(SDPSecurityDescription.CryptoSuites cryptoSuite)
{
if (this.SecurityDescriptions == null)
{
return null;
}
foreach (SDPSecurityDescription secdesc in this.SecurityDescriptions)
{
if (secdesc.CryptoSuite == cryptoSuite)
{
return secdesc;
}
}
return null;
}
public void AddCryptoLine(string crypto)
{
this.SecurityDescriptions.Add(SDPSecurityDescription.Parse(crypto));
}
/// <summary>
/// Attempts to locate a media format corresponding to telephone events. If available its
/// format ID is returned.
/// </summary>
/// <returns>If found the format ID for telephone events or -1 if not.</returns>
public int GetTelephoneEventFormatID()
{
foreach (var mediaFormat in MediaFormats.Values)
{
if (mediaFormat.Name()?.StartsWith(SDP.TELEPHONE_EVENT_ATTRIBUTE) == true)
{
return mediaFormat.ID;
}
}
return -1;
}
}
}