BIG core logic rewrite
This commit is contained in:
@@ -1,53 +0,0 @@
|
||||
package tapo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/AlexxIT/go2rtc/pkg/mpegts"
|
||||
"github.com/AlexxIT/go2rtc/pkg/streamer"
|
||||
"github.com/pion/rtp"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (c *Client) backchannelWriter() streamer.WriterFunc {
|
||||
w := mpegts.NewWriter()
|
||||
w.AddPES(68, mpegts.StreamTypePCMATapo)
|
||||
w.WritePAT()
|
||||
w.WritePMT()
|
||||
|
||||
return func(packet *rtp.Packet) (err error) {
|
||||
// don't know why 68 and 192
|
||||
w.WritePES(68, 192, packet.Payload)
|
||||
err = c.WriteBackchannel(w.Bytes())
|
||||
w.Reset()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) SetupBackchannel() (err error) {
|
||||
// if conn1 is not used - we will use it for backchannel
|
||||
// or we need to start another conn for session2
|
||||
if c.session1 != "" {
|
||||
if c.conn2, err = c.newConn(); err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
c.conn2 = c.conn1
|
||||
}
|
||||
|
||||
c.session2, err = c.Request(c.conn2, []byte(`{"params":{"talk":{"mode":"aec"},"method":"get"},"seq":3,"type":"request"}`))
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) WriteBackchannel(body []byte) (err error) {
|
||||
// TODO: fixme (size)
|
||||
buf := bytes.NewBuffer(nil)
|
||||
buf.WriteString("----client-stream-boundary--\r\n")
|
||||
buf.WriteString("Content-Type: audio/mp2t\r\n")
|
||||
buf.WriteString("X-If-Encrypt: 0\r\n")
|
||||
buf.WriteString("X-Session-Id: " + c.session2 + "\r\n")
|
||||
buf.WriteString("Content-Length: " + strconv.Itoa(len(body)) + "\r\n\r\n")
|
||||
buf.Write(body)
|
||||
|
||||
_, err = buf.WriteTo(c.conn2)
|
||||
return
|
||||
}
|
||||
+16
-7
@@ -8,8 +8,8 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||
"github.com/AlexxIT/go2rtc/pkg/mpegts"
|
||||
"github.com/AlexxIT/go2rtc/pkg/streamer"
|
||||
"github.com/AlexxIT/go2rtc/pkg/tcp"
|
||||
"mime/multipart"
|
||||
"net"
|
||||
@@ -19,12 +19,13 @@ import (
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
streamer.Element
|
||||
core.Listener
|
||||
|
||||
url string
|
||||
|
||||
medias []*streamer.Media
|
||||
tracks map[byte]*streamer.Track
|
||||
medias []*core.Media
|
||||
receivers []*core.Receiver
|
||||
sender *core.Sender
|
||||
|
||||
conn1 net.Conn
|
||||
conn2 net.Conn
|
||||
@@ -33,6 +34,9 @@ type Client struct {
|
||||
|
||||
session1 string
|
||||
session2 string
|
||||
|
||||
recv int
|
||||
send int
|
||||
}
|
||||
|
||||
// block ciphers using cipher block chaining.
|
||||
@@ -102,7 +106,7 @@ func (c *Client) newDectypter(res *http.Response, username, password string) {
|
||||
// extract nonce from response
|
||||
// cipher="AES_128_CBC" username="admin" padding="PKCS7_16" algorithm="MD5" nonce="***"
|
||||
nonce := res.Header.Get("Key-Exchange")
|
||||
nonce = streamer.Between(nonce, `nonce="`, `"`)
|
||||
nonce = core.Between(nonce, `nonce="`, `"`)
|
||||
|
||||
key := md5.Sum([]byte(nonce + ":" + password))
|
||||
iv := md5.Sum([]byte(username + ":" + nonce))
|
||||
@@ -158,6 +162,8 @@ func (c *Client) Handle() error {
|
||||
return err
|
||||
}
|
||||
|
||||
c.recv += size
|
||||
|
||||
body := make([]byte, size)
|
||||
|
||||
b := body
|
||||
@@ -178,8 +184,11 @@ func (c *Client) Handle() error {
|
||||
break
|
||||
}
|
||||
|
||||
if track := c.tracks[pkt.PayloadType]; track != nil {
|
||||
_ = track.WriteRTP(pkt)
|
||||
for _, receiver := range c.receivers {
|
||||
if receiver.ID == pkt.PayloadType {
|
||||
receiver.WriteRTP(pkt)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+53
-9
@@ -1,18 +1,62 @@
|
||||
package tapo
|
||||
|
||||
import (
|
||||
"github.com/AlexxIT/go2rtc/pkg/streamer"
|
||||
"bytes"
|
||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||
"github.com/AlexxIT/go2rtc/pkg/mpegts"
|
||||
"github.com/pion/rtp"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (c *Client) AddTrack(media *streamer.Media, track *streamer.Track) *streamer.Track {
|
||||
consCodec := media.MatchCodec(track.Codec)
|
||||
consTrack := c.GetTrack(media, consCodec)
|
||||
if consTrack == nil {
|
||||
return nil
|
||||
func (c *Client) AddTrack(media *core.Media, _ *core.Codec, track *core.Receiver) error {
|
||||
if c.sender == nil {
|
||||
if err := c.SetupBackchannel(); err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
w := mpegts.NewWriter()
|
||||
w.AddPES(68, mpegts.StreamTypePCMATapo)
|
||||
w.WritePAT()
|
||||
w.WritePMT()
|
||||
|
||||
c.sender = core.NewSender(media, track.Codec)
|
||||
c.sender.Handler = func(packet *rtp.Packet) {
|
||||
// don't know why 68 and 192
|
||||
w.WritePES(68, 192, packet.Payload)
|
||||
_ = c.WriteBackchannel(w.Bytes())
|
||||
w.Reset()
|
||||
}
|
||||
}
|
||||
|
||||
return track.Bind(func(packet *rtp.Packet) error {
|
||||
return consTrack.WriteRTP(packet)
|
||||
})
|
||||
c.sender.HandleRTP(track)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) SetupBackchannel() (err error) {
|
||||
// if conn1 is not used - we will use it for backchannel
|
||||
// or we need to start another conn for session2
|
||||
if c.session1 != "" {
|
||||
if c.conn2, err = c.newConn(); err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
c.conn2 = c.conn1
|
||||
}
|
||||
|
||||
c.session2, err = c.Request(c.conn2, []byte(`{"params":{"talk":{"mode":"aec"},"method":"get"},"seq":3,"type":"request"}`))
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) WriteBackchannel(body []byte) (err error) {
|
||||
// TODO: fixme (size)
|
||||
buf := bytes.NewBuffer(nil)
|
||||
buf.WriteString("----client-stream-boundary--\r\n")
|
||||
buf.WriteString("Content-Type: audio/mp2t\r\n")
|
||||
buf.WriteString("X-If-Encrypt: 0\r\n")
|
||||
buf.WriteString("X-Session-Id: " + c.session2 + "\r\n")
|
||||
buf.WriteString("Content-Length: " + strconv.Itoa(len(body)) + "\r\n\r\n")
|
||||
buf.Write(body)
|
||||
|
||||
_, err = buf.WriteTo(c.conn2)
|
||||
return
|
||||
}
|
||||
|
||||
+49
-47
@@ -1,34 +1,34 @@
|
||||
package tapo
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||
"github.com/AlexxIT/go2rtc/pkg/mpegts"
|
||||
"github.com/AlexxIT/go2rtc/pkg/streamer"
|
||||
)
|
||||
|
||||
func (c *Client) GetMedias() []*streamer.Media {
|
||||
// producer should have persistent medias
|
||||
func (c *Client) GetMedias() []*core.Media {
|
||||
if c.medias == nil {
|
||||
// don't know if all Tapo has this capabilities...
|
||||
c.medias = []*streamer.Media{
|
||||
c.medias = []*core.Media{
|
||||
{
|
||||
Kind: streamer.KindVideo,
|
||||
Direction: streamer.DirectionSendonly,
|
||||
Codecs: []*streamer.Codec{
|
||||
{Name: streamer.CodecH264, ClockRate: 90000, PayloadType: streamer.PayloadTypeRAW},
|
||||
Kind: core.KindVideo,
|
||||
Direction: core.DirectionRecvonly,
|
||||
Codecs: []*core.Codec{
|
||||
{Name: core.CodecH264, ClockRate: 90000, PayloadType: core.PayloadTypeRAW},
|
||||
},
|
||||
},
|
||||
{
|
||||
Kind: streamer.KindAudio,
|
||||
Direction: streamer.DirectionSendonly,
|
||||
Codecs: []*streamer.Codec{
|
||||
{Name: streamer.CodecPCMA, ClockRate: 8000, PayloadType: 8},
|
||||
Kind: core.KindAudio,
|
||||
Direction: core.DirectionRecvonly,
|
||||
Codecs: []*core.Codec{
|
||||
{Name: core.CodecPCMA, ClockRate: 8000, PayloadType: 8},
|
||||
},
|
||||
},
|
||||
{
|
||||
Kind: streamer.KindAudio,
|
||||
Direction: streamer.DirectionRecvonly,
|
||||
Codecs: []*streamer.Codec{
|
||||
{Name: streamer.CodecPCMA, ClockRate: 8000, PayloadType: 8},
|
||||
Kind: core.KindAudio,
|
||||
Direction: core.DirectionSendonly,
|
||||
Codecs: []*core.Codec{
|
||||
{Name: core.CodecPCMA, ClockRate: 8000, PayloadType: 8},
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -37,44 +37,26 @@ func (c *Client) GetMedias() []*streamer.Media {
|
||||
return c.medias
|
||||
}
|
||||
|
||||
func (c *Client) GetTrack(media *streamer.Media, codec *streamer.Codec) (track *streamer.Track) {
|
||||
for _, track := range c.tracks {
|
||||
func (c *Client) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) {
|
||||
for _, track := range c.receivers {
|
||||
if track.Codec == codec {
|
||||
return track
|
||||
return track, nil
|
||||
}
|
||||
}
|
||||
|
||||
if c.tracks == nil {
|
||||
c.tracks = map[byte]*streamer.Track{}
|
||||
if err := c.SetupStream(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if media.Direction == streamer.DirectionSendonly {
|
||||
var payloadType byte
|
||||
if media.Kind == streamer.KindVideo {
|
||||
payloadType = mpegts.StreamTypeH264
|
||||
} else {
|
||||
payloadType = mpegts.StreamTypePCMATapo
|
||||
}
|
||||
|
||||
if err := c.SetupStream(); err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
track = streamer.NewTrack(media, codec)
|
||||
c.tracks[payloadType] = track
|
||||
} else {
|
||||
if err := c.SetupBackchannel(); err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if w := c.backchannelWriter(); w != nil {
|
||||
track = streamer.NewTrack(media, codec)
|
||||
track.Bind(w)
|
||||
c.tracks[0] = track
|
||||
}
|
||||
track := core.NewReceiver(media, codec)
|
||||
switch media.Kind {
|
||||
case core.KindVideo:
|
||||
track.ID = mpegts.StreamTypeH264
|
||||
case core.KindAudio:
|
||||
track.ID = mpegts.StreamTypePCMATapo
|
||||
}
|
||||
|
||||
return
|
||||
c.receivers = append(c.receivers, track)
|
||||
return track, nil
|
||||
}
|
||||
|
||||
func (c *Client) Start() error {
|
||||
@@ -82,5 +64,25 @@ func (c *Client) Start() error {
|
||||
}
|
||||
|
||||
func (c *Client) Stop() error {
|
||||
for _, receiver := range c.receivers {
|
||||
receiver.Close()
|
||||
}
|
||||
if c.sender != nil {
|
||||
c.sender.Close()
|
||||
}
|
||||
return c.Close()
|
||||
}
|
||||
|
||||
func (c *Client) MarshalJSON() ([]byte, error) {
|
||||
info := &core.Info{
|
||||
Type: "Tapo active producer",
|
||||
Medias: c.medias,
|
||||
Recv: c.recv,
|
||||
Receivers: c.receivers,
|
||||
Send: c.send,
|
||||
}
|
||||
if c.sender != nil {
|
||||
info.Senders = []*core.Sender{c.sender}
|
||||
}
|
||||
return json.Marshal(info)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user