Adds automatic extention of nest stream before it expires.
This commit is contained in:
+55
-7
@@ -121,7 +121,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, error) {
|
func (a *API) ExchangeSDP(projectID, deviceID, offer string) (string, string, time.Time, error) {
|
||||||
var reqv struct {
|
var reqv struct {
|
||||||
Command string `json:"command"`
|
Command string `json:"command"`
|
||||||
Params struct {
|
Params struct {
|
||||||
@@ -133,14 +133,14 @@ func (a *API) ExchangeSDP(projectID, deviceID, offer string) (string, error) {
|
|||||||
|
|
||||||
b, err := json.Marshal(reqv)
|
b, err := json.Marshal(reqv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", "", time.Time{}, 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 "", err
|
return "", "", time.Time{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Authorization", "Bearer "+a.Token)
|
req.Header.Set("Authorization", "Bearer "+a.Token)
|
||||||
@@ -148,11 +148,11 @@ func (a *API) ExchangeSDP(projectID, deviceID, offer string) (string, error) {
|
|||||||
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 "", err
|
return "", "", time.Time{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if res.StatusCode != 200 {
|
if res.StatusCode != 200 {
|
||||||
return "", errors.New("nest: wrong status: " + res.Status)
|
return "", "", time.Time{}, errors.New("nest: wrong status: " + res.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
var resv struct {
|
var resv struct {
|
||||||
@@ -164,10 +164,58 @@ func (a *API) ExchangeSDP(projectID, deviceID, offer string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err = json.NewDecoder(res.Body).Decode(&resv); err != nil {
|
if err = json.NewDecoder(res.Body).Decode(&resv); err != nil {
|
||||||
return "", err
|
return "", "", time.Time{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return resv.Results.Answer, nil
|
return resv.Results.Answer, resv.Results.MediaSessionId, resv.Results.ExpiresAt, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *API) ExtendStream(projectID, deviceID, mediaSessionID string) (string, time.Time, error) {
|
||||||
|
var reqv struct {
|
||||||
|
Command string `json:"command"`
|
||||||
|
Params struct {
|
||||||
|
MediaSessionID string `json:"mediaSessionId"`
|
||||||
|
} `json:"params"`
|
||||||
|
}
|
||||||
|
reqv.Command = "sdm.devices.commands.CameraLiveStream.ExtendWebRtcStream"
|
||||||
|
reqv.Params.MediaSessionID = mediaSessionID
|
||||||
|
|
||||||
|
b, err := json.Marshal(reqv)
|
||||||
|
if err != nil {
|
||||||
|
return "", time.Time{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := "https://smartdevicemanagement.googleapis.com/v1/enterprises/" +
|
||||||
|
projectID + "/devices/" + deviceID + ":executeCommand"
|
||||||
|
req, err := http.NewRequest("POST", uri, bytes.NewReader(b))
|
||||||
|
if err != nil {
|
||||||
|
return "", time.Time{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Authorization", "Bearer "+a.Token)
|
||||||
|
|
||||||
|
client := &http.Client{Timeout: time.Second * 5000}
|
||||||
|
res, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return "", time.Time{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if res.StatusCode != 200 {
|
||||||
|
return "", time.Time{}, errors.New("nest: wrong status: " + res.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
var resv struct {
|
||||||
|
Results struct {
|
||||||
|
ExpiresAt time.Time `json:"expiresAt"`
|
||||||
|
MediaSessionId string `json:"mediaSessionId"`
|
||||||
|
} `json:"results"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = json.NewDecoder(res.Body).Decode(&resv); err != nil {
|
||||||
|
return "", time.Time{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return resv.Results.MediaSessionId, resv.Results.ExpiresAt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type Device struct {
|
type Device struct {
|
||||||
|
|||||||
+36
-3
@@ -4,13 +4,21 @@ 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
|
||||||
|
deviceId string
|
||||||
|
mediaSessionId string
|
||||||
|
streamExpiresAt time.Time
|
||||||
|
nestApi *API
|
||||||
|
timer *time.Timer
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(rawURL string) (*Client, error) {
|
func NewClient(rawURL string) (*Client, error) {
|
||||||
@@ -64,7 +72,7 @@ func NewClient(rawURL string) (*Client, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 4. Exchange SDP via Hass
|
// 4. Exchange SDP via Hass
|
||||||
answer, err := nestAPI.ExchangeSDP(projectID, deviceID, offer)
|
answer, mediaSessionId, expiresAt, err := nestAPI.ExchangeSDP(projectID, deviceID, offer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -74,7 +82,7 @@ func NewClient(rawURL string) (*Client, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Client{conn: conn}, nil
|
return &Client{conn: conn, deviceId: deviceID, projectId: projectID, mediaSessionId: mediaSessionId, streamExpiresAt: expiresAt, nestApi: nestAPI}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetMedias() []*core.Media {
|
func (c *Client) GetMedias() []*core.Media {
|
||||||
@@ -90,10 +98,35 @@ 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()
|
||||||
|
|
||||||
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()
|
||||||
return c.conn.Stop()
|
return c.conn.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user