From 943559d062f6cead45f60618ff1fcef17fb44486 Mon Sep 17 00:00:00 2001 From: Brogan Date: Fri, 25 Aug 2023 15:39:30 -0700 Subject: [PATCH 1/2] Adds support for reading and writing a comment that comes after the xml prolog and before the root element of the XML. --- mpd/fixtures/comment.mpd | 3 +++ mpd/mpd.go | 1 + mpd/mpd_read_write.go | 44 ++++++++++++++++++++++++++++++++------ mpd/mpd_read_write_test.go | 17 +++++++++++++++ 4 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 mpd/fixtures/comment.mpd diff --git a/mpd/fixtures/comment.mpd b/mpd/fixtures/comment.mpd new file mode 100644 index 0000000..d8b1c73 --- /dev/null +++ b/mpd/fixtures/comment.mpd @@ -0,0 +1,3 @@ + + + diff --git a/mpd/mpd.go b/mpd/mpd.go index 5f00840..38827bf 100644 --- a/mpd/mpd.go +++ b/mpd/mpd.go @@ -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 { diff --git a/mpd/mpd_read_write.go b/mpd/mpd_read_write.go index 35d3f9f..dd9e41e 100644 --- a/mpd/mpd_read_write.go +++ b/mpd/mpd_read_write.go @@ -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 } @@ -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 } diff --git a/mpd/mpd_read_write_test.go b/mpd/mpd_read_write_test.go index 890905f..7effffb 100644 --- a/mpd/mpd_read_write_test.go +++ b/mpd/mpd_read_write_test.go @@ -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) { + 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 := ` + + +` + require.EqualString(t, answer, s) +} From 7bcf639d44582cc22407cf16c2a24a8a1d5d02e0 Mon Sep 17 00:00:00 2001 From: Brogan Date: Mon, 28 Aug 2023 09:58:02 -0700 Subject: [PATCH 2/2] add comments to more tests --- mpd/fixtures/segment_list.mpd | 1 + mpd/fixtures/truncate.mpd | 1 + mpd/segment_list_test.go | 3 ++- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/mpd/fixtures/segment_list.mpd b/mpd/fixtures/segment_list.mpd index 57aed4d..2fc7de9 100644 --- a/mpd/fixtures/segment_list.mpd +++ b/mpd/fixtures/segment_list.mpd @@ -1,4 +1,5 @@ + http://localhost:8002/dash/ diff --git a/mpd/fixtures/truncate.mpd b/mpd/fixtures/truncate.mpd index 0b1be61..bf92528 100644 --- a/mpd/fixtures/truncate.mpd +++ b/mpd/fixtures/truncate.mpd @@ -1,4 +1,5 @@ + diff --git a/mpd/segment_list_test.go b/mpd/segment_list_test.go index d8205be..237a759 100644 --- a/mpd/segment_list_test.go +++ b/mpd/segment_list_test.go @@ -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 @@ -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")