2023-08-09 16:00:21 +05:00
|
|
|
package rtmp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2023-08-21 20:25:54 +05:00
|
|
|
// meta-struct, holding together params for each streaing peer
|
2023-08-10 15:29:10 +05:00
|
|
|
type ProtocolParams struct {
|
|
|
|
peer_chunk_size uint32
|
|
|
|
chunk_size uint32
|
|
|
|
peer_ack_win uint32
|
|
|
|
curr_read uint32
|
2023-08-10 23:51:54 +05:00
|
|
|
stream_key string
|
2023-08-15 15:53:20 +05:00
|
|
|
trans_id float64
|
2023-08-10 15:29:10 +05:00
|
|
|
}
|
|
|
|
|
2023-08-15 14:34:48 +05:00
|
|
|
func NewServer(port string) (error) {
|
|
|
|
l, err := net.Listen("tcp", ":" + port)
|
2023-08-09 16:00:21 +05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go start(l)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func start(l net.Listener) {
|
2023-08-21 20:25:54 +05:00
|
|
|
// bool to block connections while a stream is currently active
|
|
|
|
// should have it on a per user basis instead, but adding a db
|
|
|
|
// system would make this even more bloated
|
2023-08-09 16:00:21 +05:00
|
|
|
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()
|
2023-08-21 18:43:35 +05:00
|
|
|
defer func(a *bool) {
|
|
|
|
*a = false
|
2023-08-21 20:25:54 +05:00
|
|
|
}(stream_live) // flip the stream state back when done
|
2023-08-09 16:00:21 +05:00
|
|
|
if !DoHandshake(conn) {
|
|
|
|
return
|
|
|
|
}
|
2023-08-10 20:34:10 +05:00
|
|
|
chunk_wrapper := NewChunkWrapper(conn)
|
|
|
|
if !NegotiateConnect(chunk_wrapper) {
|
2023-08-09 16:00:21 +05:00
|
|
|
return
|
|
|
|
}
|
2023-08-15 15:29:46 +05:00
|
|
|
if !CreateStream(chunk_wrapper) {
|
|
|
|
return
|
|
|
|
}
|
2023-08-17 14:58:07 +05:00
|
|
|
if !PublishAsKey(chunk_wrapper) {
|
|
|
|
return
|
|
|
|
}
|
2023-08-21 20:25:54 +05:00
|
|
|
HandleDataLoop(chunk_wrapper) // no error handle since the connection ends either way
|
2023-08-24 12:51:12 +05:00
|
|
|
go StreamCleanup(chunk_wrapper.params.stream_key, 10, stream_live) // remove any media files left 1 min after stream ends
|
2023-08-09 16:00:21 +05:00
|
|
|
}
|