-
Notifications
You must be signed in to change notification settings - Fork 2
/
sopcast.go
81 lines (72 loc) · 1.93 KB
/
sopcast.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 main
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2015, Gianluca Fiore
//
// This program is free software: you can redistribute it and/or modify
// it 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.
//
////////////////////////////////////////////////////////////////////////////////
import (
"fmt"
"os"
"os/exec"
"strings"
"time"
)
const sopcast string = "sp-sc"
const player string = "mpv"
// Helper functions
func printCommand(cmd *exec.Cmd) {
fmt.Printf("--> Executing: %s\n", strings.Join(cmd.Args, " "))
}
func printError(err error) {
os.Stderr.WriteString(fmt.Sprintf("--> Error: %s\n", err.Error()))
}
func main() {
// Check we have a sop:// url as first argument
if len(os.Args) != 2 {
fmt.Printf("usage: %s [sop://link]\n", os.Args[0])
os.Exit(1)
}
// Launch Sopcast
sopcastCmd := exec.Command(sopcast, os.Args[1], "3908", "8908", "> /dev/null")
err := sopcastCmd.Start()
if err != nil {
printError(err)
os.Exit(1)
} else {
printCommand(sopcastCmd)
if err != nil {
printError(err)
}
}
// sleep a bit to allow Sopcast to start caching the stream to an amount
// that the player can reproduce without bailing out because cache is too
// small
time.Sleep(10 * time.Second)
// Launch the video player
playerCmd := exec.Command(player, "http://localhost:8908/tv.asf")
err = playerCmd.Start()
if err != nil {
printError(err)
// There has been an error with the video player, exit but kill Sopcast
// first
err := sopcastCmd.Process.Signal(os.Kill)
if err != nil {
printError(err)
} else {
os.Exit(1)
}
} else {
printCommand(playerCmd)
err = playerCmd.Wait()
if err != nil {
printError(err)
}
// Kill Sopcast process before exiting
_ = sopcastCmd.Process.Signal(os.Kill)
}
os.Exit(0)
}