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
return nil
case 5:
return errors.New("Shutdown received")
pkt.packet_type = SHUTDOWN
return nil
default:
return errors.New("Unexpected control type")
}

View file

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

View file

@ -7,6 +7,7 @@ import (
"stream_server/transcoder"
"time"
"path/filepath"
"fmt"
)
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)
base_dir, _ := os.UserHomeDir()
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 {
leftover_files, _ := filepath.Glob(stream_dir + "/*")
for _, file := range leftover_files {

View file

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