mirror of https://bitbucket.org/ausocean/av.git
75 lines
1.7 KiB
Go
75 lines
1.7 KiB
Go
/*
|
|
NAME
|
|
client.go
|
|
|
|
DESCRIPTION
|
|
client.go provides an RTP client.
|
|
|
|
AUTHOR
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
|
|
|
LICENSE
|
|
This is Copyright (C) 2019 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
|
|
in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
*/
|
|
|
|
package rtp
|
|
|
|
import (
|
|
"net"
|
|
)
|
|
|
|
// Client describes an RTP client that can receive an RTP stream and implements
|
|
// io.Reader.
|
|
type Client struct {
|
|
r *PacketReader
|
|
}
|
|
|
|
// NewClient returns a pointer to a new Client.
|
|
//
|
|
// addr is the address of form <ip>:<port> that we expect to receive
|
|
// RTP at.
|
|
func NewClient(addr string) (*Client, error) {
|
|
c := &Client{r: &PacketReader{}}
|
|
|
|
a, err := net.ResolveUDPAddr("udp", addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
c.r.PacketConn, err = net.ListenUDP("udp", a)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return c, nil
|
|
}
|
|
|
|
// Read implements io.Reader.
|
|
func (c *Client) Read(p []byte) (int, error) {
|
|
return c.r.Read(p)
|
|
}
|
|
|
|
// PacketReader provides an io.Reader interface to an underlying UDP PacketConn.
|
|
type PacketReader struct {
|
|
net.PacketConn
|
|
}
|
|
|
|
// Read implements io.Reader.
|
|
func (r PacketReader) Read(b []byte) (int, error) {
|
|
n, _, err := r.PacketConn.ReadFrom(b)
|
|
return n, err
|
|
}
|