dash

package module
v1.29.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2025 License: OSL-3.0 Imports: 9 Imported by: 2

README

DASH

Some things are hidden for a reason, and if you see them, you'll be changed forever, but I wanted to be changed forever.

Miranda July

Go language, I need a package for DASH (MPD) files

  1. standard library only
  2. use a separate file for each type
  3. only send new or updated files
  4. package is named "dash"
  5. package will include a parse method, byte slice input
  6. BaseURL is a single element not a slice
  7. support these elements and attributes
    • MPD
      • @mediaPresentationDuration
      • BaseURL
      • Period
        • @duration
        • @id
        • BaseURL
        • AdaptationSet
          • @codecs
          • @height
          • @lang
          • @mimeType
          • @width
          • ContentProtection
            • @schemeIdUri
            • cenc:pssh
          • Role
            • @value
          • SegmentTemplate
            • @duration
            • @endNumber
            • @initialization
            • @media
            • @presentationTimeOffset
            • @startNumber
            • @timescale
            • SegmentTimeline
              • S
                • @d
                • @r
          • Representation
            • @bandwidth
            • @codecs
            • @height
            • @id
            • @mimeType
            • @width
            • BaseURL
            • SegmentTemplate
            • ContentProtection
            • SegmentBase
              • @indexRange
              • Initialization
                • @range
            • SegmentList
              • Initialization
                • @sourceURL
              • SegmentURL
                • @media
  8. add navigation
    1. from AdaptationSet to Period
    2. from Initialization to SegmentList
    3. from Period to MPD
    4. from Representation to AdaptationSet
    5. from SegmentList to Representation
    6. from SegmentTemplate to AdaptationSet
    7. from SegmentTemplate to Representation
    8. from SegmentURL to SegmentList
  9. resolve BaseURL using
    1. MPD URL
    2. all parent BaseURL
  10. resolve Initialization@sourceURL using
  11. MPD URL
  12. all parent BaseURL
  13. resolve SegmentTemplate@initialization using
  14. MPD URL
  15. all parent BaseURL
  16. resolve SegmentTemplate@media using
  17. MPD URL
  18. all parent BaseURL
  19. resolve SegmentURL@media using
  20. MPD URL
  21. all parent BaseURL
  22. resolve function should return *url.URL
  23. add method to get all Representation, group by id
  24. add method to get codecs
  25. add method to get height
  26. add method to get width
  27. add method to get mimeType
  28. AdaptationSet.Role is single element not slice
  29. add method to get SegmentTemplate
  30. add method to replace SegmentTemplate@initialization
  • $RepresentationID$
  1. add method to replace SegmentTemplate@media
  • $Number$
  • $Number%02d$
  • $Number%03d$
  • $Number%04d$
  • $Number%05d$
  • $Number%06d$
  • $Number%07d$
  • $Number%08d$
  • $Number%09d$
  • $RepresentationID$
  1. add method to replace SegmentTemplate@media
  • $RepresentationID$
  • $Time$
  1. SegmentTemplate@startNumber is 1 if missing
  2. SegmentTemplate@timescale is 1 if missing
  3. Period@duration is MPD@mediaPresentationDuration if missing
  4. add method to get Time values from SegmentTimeline
  5. add method to get Number values from SegmentTimeline
  6. add method to get Number values from SegmentTemplate@startNumber to SegmentTemplate@endNumber
  7. add method to get Number values from Ceil( AsSeconds(Period@duration) / (SegmentTemplate@duration / SegmentTemplate@timescale) )
  8. add a method that returns the SegmentTemplate URLs
  9. use SegmentTemplate@presentationTimeOffset as initial $Time$
  10. add method to get time.Duration
    time.ParseDuration(strings.ToLower(
       strings.TrimPrefix(Period@duration, "PT"),
    ))
    
  11. add method to get unique ContentProtection from
  • AdaptationSet
  • Representation
  1. include test file in same package
  • test will read all ".mpd" files in the "testdata" folder
  • user will provide test files
  • for each file, get the slice of replaced SegmentTemplate@media URLs
  • only get one slice of URLs per mimeType
  • print slice length, and first and last URLs
  1. add Representation.String method. each value on its own line
  2. Representation@bandwidth
  3. Representation.GetWidth
  4. Representation.GetHeight
  5. Representation.GetCodecs
  6. Representation.GetMimeType
  7. AdaptationSet@lang
  8. Role@value
  9. Period@id
  10. Representation@id

