-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathmediamtx.go
108 lines (98 loc) · 2.98 KB
/
mediamtx.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
105
106
107
108
package media
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"time"
"github.com/livepeer/go-livepeer/clog"
)
type MediaMTXClient struct {
host string
apiPassword string
sourceID string
sourceType string
}
func NewMediaMTXClient(host, apiPassword, sourceID, sourceType string) *MediaMTXClient {
return &MediaMTXClient{
host: host,
apiPassword: apiPassword,
sourceID: sourceID,
sourceType: sourceType,
}
}
const (
mediaMTXControlPort = "9997"
mediaMTXControlTimeout = 30 * time.Second
mediaMTXControlUser = "admin"
MediaMTXWebrtcSession = "webrtcSession"
MediaMTXRtmpConn = "rtmpConn"
)
func MediamtxSourceTypeToString(s string) (string, error) {
switch s {
case MediaMTXWebrtcSession:
return "whip", nil
case MediaMTXRtmpConn:
return "rtmp", nil
default:
return "", errors.New("unknown media source")
}
}
func getApiPath(sourceType string) (string, error) {
var apiPath string
switch sourceType {
case MediaMTXWebrtcSession:
apiPath = "webrtcsessions"
case MediaMTXRtmpConn:
apiPath = "rtmpconns"
default:
return "", fmt.Errorf("invalid sourceType: %s", sourceType)
}
return apiPath, nil
}
func (mc *MediaMTXClient) KickInputConnection(ctx context.Context) error {
clog.V(8).Infof(ctx, "Kicking mediamtx input connection")
apiPath, err := getApiPath(mc.sourceType)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), mediaMTXControlTimeout)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf("http://%s:%s/v3/%s/kick/%s", mc.host, mediaMTXControlPort, apiPath, mc.sourceID), nil)
if err != nil {
return fmt.Errorf("failed to create kick request: %w", err)
}
req.SetBasicAuth(mediaMTXControlUser, mc.apiPassword)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("failed to kick connection: %w", err)
}
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusBadRequest {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("kick connection failed with status code: %d body: %s", resp.StatusCode, body)
}
return nil
}
func (mc *MediaMTXClient) StreamExists() (bool, error) {
apiPath, err := getApiPath(mc.sourceType)
if err != nil {
return false, err
}
ctx, cancel := context.WithTimeout(context.Background(), mediaMTXControlTimeout)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("http://%s:%s/v3/%s/get/%s", mc.host, mediaMTXControlPort, apiPath, mc.sourceID), nil)
if err != nil {
return false, fmt.Errorf("failed to create get stream request: %w", err)
}
req.SetBasicAuth(mediaMTXControlUser, mc.apiPassword)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return false, fmt.Errorf("failed to get stream: %w", err)
}
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusBadRequest {
body, _ := io.ReadAll(resp.Body)
return false, fmt.Errorf("get stream failed with status code: %d body: %s", resp.StatusCode, body)
}
return true, nil
}