mediamtx/internal/rtmp/handshake/handshake.go

81 lines
1.2 KiB
Go
Raw Normal View History

2022-09-17 19:19:45 +00:00
// Package handshake contains the RTMP handshake mechanism.
2022-07-16 13:18:04 +00:00
package handshake
import (
"io"
)
// DoClient performs a client-side handshake.
func DoClient(rw io.ReadWriter, validateSignature bool) error {
2022-07-16 13:18:04 +00:00
err := C0S0{}.Write(rw)
if err != nil {
return err
}
c1 := C1S1{}
err = c1.Write(rw, true)
if err != nil {
return err
}
err = C0S0{}.Read(rw)
if err != nil {
return err
}
s1 := C1S1{}
err = s1.Read(rw, false, validateSignature)
2022-07-16 13:18:04 +00:00
if err != nil {
return err
}
err = (&C2S2{Digest: c1.Digest}).Read(rw, validateSignature)
2022-07-16 13:18:04 +00:00
if err != nil {
return err
}
err = C2S2{Digest: s1.Digest}.Write(rw)
if err != nil {
return err
}
return nil
}
// DoServer performs a server-side handshake.
func DoServer(rw io.ReadWriter, validateSignature bool) error {
2022-07-16 13:18:04 +00:00
err := C0S0{}.Read(rw)
if err != nil {
return err
}
c1 := C1S1{}
err = c1.Read(rw, true, validateSignature)
2022-07-16 13:18:04 +00:00
if err != nil {
return err
}
err = C0S0{}.Write(rw)
if err != nil {
return err
}
s1 := C1S1{}
err = s1.Write(rw, false)
if err != nil {
return err
}
err = C2S2{Digest: c1.Digest}.Write(rw)
if err != nil {
return err
}
err = (&C2S2{Digest: s1.Digest}).Read(rw, validateSignature)
2022-07-16 13:18:04 +00:00
if err != nil {
return err
}
return nil
}