install go2rtc on bob
This commit is contained in:
@@ -0,0 +1,271 @@
|
||||
package legacy
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"github.com/AlexxIT/go2rtc/pkg/tutk"
|
||||
"github.com/AlexxIT/go2rtc/pkg/xiaomi/crypto"
|
||||
)
|
||||
|
||||
func NewClient(rawURL string) (*Client, error) {
|
||||
u, err := url.Parse(rawURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
query := u.Query()
|
||||
model := query.Get("model")
|
||||
|
||||
var username, password string
|
||||
var key []byte
|
||||
|
||||
if query.Has("sign") {
|
||||
// Legacy with encryption
|
||||
key, err = crypto.CalcSharedKey(query.Get("device_public"), query.Get("client_private"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
username = fmt.Sprintf(
|
||||
`{"public_key":"%s","sign":"%s","account":"admin"}`,
|
||||
query.Get("client_public"), query.Get("sign"),
|
||||
)
|
||||
} else if model == ModelMijia || model == ModelXiaobai {
|
||||
username = "admin"
|
||||
password = query.Get("password")
|
||||
} else if model == ModelDafang || model == ModelXiaofang {
|
||||
username = "admin"
|
||||
} else {
|
||||
return nil, fmt.Errorf("xiaomi: unsupported model: %s", model)
|
||||
}
|
||||
|
||||
conn, err := tutk.Dial(u.Host, query.Get("uid"), username, password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if model == ModelDafang || model == ModelXiaofang {
|
||||
err = xiaofangLogin(conn, query.Get("password"))
|
||||
if err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
c := &Client{
|
||||
Conn: conn,
|
||||
key: key,
|
||||
model: model,
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func xiaofangLogin(conn *tutk.Conn, password string) error {
|
||||
data := tutk.ICAM(0x0400be) // ask login
|
||||
if err := conn.WriteCommand(0x0100, data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, data, err := conn.ReadCommand() // login request
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
enc := data[24:] // data[23] == 3
|
||||
tutk.XXTEADecrypt(enc, enc, []byte(password))
|
||||
|
||||
enc = append(enc, 0, 0, 0, 0, 1, 1, 1)
|
||||
data = tutk.ICAM(0x0400c0, enc...) // login response
|
||||
if err = conn.WriteCommand(0x0100, data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, data, err = conn.ReadCommand()
|
||||
return err
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
*tutk.Conn
|
||||
key []byte
|
||||
model string
|
||||
}
|
||||
|
||||
func (c *Client) Version() string {
|
||||
return fmt.Sprintf("%s (%s)", c.Conn.Version(), c.model)
|
||||
}
|
||||
|
||||
func (c *Client) ReadPacket() (hdr, payload []byte, err error) {
|
||||
hdr, payload, err = c.Conn.ReadPacket()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if c.key != nil {
|
||||
if c.model == ModelAqaraG2 && hdr[0] == tutk.CodecH265 {
|
||||
payload, err = DecodeVideo(payload, c.key)
|
||||
} else {
|
||||
// ModelAqaraG2: audio AAC
|
||||
// ModelIMILABA1: video HEVC, audio PCMA
|
||||
payload, err = crypto.Decode(payload, c.key)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
const (
|
||||
cmdVideoStart = 0x01ff
|
||||
cmdVideoStop = 0x02ff
|
||||
cmdAudioStart = 0x0300
|
||||
cmdAudioStop = 0x0301
|
||||
cmdStreamCtrlReq = 0x0320
|
||||
)
|
||||
|
||||
func (c *Client) WriteCommandJSON(ctrlType uint32, format string, a ...any) error {
|
||||
if len(a) > 0 {
|
||||
format = fmt.Sprintf(format, a...)
|
||||
}
|
||||
return c.WriteCommand(ctrlType, []byte(format))
|
||||
}
|
||||
|
||||
func (c *Client) StartMedia(video, audio string) error {
|
||||
switch c.model {
|
||||
case ModelAqaraG2:
|
||||
// 0 - 1920x1080, 1 - 1280x720, 2 - ?
|
||||
switch video {
|
||||
case "", "fhd":
|
||||
video = "0"
|
||||
case "hd":
|
||||
video = "1"
|
||||
case "sd":
|
||||
video = "2"
|
||||
}
|
||||
|
||||
return errors.Join(
|
||||
c.WriteCommandJSON(cmdVideoStart, `{}`),
|
||||
c.WriteCommandJSON(0x0605, `{"channel":%s}`, video),
|
||||
c.WriteCommandJSON(0x0704, `{}`), // don't know why
|
||||
)
|
||||
|
||||
case ModelIMILABA1, ModelMijia:
|
||||
// 0 - auto, 1 - low, 3 - hd
|
||||
switch video {
|
||||
case "", "hd":
|
||||
video = "3"
|
||||
case "sd":
|
||||
video = "1" // 2 is also low quality
|
||||
case "auto":
|
||||
video = "0"
|
||||
}
|
||||
|
||||
// quality after start
|
||||
return errors.Join(
|
||||
c.WriteCommandJSON(cmdAudioStart, `{}`),
|
||||
c.WriteCommandJSON(cmdVideoStart, `{}`),
|
||||
c.WriteCommandJSON(cmdStreamCtrlReq, `{"videoquality":%s}`, video),
|
||||
)
|
||||
|
||||
case ModelXiaobai:
|
||||
// 00030000 7b7d audio on
|
||||
// 01030000 7b7d audio off
|
||||
// 20030000 0000000001000000 fhd (1920x1080)
|
||||
// 20030000 0000000002000000 hd (1280x720)
|
||||
// 20030000 0000000004000000 low (640x360)
|
||||
// 20030000 00000000ff000000 auto (1920x1080)
|
||||
// ff010000 7b7d video tart
|
||||
// ff020000 7b7d video stop
|
||||
|
||||
var b byte
|
||||
switch video {
|
||||
case "", "fhd":
|
||||
b = 1
|
||||
case "hd":
|
||||
b = 2
|
||||
case "sd":
|
||||
b = 4
|
||||
case "auto":
|
||||
b = 0xff
|
||||
}
|
||||
|
||||
// quality before start
|
||||
return errors.Join(
|
||||
c.WriteCommandJSON(cmdAudioStart, `{}`),
|
||||
c.WriteCommand(cmdStreamCtrlReq, []byte{0, 0, 0, 0, b, 0, 0, 0}),
|
||||
c.WriteCommandJSON(cmdVideoStart, `{}`),
|
||||
)
|
||||
|
||||
case ModelDafang, ModelXiaofang:
|
||||
// 00010000 4943414d 95010400000000000000000600000000000000d20400005a07 - 90k bitrate
|
||||
// 00010000 4943414d 95010400000000000000000600000000000000d20400001e07 - 30k bitrate
|
||||
//var b byte
|
||||
//switch video {
|
||||
//case "", "hd":
|
||||
// b = 0x5a // bitrate 90k
|
||||
//case "sd":
|
||||
// b = 0x1e // bitrate 30k
|
||||
//}
|
||||
//data := tutk.ICAM(0x040195, 0xd2, 4, 0, 0, b, 7)
|
||||
//if err := c.WriteCommand(0x100, data); err != nil {
|
||||
// return err
|
||||
//}
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("xiaomi: unsupported model: %s", c.model)
|
||||
}
|
||||
|
||||
func (c *Client) StopMedia() error {
|
||||
return errors.Join(
|
||||
c.WriteCommandJSON(cmdVideoStop, `{}`),
|
||||
c.WriteCommand(cmdVideoStop, make([]byte, 8)),
|
||||
)
|
||||
}
|
||||
|
||||
func DecodeVideo(data, key []byte) ([]byte, error) {
|
||||
if string(data[:4]) == "\x00\x00\x00\x01" || data[8] == 0 {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
if data[8] != 1 {
|
||||
// Support could be added, but I haven't seen such cameras.
|
||||
return nil, fmt.Errorf("xiaomi: unsupported encryption")
|
||||
}
|
||||
|
||||
nonce8 := data[:8]
|
||||
i1 := binary.LittleEndian.Uint32(data[9:])
|
||||
i2 := binary.LittleEndian.Uint32(data[13:])
|
||||
data = data[17:]
|
||||
src := data[i1 : i1+i2]
|
||||
|
||||
for i := 32; i+16 < len(src); i += 160 {
|
||||
dst, err := crypto.DecodeNonce(src[i:i+16], nonce8, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
copy(src[i:], dst) // copy result in same place
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
const (
|
||||
ModelAqaraG2 = "lumi.camera.gwagl01"
|
||||
ModelIMILABA1 = "chuangmi.camera.ipc019e"
|
||||
ModelLoockV1 = "loock.cateye.v01"
|
||||
ModelXiaobai = "chuangmi.camera.xiaobai"
|
||||
ModelXiaofang = "isa.camera.isc5"
|
||||
// ModelMijia support miss format for new fw and legacy format for old fw
|
||||
ModelMijia = "chuangmi.camera.v2"
|
||||
// ModelDafang support miss format for new fw and legacy format for old fw
|
||||
ModelDafang = "isa.camera.df3"
|
||||
)
|
||||
|
||||
func Supported(model string) bool {
|
||||
switch model {
|
||||
case ModelAqaraG2, ModelIMILABA1, ModelLoockV1, ModelXiaobai, ModelXiaofang:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
package legacy
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/AlexxIT/go2rtc/pkg/aac"
|
||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||
"github.com/AlexxIT/go2rtc/pkg/h264"
|
||||
"github.com/AlexxIT/go2rtc/pkg/h264/annexb"
|
||||
"github.com/AlexxIT/go2rtc/pkg/h265"
|
||||
"github.com/AlexxIT/go2rtc/pkg/tutk"
|
||||
"github.com/pion/rtp"
|
||||
)
|
||||
|
||||
func Dial(rawURL string) (*Producer, error) {
|
||||
client, err := NewClient(rawURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
u, _ := url.Parse(rawURL)
|
||||
query := u.Query()
|
||||
|
||||
err = client.StartMedia(query.Get("subtype"), "")
|
||||
if err != nil {
|
||||
_ = client.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
medias, err := probe(client)
|
||||
if err != nil {
|
||||
_ = client.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c := &Producer{
|
||||
Connection: core.Connection{
|
||||
ID: core.NewID(),
|
||||
FormatName: "xiaomi/legacy",
|
||||
Protocol: "tutk+udp",
|
||||
RemoteAddr: client.RemoteAddr().String(),
|
||||
UserAgent: client.Version(),
|
||||
Medias: medias,
|
||||
Transport: client,
|
||||
},
|
||||
client: client,
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
type Producer struct {
|
||||
core.Connection
|
||||
client *Client
|
||||
}
|
||||
|
||||
const codecXiaobaiPCMA = 1 // chuangmi.camera.xiaobai
|
||||
|
||||
func probe(client *Client) ([]*core.Media, error) {
|
||||
_ = client.SetDeadline(time.Now().Add(15 * time.Second))
|
||||
|
||||
var vcodec, acodec *core.Codec
|
||||
|
||||
for {
|
||||
// 0 5000 codec
|
||||
// 2 0000 codec params
|
||||
// 4 01 active clients
|
||||
// 5 34 unknown const
|
||||
// 6 0600 unknown seq(s)
|
||||
// 8 80026801 unknown fixed
|
||||
// 12 ed8d5c69 time in sec
|
||||
// 16 4c03 time in 1/1000
|
||||
// 18 0000
|
||||
hdr, payload, err := client.ReadPacket()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch codec := hdr[0]; codec {
|
||||
case tutk.CodecH264, tutk.CodecH265:
|
||||
if vcodec == nil {
|
||||
avcc := annexb.EncodeToAVCC(payload)
|
||||
if codec == tutk.CodecH264 {
|
||||
if h264.NALUType(avcc) == h264.NALUTypeSPS {
|
||||
vcodec = h264.AVCCToCodec(avcc)
|
||||
}
|
||||
} else {
|
||||
if h265.NALUType(avcc) == h265.NALUTypeVPS {
|
||||
vcodec = h265.AVCCToCodec(avcc)
|
||||
}
|
||||
}
|
||||
}
|
||||
case tutk.CodecPCMA, codecXiaobaiPCMA:
|
||||
if acodec == nil {
|
||||
acodec = &core.Codec{Name: core.CodecPCMA, ClockRate: 8000}
|
||||
}
|
||||
case tutk.CodecPCML:
|
||||
if acodec == nil {
|
||||
acodec = &core.Codec{Name: core.CodecPCML, ClockRate: 8000}
|
||||
}
|
||||
case tutk.CodecAACLATM:
|
||||
if acodec == nil {
|
||||
acodec = aac.ADTSToCodec(payload)
|
||||
if acodec != nil {
|
||||
acodec.PayloadType = core.PayloadTypeRAW
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if vcodec != nil && acodec != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
medias := []*core.Media{
|
||||
{
|
||||
Kind: core.KindVideo,
|
||||
Direction: core.DirectionRecvonly,
|
||||
Codecs: []*core.Codec{vcodec},
|
||||
},
|
||||
{
|
||||
Kind: core.KindAudio,
|
||||
Direction: core.DirectionRecvonly,
|
||||
Codecs: []*core.Codec{acodec},
|
||||
},
|
||||
}
|
||||
return medias, nil
|
||||
}
|
||||
|
||||
func (c *Producer) Protocol() string {
|
||||
return "tutk+udp"
|
||||
}
|
||||
|
||||
func (c *Producer) Start() error {
|
||||
var audioTS uint32
|
||||
var videoSeq, audioSeq uint16
|
||||
|
||||
for {
|
||||
_ = c.client.SetDeadline(time.Now().Add(5 * time.Second))
|
||||
hdr, payload, err := c.client.ReadPacket()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
n := len(payload)
|
||||
c.Recv += n
|
||||
|
||||
// TODO: rewrite this
|
||||
var name string
|
||||
var pkt *core.Packet
|
||||
|
||||
switch codec := hdr[0]; codec {
|
||||
case tutk.CodecH264, tutk.CodecH265:
|
||||
pkt = &core.Packet{
|
||||
Header: rtp.Header{
|
||||
SequenceNumber: videoSeq,
|
||||
Timestamp: core.Now90000(),
|
||||
},
|
||||
Payload: annexb.EncodeToAVCC(payload),
|
||||
}
|
||||
videoSeq++
|
||||
|
||||
if codec == tutk.CodecH264 {
|
||||
name = core.CodecH264
|
||||
} else {
|
||||
name = core.CodecH265
|
||||
}
|
||||
|
||||
case tutk.CodecPCMA, tutk.CodecPCML, codecXiaobaiPCMA:
|
||||
pkt = &core.Packet{
|
||||
Header: rtp.Header{
|
||||
Version: 2,
|
||||
Marker: true,
|
||||
SequenceNumber: audioSeq,
|
||||
Timestamp: audioTS,
|
||||
},
|
||||
Payload: payload,
|
||||
}
|
||||
audioSeq++
|
||||
|
||||
switch codec {
|
||||
case tutk.CodecPCMA, codecXiaobaiPCMA:
|
||||
name = core.CodecPCMA
|
||||
audioTS += uint32(n)
|
||||
case tutk.CodecPCML:
|
||||
name = core.CodecPCML
|
||||
audioTS += uint32(n / 2) // because 16bit
|
||||
}
|
||||
|
||||
case tutk.CodecAACLATM:
|
||||
pkt = &core.Packet{
|
||||
Header: rtp.Header{
|
||||
SequenceNumber: audioSeq,
|
||||
Timestamp: audioTS,
|
||||
},
|
||||
Payload: payload,
|
||||
}
|
||||
audioSeq++
|
||||
|
||||
name = core.CodecAAC
|
||||
audioTS += 1024
|
||||
}
|
||||
|
||||
for _, recv := range c.Receivers {
|
||||
if recv.Codec.Name == name {
|
||||
recv.WriteRTP(pkt)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Producer) Stop() error {
|
||||
_ = c.client.StopMedia()
|
||||
return c.Connection.Stop()
|
||||
}
|
||||
Reference in New Issue
Block a user