From 1323cbcae3c3df3f4e727814e370b5f97361a687 Mon Sep 17 00:00:00 2001 From: Saxon Date: Thu, 13 Jun 2019 16:30:39 +0930 Subject: [PATCH] container/mts: filled Clip.Bytes writing Clip.Bytes required a change to the Clip type. The Clip type now possess a slice that references the memory in which the Frames should reference for the media. Appropriate changes have been made to Extract and TestExtract to accomidate this change. --- container/mts/payload.go | 34 ++++++++++++++++++++-------------- container/mts/payload_test.go | 12 ++++++------ 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/container/mts/payload.go b/container/mts/payload.go index 2c9c79ac..f1f6bb6a 100644 --- a/container/mts/payload.go +++ b/container/mts/payload.go @@ -8,18 +8,16 @@ import ( ) // Extract extracts the media from an MPEG-TS clip given by p. -func Extract(p []byte) (Clip, error) { +func Extract(p []byte) (*Clip, error) { l := len(p) // Check that clip is divisible by 188, i.e. contains a series of full MPEG-TS clips. if l%PacketSize != 0 { return nil, errors.New("MTS clip is not of valid size") } - // This will hold a copy of all the media in the MPEG-TS clip. - buf := make([]byte, 0, l/PacketSize) - var ( - clip Clip // The data that will be returned. + frameStart int // Index used to indicate the start of current frame in backing slice. + clip = &Clip{} // The data that will be returned. meta map[string]string // Holds the most recently extracted meta. lenOfFrame int // Len of current frame. dataLen int // Len of data from MPEG-TS packet. @@ -29,6 +27,9 @@ func Extract(p []byte) (Clip, error) { err error ) + // This will hold a copy of all the media in the MPEG-TS clip. + clip.backing = make([]byte, 0, l/PacketSize) + // Go through the MPEGT-TS packets. var pkt packet.Packet for i := 0; i < l; i += PacketSize { @@ -61,40 +62,42 @@ func Extract(p []byte) (Clip, error) { // Extract the PTS and ID, then add a new frame to the clip. curPTS = _pes.PTS() curStreamID = _pes.StreamId() - clip = append(clip, Frame{ + clip.frames = append(clip.frames, Frame{ PTS: curPTS, ID: curStreamID, Meta: meta, }) // Append the data to the underlying buffer and get appended lenghth. - buf = append(buf, _pes.Data()...) + clip.backing = append(clip.backing, _pes.Data()...) dataLen = len(_pes.Data()) // If we haven't hit the first PUSI, then we know we have a full frame // and can add this data to the frame pertaining to the finish frame. if !firstPUSI { - clip[len(clip)-2].Media = buf[:lenOfFrame] - buf = buf[lenOfFrame:] - lenOfFrame = 0 + clip.frames[len(clip.frames)-2].Media = clip.backing[frameStart:lenOfFrame] + frameStart = lenOfFrame } firstPUSI = false } else { // We're not at the start of the frame, so we don't have a PES header. // We can append the MPEG-TS data directly to the underlying buf. dataLen = len(payload) - buf = append(buf, payload...) + clip.backing = append(clip.backing, payload...) } lenOfFrame += dataLen } } // We're finished up with media frames, so give the final Frame it's data. - clip[len(clip)-1].Media = buf[:lenOfFrame] + clip.frames[len(clip.frames)-1].Media = clip.backing[frameStart:lenOfFrame] return clip, nil } // Clip represents a clip of media, i.e. a sequence of media frames. -type Clip []Frame +type Clip struct { + frames []Frame + backing []byte +} // Frame describes a media frame that may be extracted from a PES packet. type Frame struct { @@ -106,5 +109,8 @@ type Frame struct { // Bytes returns the concatentated media bytes from each frame in the Clip c. func (c *Clip) Bytes() []byte { - return nil + if c.backing == nil { + panic("the clip backing array cannot be nil") + } + return c.backing } diff --git a/container/mts/payload_test.go b/container/mts/payload_test.go index bdbf0d21..05696a24 100644 --- a/container/mts/payload_test.go +++ b/container/mts/payload_test.go @@ -73,7 +73,7 @@ func TestExtract(t *testing.T) { } // Create an equivalent Frame and append to our Clip want. - want = append(want, Frame{ + want.frames = append(want.frames, Frame{ Media: frame, PTS: nextPTS, ID: H264ID, @@ -88,14 +88,14 @@ func TestExtract(t *testing.T) { } // Check length of got and want. - if len(want) != len(got) { - t.Fatalf("did not get expected length for got.\nGot: %v\n, Want: %v\n", len(got), len(want)) + if len(want.frames) != len(got.frames) { + t.Fatalf("did not get expected length for got.\nGot: %v\n, Want: %v\n", len(got.frames), len(want.frames)) } // Check frames individually. - for i, frame := range want { - if !reflect.DeepEqual(frame, got[i]) { - t.Fatalf("did not get expected result.\nGot: %v\n, Want: %v\n", got[i], frame) + for i, frame := range want.frames { + if !reflect.DeepEqual(frame, got.frames[i]) { + t.Fatalf("did not get expected result.\nGot: %v\n, Want: %v\n", got.frames[i], frame) } } }