encode.go (1408B)
1 package sdp 2 3 import ( 4 "bytes" 5 "fmt" 6 "strings" 7 ) 8 9 func (s Session) MarshalText() ([]byte, error) { 10 buf := &strings.Builder{} 11 fmt.Fprintln(buf, "v=0") 12 13 if s.Origin.Username == "" { 14 s.Origin.Username = NoUsername 15 } 16 17 fmt.Fprintln(buf, s.Origin) 18 19 fmt.Fprintf(buf, "s=%s\n", s.Name) 20 21 if s.Info != "" { 22 fmt.Fprintf(buf, "i=%s\n", s.Info) 23 } 24 if s.URI != nil { 25 fmt.Fprintf(buf, "u=%s\n", s.URI) 26 } 27 if s.Email != nil { 28 // Remove quotes from mail.Address.String() to be 29 // identical to examples in RFC 8866. 30 fmt.Fprintf(buf, "e=%s\n", strings.ReplaceAll(s.Email.String(), `"`, "")) 31 } 32 if s.Phone != "" { 33 fmt.Fprintf(buf, "p=%s\n", s.Phone) 34 } 35 if s.Connection != nil { 36 fmt.Fprintln(buf, s.Connection) 37 } 38 if s.Bandwidth != nil { 39 fmt.Fprintln(buf, s.Bandwidth) 40 } 41 42 if s.Time[0].IsZero() { 43 fmt.Fprintln(buf, "t=0 0") 44 } else { 45 fmt.Fprintf(buf, "t=%d %d\n", s.Time[0].Unix()+sinceTimeZero, s.Time[1].Unix()+sinceTimeZero) 46 } 47 48 if s.Repeat != nil { 49 fmt.Fprintln(buf, s.Repeat) 50 } 51 52 if s.Adjustments != nil { 53 adj := make([]string, len(s.Adjustments)) 54 for i := range s.Adjustments { 55 adj[i] = s.Adjustments[i].String() 56 } 57 fmt.Fprintf(buf, "z=%s\n", strings.Join(adj, " ")) 58 } 59 60 if s.Attributes != nil { 61 fmt.Fprintf(buf, "a=%s\n", strings.Join(s.Attributes, " ")) 62 } 63 64 for _, m := range s.Media { 65 fmt.Fprintln(buf, m) 66 } 67 68 return bytes.TrimSpace([]byte(buf.String())), nil 69 }