Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VIDROBOT-3415 Encode/Decode Shaka-Packager Comment #14

Merged
merged 2 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mpd/fixtures/comment.mpd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--Generated with https://github.com/shaka-project/shaka-packager version 288eddc863-release-->
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" profiles="urn:mpeg:dash:profile:isoff-live:2011" type="dynamic" availabilityStartTime="1970-01-01T00:00:00Z" minimumUpdatePeriod="PT5S" publishTime="1970-01-01T00:00:00Z"></MPD>
1 change: 1 addition & 0 deletions mpd/fixtures/segment_list.mpd
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--Generated with https://github.com/shaka-project/shaka-packager version 288eddc863-release-->
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" profiles="urn:mpeg:dash:profile:isoff-live:2011" type="static" mediaPresentationDuration="PT30.016S" minBufferTime="PT2.000S">
<Period>
<BaseURL>http://localhost:8002/dash/</BaseURL>
Expand Down
1 change: 1 addition & 0 deletions mpd/fixtures/truncate.mpd
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--Generated with https://github.com/shaka-project/shaka-packager version 288eddc863-release-->
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" profiles="urn:mpeg:dash:profile:isoff-live:2011" type="dynamic" minBufferTime="PT2S" availabilityStartTime="2019-12-03T20:57:14Z" minimumUpdatePeriod="PT5S" publishTime="2019-12-03T21:05:05Z" timeShiftBufferDepth="PT120S">
<Period id="0">
<AdaptationSet frameRate="90000/3000" id="0" segmentAlignment="true" maxWidth="720" contentType="video">
Expand Down
1 change: 1 addition & 0 deletions mpd/mpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type MPD struct {
Periods []*Period `xml:"Period,omitempty"`
UTCTiming *DescriptorType `xml:"UTCTiming,omitempty"`
ID string `xml:"id,attr,omitempty"`
Comment string `xml:"-"`
}

type XsiNS struct {
Expand Down
44 changes: 37 additions & 7 deletions mpd/mpd_read_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,24 @@ func ReadFromString(xmlStr string) (*MPD, error) {
func Read(r io.Reader) (*MPD, error) {
var mpd MPD
d := xml.NewDecoder(r)
err := d.Decode(&mpd)
var start *xml.StartElement
for {
token, err := d.Token()
if err != nil {
return nil, err
}
switch token := token.(type) {
case xml.Comment:
mpd.Comment = string(token.Copy())
case xml.StartElement:
start = &token
default:
}
if start != nil {
break
}
}
err := d.DecodeElement(&mpd, start)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -75,13 +92,26 @@ func (m *MPD) WriteToString() (string, error) {
// Writes an MPD object to an io.Writer interface
// w - Must implement the io.Writer interface.
func (m *MPD) Write(w io.Writer) error {
b, err := xml.MarshalIndent(m, "", " ")
_, err := w.Write([]byte(xml.Header))
if err != nil {
return err
}

_, _ = w.Write([]byte(xml.Header))
_, _ = w.Write(b)
_, _ = w.Write([]byte("\n"))
return nil
e := xml.NewEncoder(w)
e.Indent("", " ")
if string(m.Comment) != "" {
if err := e.EncodeToken(xml.Comment(m.Comment)); err != nil {
return err
}
if err := e.Flush(); err != nil {
return err
}
if _, err := w.Write([]byte("\n")); err != nil {
return err
}
}
if err := e.Encode(m); err != nil {
return err
}
_, err = w.Write([]byte("\n"))
return err
}
17 changes: 17 additions & 0 deletions mpd/mpd_read_write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,20 @@ func TestWriteToFileTruncate(t *testing.T) {
xmlStr = testfixtures.LoadFixture(out)
testfixtures.CompareFixture(t, "fixtures/truncate_short.mpd", xmlStr)
}

func TestReadComment(t *testing.T) {
broganross marked this conversation as resolved.
Show resolved Hide resolved
m, err := ReadFromFile("fixtures/comment.mpd")
require.NoError(t, err)
require.EqualString(t, "Generated with https://github.com/shaka-project/shaka-packager version 288eddc863-release", string(m.Comment))
}

func TestWriteComment(t *testing.T) {
m := MPD{Comment: "Leading Comment"}
s, err := m.WriteToString()
require.NoError(t, err)
answer := `<?xml version="1.0" encoding="UTF-8"?>
<!--Leading Comment-->
<MPD></MPD>
`
require.EqualString(t, answer, s)
}
3 changes: 2 additions & 1 deletion mpd/segment_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestSegmentListDeserialization(t *testing.T) {
require.NoError(t, err)
if err == nil {
expected := getSegmentListMPD()

require.EqualString(t, m.Comment, "Generated with https://github.com/shaka-project/shaka-packager version 288eddc863-release")
require.EqualString(t, expected.Periods[0].BaseURL, m.Periods[0].BaseURL)

expectedAudioSegList := expected.Periods[0].AdaptationSets[0].Representations[0].SegmentList
Expand Down Expand Up @@ -59,6 +59,7 @@ func TestSegmentListDeserialization(t *testing.T) {

func getSegmentListMPD() *MPD {
m := NewMPD(DASH_PROFILE_LIVE, "PT30.016S", "PT2.000S")
m.Comment = "Generated with https://github.com/shaka-project/shaka-packager version 288eddc863-release"
m.period.BaseURL = "http://localhost:8002/dash/"

aas, _ := m.AddNewAdaptationSetAudioWithID("1", "audio/mp4", true, 1, "English")
Expand Down
Loading