Basic RTMP server+handshake

This commit is contained in:
Muaz Ahmad 2023-08-09 16:00:21 +05:00
commit b2c3ca552b
6 changed files with 113 additions and 0 deletions

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module stream_server
go 1.20

12
ingest.go Normal file
View file

@ -0,0 +1,12 @@
package main
import "stream_server/rtmp"
func NewIngestServer(srvr_type uint8, port string) (error) {
var err error
switch srvr_type {
case 0:
err = rtmp.NewServer(&port)
}
return err
}

15
main.go Normal file
View file

@ -0,0 +1,15 @@
package main
const (
SRVTYPE_RTMP uint8 = iota
)
func main() {
err := NewIngestServer(SRVTYPE_RTMP, "7878")
if err != nil {
panic(err)
}
for {
}
}

9
rtmp/connect.go Normal file
View file

@ -0,0 +1,9 @@
package rtmp
import (
"net"
)
func NegotiateConnect(conn net.Conn) {
}

33
rtmp/handshake.go Normal file
View file

@ -0,0 +1,33 @@
package rtmp
import (
"net"
"time"
"encoding/binary"
)
func DoHandshake(conn net.Conn) (hs_success bool) {
C0C1C2 := make([]byte, 1536*2 + 1)
S0S1S2 := make([]byte, 1536*2 + 1)
S0S1S2[0] = 3
binary.BigEndian.PutUint32(S0S1S2[1:1+4], uint32(time.Now().UnixMilli()))
conn.SetDeadline(time.Now().Add(15 * time.Second))
if _, err := conn.Read(C0C1C2); err != nil || C0C1C2[0] != 3 {
return
}
copy(C0C1C2[1:1536], S0S1S2[1+1536:])
binary.BigEndian.PutUint32(S0S1S2[1+1536+4:1+1536+8], uint32(time.Now().UnixMilli()))
if _, err := conn.Write(S0S1S2); err != nil {
return
}
if _, err := conn.Read(C0C1C2[1+1536:]); err != nil {
return
}
hs_success = true
conn.SetDeadline(time.Time{})
return
}

41
rtmp/server.go Normal file
View file

@ -0,0 +1,41 @@
package rtmp
import (
"net"
)
func NewServer(port *string) (error) {
l, err := net.Listen("tcp", ":" + *port)
if err != nil {
return err
}
go start(l)
return nil
}
func start(l net.Listener) {
stream_live := false
for {
conn, err := l.Accept()
if err != nil {
continue
} else if stream_live {
conn.Close()
continue
}
stream_live = true
go handle_conn(conn, &stream_live)
}
}
func handle_conn(conn net.Conn, stream_live *bool) {
defer conn.Close()
if !DoHandshake(conn) {
return
}
if !NegoiateConnect(conn) {
return
}
*stream_live = false
}