contact

email
27@riseup.net
Discord username
10308

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AdaptationSet

type AdaptationSet struct {
	Codecs            string               `xml:"codecs,attr"`
	Height            int                  `xml:"height,attr"`
	Lang              string               `xml:"lang,attr"`
	MimeType          string               `xml:"mimeType,attr"`
	Width             int                  `xml:"width,attr"`
	ContentProtection []*ContentProtection `xml:"ContentProtection"`
	Role              *Role                `xml:"Role"`
	SegmentTemplate   *SegmentTemplate     `xml:"SegmentTemplate"`
	Representations   []*Representation    `xml:"Representation"`

	// Navigation
	Parent *Period `xml:"-"`
}

AdaptationSet groups Representations.

type ContentProtection

type ContentProtection struct {
	SchemeIdUri string `xml:"schemeIdUri,attr"`
	// DefaultKID maps to cenc:default_KID (urn:mpeg:cenc:2013)
	DefaultKID string `xml:"urn:mpeg:cenc:2013 default_KID,attr"`
	// Pssh maps to the cenc:pssh element (urn:mpeg:cenc:2013)
	Pssh string `xml:"urn:mpeg:cenc:2013 pssh"`
}

ContentProtection specifies DRM schemes.

func (*ContentProtection) GetDefaultKID added in v1.29.5

func (cp *ContentProtection) GetDefaultKID() ([]byte, error)

GetDefaultKID returns the DefaultKID as a byte slice. It strips hyphens from the UUID string before decoding the hex.

func (*ContentProtection) GetPSSH added in v1.29.5

func (cp *ContentProtection) GetPSSH() ([]byte, error)

GetPSSH returns the PSSH data as a byte slice. It decodes the Base64 content of the element.

type Initialization added in v1.25.4

type Initialization struct {
	// Used in SegmentBase
	Range string `xml:"range,attr"`
	// Used in SegmentList
	SourceURL string `xml:"sourceURL,attr"`

	// Navigation
	Parent *SegmentList `xml:"-"`
}

Initialization contains URL and byte range information for initialization segments.

func (*Initialization) ResolveSourceURL added in v1.28.4

func (i *Initialization) ResolveSourceURL() (*url.URL, error)

ResolveSourceURL resolves the @sourceURL attribute against the parent SegmentList's context.

type MPD added in v1.28.4

type MPD struct {
	MediaPresentationDuration string    `xml:"mediaPresentationDuration,attr"`
	BaseURL                   string    `xml:"BaseURL"`
	Periods                   []*Period `xml:"Period"`

	// MPDURL is the source URL of the MPD file itself.
	// It is used as the root for resolving relative BaseURLs.
	MPDURL *url.URL `xml:"-"`
}

MPD represents the root element of the DASH MPD file. XMLName is omitted here to prevent SA5008 conflicts.

func Parse added in v1.28.4

func Parse(data []byte) (*MPD, error)

Parse takes a byte slice of an MPD file, unmarshals it, and populates the navigation parent pointers.

func (*MPD) GetRepresentations added in v1.28.7

func (m *MPD) GetRepresentations() map[string][]*Representation

GetRepresentations returns a map of all Representations in the MPD, keyed by their ID attribute.

func (*MPD) ResolveBaseURL added in v1.28.4

func (m *MPD) ResolveBaseURL() (*url.URL, error)

ResolveBaseURL resolves the MPD's BaseURL against the MPDURL.

type Period

type Period struct {
	Duration       string           `xml:"duration,attr"`
	ID             string           `xml:"id,attr"`
	BaseURL        string           `xml:"BaseURL"`
	AdaptationSets []*AdaptationSet `xml:"AdaptationSet"`

	// Navigation
	Parent *MPD `xml:"-"`
}

Period represents a temporal part of the media content.

func (*Period) GetDuration added in v1.28.4

