mirror of
				https://github.com/AlexxIT/go2rtc.git
				synced 2025-10-31 19:53:02 +08:00 
			
		
		
		
	Code refactoring for SwitchBot format support #1629
This commit is contained in:
		| @@ -1,62 +1,40 @@ | ||||
| package webrtc | ||||
|  | ||||
| import ( | ||||
| 	"encoding/json" | ||||
| 	"net/url" | ||||
| 	"strings" | ||||
|  | ||||
| 	"github.com/AlexxIT/go2rtc/pkg/core" | ||||
| 	pion "github.com/pion/webrtc/v3" | ||||
| 	"github.com/AlexxIT/go2rtc/pkg/webrtc" | ||||
| ) | ||||
|  | ||||
| // SessionDescription is used to expose local and remote session descriptions. | ||||
| type SwitchBotSessionDescription struct { | ||||
| 	Type       string              `json:"type"` | ||||
| 	SDP        string              `json:"sdp"` | ||||
| 	Resolution SwitchBotResolution `json:"resolution"` | ||||
| 	PlayType   int                 `json:"play_type"` | ||||
| } | ||||
|  | ||||
| func switchbotClient(rawURL string, query url.Values) (core.Producer, error) { | ||||
| 	return kinesisClient(rawURL, query, "webrtc/switchbot", &kinesisClientOpts{ | ||||
| 		SessionDescriptionModifier: func(sd *pion.SessionDescription) ([]byte, error) { | ||||
| 			resolution, ok := parseSwitchBotResolution(query.Get("resolution")) | ||||
| 			if !ok { | ||||
| 				resolution = SwitchBotResolutionSD | ||||
| 			} | ||||
| 			json, err := json.Marshal(SwitchBotSessionDescription{ | ||||
| 				Type:       sd.Type.String(), | ||||
| 				SDP:        sd.SDP, | ||||
| 				Resolution: resolution, | ||||
| 				PlayType:   0, | ||||
| 			}) | ||||
| 			return json, err | ||||
| 		}, | ||||
| 		MediaModifier: func() ([]*core.Media, error) { | ||||
| 			return []*core.Media{ | ||||
| 				{Kind: core.KindVideo, Direction: core.DirectionRecvonly}, | ||||
| 				//{Kind: core.KindAudio, Direction: core.DirectionRecvonly}, | ||||
| 				//{Kind: core.KindAudio, Direction: core.DirectionSendRecv}, | ||||
| 				//{Kind: "Data", Direction: core.DirectionSendRecv}, | ||||
| 			}, nil | ||||
| 		}, | ||||
| 	return kinesisClient(rawURL, query, "webrtc/switchbot", func(prod *webrtc.Conn, query url.Values) (any, error) { | ||||
| 		medias := []*core.Media{ | ||||
| 			{Kind: core.KindVideo, Direction: core.DirectionRecvonly}, | ||||
| 		} | ||||
|  | ||||
| 		offer, err := prod.CreateOffer(medias) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
|  | ||||
| 		v := struct { | ||||
| 			Type       string `json:"type"` | ||||
| 			SDP        string `json:"sdp"` | ||||
| 			Resolution int    `json:"resolution"` | ||||
| 			PlayType   int    `json:"play_type"` | ||||
| 		}{ | ||||
| 			Type: "offer", | ||||
| 			SDP:  offer, | ||||
| 		} | ||||
|  | ||||
| 		switch query.Get("resolution") { | ||||
| 		case "hd": | ||||
| 			v.Resolution = 0 | ||||
| 		case "sd": | ||||
| 			v.Resolution = 1 | ||||
| 		} | ||||
|  | ||||
| 		return v, nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| type SwitchBotResolution int | ||||
|  | ||||
| const ( | ||||
| 	SwitchBotResolutionHD SwitchBotResolution = 0 | ||||
| 	SwitchBotResolutionSD                     = 1 | ||||
| ) | ||||
|  | ||||
| func parseSwitchBotResolution(str string) (SwitchBotResolution, bool) { | ||||
| 	var ( | ||||
| 		resolutionMap = map[string]SwitchBotResolution{ | ||||
| 			"hd": SwitchBotResolutionHD, | ||||
| 			"sd": SwitchBotResolutionSD, | ||||
| 		} | ||||
| 	) | ||||
| 	c, ok := resolutionMap[strings.ToLower(str)] | ||||
| 	return c, ok | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Alex X
					Alex X