av/parser/parser_test.go

109 lines
2.6 KiB
Go

package parser
import (
"fmt"
"os"
"strconv"
"testing"
)
const (
mjpegInputFileName = "testInput/testInput.avi"
h264InputFileName = "bitbucket.org/ausocean/test/test-data/av/input/betterInput.h264"
)
func TestH264Parser(t *testing.T) {
fmt.Println("Opening input file!")
// Open the input file
inputFile, err := os.Open(h264InputFileName)
if err != nil {
t.Errorf("Should not have got error opening file!")
}
fmt.Println("Getting file stats!")
stats, err := inputFile.Stat()
if err != nil {
t.Errorf("Could not get input file stats!")
return
}
fmt.Println("Creating space for file data!")
data := make([]byte, stats.Size())
_, err = inputFile.Read(data)
if err != nil {
t.Errorf("Should not have got read error!")
return
}
fmt.Println("Creating parser!")
parser := NewH264Parser(len(data) + 1)
parser.SetOutputChan(make(chan []byte, 10000))
parser.Start()
fmt.Printf("len(data): %v\n", len(data))
count := 0
for i := range data {
parser.GetInputChan() <- data[i]
select {
case frame:=<-parser.GetOutputChan()
outputFile, err := os.Create("")
count++
if count > 4 {
return
}
default:
}
}
for i := 0; len(parser.GetOutputChan()) > 0; i++ {
// Open a new output file
outputFile, err := os.Create("testOutput/image" + strconv.Itoa(i) + ".jpeg")
if err != nil {
t.Errorf("Should not have got error creating output file!")
return
}
outputFile.Write(<-parser.GetOutputChan())
outputFile.Close()
}
}
/*
func TestMJPEGParser(t *testing.T) {
fmt.Println("Opening input file!")
// Open the input file
inputFile, err := os.Open(testInputFileName)
if err != nil {
t.Errorf("Should not have got error opening file!")
}
fmt.Println("Getting file stats!")
stats, err := inputFile.Stat()
if err != nil {
t.Errorf("Could not get input file stats!")
return
}
fmt.Println("Creating space for file data!")
data := make([]byte, stats.Size())
_, err = inputFile.Read(data)
if err != nil {
t.Errorf("Should not have got read error!")
return
}
fmt.Println("Creating parser!")
parser := NewMJPEGParser(len(data) + 1)
parser.SetOutputChan(make(chan []byte, 10000))
parser.Start()
fmt.Printf("len(data): %v\n", len(data))
for i := range data {
parser.GetInputChan() <- data[i]
}
fmt.Println("Writing jpegs to files!")
for i := 0; len(parser.GetOutputChan()) > 0; i++ {
// Open a new output file
outputFile, err := os.Create("testOutput/image" + strconv.Itoa(i) + ".jpeg")
if err != nil {
t.Errorf("Should not have got error creating output file!")
return
}
outputFile.Write(<-parser.GetOutputChan())
outputFile.Close()
}
}
*/