Skip to content

Commit 47ff46e

Browse files
committed
Merge branch 'master' into fmp4
2 parents d53c6ac + f072dab commit 47ff46e

File tree

7 files changed

+1306
-2
lines changed

7 files changed

+1306
-2
lines changed

internal/ring/ring.go

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package ring
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/url"
7+
8+
"github.com/AlexxIT/go2rtc/internal/api"
9+
"github.com/AlexxIT/go2rtc/internal/streams"
10+
"github.com/AlexxIT/go2rtc/pkg/core"
11+
"github.com/AlexxIT/go2rtc/pkg/ring"
12+
)
13+
14+
func Init() {
15+
streams.HandleFunc("ring", func(source string) (core.Producer, error) {
16+
return ring.Dial(source)
17+
})
18+
19+
api.HandleFunc("api/ring", apiRing)
20+
}
21+
22+
func apiRing(w http.ResponseWriter, r *http.Request) {
23+
query := r.URL.Query()
24+
var ringAPI *ring.RingRestClient
25+
var err error
26+
27+
// Check auth method
28+
if email := query.Get("email"); email != "" {
29+
// Email/Password Flow
30+
password := query.Get("password")
31+
code := query.Get("code")
32+
33+
ringAPI, err = ring.NewRingRestClient(ring.EmailAuth{
34+
Email: email,
35+
Password: password,
36+
}, nil)
37+
38+
if err != nil {
39+
http.Error(w, err.Error(), http.StatusInternalServerError)
40+
return
41+
}
42+
43+
// Try authentication (this will trigger 2FA if needed)
44+
if _, err = ringAPI.GetAuth(code); err != nil {
45+
if ringAPI.Using2FA {
46+
// Return 2FA prompt
47+
json.NewEncoder(w).Encode(map[string]interface{}{
48+
"needs_2fa": true,
49+
"prompt": ringAPI.PromptFor2FA,
50+
})
51+
return
52+
}
53+
http.Error(w, err.Error(), http.StatusInternalServerError)
54+
return
55+
}
56+
} else {
57+
// Refresh Token Flow
58+
refreshToken := query.Get("refresh_token")
59+
if refreshToken == "" {
60+
http.Error(w, "either email/password or refresh_token is required", http.StatusBadRequest)
61+
return
62+
}
63+
64+
ringAPI, err = ring.NewRingRestClient(ring.RefreshTokenAuth{
65+
RefreshToken: refreshToken,
66+
}, nil)
67+
if err != nil {
68+
http.Error(w, err.Error(), http.StatusInternalServerError)
69+
return
70+
}
71+
}
72+
73+
// Fetch devices
74+
devices, err := ringAPI.FetchRingDevices()
75+
if err != nil {
76+
http.Error(w, err.Error(), http.StatusInternalServerError)
77+
return
78+
}
79+
80+
// Create clean query with only required parameters
81+
cleanQuery := url.Values{}
82+
cleanQuery.Set("refresh_token", ringAPI.RefreshToken)
83+
84+
var items []*api.Source
85+
for _, camera := range devices.AllCameras {
86+
cleanQuery.Set("device_id", camera.DeviceID)
87+
88+
// Stream source
89+
items = append(items, &api.Source{
90+
Name: camera.Description,
91+
URL: "ring:?" + cleanQuery.Encode(),
92+
})
93+
94+
// Snapshot source
95+
items = append(items, &api.Source{
96+
Name: camera.Description + " Snapshot",
97+
URL: "ring:?" + cleanQuery.Encode() + "&snapshot",
98+
})
99+
}
100+
101+
api.ResponseSources(w, items)
102+
}

main.go

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/AlexxIT/go2rtc/internal/nest"
2626
"github.com/AlexxIT/go2rtc/internal/ngrok"
2727
"github.com/AlexxIT/go2rtc/internal/onvif"
28+
"github.com/AlexxIT/go2rtc/internal/ring"
2829
"github.com/AlexxIT/go2rtc/internal/roborock"
2930
"github.com/AlexxIT/go2rtc/internal/rtmp"
3031
"github.com/AlexxIT/go2rtc/internal/rtsp"
@@ -80,6 +81,7 @@ func main() {
8081
mpegts.Init() // mpegts passive source
8182
roborock.Init() // roborock source
8283
homekit.Init() // homekit source
84+
ring.Init() // ring source
8385
nest.Init() // nest source
8486
bubble.Init() // bubble source
8587
expr.Init() // expr source

pkg/flv/producer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (c *Producer) probe() error {
141141
// 2. MedaData without stereo key for AAC
142142
// 3. Audio header after Video keyframe tag
143143

144-
// OpenIPC camera sends:
144+
// OpenIPC camera (on old firmwares) sends:
145145
// 1. Empty video/audio flag
146146
// 2. No MetaData packet
147147
// 3. Sends a video packet in more than 3 seconds

0 commit comments

Comments
 (0)