streaming

Media streaming and broadcast systems in Go
Log | Files | Refs | README | LICENSE

encode.go (2040B)


      1 package cmcd
      2 
      3 import (
      4 	"fmt"
      5 	"net/url"
      6 	"strings"
      7 	"time"
      8 )
      9 
     10 func (r Request) Encode() string {
     11 	var attrs []string
     12 	if r.BufLength > 0 {
     13 		a := fmt.Sprintf("bl=%d", r.BufLength.Round(100*time.Millisecond).Milliseconds())
     14 		attrs = append(attrs, a)
     15 	}
     16 	if r.Deadline > 0 {
     17 		a := fmt.Sprintf("dl=%d", r.Deadline.Round(100*time.Millisecond).Milliseconds())
     18 		attrs = append(attrs, a)
     19 	}
     20 	if r.Throughput > 0 {
     21 		attrs = append(attrs, fmt.Sprintf("mtp=%d", r.Throughput))
     22 	}
     23 	if r.Next != "" {
     24 		a := fmt.Sprintf("nor=%q", url.QueryEscape(r.Next))
     25 		attrs = append(attrs, a)
     26 	}
     27 	if r.NextRange != [2]int{0, 0} {
     28 		a := fmt.Sprintf("nrr=%q", r.NextRange)
     29 		attrs = append(attrs, a)
     30 	}
     31 	if r.Startup == true {
     32 		attrs = append(attrs, "su")
     33 	}
     34 	return strings.Join(attrs, ",")
     35 }
     36 
     37 func (o Object) Encode() string {
     38 	var attrs []string
     39 	if o.Bitrate > 0 {
     40 		attrs = append(attrs, fmt.Sprintf("br=%d", o.Bitrate))
     41 	}
     42 	if o.Duration > 0 {
     43 		a := fmt.Sprintf("d=%d", o.Duration.Milliseconds())
     44 		attrs = append(attrs, a)
     45 	}
     46 	if o.Type != "" {
     47 		// not a quoted string as these are reserved keywords.
     48 		attrs = append(attrs, fmt.Sprintf("ot=%s", o.Type))
     49 	}
     50 	if o.TopBitrate > 0 {
     51 		attrs = append(attrs, fmt.Sprintf("tb=%d", o.TopBitrate))
     52 	}
     53 	return strings.Join(attrs, ",")
     54 }
     55 
     56 func (s Status) Encode() string {
     57 	var attrs []string
     58 	if s.Starved {
     59 		attrs = append(attrs, "bs")
     60 	}
     61 	if s.MaxThroughput > 0 {
     62 		attrs = append(attrs, fmt.Sprintf("rtp=%d", s.MaxThroughput))
     63 	}
     64 	return strings.Join(attrs, ",")
     65 }
     66 
     67 func (s Session) Encode() string {
     68 	var attrs []string
     69 	if s.ID != "" {
     70 		attrs = append(attrs, fmt.Sprintf("sid=%q", s.ID))
     71 	}
     72 	if s.ContentID != "" {
     73 		attrs = append(attrs, fmt.Sprintf("cid=%q", s.ContentID))
     74 	}
     75 	// "SHOULD only be sent if not equal to 1": CTA-5004 page 10.
     76 	if s.PlayRate != RealTime {
     77 		attrs = append(attrs, fmt.Sprintf("pr=%v", s.PlayRate))
     78 	}
     79 	switch s.Format {
     80 	case FormatDASH, FormatHLS, FormatSmooth, FormatOther:
     81 		attrs = append(attrs, fmt.Sprintf("sf=%s", s.Format))
     82 	}
     83 	return strings.Join(attrs, ",")
     84 }