Skip to content

Add support for RTSP cameras for Nest source #1253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions internal/nest/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nest

import (
"net/http"
"strings"

"github.com/AlexxIT/go2rtc/internal/api"
"github.com/AlexxIT/go2rtc/internal/streams"
Expand Down Expand Up @@ -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(),
})
}

Expand Down
169 changes: 151 additions & 18 deletions pkg/nest/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,28 @@ type API struct {

StreamProjectID string
StreamDeviceID string
StreamSessionID string
StreamExpiresAt time.Time

// WebRTC
StreamSessionID string

// RTSP
StreamToken string
StreamExtensionToken string

extendTimer *time.Timer
}

type Auth struct {
AccessToken string
}

type DeviceInfo struct {
Name string
DeviceID string
Protocols []string
}

var cache = map[string]*API{}
var cacheMu sync.Mutex

Expand Down Expand Up @@ -78,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 {
Expand All @@ -105,24 +117,30 @@ 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 {
// only RTSP and WEB_RTC available (both supported)
if len(device.Traits.SdmDevicesTraitsCameraLiveStream.SupportedProtocols) == 0 {
continue
}

if device.Traits.SdmDevicesTraitsCameraLiveStream.SupportedProtocols[0] != "WEB_RTC" {
continue
}

i := strings.LastIndexByte(device.Name, '/')
if i <= 0 {
continue
}

name := device.Traits.SdmDevicesTraitsInfo.CustomName
devices[name] = device.Name[i+1:]
// 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 = append(devices, DeviceInfo{
Name: name,
DeviceID: device.Name[i+1:],
Protocols: device.Traits.SdmDevicesTraitsCameraLiveStream.SupportedProtocols,
})
}

return devices, nil
Expand Down Expand Up @@ -186,11 +204,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 {
Expand Down Expand Up @@ -218,8 +245,10 @@ func (a *API) ExtendStream() error {

var resv struct {
Results struct {
ExpiresAt time.Time `json:"expiresAt"`
MediaSessionID string `json:"mediaSessionId"`
ExpiresAt time.Time `json:"expiresAt"`
MediaSessionID string `json:"mediaSessionId"`
StreamExtensionToken string `json:"streamExtensionToken"`
StreamToken string `json:"streamToken"`
} `json:"results"`
}

Expand All @@ -229,6 +258,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
}
Expand Down Expand Up @@ -261,10 +395,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() {
Expand All @@ -277,7 +411,6 @@ func (a *API) StartExtendStreamTimer() {
duration = time.Until(a.StreamExpiresAt.Add(-30 * time.Second))
a.extendTimer.Reset(duration)
})

}

func (a *API) StopExtendStreamTimer() {
Expand Down
82 changes: 70 additions & 12 deletions pkg/nest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@ package nest
import (
"errors"
"net/url"
"strings"

"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
Expand All @@ -36,6 +43,42 @@ func Dial(rawURL string) (*Client, error) {
return nil, err
}

protocols := strings.Split(query.Get("protocols"), ",")
if len(protocols) > 0 && protocols[0] == "RTSP" {
return rtspConn(nestAPI, rawURL, projectID, deviceID)
}

// Default to WEB_RTC for backwards compataiility
return rtcConn(nestAPI, rawURL, projectID, deviceID)
}

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
Expand Down Expand Up @@ -77,31 +120,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()
}