av/parser/parser_test.go

128 lines
3.1 KiB
Go
Raw Normal View History

2018-07-04 15:13:17 +03:00
/*
NAME
parser_test.go
DESCRIPTION
See Readme.md
AUTHOR
Saxon Nelson-Milton <saxon@ausocean.org>
LICENSE
parser_test.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
*/
package parser
import (
2018-07-04 15:49:45 +03:00
"fmt"
"io/ioutil"
2018-07-04 15:13:17 +03:00
"log"
2018-03-14 04:18:03 +03:00
"os"
"strconv"
"testing"
)
const (
2018-07-03 17:42:44 +03:00
mjpegInputFileName = "testInput/testInput.avi"
h264fName = "../../test/test-data/av/input/betterInput.h264"
nOfFrames = 4
outChanSize = 100
)
2018-07-03 17:42:44 +03:00
func TestH264Parser(t *testing.T) {
2018-07-04 15:13:17 +03:00
log.SetOutput(os.Stderr)
log.Println("Opening input file!")
2018-07-04 15:15:03 +03:00
inputFile, err := os.Open(h264fName)
2018-07-03 17:42:44 +03:00
if err != nil {
t.Errorf("Should not have got error opening file!")
2018-07-04 08:27:23 +03:00
return
2018-07-03 17:42:44 +03:00
}
2018-07-04 08:27:23 +03:00
log.Println("Reading data from file!")
data, err := ioutil.ReadAll(inputFile)
2018-07-03 17:42:44 +03:00
if err != nil {
t.Errorf("Should not have got read error!")
return
}
2018-07-04 08:27:23 +03:00
// Create 'parser' and start it up
2018-07-04 15:13:17 +03:00
log.Println("Creating parser!")
2018-07-04 08:27:23 +03:00
parser := NewH264Parser()
parser.SetOutputChan(make(chan []byte, outChanSize))
2018-07-03 17:42:44 +03:00
parser.Start()
for i, n := 0, 0; n <= nOfFrames; i++ {
2018-07-03 17:42:44 +03:00
select {
2018-07-04 08:27:23 +03:00
case parser.InputChan() <- data[i]:
case frame := <-parser.OutputChan():
2018-07-04 15:49:45 +03:00
path := fmt.Sprintf("testOutput/" + strconv.Itoa(n) + "h264_frame")
out, err := os.Create(path)
2018-07-04 08:27:23 +03:00
if err != nil {
2018-07-04 15:49:45 +03:00
t.Errorf("Unexpected error creating %q: %v", path, err)
2018-07-04 08:27:23 +03:00
return
}
2018-07-04 15:45:24 +03:00
out.Write(frame)
out.Close()
n++
2018-07-03 17:42:44 +03:00
default:
}
}
}
/*
2018-03-14 04:18:03 +03:00
func TestMJPEGParser(t *testing.T) {
2018-07-04 15:13:17 +03:00
log.Println("Opening input file!")
2018-03-14 04:18:03 +03:00
// Open the input file
inputFile, err := os.Open(testInputFileName)
if err != nil {
t.Errorf("Should not have got error opening file!")
}
2018-07-04 15:13:17 +03:00
log.Println("Getting file stats!")
2018-03-14 04:18:03 +03:00
stats, err := inputFile.Stat()
if err != nil {
t.Errorf("Could not get input file stats!")
return
}
2018-07-04 15:13:17 +03:00
log.Println("Creating space for file data!")
2018-03-14 04:18:03 +03:00
data := make([]byte, stats.Size())
_, err = inputFile.Read(data)
if err != nil {
t.Errorf("Should not have got read error!")
return
}
2018-07-04 15:13:17 +03:00
log.Println("Creating parser!")
2018-03-14 04:18:03 +03:00
parser := NewMJPEGParser(len(data) + 1)
parser.SetOutputChan(make(chan []byte, 10000))
parser.Start()
2018-07-04 15:13:17 +03:00
log.Printf("len(data): %v\n", len(data))
2018-03-14 04:18:03 +03:00
for i := range data {
parser.GetInputChan() <- data[i]
}
2018-07-04 15:13:17 +03:00
log.Println("Writing jpegs to files!")
2018-03-14 04:18:03 +03:00
for i := 0; len(parser.GetOutputChan()) > 0; i++ {
// Open a new output file
2018-07-04 15:45:24 +03:00
out, err := os.Create("testOutput/image" + strconv.Itoa(i) + ".jpeg")
2018-03-14 04:18:03 +03:00
if err != nil {
t.Errorf("Should not have got error creating output file!")
return
}
2018-07-04 15:45:24 +03:00
out.Write(<-parser.GetOutputChan())
out.Close()
2018-03-14 04:18:03 +03:00
}
}
2018-07-03 17:42:44 +03:00
*/