forked from openairplay/goplay2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (88 loc) · 2.31 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"flag"
"github.com/grandcat/zeroconf"
"goplay2/audio"
"goplay2/config"
"goplay2/event"
"goplay2/globals"
"goplay2/handlers"
"goplay2/homekit"
"goplay2/ptp"
"goplay2/rtsp"
"log"
"net"
"os"
"strings"
"sync"
)
func main() {
var ifName string
var delay int64
flag.StringVar(&config.Config.DeviceName, "n", "goplay", "Specify device name")
flag.StringVar(&ifName, "i", "eth0", "Specify interface")
flag.Int64Var(&delay, "delay", 0, "Specify hardware delay in ms")
flag.StringVar(&config.Config.PulseSink, "sink", config.Config.PulseSink, "Specify Pulse Audio Sink - Linux only")
flag.Parse() // after declaring flags we need to call it
config.Config.Load()
defer config.Config.Store()
globals.ErrLog = log.New(os.Stderr, "Error:", log.LstdFlags|log.Lshortfile|log.Lmsgprefix)
iFace, err := net.InterfaceByName(ifName)
if err != nil {
panic(err)
}
macAddress := strings.ToUpper(iFace.HardwareAddr.String())
ipAddresses, err := iFace.Addrs()
if err != nil {
panic(err)
}
var ipStringAddr []string
for _, addr := range ipAddresses {
switch v := addr.(type) {
case *net.IPNet:
ipStringAddr = append(ipStringAddr, v.IP.String())
case *net.IPAddr:
ipStringAddr = append(ipStringAddr, v.IP.String())
}
}
homekit.Device = homekit.NewAccessory(macAddress, config.Config.DeviceUUID, airplayDevice())
log.Printf("Starting goplay for device %v", homekit.Device)
homekit.Server, err = homekit.NewServer(macAddress, config.Config.DeviceName, ipStringAddr)
server, err := zeroconf.Register(config.Config.DeviceName, "_airplay._tcp", "local.",
7000, homekit.Device.ToRecords(), []net.Interface{*iFace})
if err != nil {
panic(err)
}
defer server.Shutdown()
clock := ptp.NewVirtualClock(delay)
ptpServer := ptp.NewServer(clock)
// Divided by 100 -> average size of a RTP packet
audioBuffer := audio.NewRing(globals.BufferSize / 100)
player := audio.NewPlayer(clock, audioBuffer)
wg := new(sync.WaitGroup)
wg.Add(4)
go func() {
player.Run()
wg.Done()
}()
go func() {
event.RunEventServer()
wg.Done()
}()
go func() {
ptpServer.Serve()
wg.Done()
}()
go func() {
handler, e := handlers.NewRstpHandler(config.Config.DeviceName, player)
if e != nil {
panic(e)
}
e = rtsp.RunRtspServer(handler)
if e != nil {
panic(e)
}
wg.Done()
}()
wg.Wait()
}