container/mts: BytesForMetaInterval renamed to TrimToMetaRange and now returns Clip for similar reasons to previous commit

This commit is contained in:
Saxon 2019-06-15 02:12:17 +09:30
parent ceee163b74
commit 2bd7a009ce
2 changed files with 14 additions and 9 deletions

View File

@ -192,9 +192,9 @@ var (
errMetaUpperBound = errors.New("meta 'to' cannot be found") errMetaUpperBound = errors.New("meta 'to' cannot be found")
) )
// BytesForMetaInterval will return the media data as a slice between two meta // TrimToMetaRange returns a sub Clip with meta range described by from and to
// values 'from' and 'to', of key 'key'. The meta values must not be the same. // with key 'key'. The meta values must not be equivalent.
func (c *Clip) BytesForMetaInterval(key, from, to string) ([]byte, error) { func (c *Clip) TrimToMetaRange(key, from, to string) (*Clip, error) {
// First check that the interval makes sense. // First check that the interval makes sense.
if from == to { if from == to {
return nil, errMetaRange return nil, errMetaRange
@ -205,6 +205,7 @@ func (c *Clip) BytesForMetaInterval(key, from, to string) ([]byte, error) {
// Try and find from. // Try and find from.
for i := 0; i < len(c.frames); i++ { for i := 0; i < len(c.frames); i++ {
f := c.frames[i] f := c.frames[i]
startFrameIdx := i
if f.Meta[key] == from { if f.Meta[key] == from {
start = f.idx start = f.idx
@ -213,7 +214,11 @@ func (c *Clip) BytesForMetaInterval(key, from, to string) ([]byte, error) {
f = c.frames[i] f = c.frames[i]
if f.Meta[key] == to { if f.Meta[key] == to {
end = f.idx end = f.idx
return c.backing[start : end+len(f.Media)], nil endFrameIdx := i
return &Clip{
frames: c.frames[startFrameIdx : endFrameIdx+1],
backing: c.backing[start : end+len(f.Media)],
}, nil
} }
} }
return nil, errMetaUpperBound return nil, errMetaUpperBound

View File

@ -316,9 +316,9 @@ func TestTrimToPTSRange(t *testing.T) {
} }
} }
// TestBytesForMetaInterval checks that we can correctly get media data between // TestTrimToMetaRange checks that Clip.TrimToMetaRange correctly provides a
// two meta values using BytesForMetaInterval. // sub Clip for a given meta range.
func TestBytesForMetaInterval(t *testing.T) { func TestTrimToMetaRange(t *testing.T) {
const ( const (
numOfTestFrames = 10 numOfTestFrames = 10
ptsInterval = 4 ptsInterval = 4
@ -382,7 +382,7 @@ func TestBytesForMetaInterval(t *testing.T) {
// Run tests. // Run tests.
for i, test := range tests { for i, test := range tests {
got, err := clip.BytesForMetaInterval(key, test.from, test.to) got, err := clip.TrimToMetaRange(key, test.from, test.to)
// First check the error. // First check the error.
if err != nil && err != test.err { if err != nil && err != test.err {
@ -394,7 +394,7 @@ func TestBytesForMetaInterval(t *testing.T) {
} }
// Now check data. // Now check data.
if test.err == nil && !bytes.Equal(test.expect, got) { if test.err == nil && !bytes.Equal(test.expect, got.Bytes()) {
t.Errorf("did not get expected data for test: %v\n Got: %v\n, Want: %v\n", i, got, test.expect) t.Errorf("did not get expected data for test: %v\n Got: %v\n, Want: %v\n", i, got, test.expect)
} }
} }