Code refactoring after #855
This commit is contained in:
@@ -579,8 +579,7 @@ streams:
|
|||||||
|
|
||||||
Any cameras in WebRTC format are supported. But at the moment Home Assistant only supports some [Nest](https://www.home-assistant.io/integrations/nest/) cameras in this fomat.
|
Any cameras in WebRTC format are supported. But at the moment Home Assistant only supports some [Nest](https://www.home-assistant.io/integrations/nest/) cameras in this fomat.
|
||||||
|
|
||||||
The Nest API only allows you to get a link to a stream for 5 minutes, but a call to extend the stream is made before it expires. If for some reason the stream expires anyway, any streaming clients will lose connection.
|
**Important.** The Nest API only allows you to get a link to a stream for 5 minutes. Do not use this with Frigate! If the stream expires, Frigate will consume all available ram on your machine within seconds. It's recommended to use [Nest source](#source-nest) - it supports extending the stream.
|
||||||
Note: Do not use this with frigate. If the stream expires, Frigate will consume all available ram on your machine within seconds.
|
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
streams:
|
streams:
|
||||||
@@ -611,7 +610,7 @@ streams:
|
|||||||
|
|
||||||
*[New in v1.6.0](https://github.com/AlexxIT/go2rtc/releases/tag/v1.6.0)*
|
*[New in v1.6.0](https://github.com/AlexxIT/go2rtc/releases/tag/v1.6.0)*
|
||||||
|
|
||||||
Currently only WebRTC cameras are supported. Stream reconnects every 5 minutes.
|
Currently only WebRTC cameras are supported.
|
||||||
|
|
||||||
For simplicity, it is recommended to connect the Nest/WebRTC camera to the [Home Assistant](#source-hass). But if you can somehow get the below parameters - Nest/WebRTC source will work without Hass.
|
For simplicity, it is recommended to connect the Nest/WebRTC camera to the [Home Assistant](#source-hass). But if you can somehow get the below parameters - Nest/WebRTC source will work without Hass.
|
||||||
|
|
||||||
|
|||||||
+50
-18
@@ -14,6 +14,13 @@ import (
|
|||||||
type API struct {
|
type API struct {
|
||||||
Token string
|
Token string
|
||||||
ExpiresAt time.Time
|
ExpiresAt time.Time
|
||||||
|
|
||||||
|
StreamProjectID string
|
||||||
|
StreamDeviceID string
|
||||||
|
StreamSessionID string
|
||||||
|
StreamExpiresAt time.Time
|
||||||
|
|
||||||
|
extendTimer *time.Timer
|
||||||
}
|
}
|
||||||
|
|
||||||
type Auth struct {
|
type Auth struct {
|
||||||
@@ -121,7 +128,7 @@ func (a *API) GetDevices(projectID string) (map[string]string, error) {
|
|||||||
return devices, nil
|
return devices, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) ExchangeSDP(projectID, deviceID, offer string) (string, string, time.Time, error) {
|
func (a *API) ExchangeSDP(projectID, deviceID, offer string) (string, error) {
|
||||||
var reqv struct {
|
var reqv struct {
|
||||||
Command string `json:"command"`
|
Command string `json:"command"`
|
||||||
Params struct {
|
Params struct {
|
||||||
@@ -133,14 +140,14 @@ func (a *API) ExchangeSDP(projectID, deviceID, offer string) (string, string, ti
|
|||||||
|
|
||||||
b, err := json.Marshal(reqv)
|
b, err := json.Marshal(reqv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", time.Time{}, err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
uri := "https://smartdevicemanagement.googleapis.com/v1/enterprises/" +
|
uri := "https://smartdevicemanagement.googleapis.com/v1/enterprises/" +
|
||||||
projectID + "/devices/" + deviceID + ":executeCommand"
|
projectID + "/devices/" + deviceID + ":executeCommand"
|
||||||
req, err := http.NewRequest("POST", uri, bytes.NewReader(b))
|
req, err := http.NewRequest("POST", uri, bytes.NewReader(b))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", time.Time{}, err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Authorization", "Bearer "+a.Token)
|
req.Header.Set("Authorization", "Bearer "+a.Token)
|
||||||
@@ -148,29 +155,34 @@ func (a *API) ExchangeSDP(projectID, deviceID, offer string) (string, string, ti
|
|||||||
client := &http.Client{Timeout: time.Second * 5000}
|
client := &http.Client{Timeout: time.Second * 5000}
|
||||||
res, err := client.Do(req)
|
res, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", time.Time{}, err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if res.StatusCode != 200 {
|
if res.StatusCode != 200 {
|
||||||
return "", "", time.Time{}, errors.New("nest: wrong status: " + res.Status)
|
return "", errors.New("nest: wrong status: " + res.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
var resv struct {
|
var resv struct {
|
||||||
Results struct {
|
Results struct {
|
||||||
Answer string `json:"answerSdp"`
|
Answer string `json:"answerSdp"`
|
||||||
ExpiresAt time.Time `json:"expiresAt"`
|
ExpiresAt time.Time `json:"expiresAt"`
|
||||||
MediaSessionId string `json:"mediaSessionId"`
|
MediaSessionID string `json:"mediaSessionId"`
|
||||||
} `json:"results"`
|
} `json:"results"`
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = json.NewDecoder(res.Body).Decode(&resv); err != nil {
|
if err = json.NewDecoder(res.Body).Decode(&resv); err != nil {
|
||||||
return "", "", time.Time{}, err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return resv.Results.Answer, resv.Results.MediaSessionId, resv.Results.ExpiresAt, nil
|
a.StreamProjectID = projectID
|
||||||
|
a.StreamDeviceID = deviceID
|
||||||
|
a.StreamSessionID = resv.Results.MediaSessionID
|
||||||
|
a.StreamExpiresAt = resv.Results.ExpiresAt
|
||||||
|
|
||||||
|
return resv.Results.Answer, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) ExtendStream(projectID, deviceID, mediaSessionID string) (string, time.Time, error) {
|
func (a *API) ExtendStream() error {
|
||||||
var reqv struct {
|
var reqv struct {
|
||||||
Command string `json:"command"`
|
Command string `json:"command"`
|
||||||
Params struct {
|
Params struct {
|
||||||
@@ -178,18 +190,18 @@ func (a *API) ExtendStream(projectID, deviceID, mediaSessionID string) (string,
|
|||||||
} `json:"params"`
|
} `json:"params"`
|
||||||
}
|
}
|
||||||
reqv.Command = "sdm.devices.commands.CameraLiveStream.ExtendWebRtcStream"
|
reqv.Command = "sdm.devices.commands.CameraLiveStream.ExtendWebRtcStream"
|
||||||
reqv.Params.MediaSessionID = mediaSessionID
|
reqv.Params.MediaSessionID = a.StreamSessionID
|
||||||
|
|
||||||
b, err := json.Marshal(reqv)
|
b, err := json.Marshal(reqv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", time.Time{}, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
uri := "https://smartdevicemanagement.googleapis.com/v1/enterprises/" +
|
uri := "https://smartdevicemanagement.googleapis.com/v1/enterprises/" +
|
||||||
projectID + "/devices/" + deviceID + ":executeCommand"
|
a.StreamProjectID + "/devices/" + a.StreamDeviceID + ":executeCommand"
|
||||||
req, err := http.NewRequest("POST", uri, bytes.NewReader(b))
|
req, err := http.NewRequest("POST", uri, bytes.NewReader(b))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", time.Time{}, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Authorization", "Bearer "+a.Token)
|
req.Header.Set("Authorization", "Bearer "+a.Token)
|
||||||
@@ -197,25 +209,28 @@ func (a *API) ExtendStream(projectID, deviceID, mediaSessionID string) (string,
|
|||||||
client := &http.Client{Timeout: time.Second * 5000}
|
client := &http.Client{Timeout: time.Second * 5000}
|
||||||
res, err := client.Do(req)
|
res, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", time.Time{}, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if res.StatusCode != 200 {
|
if res.StatusCode != 200 {
|
||||||
return "", time.Time{}, errors.New("nest: wrong status: " + res.Status)
|
return errors.New("nest: wrong status: " + res.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
var resv struct {
|
var resv struct {
|
||||||
Results struct {
|
Results struct {
|
||||||
ExpiresAt time.Time `json:"expiresAt"`
|
ExpiresAt time.Time `json:"expiresAt"`
|
||||||
MediaSessionId string `json:"mediaSessionId"`
|
MediaSessionID string `json:"mediaSessionId"`
|
||||||
} `json:"results"`
|
} `json:"results"`
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = json.NewDecoder(res.Body).Decode(&resv); err != nil {
|
if err = json.NewDecoder(res.Body).Decode(&resv); err != nil {
|
||||||
return "", time.Time{}, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return resv.Results.MediaSessionId, resv.Results.ExpiresAt, nil
|
a.StreamSessionID = resv.Results.MediaSessionID
|
||||||
|
a.StreamExpiresAt = resv.Results.ExpiresAt
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type Device struct {
|
type Device struct {
|
||||||
@@ -251,3 +266,20 @@ type Device struct {
|
|||||||
// DisplayName string `json:"displayName"`
|
// DisplayName string `json:"displayName"`
|
||||||
//} `json:"parentRelations"`
|
//} `json:"parentRelations"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *API) StartExtendStreamTimer() {
|
||||||
|
// Calculate the duration until 30 seconds before the stream expires
|
||||||
|
duration := time.Until(a.StreamExpiresAt.Add(-30 * time.Second))
|
||||||
|
a.extendTimer = time.AfterFunc(duration, func() {
|
||||||
|
if err := a.ExtendStream(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
duration = time.Until(a.StreamExpiresAt.Add(-30 * time.Second))
|
||||||
|
a.extendTimer.Reset(duration)
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *API) StopExtendStreamTimer() {
|
||||||
|
a.extendTimer.Stop()
|
||||||
|
}
|
||||||
|
|||||||
+6
-36
@@ -4,21 +4,14 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
"github.com/AlexxIT/go2rtc/pkg/webrtc"
|
"github.com/AlexxIT/go2rtc/pkg/webrtc"
|
||||||
pion "github.com/pion/webrtc/v3"
|
pion "github.com/pion/webrtc/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
conn *webrtc.Conn
|
conn *webrtc.Conn
|
||||||
projectId string
|
api *API
|
||||||
deviceId string
|
|
||||||
mediaSessionId string
|
|
||||||
streamExpiresAt time.Time
|
|
||||||
nestApi *API
|
|
||||||
timer *time.Timer
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(rawURL string) (*Client, error) {
|
func NewClient(rawURL string) (*Client, error) {
|
||||||
@@ -72,7 +65,7 @@ func NewClient(rawURL string) (*Client, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 4. Exchange SDP via Hass
|
// 4. Exchange SDP via Hass
|
||||||
answer, mediaSessionId, expiresAt, err := nestAPI.ExchangeSDP(projectID, deviceID, offer)
|
answer, err := nestAPI.ExchangeSDP(projectID, deviceID, offer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -82,7 +75,7 @@ func NewClient(rawURL string) (*Client, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Client{conn: conn, deviceId: deviceID, projectId: projectID, mediaSessionId: mediaSessionId, streamExpiresAt: expiresAt, nestApi: nestAPI}, nil
|
return &Client{conn: conn, api: nestAPI}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetMedias() []*core.Media {
|
func (c *Client) GetMedias() []*core.Media {
|
||||||
@@ -98,35 +91,12 @@ func (c *Client) AddTrack(media *core.Media, codec *core.Codec, track *core.Rece
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Start() error {
|
func (c *Client) Start() error {
|
||||||
c.StartExtendStreamTimer()
|
c.api.StartExtendStreamTimer()
|
||||||
|
|
||||||
return c.conn.Start()
|
return c.conn.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) StartExtendStreamTimer() {
|
|
||||||
ontimer := func() {
|
|
||||||
c.ExtendStream()
|
|
||||||
c.StartExtendStreamTimer()
|
|
||||||
}
|
|
||||||
// Calculate the duration until 30 seconds before the stream expires
|
|
||||||
duration := time.Until(c.streamExpiresAt.Add(-30 * time.Second))
|
|
||||||
|
|
||||||
// Start the timer
|
|
||||||
c.timer = time.AfterFunc(duration, ontimer)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) ExtendStream() error {
|
|
||||||
mediaSessionId, expiresAt, err := c.nestApi.ExtendStream(c.projectId, c.deviceId, c.mediaSessionId)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
c.mediaSessionId = mediaSessionId
|
|
||||||
c.streamExpiresAt = expiresAt
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) Stop() error {
|
func (c *Client) Stop() error {
|
||||||
c.timer.Stop()
|
c.api.StopExtendStreamTimer()
|
||||||
return c.conn.Stop()
|
return c.conn.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user