-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.go
81 lines (70 loc) · 1.84 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package rtmp
import (
"bufio"
"errors"
"fmt"
"github.com/torresjeff/rtmp/config"
"io"
"net"
"net/url"
"strings"
)
var ErrInvalidScheme error = errors.New("invalid scheme in URL")
type Client struct {
// Address of the RTMP server this client is connected to
raddr string
app string
streamKey string
url *url.URL
OnAudio AudioCallback
OnVideo VideoCallback
OnMetadata MetadataCallback
}
func (c *Client) Connect(addr string) error {
u, err := url.Parse(addr)
if err != nil {
return err
}
// Always assign rtmp as the scheme
u.Scheme = "rtmp"
if u.Port() == "" {
u.Host += ":" + config.DefaultPort
}
c.url = u
c.raddr = u.Host
urlPath := u.Path
path := strings.Split(urlPath, "/")
// At the very least we need something in the path
if len(path) == 0 || (len(path) == 1 && path[0] == "") {
panic("invalid URL path")
}
if path[0] == "" {
path = path[1:]
}
elements := len(path)
// Treat the first part of the path as the app name
c.app = strings.Join(path[:elements-1], "/")
// The stream key is the last element of the path
c.streamKey = path[elements-1]
if config.Debug {
fmt.Printf("app: \"%s\", streamKey: \"%s\"\n", c.app, c.streamKey)
}
conn, err := net.Dial("tcp", c.raddr)
if err != nil {
return err
}
defer conn.Close()
if config.Debug {
fmt.Println("client: connected to", conn.RemoteAddr().String())
}
socketr := bufio.NewReaderSize(conn, config.BuffioSize)
socketw := bufio.NewWriterSize(conn, config.BuffioSize)
tcUrl := "rtmp://" + conn.RemoteAddr().String() + "/" + c.app
client := NewClientSession(c.app, tcUrl, c.streamKey, c.OnAudio, c.OnVideo, c.OnMetadata)
client.messageManager = NewMessageManager(client, NewHandshaker(socketr, socketw), NewChunkHandler(socketr, socketw))
err = client.StartPlayback()
if err != nil && err != io.EOF {
return err
}
return nil
}