func (p *Period) GetDuration() (time.Duration, error)

GetDuration parses the ISO 8601 Duration attribute. If Period@duration is missing, it falls back to MPD@mediaPresentationDuration.

func (*Period) ResolveBaseURL added in v1.28.4

func (p *Period) ResolveBaseURL() (*url.URL, error)

ResolveBaseURL resolves the Period's BaseURL against the parent MPD's resolved BaseURL.

type Representation

type Representation struct {
	Bandwidth         int                  `xml:"bandwidth,attr"`
	Codecs            string               `xml:"codecs,attr"`
	Height            int                  `xml:"height,attr"`
	ID                string               `xml:"id,attr"`
	MimeType          string               `xml:"mimeType,attr"`
	Width             int                  `xml:"width,attr"`
	BaseURL           string               `xml:"BaseURL"`
	SegmentTemplate   *SegmentTemplate     `xml:"SegmentTemplate"`
	ContentProtection []*ContentProtection `xml:"ContentProtection"`
	SegmentBase       *SegmentBase         `xml:"SegmentBase"`
	SegmentList       *SegmentList         `xml:"SegmentList"`

	// Navigation
	Parent *AdaptationSet `xml:"-"`
}

Representation describes a version of the media content.

func (*Representation) GetCodecs added in v1.28.4

func (r *Representation) GetCodecs() string

GetCodecs returns the codecs for this Representation. If the Codecs attribute is empty on the Representation, it returns the Codecs attribute from the parent AdaptationSet.

func (*Representation) GetContentProtection added in v1.28.8

func (r *Representation) GetContentProtection() []*ContentProtection

GetContentProtection returns the ContentProtection elements for this Representation. If the Representation has its own ContentProtection elements, they are returned. Otherwise, it returns the ContentProtection elements from the parent AdaptationSet.

func (*Representation) GetHeight added in v1.28.4

func (r *Representation) GetHeight() int

GetHeight returns the height for this Representation. If the Height attribute is 0 on the Representation, it returns the Height attribute from the parent AdaptationSet.

func (*Representation) GetMimeType added in v1.25.2

func (r *Representation) GetMimeType() string

GetMimeType returns the mimeType for this Representation. If the MimeType attribute is empty on the Representation, it returns the MimeType attribute from the parent AdaptationSet.

func (*Representation) GetSegmentTemplate added in v1.28.4

func (r *Representation) GetSegmentTemplate() *SegmentTemplate

GetSegmentTemplate returns the SegmentTemplate for this Representation. If the SegmentTemplate is nil on the Representation, it returns the SegmentTemplate from the parent AdaptationSet.

func (*Representation) GetWidth added in v1.28.4

func (r *Representation) GetWidth() int

GetWidth returns the width for this Representation. If the Width attribute is 0 on the Representation, it returns the Width attribute from the parent AdaptationSet.

func (*Representation) ResolveBaseURL added in v1.28.4

func (r *Representation) ResolveBaseURL() (*url.URL, error)

ResolveBaseURL resolves the Representation's BaseURL against the parent hierarchy.

func (*Representation) String

func (r *Representation) String() string

String returns a multi-line summary of the Representation. Fields: bandwidth, width, height, codecs, mimeType, lang, role, period, id. Optional fields are omitted if empty/zero.

type Role added in v1.28.4

type Role struct {
	Value string `xml:"value,attr"`
}

Role defines the role of the media content.

type S added in v1.28.4

type S struct {
	D uint `xml:"d,attr"` // Duration
	R int  `xml:"r,attr"` // Repeat count
}

S represents a segment within the timeline.

type SegmentBase

type SegmentBase struct {
	IndexRange     string          `xml:"indexRange,attr"`
	Initialization *Initialization `xml:"Initialization"`
}

SegmentBase defines base information for segments.

type SegmentList added in v1.26.1

type SegmentList struct {
	Duration       uint            `xml:"duration,attr"`
	Timescale      *uint           `xml:"timescale,attr"`
	Initialization *Initialization `xml:"Initialization"`
	SegmentURLs    []*SegmentURL   `xml:"SegmentURL"`
	// Navigation
	Parent *Representation `xml:"-"`
}

SegmentList contains a list of SegmentURLs.

func (*SegmentList) GetTimescale added in v1.29.7

func (sl *SegmentList) GetTimescale() uint

GetTimescale returns the Timescale if present, otherwise returns default 1.

type SegmentTemplate

type SegmentTemplate struct {
	Duration               uint   `xml:"duration,attr"`
	EndNumber              uint   `xml:"endNumber,attr"`
	Initialization         string `xml:"initialization,attr"`
	Media                  string `xml:"media,attr"`
	PresentationTimeOffset uint   `xml:"presentationTimeOffset,attr"`
	// StartNumber is a pointer to distinguish between missing (nil) and explicitly 0.
	StartNumber *uint `xml:"startNumber,attr"`
	// Timescale is a pointer to distinguish between missing (nil) and explicitly 0.
	Timescale       *uint            `xml:"timescale,attr"`
	SegmentTimeline *SegmentTimeline `xml:"SegmentTimeline"`

	// Navigation
	ParentAdaptationSet  *AdaptationSet  `xml:"-"`
	ParentRepresentation *Representation `xml:"-"`
}

SegmentTemplate defines specific rules for generating segment URLs.

func (*SegmentTemplate) GetDurationBasedNumbers added in v1.28.4

func (st *SegmentTemplate) GetDurationBasedNumbers() ([]uint, error)

GetDurationBasedNumbers calculates segment numbers based on Period duration.

func (*SegmentTemplate) GetNumberRange added in v1.28.4

func (st *SegmentTemplate) GetNumberRange() []uint

GetNumberRange returns a slice of numbers from StartNumber to EndNumber (inclusive).

func (*SegmentTemplate) GetSegmentURLs added in v1.28.4

func (st *SegmentTemplate) GetSegmentURLs(rep *Representation) ([]*url.URL, error)

GetSegmentURLs returns all segment URLs defined by this template.

func (*SegmentTemplate) GetStartNumber added in v1.28.4

func (st *SegmentTemplate) GetStartNumber() uint

GetStartNumber returns the StartNumber if present, otherwise returns default 1.

func (*SegmentTemplate) GetTimelineNumbers added in v1.28.4

func (st *SegmentTemplate) GetTimelineNumbers() []uint

GetTimelineNumbers returns a slice of segment numbers derived from the SegmentTimeline.

func (*SegmentTemplate) GetTimelineTimes added in v1.28.4

func (st *SegmentTemplate) GetTimelineTimes() []uint

GetTimelineTimes returns a slice of start times for segments derived from the SegmentTimeline.

func (*SegmentTemplate) GetTimescale added in v1.28.4

func (st *SegmentTemplate) GetTimescale() uint

GetTimescale returns the Timescale if present, otherwise returns default 1.

func (*SegmentTemplate) ResolveInitialization added in v1.28.4

func (st *SegmentTemplate) ResolveInitialization(rep *Representation) (*url.URL, error)

ResolveInitialization resolves the @initialization attribute.

func (*SegmentTemplate) ResolveMedia added in v1.28.4

func (st *SegmentTemplate) ResolveMedia(rep *Representation, number int) (*url.URL, error)

ResolveMedia resolves the @media attribute for number-based addressing.

func (*SegmentTemplate) ResolveMediaTime added in v1.28.4

func (st *SegmentTemplate) ResolveMediaTime(rep *Representation, timeVal int) (*url.URL, error)

ResolveMediaTime resolves the @media attribute for time-based addressing.

type SegmentTimeline added in v1.28.4

type SegmentTimeline struct {
	S []*S `xml:"S"`
}

SegmentTimeline defines specific timing for segments.

type SegmentURL added in v1.28.4

type SegmentURL struct {
	Media string `xml:"media,attr"`

	// Navigation
	Parent *SegmentList `xml:"-"`
}

SegmentURL defines a specific media segment source.

func (*SegmentURL) ResolveMedia added in v1.28.4

func (su *SegmentURL) ResolveMedia() (*url.URL, error)

ResolveMedia resolves the @media attribute against the parent SegmentList's context.

Directories

Path Synopsis
blog

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL