2018-06-09 07:38:48 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
senders.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
|
|
|
Alan Noble <alan@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
revid is Copyright (C) 2017-2018 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 revid
|
|
|
|
|
|
|
|
import (
|
2019-03-10 10:18:18 +03:00
|
|
|
"errors"
|
2018-12-14 06:05:56 +03:00
|
|
|
"fmt"
|
2019-03-29 05:19:26 +03:00
|
|
|
"io"
|
2018-11-18 05:02:11 +03:00
|
|
|
"net"
|
2018-06-09 07:38:48 +03:00
|
|
|
"os"
|
2019-01-31 14:00:08 +03:00
|
|
|
"strconv"
|
2018-06-09 07:38:48 +03:00
|
|
|
|
2019-03-01 06:00:06 +03:00
|
|
|
"github.com/Comcast/gots/packet"
|
|
|
|
|
2019-03-25 04:21:03 +03:00
|
|
|
"bitbucket.org/ausocean/av/container/mts"
|
|
|
|
"bitbucket.org/ausocean/av/protocol/rtmp"
|
|
|
|
"bitbucket.org/ausocean/av/protocol/rtp"
|
2018-06-15 10:12:29 +03:00
|
|
|
"bitbucket.org/ausocean/iot/pi/netsender"
|
2019-01-02 08:09:47 +03:00
|
|
|
"bitbucket.org/ausocean/utils/logger"
|
2018-06-09 07:38:48 +03:00
|
|
|
)
|
|
|
|
|
2019-03-24 12:34:35 +03:00
|
|
|
// Log is used by the multiSender.
|
2019-03-24 12:31:25 +03:00
|
|
|
type Log func(level int8, message string, params ...interface{})
|
|
|
|
|
2019-04-02 06:15:36 +03:00
|
|
|
// httpSender provides an implemntation of io.Writer to perform sends to a http
|
|
|
|
// destination.
|
2019-04-01 04:37:28 +03:00
|
|
|
type httpSender struct {
|
2019-02-16 06:56:51 +03:00
|
|
|
client *netsender.Sender
|
2019-03-01 05:58:34 +03:00
|
|
|
log func(lvl int8, msg string, args ...interface{})
|
2019-02-16 06:56:51 +03:00
|
|
|
}
|
|
|
|
|
2019-03-01 05:58:34 +03:00
|
|
|
// newMinimalHttpSender returns a pointer to a new minimalHttpSender.
|
2019-04-01 04:37:28 +03:00
|
|
|
func newHttpSender(ns *netsender.Sender, log func(lvl int8, msg string, args ...interface{})) *httpSender {
|
|
|
|
return &httpSender{
|
2019-02-16 06:56:51 +03:00
|
|
|
client: ns,
|
|
|
|
log: log,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 06:15:36 +03:00
|
|
|
// Write implements io.Writer.
|
2019-04-01 04:37:28 +03:00
|
|
|
func (s *httpSender) Write(d []byte) (int, error) {
|
2019-04-01 04:32:15 +03:00
|
|
|
return len(d), httpSend(d, s.client, s.log)
|
2019-02-16 06:56:51 +03:00
|
|
|
}
|
|
|
|
|
2019-04-01 04:37:28 +03:00
|
|
|
func httpSend(d []byte, client *netsender.Sender, log func(lvl int8, msg string, args ...interface{})) error {
|
|
|
|
// Only send if "V0" is configured as an input.
|
|
|
|
send := false
|
|
|
|
ip := client.Param("ip")
|
|
|
|
pins := netsender.MakePins(ip, "V")
|
|
|
|
for i, pin := range pins {
|
|
|
|
if pin.Name == "V0" {
|
|
|
|
send = true
|
|
|
|
pins[i].Value = len(d)
|
|
|
|
pins[i].Data = d
|
|
|
|
pins[i].MimeType = "video/mp2t"
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !send {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
var reply string
|
|
|
|
reply, _, err = client.Send(netsender.RequestRecv, pins)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return extractMeta(reply, log)
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractMeta looks at a reply at extracts any time or location data - then used
|
|
|
|
// to update time and location information in the mpegts encoder.
|
|
|
|
func extractMeta(r string, log func(lvl int8, msg string, args ...interface{})) error {
|
|
|
|
dec, err := netsender.NewJSONDecoder(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Extract time from reply
|
|
|
|
t, err := dec.Int("ts")
|
|
|
|
if err != nil {
|
|
|
|
log(logger.Warning, pkg+"No timestamp in reply")
|
|
|
|
} else {
|
|
|
|
log(logger.Debug, fmt.Sprintf("%v got timestamp: %v", pkg, t))
|
|
|
|
mts.Meta.Add("ts", strconv.Itoa(t))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract location from reply
|
|
|
|
g, err := dec.String("ll")
|
|
|
|
if err != nil {
|
|
|
|
log(logger.Warning, pkg+"No location in reply")
|
|
|
|
} else {
|
|
|
|
log(logger.Debug, fmt.Sprintf("%v got location: %v", pkg, g))
|
|
|
|
mts.Meta.Add("loc", g)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-09 07:38:48 +03:00
|
|
|
// restart is an optional interface for loadSenders that
|
|
|
|
// can restart their connection.
|
|
|
|
type restarter interface {
|
|
|
|
restart() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// fileSender implements loadSender for a local file destination.
|
|
|
|
type fileSender struct {
|
|
|
|
file *os.File
|
2019-03-09 07:58:07 +03:00
|
|
|
data []byte
|
2018-06-09 07:38:48 +03:00
|
|
|
}
|
|
|
|
|
2019-03-29 05:19:26 +03:00
|
|
|
func newFileSender(path string) (io.Writer, error) {
|
2018-06-09 07:38:48 +03:00
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &fileSender{file: f}, nil
|
|
|
|
}
|
|
|
|
|
2019-04-01 04:41:05 +03:00
|
|
|
// Write implements io.Writer.
|
|
|
|
func (s *fileSender) Write(d []byte) (int, error) {
|
|
|
|
return s.file.Write(d)
|
2018-06-09 07:38:48 +03:00
|
|
|
}
|
|
|
|
|
2019-03-12 13:23:27 +03:00
|
|
|
func (s *fileSender) close() error { return s.file.Close() }
|
2018-06-09 07:38:48 +03:00
|
|
|
|
2019-03-14 00:53:08 +03:00
|
|
|
// mtsSender implements loadSender and provides sending capability specifically
|
2019-03-01 05:58:34 +03:00
|
|
|
// for use with MPEGTS packetization. It handles the construction of appropriately
|
|
|
|
// lengthed clips based on PSI. It also fixes accounts for discontinuities by
|
|
|
|
// setting the discontinuity indicator for the first packet of a clip.
|
2019-02-15 15:47:13 +03:00
|
|
|
type mtsSender struct {
|
2019-04-01 04:32:15 +03:00
|
|
|
dst io.Writer
|
2019-04-01 04:20:11 +03:00
|
|
|
buf []byte
|
|
|
|
next []byte
|
|
|
|
pkt packet.Packet
|
|
|
|
repairer *mts.DiscontinuityRepairer
|
|
|
|
curPid int
|
2019-03-29 05:19:26 +03:00
|
|
|
}
|
|
|
|
|
2019-03-01 05:58:34 +03:00
|
|
|
// newMtsSender returns a new mtsSender.
|
2019-04-01 04:32:15 +03:00
|
|
|
func newMtsSender(dst io.Writer, log func(lvl int8, msg string, args ...interface{})) *mtsSender {
|
2019-02-15 15:47:13 +03:00
|
|
|
return &mtsSender{
|
2019-04-01 04:32:15 +03:00
|
|
|
dst: dst,
|
2019-02-15 04:31:07 +03:00
|
|
|
repairer: mts.NewDiscontinuityRepairer(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 04:20:11 +03:00
|
|
|
// Write implements io.Writer.
|
|
|
|
func (s *mtsSender) Write(d []byte) (int, error) {
|
2019-02-16 22:02:44 +03:00
|
|
|
if s.next != nil {
|
|
|
|
s.buf = append(s.buf, s.next...)
|
|
|
|
}
|
2019-03-09 07:58:07 +03:00
|
|
|
bytes := make([]byte, len(d))
|
|
|
|
copy(bytes, d)
|
2019-03-01 07:44:01 +03:00
|
|
|
s.next = bytes
|
|
|
|
copy(s.pkt[:], bytes)
|
2019-03-01 07:18:26 +03:00
|
|
|
s.curPid = s.pkt.PID()
|
2019-04-01 04:20:11 +03:00
|
|
|
if s.curPid == mts.PatPid && len(s.buf) > 0 {
|
|
|
|
err := s.repairer.Repair(s.buf)
|
2019-03-01 07:44:01 +03:00
|
|
|
if err == nil {
|
2019-04-01 04:32:15 +03:00
|
|
|
_, err = s.dst.Write(s.buf)
|
2019-04-01 04:20:11 +03:00
|
|
|
if err == nil {
|
|
|
|
goto done
|
|
|
|
}
|
2019-03-01 07:44:01 +03:00
|
|
|
}
|
2019-04-01 04:20:11 +03:00
|
|
|
s.repairer.Failed()
|
|
|
|
done:
|
|
|
|
s.buf = s.buf[:0]
|
2019-02-15 16:05:45 +03:00
|
|
|
}
|
2019-04-01 04:20:11 +03:00
|
|
|
return len(d), nil
|
2019-02-15 04:31:07 +03:00
|
|
|
}
|
|
|
|
|
2018-06-09 07:38:48 +03:00
|
|
|
// rtmpSender implements loadSender for a native RTMP destination.
|
|
|
|
type rtmpSender struct {
|
2019-01-19 05:40:38 +03:00
|
|
|
conn *rtmp.Conn
|
2018-06-09 07:38:48 +03:00
|
|
|
|
|
|
|
url string
|
|
|
|
timeout uint
|
|
|
|
retries int
|
2018-09-11 10:45:45 +03:00
|
|
|
log func(lvl int8, msg string, args ...interface{})
|
2018-06-09 07:38:48 +03:00
|
|
|
|
2019-03-14 03:05:09 +03:00
|
|
|
data []byte
|
2018-06-09 07:38:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ restarter = (*rtmpSender)(nil)
|
|
|
|
|
2018-09-11 10:45:45 +03:00
|
|
|
func newRtmpSender(url string, timeout uint, retries int, log func(lvl int8, msg string, args ...interface{})) (*rtmpSender, error) {
|
2019-01-19 05:40:38 +03:00
|
|
|
var conn *rtmp.Conn
|
2018-06-09 07:38:48 +03:00
|
|
|
var err error
|
|
|
|
for n := 0; n < retries; n++ {
|
2019-01-19 05:40:38 +03:00
|
|
|
conn, err = rtmp.Dial(url, timeout, log)
|
2018-06-09 07:38:48 +03:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2019-01-02 08:09:47 +03:00
|
|
|
log(logger.Error, err.Error())
|
2018-06-09 07:38:48 +03:00
|
|
|
if n < retries-1 {
|
2019-01-02 08:09:47 +03:00
|
|
|
log(logger.Info, pkg+"retry rtmp connection")
|
2018-06-09 07:38:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
s := &rtmpSender{
|
2019-01-19 05:40:38 +03:00
|
|
|
conn: conn,
|
2018-06-09 07:38:48 +03:00
|
|
|
url: url,
|
|
|
|
timeout: timeout,
|
|
|
|
retries: retries,
|
|
|
|
log: log,
|
|
|
|
}
|
2019-03-13 10:44:00 +03:00
|
|
|
return s, err
|
2018-06-09 07:38:48 +03:00
|
|
|
}
|
|
|
|
|
2019-03-29 08:54:47 +03:00
|
|
|
// Write implements io.Writer.
|
2019-03-29 05:19:26 +03:00
|
|
|
func (s *rtmpSender) Write(d []byte) (int, error) {
|
2019-03-10 10:18:18 +03:00
|
|
|
if s.conn == nil {
|
2019-04-02 05:23:42 +03:00
|
|
|
return 0, errors.New("no rtmp connection, cannot write")
|
2019-03-10 10:18:18 +03:00
|
|
|
}
|
2019-04-02 05:23:42 +03:00
|
|
|
_, err := s.conn.Write(d)
|
2019-03-24 12:31:25 +03:00
|
|
|
if err != nil {
|
|
|
|
err = s.restart()
|
2019-03-03 10:54:54 +03:00
|
|
|
}
|
2019-04-02 05:23:42 +03:00
|
|
|
return len(d), err
|
2018-06-09 07:38:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *rtmpSender) restart() error {
|
2019-03-03 10:54:54 +03:00
|
|
|
s.close()
|
2019-03-03 10:11:35 +03:00
|
|
|
var err error
|
2018-06-09 07:38:48 +03:00
|
|
|
for n := 0; n < s.retries; n++ {
|
2019-01-19 05:40:38 +03:00
|
|
|
s.conn, err = rtmp.Dial(s.url, s.timeout, s.log)
|
2018-06-09 07:38:48 +03:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2019-01-02 08:09:47 +03:00
|
|
|
s.log(logger.Error, err.Error())
|
2018-06-09 07:38:48 +03:00
|
|
|
if n < s.retries-1 {
|
2019-01-02 08:09:47 +03:00
|
|
|
s.log(logger.Info, pkg+"retry rtmp connection")
|
2018-06-09 07:38:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *rtmpSender) close() error {
|
2019-03-03 10:54:54 +03:00
|
|
|
if s.conn != nil {
|
|
|
|
return s.conn.Close()
|
|
|
|
}
|
|
|
|
return nil
|
2018-06-09 07:38:48 +03:00
|
|
|
}
|
2018-11-18 05:02:11 +03:00
|
|
|
|
2018-11-25 16:15:38 +03:00
|
|
|
// TODO: Write restart func for rtpSender
|
|
|
|
// rtpSender implements loadSender for a native udp destination with rtp packetization.
|
2018-11-25 15:40:38 +03:00
|
|
|
type rtpSender struct {
|
|
|
|
log func(lvl int8, msg string, args ...interface{})
|
|
|
|
encoder *rtp.Encoder
|
2019-03-09 07:58:07 +03:00
|
|
|
data []byte
|
2018-11-25 15:40:38 +03:00
|
|
|
}
|
|
|
|
|
2018-12-10 02:09:20 +03:00
|
|
|
func newRtpSender(addr string, log func(lvl int8, msg string, args ...interface{}), fps uint) (*rtpSender, error) {
|
2018-11-25 15:40:38 +03:00
|
|
|
conn, err := net.Dial("udp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s := &rtpSender{
|
|
|
|
log: log,
|
2018-12-10 02:09:20 +03:00
|
|
|
encoder: rtp.NewEncoder(conn, int(fps)),
|
2018-11-25 15:40:38 +03:00
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2019-04-02 05:23:42 +03:00
|
|
|
// Write implements io.Writer.
|
|
|
|
func (s *rtpSender) Write(d []byte) (int, error) {
|
|
|
|
return s.encoder.Write(s.data)
|
2019-02-15 04:31:07 +03:00
|
|
|
}
|