run cleanup when shutdown received

This commit is contained in:
Muaz Ahmad 2023-09-22 13:13:17 +05:00
parent e05180baf3
commit e8a9573b7b
4 changed files with 11 additions and 3 deletions

View file

@ -265,7 +265,8 @@ func parse_ctrl_packet(pkt *Packet, buffer []byte) (error) {
pkt.packet_type = ACKACK pkt.packet_type = ACKACK
return nil return nil
case 5: case 5:
return errors.New("Shutdown received") pkt.packet_type = SHUTDOWN
return nil
default: default:
return errors.New("Unexpected control type") return errors.New("Unexpected control type")
} }

View file

@ -14,6 +14,7 @@ const (
INDUCTION uint8 = iota INDUCTION uint8 = iota
CONCLUSION CONCLUSION
DATA_LOOP DATA_LOOP
BROKEN
) )
type SRTManager struct { type SRTManager struct {
@ -153,6 +154,7 @@ func (agent *SRTManager) process_conclusion(packet *Packet) (*Packet) {
return resp_packet return resp_packet
} else { } else {
agent.output = writer agent.output = writer
CleanFiles(agent.stream_key, 0)
} }
} }
} }
@ -253,6 +255,9 @@ func (agent *SRTManager) process_data(packet *Packet) (*Packet) {
} }
case ACKACK: case ACKACK:
agent.handle_ackack(packet) agent.handle_ackack(packet)
case SHUTDOWN:
agent.state = 3
go CleanFiles(agent.stream_key, 10)
default: default:
return nil return nil
} }
@ -280,6 +285,8 @@ func (agent *SRTManager) Process(packet *Packet) (*Packet, error) {
return agent.process_conclusion(packet), nil return agent.process_conclusion(packet), nil
case DATA_LOOP: case DATA_LOOP:
return agent.process_data(packet), nil return agent.process_data(packet), nil
case BROKEN:
return nil, errors.New("Tunnel shutdown")
default: default:
return nil, errors.New("State not implemented") return nil, errors.New("State not implemented")
} }

View file

@ -7,6 +7,7 @@ import (
"stream_server/transcoder" "stream_server/transcoder"
"time" "time"
"path/filepath" "path/filepath"
"fmt"
) )
func NewWriter(stream_key string) (io.WriteCloser, error) { func NewWriter(stream_key string) (io.WriteCloser, error) {
@ -21,7 +22,7 @@ func CleanFiles(stream_key string, delay time.Duration) {
time.Sleep(delay * time.Second) time.Sleep(delay * time.Second)
base_dir, _ := os.UserHomeDir() base_dir, _ := os.UserHomeDir()
stream_dir := base_dir + "/live/" + stream_key stream_dir := base_dir + "/live/" + stream_key
fileinfo, _ := os.Stat(stream_dir + "/stream.m3u8") fileinfo, _ := os.Stat(stream_dir + "/init.mp4")
if time.Now().Sub(fileinfo.ModTime()) > delay * time.Second { if time.Now().Sub(fileinfo.ModTime()) > delay * time.Second {
leftover_files, _ := filepath.Glob(stream_dir + "/*") leftover_files, _ := filepath.Glob(stream_dir + "/*")
for _, file := range leftover_files { for _, file := range leftover_files {

View file

@ -48,7 +48,6 @@ func (tunnel *Tunnel) Shutdown() {
tunnel.WritePacket(packet) tunnel.WritePacket(packet)
if tunnel.state.output != nil { if tunnel.state.output != nil {
tunnel.state.output.Close() tunnel.state.output.Close()
go CleanFiles(tunnel.state.stream_key, 10)
} }
} }
} }