2019-11-20 06:10:07 +03:00
|
|
|
/*
|
|
|
|
DESCRIPTION
|
2019-11-23 06:53:35 +03:00
|
|
|
extract.go provides an Extractor to get JPEG images from an RTP/JPEG stream
|
2019-12-23 05:18:08 +03:00
|
|
|
defined by RFC 2435 (see https://tools.ietf.org/html/rfc2435).
|
2019-11-20 06:10:07 +03:00
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
2019-11-23 06:53:35 +03:00
|
|
|
Copyright (C) 2019 the Australian Ocean Lab (AusOcean)
|
2019-11-20 06:10:07 +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
|
|
|
|
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package mjpeg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"bitbucket.org/ausocean/av/protocol/rtp"
|
|
|
|
)
|
|
|
|
|
2019-11-23 06:53:35 +03:00
|
|
|
const maxRTPSize = 1500 // Max ethernet transmission unit in bytes.
|
2019-11-20 06:10:07 +03:00
|
|
|
|
|
|
|
// Extractor is an Extractor for extracting JPEG from an RTP stream.
|
|
|
|
type Extractor struct {
|
2019-11-23 08:03:41 +03:00
|
|
|
dst io.Writer // The destination we'll be writing extracted JPEGs to.
|
2019-11-20 06:10:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewExtractor returns a new Extractor.
|
2019-11-23 06:53:35 +03:00
|
|
|
func NewExtractor() *Extractor { return &Extractor{} }
|
2019-11-20 06:10:07 +03:00
|
|
|
|
|
|
|
// Extract will continously read RTP packets from src containing JPEG (in RTP
|
|
|
|
// payload format) and extract the JPEG images, sending them to dst. This
|
|
|
|
// function expects that each read from src will provide a single RTP packet.
|
|
|
|
func (e *Extractor) Extract(dst io.Writer, src io.Reader, delay time.Duration) error {
|
|
|
|
buf := make([]byte, maxRTPSize)
|
2019-11-23 06:53:35 +03:00
|
|
|
c := NewCtx(dst)
|
2019-11-20 06:10:07 +03:00
|
|
|
|
|
|
|
for {
|
|
|
|
n, err := src.Read(buf)
|
|
|
|
switch err {
|
|
|
|
case nil: // Do nothing.
|
|
|
|
case io.EOF:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("source read error: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get payload from RTP packet.
|
|
|
|
p, err := rtp.Payload(buf[:n])
|
|
|
|
if err != nil {
|
2019-12-23 05:21:21 +03:00
|
|
|
return fmt.Errorf("could not get RTP payload: %w\n", err)
|
2019-11-20 06:10:07 +03:00
|
|
|
}
|
|
|
|
|
2019-11-23 06:53:35 +03:00
|
|
|
// Also grab the marker so that we know when the JPEG is finished.
|
2019-11-20 06:10:07 +03:00
|
|
|
m, err := rtp.Marker(buf[:n])
|
|
|
|
if err != nil {
|
2019-11-23 06:53:35 +03:00
|
|
|
return fmt.Errorf("could not read RTP marker: %w", err)
|
2019-11-20 06:10:07 +03:00
|
|
|
}
|
|
|
|
|
2019-12-23 05:29:25 +03:00
|
|
|
err = c.ParsePayload(p, m)
|
2019-11-23 06:53:35 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not parse JPEG scan: %w", err)
|
2019-11-20 06:10:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|