-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.go
72 lines (60 loc) · 1.35 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
package main
import (
"flag"
"log"
"os"
"time"
"github.com/gongo/go-airplay"
)
var opts struct {
mediaURL string
startingPosition float64
playTimeout int
showHelpFlag bool
}
func init() {
flag.StringVar(&opts.mediaURL, "i", "", "Input media URL")
flag.Float64Var(&opts.startingPosition, "s", 0.0, "Starting position between 0 (0%) to 1 (100%)")
flag.IntVar(&opts.playTimeout, "t", -1, "Timeout for play to end (sec)")
flag.BoolVar(&opts.showHelpFlag, "h", false, "Show this message")
flag.Parse()
if opts.showHelpFlag {
flag.Usage()
os.Exit(0)
}
if opts.mediaURL == "" {
log.Fatal("options: Missing media URL")
}
if opts.startingPosition < 0 || opts.startingPosition > 1 {
log.Fatal("options: Starting position should between 0 to 1")
}
}
func airplayClient() *airplay.Client {
client, err := airplay.FirstClient()
if err != nil {
log.Fatal(err)
}
return client
}
func playToEnd() {
client := airplayClient()
ch := client.PlayAt(opts.mediaURL, opts.startingPosition)
<-ch
}
func playUntilTimeoutOrEnd() {
client := airplayClient()
ch := client.PlayAt(opts.mediaURL, opts.startingPosition)
timeout := time.After(time.Duration(opts.playTimeout) * time.Second)
select {
case <-timeout:
client.Stop()
case <-ch:
}
}
func main() {
if opts.playTimeout > 0 {
playUntilTimeoutOrEnd()
} else {
playToEnd()
}
}