103 lines
2.2 KiB
Go
103 lines
2.2 KiB
Go
|
package media
|
||
|
|
||
|
import (
|
||
|
"io"
|
||
|
"io/ioutil"
|
||
|
"os"
|
||
|
"sync"
|
||
|
"testing"
|
||
|
|
||
|
. "github.com/smartystreets/goconvey/convey"
|
||
|
)
|
||
|
|
||
|
func Test_Demux(t *testing.T) {
|
||
|
Convey("Demux", t, func() {
|
||
|
Convey("audio-only", func() {
|
||
|
reader, _ := os.Open("mpthreetest.mp3")
|
||
|
defer reader.Close()
|
||
|
|
||
|
demuxer := Demux(reader)
|
||
|
var audioStream *DemuxedStream
|
||
|
var err error
|
||
|
forloop:
|
||
|
for {
|
||
|
select {
|
||
|
case err = <-demuxer.Error():
|
||
|
break forloop
|
||
|
case stream := <-demuxer.Streams():
|
||
|
So(audioStream, ShouldBeNil)
|
||
|
So(stream.StreamId, ShouldEqual, 0)
|
||
|
So(stream.Pts, ShouldEqual, 0)
|
||
|
So(stream.CodecInfo.CodecName, ShouldEqual, "mp3")
|
||
|
So(stream.CodecInfo.Type, ShouldEqual, Audio)
|
||
|
audioStream = stream
|
||
|
}
|
||
|
}
|
||
|
So(err, ShouldBeNil)
|
||
|
|
||
|
audioReader := audioStream.Sub()
|
||
|
|
||
|
n, err := io.Copy(ioutil.Discard, audioReader)
|
||
|
So(err, ShouldBeNil)
|
||
|
So(n, ShouldBeGreaterThan, 0)
|
||
|
})
|
||
|
|
||
|
Convey("video and audio", func() {
|
||
|
reader, _ := os.Open("small.ogv")
|
||
|
defer reader.Close()
|
||
|
|
||
|
demuxer := Demux(reader)
|
||
|
var audioStream, videoStream *DemuxedStream
|
||
|
var err error
|
||
|
forloop:
|
||
|
for {
|
||
|
select {
|
||
|
case err = <-demuxer.Error():
|
||
|
break forloop
|
||
|
case stream := <-demuxer.Streams():
|
||
|
So(stream.Pts, ShouldEqual, 0)
|
||
|
switch stream.CodecInfo.Type {
|
||
|
case Audio:
|
||
|
So(stream.StreamId, ShouldEqual, 1)
|
||
|
So(audioStream, ShouldBeNil)
|
||
|
So(stream.CodecInfo.CodecName, ShouldEqual, "vorbis")
|
||
|
audioStream = stream
|
||
|
case Video:
|
||
|
So(stream.StreamId, ShouldEqual, 0)
|
||
|
So(videoStream, ShouldBeNil)
|
||
|
So(stream.CodecInfo.CodecName, ShouldEqual, "theora")
|
||
|
videoStream = stream
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
So(err, ShouldBeNil)
|
||
|
|
||
|
audioReader := audioStream.Sub()
|
||
|
videoReader := videoStream.Sub()
|
||
|
|
||
|
var videoN, audioN int64
|
||
|
var videoErr, audioErr error
|
||
|
|
||
|
var wg sync.WaitGroup
|
||
|
wg.Add(2)
|
||
|
go func() {
|
||
|
audioN, audioErr = io.Copy(ioutil.Discard, audioReader)
|
||
|
wg.Done()
|
||
|
}()
|
||
|
go func() {
|
||
|
videoN, videoErr = io.Copy(ioutil.Discard, videoReader)
|
||
|
wg.Done()
|
||
|
}()
|
||
|
wg.Wait()
|
||
|
|
||
|
t.Log("Audio read:", audioN)
|
||
|
t.Log("Video read:", videoN)
|
||
|
|
||
|
So(audioErr, ShouldBeNil)
|
||
|
So(audioN, ShouldBeGreaterThan, 0)
|
||
|
So(videoErr, ShouldBeNil)
|
||
|
So(videoN, ShouldBeGreaterThan, 0)
|
||
|
})
|
||
|
})
|
||
|
}
|