2019-11-02 03:24:50 +03:00
|
|
|
/*
|
|
|
|
DESCRIPTION
|
2019-11-04 11:57:05 +03:00
|
|
|
geovision.go provides an implementation of the AVDevice interface for the
|
|
|
|
GeoVision IP camera.
|
2019-11-02 03:24:50 +03:00
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
2019-11-04 11:57:05 +03:00
|
|
|
Copyright (C) 2019 the Australian Ocean Lab (AusOcean)
|
2019-11-02 03:24:50 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
*/
|
|
|
|
|
2019-11-06 09:57:10 +03:00
|
|
|
package geovision
|
2019-11-02 03:24:50 +03:00
|
|
|
|
2019-11-04 08:17:18 +03:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-11-04 11:57:05 +03:00
|
|
|
"net"
|
2019-11-06 09:57:10 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-11-04 11:42:13 +03:00
|
|
|
"time"
|
2019-11-02 03:24:50 +03:00
|
|
|
|
2019-11-04 08:17:18 +03:00
|
|
|
"bitbucket.org/ausocean/av/codec/codecutil"
|
2019-11-06 09:57:10 +03:00
|
|
|
"bitbucket.org/ausocean/av/device"
|
2019-11-08 09:59:01 +03:00
|
|
|
gvconfig "bitbucket.org/ausocean/av/device/geovision/config"
|
2019-11-04 11:42:13 +03:00
|
|
|
"bitbucket.org/ausocean/av/protocol/rtcp"
|
|
|
|
"bitbucket.org/ausocean/av/protocol/rtp"
|
|
|
|
"bitbucket.org/ausocean/av/protocol/rtsp"
|
2019-11-08 09:59:01 +03:00
|
|
|
avconfig "bitbucket.org/ausocean/av/revid/config"
|
2019-11-04 11:42:13 +03:00
|
|
|
"bitbucket.org/ausocean/utils/logger"
|
2019-11-04 08:17:18 +03:00
|
|
|
)
|
|
|
|
|
2019-11-06 09:57:10 +03:00
|
|
|
// Indicate package when logging.
|
|
|
|
const pkg = "geovision: "
|
|
|
|
|
|
|
|
// Constants for real time clients.
|
|
|
|
const (
|
|
|
|
rtpPort = 60000
|
|
|
|
rtcpPort = 60001
|
|
|
|
defaultServerRTCPPort = 17301
|
|
|
|
)
|
|
|
|
|
2019-11-08 09:59:01 +03:00
|
|
|
// TODO: remove this when config has configurable user and pass.
|
2019-11-06 09:57:10 +03:00
|
|
|
const (
|
|
|
|
ipCamUser = "admin"
|
|
|
|
ipCamPass = "admin"
|
|
|
|
)
|
|
|
|
|
2019-11-05 12:44:04 +03:00
|
|
|
// Configuration defaults.
|
2019-11-04 08:17:18 +03:00
|
|
|
const (
|
2019-11-06 09:57:10 +03:00
|
|
|
defaultCameraIP = "192.168.1.50"
|
|
|
|
defaultCodec = codecutil.H264
|
|
|
|
defaultHeight = 720
|
|
|
|
defaultFrameRate = 25
|
|
|
|
defaultBitrate = 400
|
|
|
|
defaultVBRBitrate = 400
|
2019-11-22 05:35:11 +03:00
|
|
|
defaultMinFrames = 3
|
2019-11-08 09:59:01 +03:00
|
|
|
defaultVBRQuality = avconfig.QualityStandard
|
2019-11-06 09:57:10 +03:00
|
|
|
defaultCameraChan = 2
|
2019-11-04 08:17:18 +03:00
|
|
|
)
|
|
|
|
|
2019-11-05 12:44:04 +03:00
|
|
|
// Configuration field errors.
|
2019-11-04 08:17:18 +03:00
|
|
|
var (
|
|
|
|
errGVBadCameraIP = errors.New("camera IP bad or unset, defaulting")
|
|
|
|
errGVBadCodec = errors.New("codec bad or unset, defaulting")
|
|
|
|
errGVBadFrameRate = errors.New("frame rate bad or unset, defaulting")
|
|
|
|
errGVBadBitrate = errors.New("bitrate bad or unset, defaulting")
|
|
|
|
errGVBadVBRQuality = errors.New("VBR quality bad or unset, defaulting")
|
|
|
|
errGVBadHeight = errors.New("height bad or unset, defaulting")
|
|
|
|
errGVBadMinFrames = errors.New("min frames bad or unset, defaulting")
|
|
|
|
)
|
|
|
|
|
2019-11-05 12:44:04 +03:00
|
|
|
// GeoVision is an implementation of the AVDevice interface for a GeoVision
|
|
|
|
// IP camera. This has been designed to implement the GV-BX4700-8F in particular.
|
|
|
|
// Any other models are untested.
|
2019-11-04 08:17:18 +03:00
|
|
|
type GeoVision struct {
|
2019-11-08 09:59:01 +03:00
|
|
|
cfg avconfig.Config
|
|
|
|
log avconfig.Logger
|
2019-11-04 11:57:05 +03:00
|
|
|
rtpClt *rtp.Client
|
|
|
|
rtspClt *rtsp.Client
|
|
|
|
rtcpClt *rtcp.Client
|
2019-11-04 08:17:18 +03:00
|
|
|
}
|
2019-11-02 03:24:50 +03:00
|
|
|
|
2019-11-05 12:44:04 +03:00
|
|
|
// NewGeoVision returns a new GeoVision.
|
2019-11-12 08:34:07 +03:00
|
|
|
func New(l avconfig.Logger) *GeoVision { return &GeoVision{log: l} }
|
2019-11-04 11:42:13 +03:00
|
|
|
|
2019-11-05 12:44:04 +03:00
|
|
|
// Set will take a Config struct, check the validity of the relevant fields
|
2019-11-08 09:59:01 +03:00
|
|
|
// and then performs any configuration necessary using config to control the
|
2019-11-05 12:44:04 +03:00
|
|
|
// GeoVision web interface. If fields are not valid, an error is added to the
|
|
|
|
// multiError and a default value is used for that particular field.
|
2019-11-08 09:59:01 +03:00
|
|
|
func (g *GeoVision) Set(c avconfig.Config) error {
|
2019-11-06 09:57:10 +03:00
|
|
|
var errs device.MultiError
|
2019-11-04 08:17:18 +03:00
|
|
|
if c.CameraIP == "" {
|
|
|
|
errs = append(errs, errGVBadCameraIP)
|
2019-11-06 09:57:10 +03:00
|
|
|
c.CameraIP = defaultCameraIP
|
2019-11-04 08:17:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch c.InputCodec {
|
|
|
|
case codecutil.H264, codecutil.H265, codecutil.MJPEG:
|
|
|
|
default:
|
|
|
|
errs = append(errs, errGVBadCodec)
|
2019-11-06 09:57:10 +03:00
|
|
|
c.InputCodec = defaultCodec
|
2019-11-04 08:17:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Height <= 0 {
|
|
|
|
errs = append(errs, errGVBadHeight)
|
2019-11-06 09:57:10 +03:00
|
|
|
c.Height = defaultHeight
|
2019-11-04 08:17:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.FrameRate <= 0 {
|
|
|
|
errs = append(errs, errGVBadFrameRate)
|
2019-11-06 09:57:10 +03:00
|
|
|
c.FrameRate = defaultFrameRate
|
2019-11-04 08:17:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Bitrate <= 0 {
|
|
|
|
errs = append(errs, errGVBadBitrate)
|
2019-11-06 09:57:10 +03:00
|
|
|
c.Bitrate = defaultBitrate
|
2019-11-04 08:17:18 +03:00
|
|
|
}
|
|
|
|
|
2019-11-22 05:35:11 +03:00
|
|
|
refresh := float64(c.MinFrames) / float64(c.FrameRate)
|
|
|
|
if refresh < 1 || refresh > 5 {
|
2019-11-04 08:17:18 +03:00
|
|
|
errs = append(errs, errGVBadMinFrames)
|
2019-11-22 05:35:11 +03:00
|
|
|
c.MinFrames = 4 * c.FrameRate
|
2019-11-04 08:17:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch c.VBRQuality {
|
2019-11-08 09:59:01 +03:00
|
|
|
case avconfig.QualityStandard, avconfig.QualityFair, avconfig.QualityGood, avconfig.QualityGreat, avconfig.QualityExcellent:
|
2019-11-04 08:17:18 +03:00
|
|
|
default:
|
|
|
|
errs = append(errs, errGVBadVBRQuality)
|
|
|
|
c.VBRQuality = defaultVBRQuality
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.VBRBitrate <= 0 {
|
|
|
|
errs = append(errs, errGVBadVBRQuality)
|
|
|
|
c.VBRBitrate = defaultVBRBitrate
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.CameraChan != 1 && c.CameraChan != 2 {
|
|
|
|
errs = append(errs, errGVBadVBRQuality)
|
|
|
|
c.CameraChan = defaultCameraChan
|
|
|
|
}
|
|
|
|
|
|
|
|
g.cfg = c
|
|
|
|
|
2019-11-08 09:59:01 +03:00
|
|
|
err := gvconfig.Set(
|
2019-11-04 08:17:18 +03:00
|
|
|
g.cfg.CameraIP,
|
2019-11-08 09:59:01 +03:00
|
|
|
gvconfig.Channel(g.cfg.CameraChan),
|
|
|
|
gvconfig.CodecOut(
|
|
|
|
map[uint8]gvconfig.Codec{
|
|
|
|
codecutil.H264: gvconfig.CodecH264,
|
|
|
|
codecutil.H265: gvconfig.CodecH265,
|
|
|
|
codecutil.MJPEG: gvconfig.CodecMJPEG,
|
2019-11-04 08:17:18 +03:00
|
|
|
}[g.cfg.InputCodec],
|
2019-11-02 03:24:50 +03:00
|
|
|
),
|
2019-11-08 09:59:01 +03:00
|
|
|
gvconfig.Height(int(g.cfg.Height)),
|
|
|
|
gvconfig.FrameRate(int(g.cfg.FrameRate)),
|
|
|
|
gvconfig.VariableBitrate(g.cfg.VBR),
|
|
|
|
gvconfig.VBRQuality(
|
|
|
|
map[avconfig.Quality]gvconfig.Quality{
|
|
|
|
avconfig.QualityStandard: gvconfig.QualityStandard,
|
|
|
|
avconfig.QualityFair: gvconfig.QualityFair,
|
|
|
|
avconfig.QualityGood: gvconfig.QualityGood,
|
|
|
|
avconfig.QualityGreat: gvconfig.QualityGreat,
|
|
|
|
avconfig.QualityExcellent: gvconfig.QualityExcellent,
|
2019-11-04 08:17:18 +03:00
|
|
|
}[g.cfg.VBRQuality],
|
2019-11-02 03:24:50 +03:00
|
|
|
),
|
2019-11-08 09:59:01 +03:00
|
|
|
gvconfig.VBRBitrate(g.cfg.VBRBitrate),
|
|
|
|
gvconfig.CBRBitrate(int(g.cfg.Bitrate)),
|
|
|
|
gvconfig.Refresh(float64(g.cfg.MinFrames)/float64(g.cfg.FrameRate)),
|
2019-11-02 03:24:50 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
2019-11-04 08:17:18 +03:00
|
|
|
return fmt.Errorf("could not set IPCamera settings: %w", err)
|
2019-11-02 03:24:50 +03:00
|
|
|
}
|
2019-11-04 11:42:13 +03:00
|
|
|
|
|
|
|
// Give the camera some time to change it's configuration.
|
|
|
|
const setupDelay = 5 * time.Second
|
|
|
|
time.Sleep(setupDelay)
|
|
|
|
|
2019-11-06 09:57:10 +03:00
|
|
|
return errs
|
2019-11-02 03:24:50 +03:00
|
|
|
}
|
2019-11-04 11:42:13 +03:00
|
|
|
|
2019-11-05 12:44:04 +03:00
|
|
|
// Start uses an RTSP client to communicate with the GeoVision RTSP server and
|
|
|
|
// request a stream that is then received by an RTP client, from which packets
|
|
|
|
// can be read from using the Read method.
|
2019-11-04 11:42:13 +03:00
|
|
|
func (g *GeoVision) Start() error {
|
2019-11-04 11:57:05 +03:00
|
|
|
var (
|
|
|
|
local, remote *net.TCPAddr
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
g.rtspClt, local, remote, err = rtsp.NewClient("rtsp://" + ipCamUser + ":" + ipCamPass + "@" + g.cfg.CameraIP + ":8554/" + "CH002.sdp")
|
2019-11-04 11:42:13 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not create RTSP client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
g.log.Log(logger.Info, pkg+"created RTSP client")
|
|
|
|
|
2019-11-04 11:57:05 +03:00
|
|
|
resp, err := g.rtspClt.Options()
|
2019-11-04 11:42:13 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("options request unsuccessful: %w", err)
|
|
|
|
}
|
|
|
|
g.log.Log(logger.Debug, pkg+"RTSP OPTIONS response", "response", resp.String())
|
|
|
|
|
2019-11-04 11:57:05 +03:00
|
|
|
resp, err = g.rtspClt.Describe()
|
2019-11-04 11:42:13 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("describe request unsuccessful: %w", err)
|
|
|
|
}
|
|
|
|
g.log.Log(logger.Debug, pkg+"RTSP DESCRIBE response", "response", resp.String())
|
|
|
|
|
2019-11-04 11:57:05 +03:00
|
|
|
resp, err = g.rtspClt.Setup("track1", fmt.Sprintf("RTP/AVP;unicast;client_port=%d-%d", rtpPort, rtcpPort))
|
2019-11-04 11:42:13 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("setup request unsuccessful: %w", err)
|
|
|
|
}
|
|
|
|
g.log.Log(logger.Debug, pkg+"RTSP SETUP response", "response", resp.String())
|
|
|
|
|
|
|
|
rtpCltAddr, rtcpCltAddr, rtcpSvrAddr, err := formAddrs(local, remote, *resp)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not format addresses: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
g.log.Log(logger.Info, pkg+"RTSP session setup complete")
|
|
|
|
|
2019-11-04 11:57:05 +03:00
|
|
|
g.rtpClt, err = rtp.NewClient(rtpCltAddr)
|
2019-11-04 11:42:13 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not create RTP client: %w", err)
|
|
|
|
}
|
|
|
|
|
2019-11-04 11:57:05 +03:00
|
|
|
g.rtcpClt, err = rtcp.NewClient(rtcpCltAddr, rtcpSvrAddr, g.rtpClt, g.log.Log)
|
2019-11-04 11:42:13 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not create RTCP client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
g.log.Log(logger.Info, pkg+"RTCP and RTP clients created")
|
|
|
|
|
|
|
|
// Check errors from RTCP client until it has stopped running.
|
|
|
|
go func() {
|
|
|
|
for {
|
2019-11-04 11:57:05 +03:00
|
|
|
err, ok := <-g.rtcpClt.Err()
|
2019-11-04 11:42:13 +03:00
|
|
|
if ok {
|
|
|
|
g.log.Log(logger.Warning, pkg+"RTCP error", "error", err.Error())
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Start the RTCP client.
|
2019-11-04 11:57:05 +03:00
|
|
|
g.rtcpClt.Start()
|
2019-11-04 11:42:13 +03:00
|
|
|
g.log.Log(logger.Info, pkg+"RTCP client started")
|
|
|
|
|
2019-11-04 11:57:05 +03:00
|
|
|
resp, err = g.rtspClt.Play()
|
2019-11-04 11:42:13 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("play request unsuccessful: %w", err)
|
|
|
|
}
|
|
|
|
g.log.Log(logger.Debug, pkg+"RTSP server PLAY response", "response", resp.String())
|
|
|
|
g.log.Log(logger.Info, pkg+"play requested, now receiving stream")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-05 12:44:04 +03:00
|
|
|
// Stop will close the RTSP, RTCP, and RTP connections and in turn end the
|
|
|
|
// stream from the GeoVision. Future reads using Read will result in error.
|
2019-11-04 11:57:05 +03:00
|
|
|
func (g *GeoVision) Stop() error {
|
|
|
|
err := g.rtpClt.Close()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not close RTP client: %w", err)
|
|
|
|
}
|
2019-11-04 11:42:13 +03:00
|
|
|
|
2019-11-04 11:57:05 +03:00
|
|
|
err = g.rtspClt.Close()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not close RTSP client: %w", err)
|
|
|
|
}
|
2019-11-04 11:42:13 +03:00
|
|
|
|
2019-11-04 11:57:05 +03:00
|
|
|
g.rtcpClt.Stop()
|
2019-11-04 11:42:13 +03:00
|
|
|
|
2019-11-04 11:57:05 +03:00
|
|
|
g.log.Log(logger.Info, pkg+"RTP, RTSP and RTCP clients stopped and closed")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-04 12:13:02 +03:00
|
|
|
|
2019-11-05 12:44:04 +03:00
|
|
|
// Read implements io.Reader. If the GeoVision has not been started an error is
|
|
|
|
// returned.
|
2019-11-04 12:13:02 +03:00
|
|
|
func (g *GeoVision) Read(p []byte) (int, error) {
|
2019-11-05 12:44:04 +03:00
|
|
|
if g.rtpClt != nil {
|
|
|
|
return g.rtpClt.Read(p)
|
|
|
|
}
|
|
|
|
return 0, errors.New("cannot read, GeoVision not streaming")
|
2019-11-04 12:13:02 +03:00
|
|
|
}
|
2019-11-06 09:57:10 +03:00
|
|
|
|
|
|
|
// formAddrs is a helper function to form the addresses for the RTP client,
|
|
|
|
// RTCP client, and the RTSP server's RTCP addr using the local, remote addresses
|
|
|
|
// of the RTSP conn, and the SETUP method response.
|
|
|
|
func formAddrs(local, remote *net.TCPAddr, setupResp rtsp.Response) (rtpCltAddr, rtcpCltAddr, rtcpSvrAddr string, err error) {
|
|
|
|
svrRTCPPort, err := parseSvrRTCPPort(setupResp)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", "", err
|
|
|
|
}
|
|
|
|
rtpCltAddr = strings.Split(local.String(), ":")[0] + ":" + strconv.Itoa(rtpPort)
|
|
|
|
rtcpCltAddr = strings.Split(local.String(), ":")[0] + ":" + strconv.Itoa(rtcpPort)
|
|
|
|
rtcpSvrAddr = strings.Split(remote.String(), ":")[0] + ":" + strconv.Itoa(svrRTCPPort)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseServerRTCPPort is a helper function to get the RTSP server's RTCP port.
|
|
|
|
func parseSvrRTCPPort(resp rtsp.Response) (int, error) {
|
|
|
|
transport := resp.Header.Get("Transport")
|
|
|
|
for _, p := range strings.Split(transport, ";") {
|
|
|
|
if strings.Contains(p, "server_port") {
|
|
|
|
port, err := strconv.Atoi(strings.Split(p, "-")[1])
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return port, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, errors.New("SETUP response did not provide RTCP port")
|
|
|
|
}
|