media_description.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package sdp
  2. import (
  3. "strconv"
  4. "strings"
  5. )
  6. // MediaDescription represents a media type.
  7. // https://tools.ietf.org/html/rfc4566#section-5.14
  8. type MediaDescription struct {
  9. // m=<media> <port>/<number of ports> <proto> <fmt> ...
  10. // https://tools.ietf.org/html/rfc4566#section-5.14
  11. MediaName MediaName
  12. // i=<session description>
  13. // https://tools.ietf.org/html/rfc4566#section-5.4
  14. MediaTitle *Information
  15. // c=<nettype> <addrtype> <connection-address>
  16. // https://tools.ietf.org/html/rfc4566#section-5.7
  17. ConnectionInformation *ConnectionInformation
  18. // b=<bwtype>:<bandwidth>
  19. // https://tools.ietf.org/html/rfc4566#section-5.8
  20. Bandwidth []Bandwidth
  21. // k=<method>
  22. // k=<method>:<encryption key>
  23. // https://tools.ietf.org/html/rfc4566#section-5.12
  24. EncryptionKey *EncryptionKey
  25. // a=<attribute>
  26. // a=<attribute>:<value>
  27. // https://tools.ietf.org/html/rfc4566#section-5.13
  28. Attributes []Attribute
  29. }
  30. // Attribute returns the value of an attribute and if it exists
  31. func (d *MediaDescription) Attribute(key string) (string, bool) {
  32. for _, a := range d.Attributes {
  33. if a.Key == key {
  34. return a.Value, true
  35. }
  36. }
  37. return "", false
  38. }
  39. // RangedPort supports special format for the media field "m=" port value. If
  40. // it may be necessary to specify multiple transport ports, the protocol allows
  41. // to write it as: <port>/<number of ports> where number of ports is a an
  42. // offsetting range.
  43. type RangedPort struct {
  44. Value int
  45. Range *int
  46. }
  47. func (p *RangedPort) String() string {
  48. output := strconv.Itoa(p.Value)
  49. if p.Range != nil {
  50. output += "/" + strconv.Itoa(*p.Range)
  51. }
  52. return output
  53. }
  54. // MediaName describes the "m=" field storage structure.
  55. type MediaName struct {
  56. Media string
  57. Port RangedPort
  58. Protos []string
  59. Formats []string
  60. }
  61. func (m MediaName) String() string {
  62. return strings.Join([]string{
  63. m.Media,
  64. m.Port.String(),
  65. strings.Join(m.Protos, "/"),
  66. strings.Join(m.Formats, " "),
  67. }, " ")
  68. }