mirror of
https://github.com/bluenviron/mediamtx.git
synced 2026-01-10 04:11:59 -08:00
rtmp: split net.Conn from rtmp.Conn
This commit is contained in:
parent
bf1f45df32
commit
67e8a01d56
12 changed files with 141 additions and 123 deletions
|
|
@ -1,38 +0,0 @@
|
|||
package rtmp
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"net"
|
||||
"net/url"
|
||||
|
||||
"github.com/notedit/rtmp/format/rtmp"
|
||||
)
|
||||
|
||||
// DialContext connects to a server in reading mode.
|
||||
func DialContext(ctx context.Context, address string) (*Conn, error) {
|
||||
// https://github.com/aler9/rtmp/blob/3be4a55359274dcd88762e72aa0a702e2d8ba2fd/format/rtmp/client.go#L74
|
||||
|
||||
u, err := url.Parse(address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
host := rtmp.UrlGetHost(u)
|
||||
|
||||
var d net.Dialer
|
||||
nconn, err := d.DialContext(ctx, "tcp", host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rconn := rtmp.NewConn(&bufio.ReadWriter{
|
||||
Reader: bufio.NewReaderSize(nconn, readBufferSize),
|
||||
Writer: bufio.NewWriterSize(nconn, writeBufferSize),
|
||||
})
|
||||
rconn.URL = u
|
||||
|
||||
return &Conn{
|
||||
rconn: rconn,
|
||||
nconn: nconn,
|
||||
}, nil
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
package rtmp
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
|
|
@ -26,12 +27,33 @@ const (
|
|||
// Conn is a RTMP connection.
|
||||
type Conn struct {
|
||||
rconn *rtmp.Conn
|
||||
nconn net.Conn
|
||||
}
|
||||
|
||||
// Close closes the connection.
|
||||
func (c *Conn) Close() error {
|
||||
return c.nconn.Close()
|
||||
// NewClientConn initializes a client-side connection.
|
||||
func NewClientConn(nconn net.Conn, u *url.URL) *Conn {
|
||||
c := rtmp.NewConn(&bufio.ReadWriter{
|
||||
Reader: bufio.NewReaderSize(nconn, readBufferSize),
|
||||
Writer: bufio.NewWriterSize(nconn, writeBufferSize),
|
||||
})
|
||||
c.URL = u
|
||||
|
||||
return &Conn{
|
||||
rconn: c,
|
||||
}
|
||||
}
|
||||
|
||||
// NewServerConn initializes a server-side connection.
|
||||
func NewServerConn(nconn net.Conn) *Conn {
|
||||
// https://github.com/aler9/rtmp/blob/master/format/rtmp/server.go#L46
|
||||
c := rtmp.NewConn(&bufio.ReadWriter{
|
||||
Reader: bufio.NewReaderSize(nconn, readBufferSize),
|
||||
Writer: bufio.NewWriterSize(nconn, writeBufferSize),
|
||||
})
|
||||
c.IsServer = true
|
||||
|
||||
return &Conn{
|
||||
rconn: c,
|
||||
}
|
||||
}
|
||||
|
||||
// ClientHandshake performs the handshake of a client-side connection.
|
||||
|
|
@ -50,21 +72,6 @@ func (c *Conn) ServerHandshake() error {
|
|||
return c.rconn.Prepare(rtmp.StageGotPublishOrPlayCommand, 0)
|
||||
}
|
||||
|
||||
// SetReadDeadline sets the read deadline.
|
||||
func (c *Conn) SetReadDeadline(t time.Time) error {
|
||||
return c.nconn.SetReadDeadline(t)
|
||||
}
|
||||
|
||||
// SetWriteDeadline sets the write deadline.
|
||||
func (c *Conn) SetWriteDeadline(t time.Time) error {
|
||||
return c.nconn.SetWriteDeadline(t)
|
||||
}
|
||||
|
||||
// RemoteAddr returns the remote network address.
|
||||
func (c *Conn) RemoteAddr() net.Addr {
|
||||
return c.nconn.RemoteAddr()
|
||||
}
|
||||
|
||||
// IsPublishing returns whether the connection is publishing.
|
||||
func (c *Conn) IsPublishing() bool {
|
||||
return c.rconn.Publishing
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package rtmp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
|
@ -218,9 +217,13 @@ func TestClientHandshake(t *testing.T) {
|
|||
close(done)
|
||||
}()
|
||||
|
||||
conn, err := DialContext(context.Background(), "rtmp://127.0.0.1:9121/stream")
|
||||
u, err := url.Parse("rtmp://127.0.0.1:9121/stream")
|
||||
require.NoError(t, err)
|
||||
defer conn.Close()
|
||||
|
||||
nconn, err := net.Dial("tcp", u.Host)
|
||||
require.NoError(t, err)
|
||||
defer nconn.Close()
|
||||
conn := NewClientConn(nconn, u)
|
||||
|
||||
err = conn.ClientHandshake(true)
|
||||
require.NoError(t, err)
|
||||
|
|
|
|||
|
|
@ -1,23 +0,0 @@
|
|||
package rtmp
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"net"
|
||||
|
||||
"github.com/notedit/rtmp/format/rtmp"
|
||||
)
|
||||
|
||||
// NewServerConn initializes a server-side connection.
|
||||
func NewServerConn(nconn net.Conn) *Conn {
|
||||
// https://github.com/aler9/rtmp/blob/master/format/rtmp/server.go#L46
|
||||
c := rtmp.NewConn(&bufio.ReadWriter{
|
||||
Reader: bufio.NewReaderSize(nconn, readBufferSize),
|
||||
Writer: bufio.NewWriterSize(nconn, writeBufferSize),
|
||||
})
|
||||
c.IsServer = true
|
||||
|
||||
return &Conn{
|
||||
rconn: c,
|
||||
nconn: nconn,
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue