From 5b0781253ffb8c690226c7dc8d044fbdd722ed6a Mon Sep 17 00:00:00 2001 From: Jamal Fanaian Date: Thu, 11 Jul 2024 17:54:04 -0700 Subject: [PATCH 1/4] Add support for Nest cameras with RTSP --- pkg/nest/api.go | 180 ++++++++++++++++++++++++++++++++++++++++++--- pkg/nest/client.go | 90 +++++++++++++++++++---- 2 files changed, 247 insertions(+), 23 deletions(-) diff --git a/pkg/nest/api.go b/pkg/nest/api.go index 5e0d3407c..8aae2df22 100644 --- a/pkg/nest/api.go +++ b/pkg/nest/api.go @@ -17,9 +17,15 @@ type API struct { StreamProjectID string StreamDeviceID string - StreamSessionID string StreamExpiresAt time.Time + // WebRTC + StreamSessionID string + + // RTSP + StreamToken string + StreamExtensionToken string + extendTimer *time.Timer } @@ -112,7 +118,14 @@ func (a *API) GetDevices(projectID string) (map[string]string, error) { continue } - if device.Traits.SdmDevicesTraitsCameraLiveStream.SupportedProtocols[0] != "WEB_RTC" { + supported := false + for _, protocol := range device.Traits.SdmDevicesTraitsCameraLiveStream.SupportedProtocols { + if (protocol == "WEB_RTC" || protocol == "RTSP") { + supported = true + break + } + } + if !supported { continue } @@ -122,12 +135,44 @@ func (a *API) GetDevices(projectID string) (map[string]string, error) { } name := device.Traits.SdmDevicesTraitsInfo.CustomName + // Devices configured through the Nest app use the container/room name as opposed to the customName trait + if name == "" && len(device.ParentRelations) > 0 { + name = device.ParentRelations[0].DisplayName + } devices[name] = device.Name[i+1:] } return devices, nil } +func (a *API) GetDevice(projectID, deviceID string) (Device, error) { + uri := "https://smartdevicemanagement.googleapis.com/v1/enterprises/" + projectID + "/devices/" + deviceID + req, err := http.NewRequest("GET", uri, nil) + if err != nil { + return Device{}, err + } + + req.Header.Set("Authorization", "Bearer "+a.Token) + + client := &http.Client{Timeout: time.Second * 5000} + res, err := client.Do(req) + if err != nil { + return Device{}, err + } + + if res.StatusCode != 200 { + return Device{}, errors.New("nest: wrong status: " + res.Status) + } + + var device Device + + if err = json.NewDecoder(res.Body).Decode(&device); err != nil { + return Device{}, err + } + + return device, nil +} + func (a *API) ExchangeSDP(projectID, deviceID, offer string) (string, error) { var reqv struct { Command string `json:"command"` @@ -186,11 +231,20 @@ func (a *API) ExtendStream() error { var reqv struct { Command string `json:"command"` Params struct { - MediaSessionID string `json:"mediaSessionId"` + MediaSessionID string `json:"mediaSessionId,omitempty"` + StreamExtensionToken string `json:"streamExtensionToken,omitempty"` } `json:"params"` } - reqv.Command = "sdm.devices.commands.CameraLiveStream.ExtendWebRtcStream" - reqv.Params.MediaSessionID = a.StreamSessionID + + if a.StreamToken != "" { + // RTSP + reqv.Command = "sdm.devices.commands.CameraLiveStream.ExtendRtspStream" + reqv.Params.StreamExtensionToken = a.StreamExtensionToken + } else { + // WebRTC + reqv.Command = "sdm.devices.commands.CameraLiveStream.ExtendWebRtcStream" + reqv.Params.MediaSessionID = a.StreamSessionID + } b, err := json.Marshal(reqv) if err != nil { @@ -220,6 +274,8 @@ func (a *API) ExtendStream() error { Results struct { ExpiresAt time.Time `json:"expiresAt"` MediaSessionID string `json:"mediaSessionId"` + StreamExtensionToken string `json:"streamExtensionToken"` + StreamToken string `json:"streamToken"` } `json:"results"` } @@ -229,6 +285,111 @@ func (a *API) ExtendStream() error { a.StreamSessionID = resv.Results.MediaSessionID a.StreamExpiresAt = resv.Results.ExpiresAt + a.StreamExtensionToken = resv.Results.StreamExtensionToken + a.StreamToken = resv.Results.StreamToken + + return nil +} + +func (a *API) GenerateRtspStream(projectID, deviceID string) (string, error) { + var reqv struct { + Command string `json:"command"` + Params struct {} `json:"params"` + } + reqv.Command = "sdm.devices.commands.CameraLiveStream.GenerateRtspStream" + + b, err := json.Marshal(reqv) + if err != nil { + return "", 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 "", err + } + + req.Header.Set("Authorization", "Bearer "+a.Token) + + client := &http.Client{Timeout: time.Second * 5000} + res, err := client.Do(req) + if err != nil { + return "", err + } + + if res.StatusCode != 200 { + return "", errors.New("nest: wrong status: " + res.Status) + } + + var resv struct { + Results struct { + StreamURLs map[string]string `json:"streamUrls"` + StreamExtensionToken string `json:"streamExtensionToken"` + StreamToken string `json:"streamToken"` + ExpiresAt time.Time `json:"expiresAt"` + } `json:"results"` + } + + if err = json.NewDecoder(res.Body).Decode(&resv); err != nil { + return "", err + } + + if _, ok := resv.Results.StreamURLs["rtspUrl"]; !ok { + return "", errors.New("nest: failed to generate rtsp url") + } + + a.StreamProjectID = projectID + a.StreamDeviceID = deviceID + a.StreamToken = resv.Results.StreamToken + a.StreamExtensionToken = resv.Results.StreamExtensionToken + a.StreamExpiresAt = resv.Results.ExpiresAt + + return resv.Results.StreamURLs["rtspUrl"], nil +} + +func (a *API) StopRTSPStream() error { + if a.StreamProjectID == "" || a.StreamDeviceID == "" { + return errors.New("nest: tried to stop rtsp stream without a project or device ID") + } + + var reqv struct { + Command string `json:"command"` + Params struct { + StreamExtensionToken string `json:"streamExtensionToken"` + } `json:"params"` + } + reqv.Command = "sdm.devices.commands.CameraLiveStream.StopRtspStream" + reqv.Params.StreamExtensionToken = a.StreamExtensionToken + + b, err := json.Marshal(reqv) + if err != nil { + return err + } + + uri := "https://smartdevicemanagement.googleapis.com/v1/enterprises/" + + a.StreamProjectID + "/devices/" + a.StreamDeviceID + ":executeCommand" + req, err := http.NewRequest("POST", uri, bytes.NewReader(b)) + if err != nil { + return err + } + + req.Header.Set("Authorization", "Bearer "+a.Token) + + client := &http.Client{Timeout: time.Second * 5000} + res, err := client.Do(req) + if err != nil { + return err + } + + if res.StatusCode != 200 { + return errors.New("nest: wrong status: " + res.Status) + } + + a.StreamProjectID = "" + a.StreamDeviceID = "" + a.StreamExtensionToken = "" + a.StreamToken = "" return nil } @@ -261,10 +422,10 @@ type Device struct { //SdmDevicesTraitsCameraClipPreview struct { //} `json:"sdm.devices.traits.CameraClipPreview"` } `json:"traits"` - //ParentRelations []struct { - // Parent string `json:"parent"` - // DisplayName string `json:"displayName"` - //} `json:"parentRelations"` + ParentRelations []struct { + Parent string `json:"parent"` + DisplayName string `json:"displayName"` + } `json:"parentRelations"` } func (a *API) StartExtendStreamTimer() { @@ -277,7 +438,6 @@ func (a *API) StartExtendStreamTimer() { duration = time.Until(a.StreamExpiresAt.Add(-30 * time.Second)) a.extendTimer.Reset(duration) }) - } func (a *API) StopExtendStreamTimer() { diff --git a/pkg/nest/client.go b/pkg/nest/client.go index 0b243384b..5fc589cb2 100644 --- a/pkg/nest/client.go +++ b/pkg/nest/client.go @@ -5,16 +5,22 @@ import ( "net/url" "github.com/AlexxIT/go2rtc/pkg/core" + "github.com/AlexxIT/go2rtc/pkg/rtsp" "github.com/AlexxIT/go2rtc/pkg/webrtc" pion "github.com/pion/webrtc/v3" ) -type Client struct { +type WebRTCClient struct { conn *webrtc.Conn api *API } -func Dial(rawURL string) (*Client, error) { +type RTSPClient struct { + conn *rtsp.Conn + api *API +} + +func Dial(rawURL string) (core.Producer, error) { u, err := url.Parse(rawURL) if err != nil { return nil, err @@ -36,6 +42,49 @@ func Dial(rawURL string) (*Client, error) { return nil, err } + device, err := nestAPI.GetDevice(projectID, deviceID) + if err != nil { + return nil, err + } + + for _, proto := range device.Traits.SdmDevicesTraitsCameraLiveStream.SupportedProtocols { + if proto == "WEB_RTC" { + return rtcConn(nestAPI, rawURL, projectID, deviceID) + } else if proto == "RTSP" { + return rtspConn(nestAPI, rawURL, projectID, deviceID) + } + } + + return nil, errors.New("nest: unsupported camera") +} + +func (c *WebRTCClient) GetMedias() []*core.Media { + return c.conn.GetMedias() +} + +func (c *WebRTCClient) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { + return c.conn.GetTrack(media, codec) +} + +func (c *WebRTCClient) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error { + return c.conn.AddTrack(media, codec, track) +} + +func (c *WebRTCClient) Start() error { + c.api.StartExtendStreamTimer() + return c.conn.Start() +} + +func (c *WebRTCClient) Stop() error { + c.api.StopExtendStreamTimer() + return c.conn.Stop() +} + +func (c *WebRTCClient) MarshalJSON() ([]byte, error) { + return c.conn.MarshalJSON() +} + +func rtcConn(nestAPI *API, rawURL, projectID, deviceID string) (*WebRTCClient, error) { rtcAPI, err := webrtc.NewAPI() if err != nil { return nil, err @@ -77,31 +126,46 @@ func Dial(rawURL string) (*Client, error) { return nil, err } - return &Client{conn: conn, api: nestAPI}, nil + return &WebRTCClient{conn: conn, api: nestAPI}, nil } -func (c *Client) GetMedias() []*core.Media { - return c.conn.GetMedias() +func rtspConn(nestAPI *API, rawURL, projectID, deviceID string) (*RTSPClient, error) { + rtspURL, err := nestAPI.GenerateRtspStream(projectID, deviceID) + if err != nil { + return nil, err + } + + rtspClient := rtsp.NewClient(rtspURL) + if err := rtspClient.Dial(); err != nil { + return nil, err + } + if err := rtspClient.Describe(); err != nil { + return nil, err + } + + return &RTSPClient{conn: rtspClient, api: nestAPI}, nil } -func (c *Client) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { - return c.conn.GetTrack(media, codec) +func (c *RTSPClient) GetMedias() []*core.Media { + result := c.conn.GetMedias() + return result } -func (c *Client) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error { - return c.conn.AddTrack(media, codec, track) +func (c *RTSPClient) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { + return c.conn.GetTrack(media, codec) } -func (c *Client) Start() error { +func (c *RTSPClient) Start() error { c.api.StartExtendStreamTimer() return c.conn.Start() } -func (c *Client) Stop() error { +func (c *RTSPClient) Stop() error { + c.api.StopRTSPStream() c.api.StopExtendStreamTimer() return c.conn.Stop() } -func (c *Client) MarshalJSON() ([]byte, error) { +func (c *RTSPClient) MarshalJSON() ([]byte, error) { return c.conn.MarshalJSON() -} +} \ No newline at end of file From e1021a96af447a9448f92100f49a3a865f1d36a2 Mon Sep 17 00:00:00 2001 From: Jamal Fanaian Date: Thu, 11 Jul 2024 17:58:31 -0700 Subject: [PATCH 2/4] go fmt --- pkg/nest/api.go | 26 +++++++++++++------------- pkg/nest/client.go | 4 ++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/pkg/nest/api.go b/pkg/nest/api.go index 8aae2df22..80a421ba0 100644 --- a/pkg/nest/api.go +++ b/pkg/nest/api.go @@ -23,7 +23,7 @@ type API struct { StreamSessionID string // RTSP - StreamToken string + StreamToken string StreamExtensionToken string extendTimer *time.Timer @@ -120,7 +120,7 @@ func (a *API) GetDevices(projectID string) (map[string]string, error) { supported := false for _, protocol := range device.Traits.SdmDevicesTraitsCameraLiveStream.SupportedProtocols { - if (protocol == "WEB_RTC" || protocol == "RTSP") { + if protocol == "WEB_RTC" || protocol == "RTSP" { supported = true break } @@ -231,7 +231,7 @@ func (a *API) ExtendStream() error { var reqv struct { Command string `json:"command"` Params struct { - MediaSessionID string `json:"mediaSessionId,omitempty"` + MediaSessionID string `json:"mediaSessionId,omitempty"` StreamExtensionToken string `json:"streamExtensionToken,omitempty"` } `json:"params"` } @@ -272,10 +272,10 @@ func (a *API) ExtendStream() error { var resv struct { Results struct { - ExpiresAt time.Time `json:"expiresAt"` - MediaSessionID string `json:"mediaSessionId"` - StreamExtensionToken string `json:"streamExtensionToken"` - StreamToken string `json:"streamToken"` + ExpiresAt time.Time `json:"expiresAt"` + MediaSessionID string `json:"mediaSessionId"` + StreamExtensionToken string `json:"streamExtensionToken"` + StreamToken string `json:"streamToken"` } `json:"results"` } @@ -293,8 +293,8 @@ func (a *API) ExtendStream() error { func (a *API) GenerateRtspStream(projectID, deviceID string) (string, error) { var reqv struct { - Command string `json:"command"` - Params struct {} `json:"params"` + Command string `json:"command"` + Params struct{} `json:"params"` } reqv.Command = "sdm.devices.commands.CameraLiveStream.GenerateRtspStream" @@ -324,10 +324,10 @@ func (a *API) GenerateRtspStream(projectID, deviceID string) (string, error) { var resv struct { Results struct { - StreamURLs map[string]string `json:"streamUrls"` - StreamExtensionToken string `json:"streamExtensionToken"` - StreamToken string `json:"streamToken"` - ExpiresAt time.Time `json:"expiresAt"` + StreamURLs map[string]string `json:"streamUrls"` + StreamExtensionToken string `json:"streamExtensionToken"` + StreamToken string `json:"streamToken"` + ExpiresAt time.Time `json:"expiresAt"` } `json:"results"` } diff --git a/pkg/nest/client.go b/pkg/nest/client.go index 5fc589cb2..6d867dea3 100644 --- a/pkg/nest/client.go +++ b/pkg/nest/client.go @@ -17,7 +17,7 @@ type WebRTCClient struct { type RTSPClient struct { conn *rtsp.Conn - api *API + api *API } func Dial(rawURL string) (core.Producer, error) { @@ -168,4 +168,4 @@ func (c *RTSPClient) Stop() error { func (c *RTSPClient) MarshalJSON() ([]byte, error) { return c.conn.MarshalJSON() -} \ No newline at end of file +} From 13dd3084c20b3bea53506382f0820ea2c8203415 Mon Sep 17 00:00:00 2001 From: Jamal Fanaian Date: Thu, 11 Jul 2024 18:47:05 -0700 Subject: [PATCH 3/4] Carry protocol info in stream URL --- internal/nest/init.go | 8 +++++--- pkg/nest/api.go | 41 ++++++++++------------------------------- pkg/nest/client.go | 15 +++++++++------ 3 files changed, 24 insertions(+), 40 deletions(-) diff --git a/internal/nest/init.go b/internal/nest/init.go index 01682414b..8289af73e 100644 --- a/internal/nest/init.go +++ b/internal/nest/init.go @@ -2,6 +2,7 @@ package nest import ( "net/http" + "strings" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/streams" @@ -38,11 +39,12 @@ func apiNest(w http.ResponseWriter, r *http.Request) { var items []*api.Source - for name, deviceID := range devices { - query.Set("device_id", deviceID) + for _, device := range devices { + query.Set("device_id", device.DeviceID) + query.Set("protocols", strings.Join(device.Protocols, ",")) items = append(items, &api.Source{ - Name: name, URL: "nest:?" + query.Encode(), + Name: device.Name, URL: "nest:?" + query.Encode(), }) } diff --git a/pkg/nest/api.go b/pkg/nest/api.go index 80a421ba0..9e32cbcf6 100644 --- a/pkg/nest/api.go +++ b/pkg/nest/api.go @@ -33,6 +33,12 @@ type Auth struct { AccessToken string } +type DeviceInfo struct { + Name string + DeviceID string + Protocols []string +} + var cache = map[string]*API{} var cacheMu sync.Mutex @@ -84,7 +90,7 @@ func NewAPI(clientID, clientSecret, refreshToken string) (*API, error) { return api, nil } -func (a *API) GetDevices(projectID string) (map[string]string, error) { +func (a *API) GetDevices(projectID string) ([]DeviceInfo, error) { uri := "https://smartdevicemanagement.googleapis.com/v1/enterprises/" + projectID + "/devices" req, err := http.NewRequest("GET", uri, nil) if err != nil { @@ -111,7 +117,7 @@ func (a *API) GetDevices(projectID string) (map[string]string, error) { return nil, err } - devices := map[string]string{} + devices := make([]DeviceInfo, 0, len(resv.Devices)) for _, device := range resv.Devices { if len(device.Traits.SdmDevicesTraitsCameraLiveStream.SupportedProtocols) == 0 { @@ -139,38 +145,11 @@ func (a *API) GetDevices(projectID string) (map[string]string, error) { if name == "" && len(device.ParentRelations) > 0 { name = device.ParentRelations[0].DisplayName } - devices[name] = device.Name[i+1:] - } - - return devices, nil -} - -func (a *API) GetDevice(projectID, deviceID string) (Device, error) { - uri := "https://smartdevicemanagement.googleapis.com/v1/enterprises/" + projectID + "/devices/" + deviceID - req, err := http.NewRequest("GET", uri, nil) - if err != nil { - return Device{}, err - } - - req.Header.Set("Authorization", "Bearer "+a.Token) - client := &http.Client{Timeout: time.Second * 5000} - res, err := client.Do(req) - if err != nil { - return Device{}, err + devices = append(devices, DeviceInfo{Name: name, DeviceID: device.Name[i+1:], Protocols: device.Traits.SdmDevicesTraitsCameraLiveStream.SupportedProtocols}) } - if res.StatusCode != 200 { - return Device{}, errors.New("nest: wrong status: " + res.Status) - } - - var device Device - - if err = json.NewDecoder(res.Body).Decode(&device); err != nil { - return Device{}, err - } - - return device, nil + return devices, nil } func (a *API) ExchangeSDP(projectID, deviceID, offer string) (string, error) { diff --git a/pkg/nest/client.go b/pkg/nest/client.go index 6d867dea3..e692359c0 100644 --- a/pkg/nest/client.go +++ b/pkg/nest/client.go @@ -3,6 +3,7 @@ package nest import ( "errors" "net/url" + "strings" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/rtsp" @@ -32,6 +33,12 @@ func Dial(rawURL string) (core.Producer, error) { refreshToken := query.Get("refresh_token") projectID := query.Get("project_id") deviceID := query.Get("device_id") + protocols := strings.Split(query.Get("protocols"), ",") + + // Default to WEB_RTC for backwards compataiility + if len(protocols) == 0 { + protocols = append(protocols, "WEB_RTC") + } if cliendID == "" || cliendSecret == "" || refreshToken == "" || projectID == "" || deviceID == "" { return nil, errors.New("nest: wrong query") @@ -42,12 +49,8 @@ func Dial(rawURL string) (core.Producer, error) { return nil, err } - device, err := nestAPI.GetDevice(projectID, deviceID) - if err != nil { - return nil, err - } - - for _, proto := range device.Traits.SdmDevicesTraitsCameraLiveStream.SupportedProtocols { + // Pick the first supported protocol in order of priority (WEB_RTC, RTSP) + for _, proto := range protocols { if proto == "WEB_RTC" { return rtcConn(nestAPI, rawURL, projectID, deviceID) } else if proto == "RTSP" { From 1abb3c8c22ab20fd483c8628ccedb938f4ff7c73 Mon Sep 17 00:00:00 2001 From: Alex X Date: Sat, 22 Feb 2025 11:39:32 +0300 Subject: [PATCH 4/4] Code refactoring for Nest RTSP source --- pkg/nest/api.go | 18 ++++++------------ pkg/nest/client.go | 19 +++++-------------- 2 files changed, 11 insertions(+), 26 deletions(-) diff --git a/pkg/nest/api.go b/pkg/nest/api.go index 9e32cbcf6..4ca1e8b8c 100644 --- a/pkg/nest/api.go +++ b/pkg/nest/api.go @@ -120,21 +120,11 @@ func (a *API) GetDevices(projectID string) ([]DeviceInfo, error) { devices := make([]DeviceInfo, 0, len(resv.Devices)) for _, device := range resv.Devices { + // only RTSP and WEB_RTC available (both supported) if len(device.Traits.SdmDevicesTraitsCameraLiveStream.SupportedProtocols) == 0 { continue } - supported := false - for _, protocol := range device.Traits.SdmDevicesTraitsCameraLiveStream.SupportedProtocols { - if protocol == "WEB_RTC" || protocol == "RTSP" { - supported = true - break - } - } - if !supported { - continue - } - i := strings.LastIndexByte(device.Name, '/') if i <= 0 { continue @@ -146,7 +136,11 @@ func (a *API) GetDevices(projectID string) ([]DeviceInfo, error) { name = device.ParentRelations[0].DisplayName } - devices = append(devices, DeviceInfo{Name: name, DeviceID: device.Name[i+1:], Protocols: device.Traits.SdmDevicesTraitsCameraLiveStream.SupportedProtocols}) + devices = append(devices, DeviceInfo{ + Name: name, + DeviceID: device.Name[i+1:], + Protocols: device.Traits.SdmDevicesTraitsCameraLiveStream.SupportedProtocols, + }) } return devices, nil diff --git a/pkg/nest/client.go b/pkg/nest/client.go index e692359c0..93c4ce640 100644 --- a/pkg/nest/client.go +++ b/pkg/nest/client.go @@ -33,12 +33,6 @@ func Dial(rawURL string) (core.Producer, error) { refreshToken := query.Get("refresh_token") projectID := query.Get("project_id") deviceID := query.Get("device_id") - protocols := strings.Split(query.Get("protocols"), ",") - - // Default to WEB_RTC for backwards compataiility - if len(protocols) == 0 { - protocols = append(protocols, "WEB_RTC") - } if cliendID == "" || cliendSecret == "" || refreshToken == "" || projectID == "" || deviceID == "" { return nil, errors.New("nest: wrong query") @@ -49,16 +43,13 @@ func Dial(rawURL string) (core.Producer, error) { return nil, err } - // Pick the first supported protocol in order of priority (WEB_RTC, RTSP) - for _, proto := range protocols { - if proto == "WEB_RTC" { - return rtcConn(nestAPI, rawURL, projectID, deviceID) - } else if proto == "RTSP" { - return rtspConn(nestAPI, rawURL, projectID, deviceID) - } + protocols := strings.Split(query.Get("protocols"), ",") + if len(protocols) > 0 && protocols[0] == "RTSP" { + return rtspConn(nestAPI, rawURL, projectID, deviceID) } - return nil, errors.New("nest: unsupported camera") + // Default to WEB_RTC for backwards compataiility + return rtcConn(nestAPI, rawURL, projectID, deviceID) } func (c *WebRTCClient) GetMedias() []*core.Media {